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