author | xuelei |
Mon, 14 Feb 2011 13:31:13 -0800 | |
changeset 8395 | 29afb3ec47e2 |
parent 8163 | d9bcc1208691 |
child 8790 | 2a8d836ee007 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
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.IOException; |
|
29 |
import java.util.*; |
|
30 |
||
31 |
import java.security.GeneralSecurityException; |
|
32 |
import java.security.InvalidKeyException; |
|
3225
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
33 |
import java.security.cert.Certificate; |
2 | 34 |
import java.security.cert.CertificateException; |
35 |
import java.security.cert.CertPathValidatorException; |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
792
diff
changeset
|
36 |
import java.security.cert.PKIXReason; |
2 | 37 |
import java.security.cert.CertStore; |
38 |
import java.security.cert.CertStoreException; |
|
39 |
import java.security.cert.PKIXBuilderParameters; |
|
40 |
import java.security.cert.PKIXCertPathChecker; |
|
41 |
import java.security.cert.TrustAnchor; |
|
42 |
import java.security.cert.X509Certificate; |
|
43 |
import java.security.cert.X509CertSelector; |
|
44 |
import javax.security.auth.x500.X500Principal; |
|
45 |
||
46 |
import sun.security.util.Debug; |
|
3225
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
47 |
import sun.security.util.DerOutputStream; |
2 | 48 |
import sun.security.x509.AccessDescription; |
49 |
import sun.security.x509.AuthorityInfoAccessExtension; |
|
50 |
import sun.security.x509.PKIXExtensions; |
|
51 |
import sun.security.x509.PolicyMappingsExtension; |
|
52 |
import sun.security.x509.X500Name; |
|
53 |
import sun.security.x509.X509CertImpl; |
|
3225
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
54 |
import sun.security.x509.X509CRLImpl; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
55 |
import sun.security.x509.AuthorityKeyIdentifierExtension; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
56 |
import sun.security.x509.KeyIdentifier; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
57 |
import sun.security.x509.SubjectKeyIdentifierExtension; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
58 |
import sun.security.x509.SerialNumber; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
59 |
import sun.security.x509.GeneralNames; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
60 |
import sun.security.x509.GeneralName; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
61 |
import sun.security.x509.GeneralNameInterface; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
62 |
import java.math.BigInteger; |
2 | 63 |
|
64 |
/** |
|
65 |
* This class represents a forward builder, which is able to retrieve |
|
66 |
* matching certificates from CertStores and verify a particular certificate |
|
67 |
* against a ForwardState. |
|
68 |
* |
|
69 |
* @since 1.4 |
|
70 |
* @author Yassir Elley |
|
71 |
* @author Sean Mullan |
|
72 |
*/ |
|
73 |
class ForwardBuilder extends Builder { |
|
74 |
||
75 |
private static final Debug debug = Debug.getInstance("certpath"); |
|
76 |
private final Set<X509Certificate> trustedCerts; |
|
77 |
private final Set<X500Principal> trustedSubjectDNs; |
|
78 |
private final Set<TrustAnchor> trustAnchors; |
|
79 |
private X509CertSelector eeSelector; |
|
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
80 |
private AdaptableX509CertSelector caSelector; |
2 | 81 |
private X509CertSelector caTargetSelector; |
82 |
TrustAnchor trustAnchor; |
|
83 |
private Comparator<X509Certificate> comparator; |
|
84 |
private boolean searchAllCertStores = true; |
|
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
3225
diff
changeset
|
85 |
private boolean onlyEECert = false; |
2 | 86 |
|
87 |
/** |
|
88 |
* Initialize the builder with the input parameters. |
|
89 |
* |
|
90 |
* @param params the parameter set used to build a certification path |
|
91 |
*/ |
|
92 |
ForwardBuilder(PKIXBuilderParameters buildParams, |
|
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
3225
diff
changeset
|
93 |
X500Principal targetSubjectDN, boolean searchAllCertStores, |
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
3225
diff
changeset
|
94 |
boolean onlyEECert) |
2 | 95 |
{ |
96 |
super(buildParams, targetSubjectDN); |
|
97 |
||
98 |
// populate sets of trusted certificates and subject DNs |
|
99 |
trustAnchors = buildParams.getTrustAnchors(); |
|
100 |
trustedCerts = new HashSet<X509Certificate>(trustAnchors.size()); |
|
101 |
trustedSubjectDNs = new HashSet<X500Principal>(trustAnchors.size()); |
|
102 |
for (TrustAnchor anchor : trustAnchors) { |
|
103 |
X509Certificate trustedCert = anchor.getTrustedCert(); |
|
104 |
if (trustedCert != null) { |
|
105 |
trustedCerts.add(trustedCert); |
|
106 |
trustedSubjectDNs.add(trustedCert.getSubjectX500Principal()); |
|
107 |
} else { |
|
108 |
trustedSubjectDNs.add(anchor.getCA()); |
|
109 |
} |
|
110 |
} |
|
111 |
comparator = new PKIXCertComparator(trustedSubjectDNs); |
|
112 |
this.searchAllCertStores = searchAllCertStores; |
|
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
3225
diff
changeset
|
113 |
this.onlyEECert = onlyEECert; |
2 | 114 |
} |
115 |
||
116 |
/** |
|
117 |
* Retrieves all certs from the specified CertStores that satisfy the |
|
118 |
* requirements specified in the parameters and the current |
|
119 |
* PKIX state (name constraints, policy constraints, etc). |
|
120 |
* |
|
121 |
* @param currentState the current state. |
|
122 |
* Must be an instance of <code>ForwardState</code> |
|
123 |
* @param certStores list of CertStores |
|
124 |
*/ |
|
125 |
Collection<X509Certificate> getMatchingCerts |
|
126 |
(State currentState, List<CertStore> certStores) |
|
127 |
throws CertStoreException, CertificateException, IOException |
|
128 |
{ |
|
129 |
if (debug != null) { |
|
130 |
debug.println("ForwardBuilder.getMatchingCerts()..."); |
|
131 |
} |
|
132 |
||
133 |
ForwardState currState = (ForwardState) currentState; |
|
134 |
||
135 |
/* |
|
136 |
* We store certs in a Set because we don't want duplicates. |
|
137 |
* As each cert is added, it is sorted based on the PKIXCertComparator |
|
138 |
* algorithm. |
|
139 |
*/ |
|
140 |
Set<X509Certificate> certs = new TreeSet<X509Certificate>(comparator); |
|
141 |
||
142 |
/* |
|
143 |
* Only look for EE certs if search has just started. |
|
144 |
*/ |
|
145 |
if (currState.isInitial()) { |
|
146 |
getMatchingEECerts(currState, certStores, certs); |
|
147 |
} |
|
148 |
getMatchingCACerts(currState, certStores, certs); |
|
149 |
||
150 |
return certs; |
|
151 |
} |
|
152 |
||
153 |
/* |
|
154 |
* Retrieves all end-entity certificates which satisfy constraints |
|
155 |
* and requirements specified in the parameters and PKIX state. |
|
156 |
*/ |
|
157 |
private void getMatchingEECerts(ForwardState currentState, |
|
158 |
List<CertStore> certStores, Collection<X509Certificate> eeCerts) |
|
159 |
throws IOException { |
|
160 |
||
161 |
if (debug != null) { |
|
162 |
debug.println("ForwardBuilder.getMatchingEECerts()..."); |
|
163 |
} |
|
164 |
/* |
|
165 |
* Compose a certificate matching rule to filter out |
|
166 |
* certs which don't satisfy constraints |
|
167 |
* |
|
168 |
* First, retrieve clone of current target cert constraints, |
|
169 |
* and then add more selection criteria based on current validation |
|
170 |
* state. Since selector never changes, cache local copy & reuse. |
|
171 |
*/ |
|
172 |
if (eeSelector == null) { |
|
173 |
eeSelector = (X509CertSelector) targetCertConstraints.clone(); |
|
174 |
||
175 |
/* |
|
176 |
* Match on certificate validity date |
|
177 |
*/ |
|
178 |
eeSelector.setCertificateValid(date); |
|
179 |
||
180 |
/* |
|
181 |
* Policy processing optimizations |
|
182 |
*/ |
|
183 |
if (buildParams.isExplicitPolicyRequired()) { |
|
184 |
eeSelector.setPolicy(getMatchingPolicies()); |
|
185 |
} |
|
186 |
/* |
|
187 |
* Require EE certs |
|
188 |
*/ |
|
189 |
eeSelector.setBasicConstraints(-2); |
|
190 |
} |
|
191 |
||
192 |
/* Retrieve matching EE certs from CertStores */ |
|
193 |
addMatchingCerts(eeSelector, certStores, eeCerts, searchAllCertStores); |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Retrieves all CA certificates which satisfy constraints |
|
198 |
* and requirements specified in the parameters and PKIX state. |
|
199 |
*/ |
|
200 |
private void getMatchingCACerts(ForwardState currentState, |
|
201 |
List<CertStore> certStores, Collection<X509Certificate> caCerts) |
|
202 |
throws IOException { |
|
203 |
||
204 |
if (debug != null) { |
|
205 |
debug.println("ForwardBuilder.getMatchingCACerts()..."); |
|
206 |
} |
|
207 |
int initialSize = caCerts.size(); |
|
208 |
||
209 |
/* |
|
210 |
* Compose a CertSelector to filter out |
|
211 |
* certs which do not satisfy requirements. |
|
212 |
*/ |
|
213 |
X509CertSelector sel = null; |
|
214 |
||
215 |
if (currentState.isInitial()) { |
|
792
214933bde360
6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
2
diff
changeset
|
216 |
if (targetCertConstraints.getBasicConstraints() == -2) { |
214933bde360
6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
2
diff
changeset
|
217 |
// no need to continue: this means we never can match a CA cert |
214933bde360
6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
2
diff
changeset
|
218 |
return; |
214933bde360
6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
2
diff
changeset
|
219 |
} |
214933bde360
6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
2
diff
changeset
|
220 |
|
2 | 221 |
/* This means a CA is the target, so match on same stuff as |
222 |
* getMatchingEECerts |
|
223 |
*/ |
|
224 |
if (debug != null) { |
|
225 |
debug.println("ForwardBuilder.getMatchingCACerts(): ca is target"); |
|
226 |
} |
|
227 |
||
228 |
if (caTargetSelector == null) { |
|
229 |
caTargetSelector = (X509CertSelector) |
|
230 |
targetCertConstraints.clone(); |
|
231 |
||
232 |
/* |
|
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
233 |
* Since we don't check the validity period of trusted |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
234 |
* certificates, please don't set the certificate valid |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
235 |
* criterion unless the trusted certificate matching is |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
236 |
* completed. |
2 | 237 |
*/ |
238 |
||
239 |
/* |
|
240 |
* Policy processing optimizations |
|
241 |
*/ |
|
242 |
if (buildParams.isExplicitPolicyRequired()) |
|
243 |
caTargetSelector.setPolicy(getMatchingPolicies()); |
|
244 |
} |
|
245 |
||
246 |
/* |
|
247 |
* Require CA certs with a pathLenConstraint that allows |
|
248 |
* at least as many CA certs that have already been traversed |
|
249 |
*/ |
|
250 |
caTargetSelector.setBasicConstraints(currentState.traversedCACerts); |
|
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
251 |
|
2 | 252 |
sel = caTargetSelector; |
253 |
} else { |
|
254 |
||
255 |
if (caSelector == null) { |
|
3225
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
256 |
caSelector = new AdaptableX509CertSelector(); |
2 | 257 |
|
258 |
/* |
|
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
259 |
* Since we don't check the validity period of trusted |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
260 |
* certificates, please don't set the certificate valid |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
261 |
* criterion unless the trusted certificate matching is |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
262 |
* completed. |
2 | 263 |
*/ |
264 |
||
265 |
/* |
|
266 |
* Policy processing optimizations |
|
267 |
*/ |
|
268 |
if (buildParams.isExplicitPolicyRequired()) |
|
269 |
caSelector.setPolicy(getMatchingPolicies()); |
|
270 |
} |
|
271 |
||
272 |
/* |
|
273 |
* Match on subject (issuer of previous cert) |
|
274 |
*/ |
|
275 |
caSelector.setSubject(currentState.issuerDN); |
|
276 |
||
277 |
/* |
|
278 |
* Match on subjectNamesTraversed (both DNs and AltNames) |
|
279 |
* (checks that current cert's name constraints permit it |
|
280 |
* to certify all the DNs and AltNames that have been traversed) |
|
281 |
*/ |
|
282 |
CertPathHelper.setPathToNames |
|
283 |
(caSelector, currentState.subjectNamesTraversed); |
|
284 |
||
285 |
/* |
|
286 |
* Require CA certs with a pathLenConstraint that allows |
|
287 |
* at least as many CA certs that have already been traversed |
|
288 |
*/ |
|
289 |
caSelector.setBasicConstraints(currentState.traversedCACerts); |
|
3225
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
290 |
|
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
291 |
/* |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
292 |
* Facilitate certification path construction with authority |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
293 |
* key identifier and subject key identifier. |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
294 |
*/ |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
295 |
AuthorityKeyIdentifierExtension akidext = |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
296 |
currentState.cert.getAuthorityKeyIdentifierExtension(); |
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
297 |
caSelector.parseAuthorityKeyIdentifierExtension(akidext); |
3225
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
298 |
|
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
299 |
/* |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
300 |
* check the validity period |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
301 |
*/ |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
302 |
caSelector.setValidityPeriod(currentState.cert.getNotBefore(), |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
303 |
currentState.cert.getNotAfter()); |
3225
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
304 |
|
2 | 305 |
sel = caSelector; |
306 |
} |
|
307 |
||
308 |
for (X509Certificate trustedCert : trustedCerts) { |
|
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
309 |
if (sel.match(trustedCert)) { |
2 | 310 |
if (debug != null) { |
311 |
debug.println("ForwardBuilder.getMatchingCACerts: " |
|
312 |
+ "found matching trust anchor"); |
|
313 |
} |
|
314 |
if (caCerts.add(trustedCert) && !searchAllCertStores) { |
|
315 |
return; |
|
316 |
} |
|
317 |
} |
|
318 |
} |
|
319 |
||
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
320 |
/* |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
321 |
* The trusted certificate matching is completed. We need to match |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
322 |
* on certificate validity date. |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
323 |
*/ |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
324 |
sel.setCertificateValid(date); |
2 | 325 |
|
326 |
/* |
|
327 |
* If we have already traversed as many CA certs as the maxPathLength |
|
328 |
* will allow us to, then we don't bother looking through these |
|
329 |
* certificate pairs. If maxPathLength has a value of -1, this |
|
330 |
* means it is unconstrained, so we always look through the |
|
331 |
* certificate pairs. |
|
332 |
*/ |
|
333 |
if (currentState.isInitial() || |
|
334 |
(buildParams.getMaxPathLength() == -1) || |
|
335 |
(buildParams.getMaxPathLength() > currentState.traversedCACerts)) |
|
336 |
{ |
|
8163
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
337 |
if (addMatchingCerts(sel, certStores, |
d9bcc1208691
7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
7040
diff
changeset
|
338 |
caCerts, searchAllCertStores) && !searchAllCertStores) { |
2 | 339 |
return; |
340 |
} |
|
341 |
} |
|
342 |
||
343 |
if (!currentState.isInitial() && Builder.USE_AIA) { |
|
344 |
// check for AuthorityInformationAccess extension |
|
345 |
AuthorityInfoAccessExtension aiaExt = |
|
346 |
currentState.cert.getAuthorityInfoAccessExtension(); |
|
347 |
if (aiaExt != null) { |
|
348 |
getCerts(aiaExt, caCerts); |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
if (debug != null) { |
|
353 |
int numCerts = caCerts.size() - initialSize; |
|
354 |
debug.println("ForwardBuilder.getMatchingCACerts: found " + |
|
355 |
numCerts + " CA certs"); |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
/** |
|
360 |
* Download Certificates from the given AIA and add them to the |
|
361 |
* specified Collection. |
|
362 |
*/ |
|
363 |
private boolean getCerts(AuthorityInfoAccessExtension aiaExt, |
|
364 |
Collection<X509Certificate> certs) { |
|
365 |
if (Builder.USE_AIA == false) { |
|
366 |
return false; |
|
367 |
} |
|
368 |
List<AccessDescription> adList = aiaExt.getAccessDescriptions(); |
|
369 |
if (adList == null || adList.isEmpty()) { |
|
370 |
return false; |
|
371 |
} |
|
372 |
||
373 |
boolean add = false; |
|
374 |
for (AccessDescription ad : adList) { |
|
375 |
CertStore cs = URICertStore.getInstance(ad); |
|
376 |
try { |
|
377 |
if (certs.addAll((Collection<X509Certificate>) |
|
378 |
cs.getCertificates(caSelector))) { |
|
379 |
add = true; |
|
380 |
if (!searchAllCertStores) { |
|
381 |
return true; |
|
382 |
} |
|
383 |
} |
|
384 |
} catch (CertStoreException cse) { |
|
385 |
if (debug != null) { |
|
386 |
debug.println("exception getting certs from CertStore:"); |
|
387 |
cse.printStackTrace(); |
|
388 |
} |
|
389 |
continue; |
|
390 |
} |
|
391 |
} |
|
392 |
return add; |
|
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* This inner class compares 2 PKIX certificates according to which |
|
397 |
* should be tried first when building a path from the target. |
|
398 |
* The preference order is as follows: |
|
399 |
* |
|
400 |
* Given trusted certificate(s): |
|
401 |
* Subject:ou=D,ou=C,o=B,c=A |
|
402 |
* |
|
403 |
* Preference order for current cert: |
|
404 |
* |
|
405 |
* 1) Issuer matches a trusted subject |
|
406 |
* Issuer: ou=D,ou=C,o=B,c=A |
|
407 |
* |
|
408 |
* 2) Issuer is a descendant of a trusted subject (in order of |
|
409 |
* number of links to the trusted subject) |
|
410 |
* a) Issuer: ou=E,ou=D,ou=C,o=B,c=A [links=1] |
|
411 |
* b) Issuer: ou=F,ou=E,ou=D,ou=C,ou=B,c=A [links=2] |
|
412 |
* |
|
413 |
* 3) Issuer is an ancestor of a trusted subject (in order of number of |
|
414 |
* links to the trusted subject) |
|
415 |
* a) Issuer: ou=C,o=B,c=A [links=1] |
|
416 |
* b) Issuer: o=B,c=A [links=2] |
|
417 |
* |
|
418 |
* 4) Issuer is in the same namespace as a trusted subject (in order of |
|
419 |
* number of links to the trusted subject) |
|
420 |
* a) Issuer: ou=G,ou=C,o=B,c=A [links=2] |
|
421 |
* b) Issuer: ou=H,o=B,c=A [links=3] |
|
422 |
* |
|
423 |
* 5) Issuer is an ancestor of certificate subject (in order of number |
|
424 |
* of links to the certificate subject) |
|
425 |
* a) Issuer: ou=K,o=J,c=A |
|
426 |
* Subject: ou=L,ou=K,o=J,c=A |
|
427 |
* b) Issuer: o=J,c=A |
|
428 |
* Subject: ou=L,ou=K,0=J,c=A |
|
429 |
* |
|
430 |
* 6) Any other certificates |
|
431 |
*/ |
|
432 |
static class PKIXCertComparator implements Comparator<X509Certificate> { |
|
433 |
||
434 |
final static String METHOD_NME = "PKIXCertComparator.compare()"; |
|
435 |
||
436 |
private final Set<X500Principal> trustedSubjectDNs; |
|
437 |
||
438 |
PKIXCertComparator(Set<X500Principal> trustedSubjectDNs) { |
|
439 |
this.trustedSubjectDNs = trustedSubjectDNs; |
|
440 |
} |
|
441 |
||
442 |
/** |
|
443 |
* @param oCert1 First X509Certificate to be compared |
|
444 |
* @param oCert2 Second X509Certificate to be compared |
|
445 |
* @return -1 if oCert1 is preferable to oCert2, or |
|
446 |
* if oCert1 and oCert2 are equally preferable (in this |
|
447 |
* case it doesn't matter which is preferable, but we don't |
|
448 |
* return 0 because the comparator would behave strangely |
|
449 |
* when used in a SortedSet). |
|
450 |
* 1 if oCert2 is preferable to oCert1 |
|
451 |
* 0 if oCert1.equals(oCert2). We only return 0 if the |
|
452 |
* certs are equal so that this comparator behaves |
|
453 |
* correctly when used in a SortedSet. |
|
454 |
* @throws ClassCastException if either argument is not of type |
|
455 |
* X509Certificate |
|
456 |
*/ |
|
457 |
public int compare(X509Certificate oCert1, X509Certificate oCert2) { |
|
458 |
||
459 |
// if certs are the same, return 0 |
|
460 |
if (oCert1.equals(oCert2)) return 0; |
|
461 |
||
462 |
X500Principal cIssuer1 = oCert1.getIssuerX500Principal(); |
|
463 |
X500Principal cIssuer2 = oCert2.getIssuerX500Principal(); |
|
464 |
X500Name cIssuer1Name = X500Name.asX500Name(cIssuer1); |
|
465 |
X500Name cIssuer2Name = X500Name.asX500Name(cIssuer2); |
|
466 |
||
467 |
if (debug != null) { |
|
468 |
debug.println(METHOD_NME + " o1 Issuer: " + cIssuer1); |
|
469 |
debug.println(METHOD_NME + " o2 Issuer: " + cIssuer2); |
|
470 |
} |
|
471 |
||
472 |
/* If one cert's issuer matches a trusted subject, then it is |
|
473 |
* preferable. |
|
474 |
*/ |
|
475 |
if (debug != null) { |
|
476 |
debug.println(METHOD_NME + " MATCH TRUSTED SUBJECT TEST..."); |
|
477 |
} |
|
478 |
||
479 |
boolean m1 = trustedSubjectDNs.contains(cIssuer1); |
|
480 |
boolean m2 = trustedSubjectDNs.contains(cIssuer2); |
|
481 |
if (debug != null) { |
|
482 |
debug.println(METHOD_NME + " m1: " + m1); |
|
483 |
debug.println(METHOD_NME + " m2: " + m2); |
|
484 |
} |
|
485 |
if (m1 && m2) { |
|
486 |
return -1; |
|
487 |
} else if (m1) { |
|
488 |
return -1; |
|
489 |
} else if (m2) { |
|
490 |
return 1; |
|
491 |
} |
|
492 |
||
493 |
/* If one cert's issuer is a naming descendant of a trusted subject, |
|
494 |
* then it is preferable, in order of increasing naming distance. |
|
495 |
*/ |
|
496 |
if (debug != null) { |
|
497 |
debug.println(METHOD_NME + " NAMING DESCENDANT TEST..."); |
|
498 |
} |
|
499 |
for (X500Principal tSubject : trustedSubjectDNs) { |
|
500 |
X500Name tSubjectName = X500Name.asX500Name(tSubject); |
|
501 |
int distanceTto1 = |
|
502 |
Builder.distance(tSubjectName, cIssuer1Name, -1); |
|
503 |
int distanceTto2 = |
|
504 |
Builder.distance(tSubjectName, cIssuer2Name, -1); |
|
505 |
if (debug != null) { |
|
506 |
debug.println(METHOD_NME +" distanceTto1: " + distanceTto1); |
|
507 |
debug.println(METHOD_NME +" distanceTto2: " + distanceTto2); |
|
508 |
} |
|
509 |
if (distanceTto1 > 0 || distanceTto2 > 0) { |
|
510 |
if (distanceTto1 == distanceTto2) { |
|
511 |
return -1; |
|
512 |
} else if (distanceTto1 > 0 && distanceTto2 <= 0) { |
|
513 |
return -1; |
|
514 |
} else if (distanceTto1 <= 0 && distanceTto2 > 0) { |
|
515 |
return 1; |
|
516 |
} else if (distanceTto1 < distanceTto2) { |
|
517 |
return -1; |
|
518 |
} else { // distanceTto1 > distanceTto2 |
|
519 |
return 1; |
|
520 |
} |
|
521 |
} |
|
522 |
} |
|
523 |
||
524 |
/* If one cert's issuer is a naming ancestor of a trusted subject, |
|
525 |
* then it is preferable, in order of increasing naming distance. |
|
526 |
*/ |
|
527 |
if (debug != null) { |
|
528 |
debug.println(METHOD_NME + " NAMING ANCESTOR TEST..."); |
|
529 |
} |
|
530 |
for (X500Principal tSubject : trustedSubjectDNs) { |
|
531 |
X500Name tSubjectName = X500Name.asX500Name(tSubject); |
|
532 |
||
533 |
int distanceTto1 = Builder.distance |
|
534 |
(tSubjectName, cIssuer1Name, Integer.MAX_VALUE); |
|
535 |
int distanceTto2 = Builder.distance |
|
536 |
(tSubjectName, cIssuer2Name, Integer.MAX_VALUE); |
|
537 |
if (debug != null) { |
|
538 |
debug.println(METHOD_NME +" distanceTto1: " + distanceTto1); |
|
539 |
debug.println(METHOD_NME +" distanceTto2: " + distanceTto2); |
|
540 |
} |
|
541 |
if (distanceTto1 < 0 || distanceTto2 < 0) { |
|
542 |
if (distanceTto1 == distanceTto2) { |
|
543 |
return -1; |
|
544 |
} else if (distanceTto1 < 0 && distanceTto2 >= 0) { |
|
545 |
return -1; |
|
546 |
} else if (distanceTto1 >= 0 && distanceTto2 < 0) { |
|
547 |
return 1; |
|
548 |
} else if (distanceTto1 > distanceTto2) { |
|
549 |
return -1; |
|
550 |
} else { |
|
551 |
return 1; |
|
552 |
} |
|
553 |
} |
|
554 |
} |
|
555 |
||
556 |
/* If one cert's issuer is in the same namespace as a trusted |
|
557 |
* subject, then it is preferable, in order of increasing naming |
|
558 |
* distance. |
|
559 |
*/ |
|
560 |
if (debug != null) { |
|
561 |
debug.println(METHOD_NME +" SAME NAMESPACE AS TRUSTED TEST..."); |
|
562 |
} |
|
563 |
for (X500Principal tSubject : trustedSubjectDNs) { |
|
564 |
X500Name tSubjectName = X500Name.asX500Name(tSubject); |
|
565 |
X500Name tAo1 = tSubjectName.commonAncestor(cIssuer1Name); |
|
566 |
X500Name tAo2 = tSubjectName.commonAncestor(cIssuer2Name); |
|
567 |
if (debug != null) { |
|
568 |
debug.println(METHOD_NME +" tAo1: " + String.valueOf(tAo1)); |
|
569 |
debug.println(METHOD_NME +" tAo2: " + String.valueOf(tAo2)); |
|
570 |
} |
|
571 |
if (tAo1 != null || tAo2 != null) { |
|
572 |
if (tAo1 != null && tAo2 != null) { |
|
573 |
int hopsTto1 = Builder.hops |
|
574 |
(tSubjectName, cIssuer1Name, Integer.MAX_VALUE); |
|
575 |
int hopsTto2 = Builder.hops |
|
576 |
(tSubjectName, cIssuer2Name, Integer.MAX_VALUE); |
|
577 |
if (debug != null) { |
|
578 |
debug.println(METHOD_NME +" hopsTto1: " + hopsTto1); |
|
579 |
debug.println(METHOD_NME +" hopsTto2: " + hopsTto2); |
|
580 |
} |
|
581 |
if (hopsTto1 == hopsTto2) { |
|
582 |
} else if (hopsTto1 > hopsTto2) { |
|
583 |
return 1; |
|
584 |
} else { // hopsTto1 < hopsTto2 |
|
585 |
return -1; |
|
586 |
} |
|
587 |
} else if (tAo1 == null) { |
|
588 |
return 1; |
|
589 |
} else { |
|
590 |
return -1; |
|
591 |
} |
|
592 |
} |
|
593 |
} |
|
594 |
||
595 |
||
596 |
/* If one cert's issuer is an ancestor of that cert's subject, |
|
597 |
* then it is preferable, in order of increasing naming distance. |
|
598 |
*/ |
|
599 |
if (debug != null) { |
|
600 |
debug.println(METHOD_NME+" CERT ISSUER/SUBJECT COMPARISON TEST..."); |
|
601 |
} |
|
602 |
X500Principal cSubject1 = oCert1.getSubjectX500Principal(); |
|
603 |
X500Principal cSubject2 = oCert2.getSubjectX500Principal(); |
|
604 |
X500Name cSubject1Name = X500Name.asX500Name(cSubject1); |
|
605 |
X500Name cSubject2Name = X500Name.asX500Name(cSubject2); |
|
606 |
||
607 |
if (debug != null) { |
|
608 |
debug.println(METHOD_NME + " o1 Subject: " + cSubject1); |
|
609 |
debug.println(METHOD_NME + " o2 Subject: " + cSubject2); |
|
610 |
} |
|
611 |
int distanceStoI1 = Builder.distance |
|
612 |
(cSubject1Name, cIssuer1Name, Integer.MAX_VALUE); |
|
613 |
int distanceStoI2 = Builder.distance |
|
614 |
(cSubject2Name, cIssuer2Name, Integer.MAX_VALUE); |
|
615 |
if (debug != null) { |
|
616 |
debug.println(METHOD_NME + " distanceStoI1: " + distanceStoI1); |
|
617 |
debug.println(METHOD_NME + " distanceStoI2: " + distanceStoI2); |
|
618 |
} |
|
619 |
if (distanceStoI2 > distanceStoI1) { |
|
620 |
return -1; |
|
621 |
} else if (distanceStoI2 < distanceStoI1) { |
|
622 |
return 1; |
|
623 |
} |
|
624 |
||
625 |
/* Otherwise, certs are equally preferable. |
|
626 |
*/ |
|
627 |
if (debug != null) { |
|
628 |
debug.println(METHOD_NME + " no tests matched; RETURN 0"); |
|
629 |
} |
|
630 |
return -1; |
|
631 |
} |
|
632 |
} |
|
633 |
||
634 |
/** |
|
635 |
* Verifies a matching certificate. |
|
636 |
* |
|
637 |
* This method executes the validation steps in the PKIX path |
|
638 |
* validation algorithm <draft-ietf-pkix-new-part1-08.txt> which were |
|
639 |
* not satisfied by the selection criteria used by getCertificates() |
|
640 |
* to find the certs and only the steps that can be executed in a |
|
641 |
* forward direction (target to trust anchor). Those steps that can |
|
642 |
* only be executed in a reverse direction are deferred until the |
|
643 |
* complete path has been built. |
|
644 |
* |
|
645 |
* Trust anchor certs are not validated, but are used to verify the |
|
646 |
* signature and revocation status of the previous cert. |
|
647 |
* |
|
648 |
* If the last certificate is being verified (the one whose subject |
|
649 |
* matches the target subject, then steps in 6.1.4 of the PKIX |
|
650 |
* Certification Path Validation algorithm are NOT executed, |
|
651 |
* regardless of whether or not the last cert is an end-entity |
|
652 |
* cert or not. This allows callers to certify CA certs as |
|
653 |
* well as EE certs. |
|
654 |
* |
|
655 |
* @param cert the certificate to be verified |
|
656 |
* @param currentState the current state against which the cert is verified |
|
657 |
* @param certPathList the certPathList generated thus far |
|
658 |
*/ |
|
659 |
void verifyCert(X509Certificate cert, State currentState, |
|
660 |
List<X509Certificate> certPathList) throws GeneralSecurityException |
|
661 |
{ |
|
662 |
if (debug != null) { |
|
663 |
debug.println("ForwardBuilder.verifyCert(SN: " |
|
664 |
+ Debug.toHexString(cert.getSerialNumber()) |
|
665 |
+ "\n Issuer: " + cert.getIssuerX500Principal() + ")" |
|
666 |
+ "\n Subject: " + cert.getSubjectX500Principal() + ")"); |
|
667 |
} |
|
668 |
||
669 |
ForwardState currState = (ForwardState) currentState; |
|
670 |
||
671 |
/* |
|
672 |
* check for looping - abort a loop if |
|
673 |
* ((we encounter the same certificate twice) AND |
|
674 |
* ((policyMappingInhibited = true) OR (no policy mapping |
|
675 |
* extensions can be found between the occurences of the same |
|
676 |
* certificate))) |
|
677 |
*/ |
|
678 |
if (certPathList != null) { |
|
679 |
boolean policyMappingFound = false; |
|
680 |
for (X509Certificate cpListCert : certPathList) { |
|
681 |
X509CertImpl cpListCertImpl = X509CertImpl.toImpl(cpListCert); |
|
682 |
PolicyMappingsExtension policyMappingsExt |
|
683 |
= cpListCertImpl.getPolicyMappingsExtension(); |
|
684 |
if (policyMappingsExt != null) { |
|
685 |
policyMappingFound = true; |
|
686 |
} |
|
687 |
if (debug != null) { |
|
688 |
debug.println("policyMappingFound = " + policyMappingFound); |
|
689 |
} |
|
690 |
if (cert.equals(cpListCert)) { |
|
691 |
if ((buildParams.isPolicyMappingInhibited()) || |
|
692 |
(!policyMappingFound)) { |
|
693 |
if (debug != null) { |
|
694 |
debug.println("loop detected!!"); |
|
695 |
} |
|
696 |
throw new CertPathValidatorException("loop detected"); |
|
697 |
} |
|
698 |
} |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
/* check if trusted cert */ |
|
703 |
boolean isTrustedCert = trustedCerts.contains(cert); |
|
704 |
||
705 |
/* we don't perform any validation of the trusted cert */ |
|
706 |
if (!isTrustedCert) { |
|
707 |
/* |
|
708 |
* Check CRITICAL private extensions for user checkers that |
|
709 |
* support forward checking (forwardCheckers) and remove |
|
710 |
* ones we know how to check. |
|
711 |
*/ |
|
712 |
Set<String> unresCritExts = cert.getCriticalExtensionOIDs(); |
|
713 |
if (unresCritExts == null) { |
|
714 |
unresCritExts = Collections.<String>emptySet(); |
|
715 |
} |
|
716 |
for (PKIXCertPathChecker checker : currState.forwardCheckers) { |
|
717 |
checker.check(cert, unresCritExts); |
|
718 |
} |
|
719 |
||
720 |
/* |
|
721 |
* Remove extensions from user checkers that don't support |
|
722 |
* forward checking. After this step, we will have removed |
|
723 |
* all extensions that all user checkers are capable of |
|
724 |
* processing. |
|
725 |
*/ |
|
726 |
for (PKIXCertPathChecker checker : buildParams.getCertPathCheckers()) { |
|
727 |
if (!checker.isForwardCheckingSupported()) { |
|
728 |
Set<String> supportedExts = checker.getSupportedExtensions(); |
|
729 |
if (supportedExts != null) { |
|
730 |
unresCritExts.removeAll(supportedExts); |
|
731 |
} |
|
732 |
} |
|
733 |
} |
|
734 |
||
735 |
/* |
|
736 |
* Look at the remaining extensions and remove any ones we know how |
|
737 |
* to check. If there are any left, throw an exception! |
|
738 |
*/ |
|
739 |
if (!unresCritExts.isEmpty()) { |
|
740 |
unresCritExts.remove( |
|
741 |
PKIXExtensions.BasicConstraints_Id.toString()); |
|
742 |
unresCritExts.remove( |
|
743 |
PKIXExtensions.NameConstraints_Id.toString()); |
|
744 |
unresCritExts.remove( |
|
745 |
PKIXExtensions.CertificatePolicies_Id.toString()); |
|
746 |
unresCritExts.remove( |
|
747 |
PKIXExtensions.PolicyMappings_Id.toString()); |
|
748 |
unresCritExts.remove( |
|
749 |
PKIXExtensions.PolicyConstraints_Id.toString()); |
|
750 |
unresCritExts.remove( |
|
751 |
PKIXExtensions.InhibitAnyPolicy_Id.toString()); |
|
752 |
unresCritExts.remove( |
|
753 |
PKIXExtensions.SubjectAlternativeName_Id.toString()); |
|
754 |
unresCritExts.remove(PKIXExtensions.KeyUsage_Id.toString()); |
|
755 |
unresCritExts.remove( |
|
756 |
PKIXExtensions.ExtendedKeyUsage_Id.toString()); |
|
757 |
||
758 |
if (!unresCritExts.isEmpty()) |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
792
diff
changeset
|
759 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
792
diff
changeset
|
760 |
("Unrecognized critical extension(s)", null, null, -1, |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
792
diff
changeset
|
761 |
PKIXReason.UNRECOGNIZED_CRIT_EXT); |
2 | 762 |
} |
763 |
} |
|
764 |
||
765 |
/* |
|
766 |
* if this is the target certificate (init=true), then we are |
|
767 |
* not able to do any more verification, so just return |
|
768 |
*/ |
|
769 |
if (currState.isInitial()) { |
|
770 |
return; |
|
771 |
} |
|
772 |
||
773 |
/* we don't perform any validation of the trusted cert */ |
|
774 |
if (!isTrustedCert) { |
|
775 |
/* Make sure this is a CA cert */ |
|
776 |
if (cert.getBasicConstraints() == -1) { |
|
777 |
throw new CertificateException("cert is NOT a CA cert"); |
|
778 |
} |
|
779 |
||
780 |
/* |
|
781 |
* Check keyUsage extension |
|
782 |
*/ |
|
783 |
KeyChecker.verifyCAKeyUsage(cert); |
|
784 |
} |
|
785 |
||
786 |
/* |
|
787 |
* the following checks are performed even when the cert |
|
788 |
* is a trusted cert, since we are only extracting the |
|
789 |
* subjectDN, and publicKey from the cert |
|
790 |
* in order to verify a previous cert |
|
791 |
*/ |
|
792 |
||
793 |
/* |
|
794 |
* Check revocation for the previous cert |
|
795 |
*/ |
|
796 |
if (buildParams.isRevocationEnabled()) { |
|
797 |
||
798 |
// first off, see if this cert can authorize revocation... |
|
799 |
if (CrlRevocationChecker.certCanSignCrl(cert)) { |
|
800 |
// And then check to be sure no key requiring key parameters |
|
801 |
// has been encountered |
|
802 |
if (!currState.keyParamsNeeded()) |
|
803 |
// If all that checks out, we can check the |
|
804 |
// revocation status of the cert. Otherwise, |
|
805 |
// we'll just wait until the end. |
|
806 |
currState.crlChecker.check(currState.cert, |
|
807 |
cert.getPublicKey(), |
|
808 |
true); |
|
809 |
} |
|
810 |
} |
|
811 |
||
812 |
/* |
|
813 |
* Check signature only if no key requiring key parameters has been |
|
814 |
* encountered. |
|
815 |
*/ |
|
816 |
if (!currState.keyParamsNeeded()) { |
|
817 |
(currState.cert).verify(cert.getPublicKey(), |
|
818 |
buildParams.getSigProvider()); |
|
819 |
} |
|
820 |
} |
|
821 |
||
822 |
/** |
|
823 |
* Verifies whether the input certificate completes the path. |
|
824 |
* Checks the cert against each trust anchor that was specified, in order, |
|
825 |
* and returns true as soon as it finds a valid anchor. |
|
826 |
* Returns true if the cert matches a trust anchor specified as a |
|
827 |
* certificate or if the cert verifies with a trust anchor that |
|
828 |
* was specified as a trusted {pubkey, caname} pair. Returns false if none |
|
829 |
* of the trust anchors are valid for this cert. |
|
830 |
* |
|
831 |
* @param cert the certificate to test |
|
832 |
* @return a boolean value indicating whether the cert completes the path. |
|
833 |
*/ |
|
834 |
boolean isPathCompleted(X509Certificate cert) { |
|
835 |
for (TrustAnchor anchor : trustAnchors) { |
|
836 |
if (anchor.getTrustedCert() != null) { |
|
837 |
if (cert.equals(anchor.getTrustedCert())) { |
|
838 |
this.trustAnchor = anchor; |
|
839 |
return true; |
|
840 |
} else { |
|
841 |
continue; |
|
842 |
} |
|
3225
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
843 |
} else { |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
844 |
X500Principal principal = anchor.getCA(); |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
845 |
java.security.PublicKey publicKey = anchor.getCAPublicKey(); |
2 | 846 |
|
3225
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
847 |
if (principal != null && publicKey != null && |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
848 |
principal.equals(cert.getSubjectX500Principal())) { |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
849 |
if (publicKey.equals(cert.getPublicKey())) { |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
850 |
// the cert itself is a trust anchor |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
851 |
this.trustAnchor = anchor; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
852 |
return true; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
853 |
} |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
854 |
// else, it is a self-issued certificate of the anchor |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
855 |
} |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
856 |
|
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
857 |
// Check subject/issuer name chaining |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
858 |
if (principal == null || |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
859 |
!principal.equals(cert.getIssuerX500Principal())) { |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
860 |
continue; |
023603ebeb1f
6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents:
1238
diff
changeset
|
861 |
} |
2 | 862 |
} |
863 |
||
864 |
/* Check revocation if it is enabled */ |
|
865 |
if (buildParams.isRevocationEnabled()) { |
|
866 |
try { |
|
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
3225
diff
changeset
|
867 |
CrlRevocationChecker crlChecker = new CrlRevocationChecker |
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
3225
diff
changeset
|
868 |
(anchor, buildParams, null, onlyEECert); |
2 | 869 |
crlChecker.check(cert, anchor.getCAPublicKey(), true); |
870 |
} catch (CertPathValidatorException cpve) { |
|
871 |
if (debug != null) { |
|
872 |
debug.println("ForwardBuilder.isPathCompleted() cpve"); |
|
873 |
cpve.printStackTrace(); |
|
874 |
} |
|
875 |
continue; |
|
876 |
} |
|
877 |
} |
|
878 |
||
879 |
/* |
|
880 |
* Check signature |
|
881 |
*/ |
|
882 |
try { |
|
883 |
// NOTE: the DSA public key in the buildParams may lack |
|
884 |
// parameters, yet there is no key to inherit the parameters |
|
885 |
// from. This is probably such a rare case that it is not worth |
|
886 |
// trying to detect the situation earlier. |
|
887 |
cert.verify(anchor.getCAPublicKey(), |
|
888 |
buildParams.getSigProvider()); |
|
889 |
} catch (InvalidKeyException ike) { |
|
890 |
if (debug != null) { |
|
891 |
debug.println("ForwardBuilder.isPathCompleted() invalid " |
|
892 |
+ "DSA key found"); |
|
893 |
} |
|
894 |
continue; |
|
895 |
} catch (Exception e){ |
|
896 |
if (debug != null) { |
|
897 |
debug.println("ForwardBuilder.isPathCompleted() " + |
|
898 |
"unexpected exception"); |
|
899 |
e.printStackTrace(); |
|
900 |
} |
|
901 |
continue; |
|
902 |
} |
|
903 |
||
904 |
this.trustAnchor = anchor; |
|
905 |
return true; |
|
906 |
} |
|
907 |
||
908 |
return false; |
|
909 |
} |
|
910 |
||
911 |
/** Adds the certificate to the certPathList |
|
912 |
* |
|
913 |
* @param cert the certificate to be added |
|
914 |
* @param certPathList the certification path list |
|
915 |
*/ |
|
916 |
void addCertToPath(X509Certificate cert, |
|
917 |
LinkedList<X509Certificate> certPathList) { |
|
918 |
certPathList.addFirst(cert); |
|
919 |
} |
|
920 |
||
921 |
/** Removes final certificate from the certPathList |
|
922 |
* |
|
923 |
* @param certPathList the certification path list |
|
924 |
*/ |
|
925 |
void removeFinalCertFromPath(LinkedList<X509Certificate> certPathList) { |
|
926 |
certPathList.removeFirst(); |
|
927 |
} |
|
928 |
} |