author | mullan |
Tue, 18 Oct 2011 10:12:14 -0400 | |
changeset 10785 | 1d42311b6355 |
parent 5506 | 202f599c92aa |
child 12860 | 9ffbd4e43413 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10785
1d42311b6355
7092897: sun.security.util.Cache should be generified
mullan
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package sun.security.provider.certpath; |
|
26 |
||
27 |
import java.io.IOException; |
|
28 |
import java.security.GeneralSecurityException; |
|
29 |
import java.security.PublicKey; |
|
30 |
import java.security.cert.CertificateEncodingException; |
|
31 |
import java.security.cert.CertificateException; |
|
32 |
import java.security.cert.X509Certificate; |
|
33 |
import java.security.interfaces.DSAPublicKey; |
|
34 |
||
35 |
import javax.security.auth.x500.X500Principal; |
|
36 |
||
37 |
import sun.security.util.DerOutputStream; |
|
38 |
import sun.security.util.DerValue; |
|
39 |
import sun.security.util.Cache; |
|
40 |
import sun.security.x509.X509CertImpl; |
|
41 |
import sun.security.provider.X509Factory; |
|
42 |
||
43 |
/** |
|
44 |
* This class represents an X.509 Certificate Pair object, which is primarily |
|
45 |
* used to hold a pair of cross certificates issued between Certification |
|
46 |
* Authorities. The ASN.1 structure is listed below. The forward certificate |
|
47 |
* of the CertificatePair contains a certificate issued to this CA by another |
|
48 |
* CA. The reverse certificate of the CertificatePair contains a certificate |
|
49 |
* issued by this CA to another CA. When both the forward and the reverse |
|
50 |
* certificates are present in the CertificatePair, the issuer name in one |
|
51 |
* certificate shall match the subject name in the other and vice versa, and |
|
52 |
* the subject public key in one certificate shall be capable of verifying the |
|
53 |
* digital signature on the other certificate and vice versa. If a subject |
|
54 |
* public key in one certificate does not contain required key algorithm |
|
55 |
* parameters, then the signature check involving that key is not done.<p> |
|
56 |
* |
|
57 |
* The ASN.1 syntax for this object is: |
|
58 |
* <pre> |
|
59 |
* CertificatePair ::= SEQUENCE { |
|
60 |
* forward [0] Certificate OPTIONAL, |
|
61 |
* reverse [1] Certificate OPTIONAL |
|
62 |
* -- at least one of the pair shall be present -- } |
|
63 |
* </pre><p> |
|
64 |
* |
|
65 |
* This structure uses EXPLICIT tagging. References: Annex A of |
|
66 |
* X.509(2000), X.509(1997). |
|
67 |
* |
|
68 |
* @author Sean Mullan |
|
69 |
* @since 1.4 |
|
70 |
*/ |
|
71 |
||
72 |
public class X509CertificatePair { |
|
73 |
||
74 |
/* ASN.1 explicit tags */ |
|
75 |
private static final byte TAG_FORWARD = 0; |
|
76 |
private static final byte TAG_REVERSE = 1; |
|
77 |
||
78 |
private X509Certificate forward; |
|
79 |
private X509Certificate reverse; |
|
80 |
private byte[] encoded; |
|
81 |
||
10785
1d42311b6355
7092897: sun.security.util.Cache should be generified
mullan
parents:
5506
diff
changeset
|
82 |
private static final Cache<Object, X509CertificatePair> cache |
1d42311b6355
7092897: sun.security.util.Cache should be generified
mullan
parents:
5506
diff
changeset
|
83 |
= Cache.newSoftMemoryCache(750); |
2 | 84 |
|
85 |
/** |
|
86 |
* Creates an empty instance of X509CertificatePair. |
|
87 |
*/ |
|
88 |
public X509CertificatePair() {} |
|
89 |
||
90 |
/** |
|
91 |
* Creates an instance of X509CertificatePair. At least one of |
|
92 |
* the pair must be non-null. |
|
93 |
* |
|
94 |
* @param forward The forward component of the certificate pair |
|
95 |
* which represents a certificate issued to this CA by other CAs. |
|
96 |
* @param reverse The reverse component of the certificate pair |
|
97 |
* which represents a certificate issued by this CA to other CAs. |
|
98 |
* @throws CertificateException If an exception occurs. |
|
99 |
*/ |
|
100 |
public X509CertificatePair(X509Certificate forward, X509Certificate reverse) |
|
101 |
throws CertificateException { |
|
102 |
if (forward == null && reverse == null) { |
|
103 |
throw new CertificateException("at least one of certificate pair " |
|
104 |
+ "must be non-null"); |
|
105 |
} |
|
106 |
||
107 |
this.forward = forward; |
|
108 |
this.reverse = reverse; |
|
109 |
||
110 |
checkPair(); |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Create a new X509CertificatePair from its encoding. |
|
115 |
* |
|
116 |
* For internal use only, external code should use generateCertificatePair. |
|
117 |
*/ |
|
10785
1d42311b6355
7092897: sun.security.util.Cache should be generified
mullan
parents:
5506
diff
changeset
|
118 |
private X509CertificatePair(byte[] encoded) throws CertificateException { |
2 | 119 |
try { |
120 |
parse(new DerValue(encoded)); |
|
121 |
this.encoded = encoded; |
|
122 |
} catch (IOException ex) { |
|
123 |
throw new CertificateException(ex.toString()); |
|
124 |
} |
|
125 |
checkPair(); |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Clear the cache for debugging. |
|
130 |
*/ |
|
131 |
public static synchronized void clearCache() { |
|
132 |
cache.clear(); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Create a X509CertificatePair from its encoding. Uses cache lookup |
|
137 |
* if possible. |
|
138 |
*/ |
|
139 |
public static synchronized X509CertificatePair generateCertificatePair |
|
140 |
(byte[] encoded) throws CertificateException { |
|
141 |
Object key = new Cache.EqualByteArray(encoded); |
|
10785
1d42311b6355
7092897: sun.security.util.Cache should be generified
mullan
parents:
5506
diff
changeset
|
142 |
X509CertificatePair pair = cache.get(key); |
2 | 143 |
if (pair != null) { |
144 |
return pair; |
|
145 |
} |
|
146 |
pair = new X509CertificatePair(encoded); |
|
147 |
key = new Cache.EqualByteArray(pair.encoded); |
|
148 |
cache.put(key, pair); |
|
149 |
return pair; |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Sets the forward component of the certificate pair. |
|
154 |
*/ |
|
155 |
public void setForward(X509Certificate cert) throws CertificateException { |
|
156 |
checkPair(); |
|
157 |
forward = cert; |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Sets the reverse component of the certificate pair. |
|
162 |
*/ |
|
163 |
public void setReverse(X509Certificate cert) throws CertificateException { |
|
164 |
checkPair(); |
|
165 |
reverse = cert; |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Returns the forward component of the certificate pair. |
|
170 |
* |
|
171 |
* @return The forward certificate, or null if not set. |
|
172 |
*/ |
|
173 |
public X509Certificate getForward() { |
|
174 |
return forward; |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Returns the reverse component of the certificate pair. |
|
179 |
* |
|
180 |
* @return The reverse certificate, or null if not set. |
|
181 |
*/ |
|
182 |
public X509Certificate getReverse() { |
|
183 |
return reverse; |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* Return the DER encoded form of the certificate pair. |
|
188 |
* |
|
189 |
* @return The encoded form of the certificate pair. |
|
190 |
* @throws CerticateEncodingException If an encoding exception occurs. |
|
191 |
*/ |
|
192 |
public byte[] getEncoded() throws CertificateEncodingException { |
|
193 |
try { |
|
194 |
if (encoded == null) { |
|
195 |
DerOutputStream tmp = new DerOutputStream(); |
|
196 |
emit(tmp); |
|
197 |
encoded = tmp.toByteArray(); |
|
198 |
} |
|
199 |
} catch (IOException ex) { |
|
200 |
throw new CertificateEncodingException(ex.toString()); |
|
201 |
} |
|
202 |
return encoded; |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Return a printable representation of the certificate pair. |
|
207 |
* |
|
208 |
* @return A String describing the contents of the pair. |
|
209 |
*/ |
|
210 |
public String toString() { |
|
211 |
StringBuffer sb = new StringBuffer(); |
|
212 |
sb.append("X.509 Certificate Pair: [\n"); |
|
213 |
if (forward != null) |
|
214 |
sb.append(" Forward: " + forward + "\n"); |
|
215 |
if (reverse != null) |
|
216 |
sb.append(" Reverse: " + reverse + "\n"); |
|
217 |
sb.append("]"); |
|
218 |
return sb.toString(); |
|
219 |
} |
|
220 |
||
221 |
/* Parse the encoded bytes */ |
|
222 |
private void parse(DerValue val) |
|
223 |
throws IOException, CertificateException |
|
224 |
{ |
|
225 |
if (val.tag != DerValue.tag_Sequence) { |
|
226 |
throw new IOException |
|
227 |
("Sequence tag missing for X509CertificatePair"); |
|
228 |
} |
|
229 |
||
230 |
while (val.data != null && val.data.available() != 0) { |
|
231 |
DerValue opt = val.data.getDerValue(); |
|
232 |
short tag = (byte) (opt.tag & 0x01f); |
|
233 |
switch (tag) { |
|
234 |
case TAG_FORWARD: |
|
235 |
if (opt.isContextSpecific() && opt.isConstructed()) { |
|
236 |
if (forward != null) { |
|
237 |
throw new IOException("Duplicate forward " |
|
238 |
+ "certificate in X509CertificatePair"); |
|
239 |
} |
|
240 |
opt = opt.data.getDerValue(); |
|
241 |
forward = X509Factory.intern |
|
242 |
(new X509CertImpl(opt.toByteArray())); |
|
243 |
} |
|
244 |
break; |
|
245 |
case TAG_REVERSE: |
|
246 |
if (opt.isContextSpecific() && opt.isConstructed()) { |
|
247 |
if (reverse != null) { |
|
248 |
throw new IOException("Duplicate reverse " |
|
249 |
+ "certificate in X509CertificatePair"); |
|
250 |
} |
|
251 |
opt = opt.data.getDerValue(); |
|
252 |
reverse = X509Factory.intern |
|
253 |
(new X509CertImpl(opt.toByteArray())); |
|
254 |
} |
|
255 |
break; |
|
256 |
default: |
|
257 |
throw new IOException("Invalid encoding of " |
|
258 |
+ "X509CertificatePair"); |
|
259 |
} |
|
260 |
} |
|
261 |
if (forward == null && reverse == null) { |
|
262 |
throw new CertificateException("at least one of certificate pair " |
|
263 |
+ "must be non-null"); |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
/* Translate to encoded bytes */ |
|
268 |
private void emit(DerOutputStream out) |
|
269 |
throws IOException, CertificateEncodingException |
|
270 |
{ |
|
271 |
DerOutputStream tagged = new DerOutputStream(); |
|
272 |
||
273 |
if (forward != null) { |
|
274 |
DerOutputStream tmp = new DerOutputStream(); |
|
275 |
tmp.putDerValue(new DerValue(forward.getEncoded())); |
|
276 |
tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
277 |
true, TAG_FORWARD), tmp); |
|
278 |
} |
|
279 |
||
280 |
if (reverse != null) { |
|
281 |
DerOutputStream tmp = new DerOutputStream(); |
|
282 |
tmp.putDerValue(new DerValue(reverse.getEncoded())); |
|
283 |
tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
284 |
true, TAG_REVERSE), tmp); |
|
285 |
} |
|
286 |
||
287 |
out.write(DerValue.tag_Sequence, tagged); |
|
288 |
} |
|
289 |
||
290 |
/* |
|
291 |
* Check for a valid certificate pair |
|
292 |
*/ |
|
293 |
private void checkPair() throws CertificateException { |
|
294 |
||
295 |
/* if either of pair is missing, return w/o error */ |
|
296 |
if (forward == null || reverse == null) { |
|
297 |
return; |
|
298 |
} |
|
299 |
/* |
|
300 |
* If both elements of the pair are present, check that they |
|
301 |
* are a valid pair. |
|
302 |
*/ |
|
303 |
X500Principal fwSubject = forward.getSubjectX500Principal(); |
|
304 |
X500Principal fwIssuer = forward.getIssuerX500Principal(); |
|
305 |
X500Principal rvSubject = reverse.getSubjectX500Principal(); |
|
306 |
X500Principal rvIssuer = reverse.getIssuerX500Principal(); |
|
307 |
if (!fwIssuer.equals(rvSubject) || !rvIssuer.equals(fwSubject)) { |
|
308 |
throw new CertificateException("subject and issuer names in " |
|
309 |
+ "forward and reverse certificates do not match"); |
|
310 |
} |
|
311 |
||
312 |
/* check signatures unless key parameters are missing */ |
|
313 |
try { |
|
314 |
PublicKey pk = reverse.getPublicKey(); |
|
315 |
if (!(pk instanceof DSAPublicKey) || |
|
316 |
((DSAPublicKey)pk).getParams() != null) { |
|
317 |
forward.verify(pk); |
|
318 |
} |
|
319 |
pk = forward.getPublicKey(); |
|
320 |
if (!(pk instanceof DSAPublicKey) || |
|
321 |
((DSAPublicKey)pk).getParams() != null) { |
|
322 |
reverse.verify(pk); |
|
323 |
} |
|
324 |
} catch (GeneralSecurityException e) { |
|
325 |
throw new CertificateException("invalid signature: " |
|
326 |
+ e.getMessage()); |
|
327 |
} |
|
328 |
} |
|
329 |
} |