author | jjg |
Mon, 15 Aug 2011 11:48:20 -0700 | |
changeset 10336 | 0bb1999251f8 |
parent 5506 | 202f599c92aa |
child 10358 | 5a4ec0a5f7ee |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
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 |
||
26 |
package sun.security.provider.certpath; |
|
27 |
||
28 |
import java.io.ByteArrayInputStream; |
|
29 |
import java.io.ByteArrayOutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.security.cert.CertificateEncodingException; |
|
33 |
import java.security.cert.Certificate; |
|
34 |
import java.security.cert.CertificateException; |
|
35 |
import java.security.cert.CertificateFactory; |
|
36 |
import java.security.cert.X509Certificate; |
|
37 |
import java.util.*; |
|
38 |
import java.security.cert.CertPath; |
|
39 |
import sun.security.pkcs.ContentInfo; |
|
40 |
import sun.security.pkcs.PKCS7; |
|
41 |
import sun.security.pkcs.SignerInfo; |
|
42 |
import sun.security.x509.AlgorithmId; |
|
43 |
import sun.security.util.DerValue; |
|
44 |
import sun.security.util.DerOutputStream; |
|
45 |
import sun.security.util.DerInputStream; |
|
46 |
||
47 |
||
48 |
/** |
|
49 |
* A {@link java.security.cert.CertPath CertPath} (certification path) |
|
50 |
* consisting exclusively of |
|
51 |
* {@link java.security.cert.X509Certificate X509Certificate}s. |
|
52 |
* <p> |
|
53 |
* By convention, X.509 <code>CertPath</code>s are stored from target |
|
54 |
* to trust anchor. |
|
55 |
* That is, the issuer of one certificate is the subject of the following |
|
56 |
* one. However, unvalidated X.509 <code>CertPath</code>s may not follow |
|
57 |
* this convention. PKIX <code>CertPathValidator</code>s will detect any |
|
58 |
* departure from this convention and throw a |
|
59 |
* <code>CertPathValidatorException</code>. |
|
60 |
* |
|
61 |
* @author Yassir Elley |
|
62 |
* @since 1.4 |
|
63 |
*/ |
|
64 |
public class X509CertPath extends CertPath { |
|
65 |
||
66 |
private static final long serialVersionUID = 4989800333263052980L; |
|
67 |
||
68 |
/** |
|
69 |
* List of certificates in this chain |
|
70 |
*/ |
|
71 |
private List<X509Certificate> certs; |
|
72 |
||
73 |
/** |
|
74 |
* The names of our encodings. PkiPath is the default. |
|
75 |
*/ |
|
76 |
private static final String COUNT_ENCODING = "count"; |
|
77 |
private static final String PKCS7_ENCODING = "PKCS7"; |
|
78 |
private static final String PKIPATH_ENCODING = "PkiPath"; |
|
79 |
||
80 |
/** |
|
81 |
* List of supported encodings |
|
82 |
*/ |
|
83 |
private static final Collection<String> encodingList; |
|
84 |
||
85 |
static { |
|
86 |
List<String> list = new ArrayList<String>(2); |
|
87 |
list.add(PKIPATH_ENCODING); |
|
88 |
list.add(PKCS7_ENCODING); |
|
89 |
encodingList = Collections.unmodifiableCollection(list); |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Creates an <code>X509CertPath</code> from a <code>List</code> of |
|
94 |
* <code>X509Certificate</code>s. |
|
95 |
* <p> |
|
96 |
* The certificates are copied out of the supplied <code>List</code> |
|
97 |
* object. |
|
98 |
* |
|
99 |
* @param certs a <code>List</code> of <code>X509Certificate</code>s |
|
100 |
* @exception CertificateException if <code>certs</code> contains an element |
|
101 |
* that is not an <code>X509Certificate</code> |
|
102 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
103 |
@SuppressWarnings("unchecked") |
2 | 104 |
public X509CertPath(List<? extends Certificate> certs) throws CertificateException { |
105 |
super("X.509"); |
|
106 |
||
107 |
// Ensure that the List contains only X509Certificates |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
108 |
for (Certificate obj : certs) { |
2 | 109 |
if (obj instanceof X509Certificate == false) { |
110 |
throw new CertificateException |
|
111 |
("List is not all X509Certificates: " |
|
112 |
+ obj.getClass().getName()); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
// Assumes that the resulting List is thread-safe. This is true |
|
117 |
// because we ensure that it cannot be modified after construction |
|
118 |
// and the methods in the Sun JDK 1.4 implementation of ArrayList that |
|
119 |
// allow read-only access are thread-safe. |
|
120 |
this.certs = Collections.unmodifiableList( |
|
121 |
new ArrayList<X509Certificate>((List<X509Certificate>)certs)); |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Creates an <code>X509CertPath</code>, reading the encoded form |
|
126 |
* from an <code>InputStream</code>. The data is assumed to be in |
|
127 |
* the default encoding. |
|
128 |
* |
|
129 |
* @param is the <code>InputStream</code> to read the data from |
|
130 |
* @exception CertificateException if an exception occurs while decoding |
|
131 |
*/ |
|
132 |
public X509CertPath(InputStream is) throws CertificateException { |
|
133 |
this(is, PKIPATH_ENCODING); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Creates an <code>X509CertPath</code>, reading the encoded form |
|
138 |
* from an InputStream. The data is assumed to be in the specified |
|
139 |
* encoding. |
|
140 |
* |
|
141 |
* @param is the <code>InputStream</code> to read the data from |
|
142 |
* @param encoding the encoding used |
|
143 |
* @exception CertificateException if an exception occurs while decoding or |
|
144 |
* the encoding requested is not supported |
|
145 |
*/ |
|
146 |
public X509CertPath(InputStream is, String encoding) |
|
147 |
throws CertificateException { |
|
148 |
super("X.509"); |
|
149 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
150 |
switch (encoding) { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
151 |
case PKIPATH_ENCODING: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
152 |
certs = parsePKIPATH(is); |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
153 |
break; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
154 |
case PKCS7_ENCODING: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
155 |
certs = parsePKCS7(is); |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
156 |
break; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
157 |
default: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
158 |
throw new CertificateException("unsupported encoding"); |
2 | 159 |
} |
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Parse a PKIPATH format CertPath from an InputStream. Return an |
|
164 |
* unmodifiable List of the certificates. |
|
165 |
* |
|
166 |
* @param is the <code>InputStream</code> to read the data from |
|
167 |
* @return an unmodifiable List of the certificates |
|
168 |
* @exception CertificateException if an exception occurs |
|
169 |
*/ |
|
170 |
private static List<X509Certificate> parsePKIPATH(InputStream is) |
|
171 |
throws CertificateException { |
|
172 |
List<X509Certificate> certList = null; |
|
173 |
CertificateFactory certFac = null; |
|
174 |
||
175 |
if (is == null) { |
|
176 |
throw new CertificateException("input stream is null"); |
|
177 |
} |
|
178 |
||
179 |
try { |
|
180 |
DerInputStream dis = new DerInputStream(readAllBytes(is)); |
|
181 |
DerValue[] seq = dis.getSequence(3); |
|
182 |
if (seq.length == 0) { |
|
183 |
return Collections.<X509Certificate>emptyList(); |
|
184 |
} |
|
185 |
||
186 |
certFac = CertificateFactory.getInstance("X.509"); |
|
187 |
certList = new ArrayList<X509Certificate>(seq.length); |
|
188 |
||
189 |
// append certs in reverse order (target to trust anchor) |
|
190 |
for (int i = seq.length-1; i >= 0; i--) { |
|
191 |
certList.add((X509Certificate)certFac.generateCertificate |
|
192 |
(new ByteArrayInputStream(seq[i].toByteArray()))); |
|
193 |
} |
|
194 |
||
195 |
return Collections.unmodifiableList(certList); |
|
196 |
||
197 |
} catch (IOException ioe) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
198 |
throw new CertificateException("IOException parsing PkiPath data: " |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
199 |
+ ioe, ioe); |
2 | 200 |
} |
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Parse a PKCS#7 format CertPath from an InputStream. Return an |
|
205 |
* unmodifiable List of the certificates. |
|
206 |
* |
|
207 |
* @param is the <code>InputStream</code> to read the data from |
|
208 |
* @return an unmodifiable List of the certificates |
|
209 |
* @exception CertificateException if an exception occurs |
|
210 |
*/ |
|
211 |
private static List<X509Certificate> parsePKCS7(InputStream is) |
|
212 |
throws CertificateException { |
|
213 |
List<X509Certificate> certList; |
|
214 |
||
215 |
if (is == null) { |
|
216 |
throw new CertificateException("input stream is null"); |
|
217 |
} |
|
218 |
||
219 |
try { |
|
220 |
if (is.markSupported() == false) { |
|
221 |
// Copy the entire input stream into an InputStream that does |
|
222 |
// support mark |
|
223 |
is = new ByteArrayInputStream(readAllBytes(is)); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
224 |
} |
2 | 225 |
PKCS7 pkcs7 = new PKCS7(is); |
226 |
||
227 |
X509Certificate[] certArray = pkcs7.getCertificates(); |
|
228 |
// certs are optional in PKCS #7 |
|
229 |
if (certArray != null) { |
|
230 |
certList = Arrays.asList(certArray); |
|
231 |
} else { |
|
232 |
// no certs provided |
|
233 |
certList = new ArrayList<X509Certificate>(0); |
|
234 |
} |
|
235 |
} catch (IOException ioe) { |
|
236 |
throw new CertificateException("IOException parsing PKCS7 data: " + |
|
237 |
ioe); |
|
238 |
} |
|
239 |
// Assumes that the resulting List is thread-safe. This is true |
|
240 |
// because we ensure that it cannot be modified after construction |
|
241 |
// and the methods in the Sun JDK 1.4 implementation of ArrayList that |
|
242 |
// allow read-only access are thread-safe. |
|
243 |
return Collections.unmodifiableList(certList); |
|
244 |
} |
|
245 |
||
246 |
/* |
|
247 |
* Reads the entire contents of an InputStream into a byte array. |
|
248 |
* |
|
249 |
* @param is the InputStream to read from |
|
250 |
* @return the bytes read from the InputStream |
|
251 |
*/ |
|
252 |
private static byte[] readAllBytes(InputStream is) throws IOException { |
|
253 |
byte[] buffer = new byte[8192]; |
|
254 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); |
|
255 |
int n; |
|
256 |
while ((n = is.read(buffer)) != -1) { |
|
257 |
baos.write(buffer, 0, n); |
|
258 |
} |
|
259 |
return baos.toByteArray(); |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* Returns the encoded form of this certification path, using the |
|
264 |
* default encoding. |
|
265 |
* |
|
266 |
* @return the encoded bytes |
|
267 |
* @exception CertificateEncodingException if an encoding error occurs |
|
268 |
*/ |
|
269 |
public byte[] getEncoded() throws CertificateEncodingException { |
|
270 |
// @@@ Should cache the encoded form |
|
271 |
return encodePKIPATH(); |
|
272 |
} |
|
273 |
||
274 |
/** |
|
275 |
* Encode the CertPath using PKIPATH format. |
|
276 |
* |
|
277 |
* @return a byte array containing the binary encoding of the PkiPath object |
|
278 |
* @exception CertificateEncodingException if an exception occurs |
|
279 |
*/ |
|
280 |
private byte[] encodePKIPATH() throws CertificateEncodingException { |
|
281 |
||
282 |
ListIterator<X509Certificate> li = certs.listIterator(certs.size()); |
|
283 |
try { |
|
284 |
DerOutputStream bytes = new DerOutputStream(); |
|
285 |
// encode certs in reverse order (trust anchor to target) |
|
286 |
// according to PkiPath format |
|
287 |
while (li.hasPrevious()) { |
|
288 |
X509Certificate cert = li.previous(); |
|
289 |
// check for duplicate cert |
|
290 |
if (certs.lastIndexOf(cert) != certs.indexOf(cert)) { |
|
291 |
throw new CertificateEncodingException |
|
292 |
("Duplicate Certificate"); |
|
293 |
} |
|
294 |
// get encoded certificates |
|
295 |
byte[] encoded = cert.getEncoded(); |
|
296 |
bytes.write(encoded); |
|
297 |
} |
|
298 |
||
299 |
// Wrap the data in a SEQUENCE |
|
300 |
DerOutputStream derout = new DerOutputStream(); |
|
301 |
derout.write(DerValue.tag_SequenceOf, bytes); |
|
302 |
return derout.toByteArray(); |
|
303 |
||
304 |
} catch (IOException ioe) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
305 |
throw new CertificateEncodingException("IOException encoding " + |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
306 |
"PkiPath data: " + ioe, ioe); |
2 | 307 |
} |
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Encode the CertPath using PKCS#7 format. |
|
312 |
* |
|
313 |
* @return a byte array containing the binary encoding of the PKCS#7 object |
|
314 |
* @exception CertificateEncodingException if an exception occurs |
|
315 |
*/ |
|
316 |
private byte[] encodePKCS7() throws CertificateEncodingException { |
|
317 |
PKCS7 p7 = new PKCS7(new AlgorithmId[0], |
|
318 |
new ContentInfo(ContentInfo.DATA_OID, null), |
|
319 |
certs.toArray(new X509Certificate[certs.size()]), |
|
320 |
new SignerInfo[0]); |
|
321 |
DerOutputStream derout = new DerOutputStream(); |
|
322 |
try { |
|
323 |
p7.encodeSignedData(derout); |
|
324 |
} catch (IOException ioe) { |
|
325 |
throw new CertificateEncodingException(ioe.getMessage()); |
|
326 |
} |
|
327 |
return derout.toByteArray(); |
|
328 |
} |
|
329 |
||
330 |
/** |
|
331 |
* Returns the encoded form of this certification path, using the |
|
332 |
* specified encoding. |
|
333 |
* |
|
334 |
* @param encoding the name of the encoding to use |
|
335 |
* @return the encoded bytes |
|
336 |
* @exception CertificateEncodingException if an encoding error occurs or |
|
337 |
* the encoding requested is not supported |
|
338 |
*/ |
|
339 |
public byte[] getEncoded(String encoding) |
|
340 |
throws CertificateEncodingException { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
341 |
switch (encoding) { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
342 |
case PKIPATH_ENCODING: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
343 |
return encodePKIPATH(); |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
344 |
case PKCS7_ENCODING: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
345 |
return encodePKCS7(); |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
346 |
default: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
347 |
throw new CertificateEncodingException("unsupported encoding"); |
2 | 348 |
} |
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Returns the encodings supported by this certification path, with the |
|
353 |
* default encoding first. |
|
354 |
* |
|
355 |
* @return an <code>Iterator</code> over the names of the supported |
|
356 |
* encodings (as Strings) |
|
357 |
*/ |
|
358 |
public static Iterator<String> getEncodingsStatic() { |
|
359 |
return encodingList.iterator(); |
|
360 |
} |
|
361 |
||
362 |
/** |
|
363 |
* Returns an iteration of the encodings supported by this certification |
|
364 |
* path, with the default encoding first. |
|
365 |
* <p> |
|
366 |
* Attempts to modify the returned <code>Iterator</code> via its |
|
367 |
* <code>remove</code> method result in an |
|
368 |
* <code>UnsupportedOperationException</code>. |
|
369 |
* |
|
370 |
* @return an <code>Iterator</code> over the names of the supported |
|
371 |
* encodings (as Strings) |
|
372 |
*/ |
|
373 |
public Iterator<String> getEncodings() { |
|
374 |
return getEncodingsStatic(); |
|
375 |
} |
|
376 |
||
377 |
/** |
|
378 |
* Returns the list of certificates in this certification path. |
|
379 |
* The <code>List</code> returned must be immutable and thread-safe. |
|
380 |
* |
|
381 |
* @return an immutable <code>List</code> of <code>X509Certificate</code>s |
|
382 |
* (may be empty, but not null) |
|
383 |
*/ |
|
384 |
public List<X509Certificate> getCertificates() { |
|
385 |
return certs; |
|
386 |
} |
|
387 |
} |