author | mullan |
Wed, 09 Sep 2009 09:54:13 -0400 | |
changeset 3841 | 6738c111d48f |
parent 1238 | 6d1f4b722acd |
child 4209 | e2e5a973b879 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
2 |
* Copyright 2000-2009 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.provider.certpath; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.security.AccessController; |
|
30 |
import java.security.InvalidAlgorithmParameterException; |
|
31 |
import java.security.cert.CertPath; |
|
32 |
import java.security.cert.CertPathParameters; |
|
33 |
import java.security.cert.CertPathValidatorException; |
|
34 |
import java.security.cert.CertPathValidatorSpi; |
|
35 |
import java.security.cert.CertPathValidatorResult; |
|
36 |
import java.security.cert.PKIXCertPathChecker; |
|
37 |
import java.security.cert.PKIXCertPathValidatorResult; |
|
38 |
import java.security.cert.PKIXParameters; |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
39 |
import java.security.cert.PKIXReason; |
2 | 40 |
import java.security.cert.PolicyNode; |
41 |
import java.security.cert.TrustAnchor; |
|
42 |
import java.security.cert.X509Certificate; |
|
43 |
import java.security.cert.X509CertSelector; |
|
44 |
import java.util.Collections; |
|
45 |
import java.util.List; |
|
46 |
import java.util.ArrayList; |
|
47 |
import java.util.Date; |
|
48 |
import java.util.Set; |
|
49 |
import javax.security.auth.x500.X500Principal; |
|
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
50 |
import sun.security.action.GetBooleanSecurityPropertyAction; |
2 | 51 |
import sun.security.util.Debug; |
52 |
||
53 |
/** |
|
54 |
* This class implements the PKIX validation algorithm for certification |
|
55 |
* paths consisting exclusively of <code>X509Certificates</code>. It uses |
|
56 |
* the specified input parameter set (which must be a |
|
57 |
* <code>PKIXParameters</code> object) and signature provider (if any). |
|
58 |
* |
|
59 |
* @since 1.4 |
|
60 |
* @author Yassir Elley |
|
61 |
*/ |
|
62 |
public class PKIXCertPathValidator extends CertPathValidatorSpi { |
|
63 |
||
64 |
private static final Debug debug = Debug.getInstance("certpath"); |
|
65 |
private Date testDate; |
|
66 |
private List<PKIXCertPathChecker> userCheckers; |
|
67 |
private String sigProvider; |
|
68 |
private BasicChecker basicChecker; |
|
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
69 |
private boolean ocspEnabled = false; |
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
70 |
private boolean onlyEECert = false; |
2 | 71 |
|
72 |
/** |
|
73 |
* Default constructor. |
|
74 |
*/ |
|
75 |
public PKIXCertPathValidator() {} |
|
76 |
||
77 |
/** |
|
78 |
* Validates a certification path consisting exclusively of |
|
79 |
* <code>X509Certificate</code>s using the PKIX validation algorithm, |
|
80 |
* which uses the specified input parameter set. |
|
81 |
* The input parameter set must be a <code>PKIXParameters</code> object. |
|
82 |
* |
|
83 |
* @param cp the X509 certification path |
|
84 |
* @param param the input PKIX parameter set |
|
85 |
* @return the result |
|
86 |
* @exception CertPathValidatorException Exception thrown if cert path |
|
87 |
* does not validate. |
|
88 |
* @exception InvalidAlgorithmParameterException if the specified |
|
89 |
* parameters are inappropriate for this certification path validator |
|
90 |
*/ |
|
91 |
public CertPathValidatorResult engineValidate(CertPath cp, |
|
92 |
CertPathParameters param) |
|
93 |
throws CertPathValidatorException, InvalidAlgorithmParameterException |
|
94 |
{ |
|
95 |
if (debug != null) |
|
96 |
debug.println("PKIXCertPathValidator.engineValidate()..."); |
|
97 |
||
98 |
if (!(param instanceof PKIXParameters)) { |
|
99 |
throw new InvalidAlgorithmParameterException("inappropriate " |
|
100 |
+ "parameters, must be an instance of PKIXParameters"); |
|
101 |
} |
|
102 |
||
103 |
if (!cp.getType().equals("X.509") && !cp.getType().equals("X509")) { |
|
104 |
throw new InvalidAlgorithmParameterException("inappropriate " |
|
105 |
+ "certification path type specified, must be X.509 or X509"); |
|
106 |
} |
|
107 |
||
108 |
PKIXParameters pkixParam = (PKIXParameters) param; |
|
109 |
||
110 |
// Make sure that none of the trust anchors include name constraints |
|
111 |
// (not supported). |
|
112 |
Set<TrustAnchor> anchors = pkixParam.getTrustAnchors(); |
|
113 |
for (TrustAnchor anchor : anchors) { |
|
114 |
if (anchor.getNameConstraints() != null) { |
|
115 |
throw new InvalidAlgorithmParameterException |
|
116 |
("name constraints in trust anchor not supported"); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
// the certpath which has been passed in (cp) |
|
121 |
// has the target cert as the first certificate - we |
|
122 |
// need to keep this cp so we can return it |
|
123 |
// in case of an exception and for policy qualifier |
|
124 |
// processing - however, for certpath validation, |
|
125 |
// we need to create a reversed path, where we reverse the |
|
126 |
// ordering so that the target cert is the last certificate |
|
127 |
||
128 |
// Must copy elements of certList into a new modifiable List before |
|
129 |
// calling Collections.reverse(). |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
130 |
ArrayList<X509Certificate> certList = new ArrayList<X509Certificate> |
2 | 131 |
((List<X509Certificate>)cp.getCertificates()); |
132 |
if (debug != null) { |
|
133 |
if (certList.isEmpty()) { |
|
134 |
debug.println("PKIXCertPathValidator.engineValidate() " |
|
135 |
+ "certList is empty"); |
|
136 |
} |
|
137 |
debug.println("PKIXCertPathValidator.engineValidate() " |
|
138 |
+ "reversing certpath..."); |
|
139 |
} |
|
140 |
Collections.reverse(certList); |
|
141 |
||
142 |
// now certList has the target cert as the last cert and we |
|
143 |
// can proceed with normal validation |
|
144 |
||
145 |
populateVariables(pkixParam); |
|
146 |
||
147 |
// Retrieve the first certificate in the certpath |
|
148 |
// (to be used later in pre-screening) |
|
149 |
X509Certificate firstCert = null; |
|
150 |
if (!certList.isEmpty()) { |
|
151 |
firstCert = certList.get(0); |
|
152 |
} |
|
153 |
||
154 |
CertPathValidatorException lastException = null; |
|
155 |
||
156 |
// We iterate through the set of trust anchors until we find |
|
157 |
// one that works at which time we stop iterating |
|
158 |
for (TrustAnchor anchor : anchors) { |
|
159 |
X509Certificate trustedCert = anchor.getTrustedCert(); |
|
160 |
if (trustedCert != null) { |
|
161 |
if (debug != null) { |
|
162 |
debug.println("PKIXCertPathValidator.engineValidate() " |
|
163 |
+ "anchor.getTrustedCert() != null"); |
|
164 |
} |
|
165 |
// if this trust anchor is not worth trying, |
|
166 |
// we move on to the next one |
|
167 |
if (!isWorthTrying(trustedCert, firstCert)) { |
|
168 |
continue; |
|
169 |
} |
|
170 |
||
171 |
if (debug != null) { |
|
172 |
debug.println("anchor.getTrustedCert()." |
|
173 |
+ "getSubjectX500Principal() = " |
|
174 |
+ trustedCert.getSubjectX500Principal()); |
|
175 |
} |
|
176 |
} else { |
|
177 |
if (debug != null) { |
|
178 |
debug.println("PKIXCertPathValidator.engineValidate(): " |
|
179 |
+ "anchor.getTrustedCert() == null"); |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
try { |
|
184 |
PolicyNodeImpl rootNode = new PolicyNodeImpl(null, |
|
185 |
PolicyChecker.ANY_POLICY, null, false, |
|
186 |
Collections.singleton(PolicyChecker.ANY_POLICY), false); |
|
187 |
PolicyNode policyTree = |
|
188 |
doValidate(anchor, cp, certList, pkixParam, rootNode); |
|
189 |
// if this anchor works, return success |
|
190 |
return new PKIXCertPathValidatorResult(anchor, policyTree, |
|
191 |
basicChecker.getPublicKey()); |
|
192 |
} catch (CertPathValidatorException cpe) { |
|
193 |
// remember this exception |
|
194 |
lastException = cpe; |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
// could not find a trust anchor that verified |
|
199 |
// (a) if we did a validation and it failed, use that exception |
|
200 |
if (lastException != null) { |
|
201 |
throw lastException; |
|
202 |
} |
|
203 |
// (b) otherwise, generate new exception |
|
204 |
throw new CertPathValidatorException |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
205 |
("Path does not chain with any of the trust anchors", |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
206 |
null, null, -1, PKIXReason.NO_TRUST_ANCHOR); |
2 | 207 |
} |
208 |
||
209 |
/** |
|
210 |
* Internal method to do some simple checks to see if a given cert is |
|
211 |
* worth trying to validate in the chain. |
|
212 |
*/ |
|
213 |
private boolean isWorthTrying(X509Certificate trustedCert, |
|
214 |
X509Certificate firstCert) |
|
215 |
{ |
|
216 |
if (debug != null) { |
|
217 |
debug.println("PKIXCertPathValidator.isWorthTrying() checking " |
|
218 |
+ "if this trusted cert is worth trying ..."); |
|
219 |
} |
|
220 |
||
221 |
if (firstCert == null) { |
|
222 |
return true; |
|
223 |
} |
|
224 |
||
225 |
// the subject of the trusted cert should match the |
|
226 |
// issuer of the first cert in the certpath |
|
227 |
||
228 |
X500Principal trustedSubject = trustedCert.getSubjectX500Principal(); |
|
229 |
if (trustedSubject.equals(firstCert.getIssuerX500Principal())) { |
|
230 |
if (debug != null) |
|
231 |
debug.println("YES - try this trustedCert"); |
|
232 |
return true; |
|
233 |
} else { |
|
234 |
if (debug != null) |
|
235 |
debug.println("NO - don't try this trustedCert"); |
|
236 |
return false; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Internal method to setup the internal state |
|
242 |
*/ |
|
243 |
private void populateVariables(PKIXParameters pkixParam) |
|
244 |
{ |
|
245 |
// default value for testDate is current time |
|
246 |
testDate = pkixParam.getDate(); |
|
247 |
if (testDate == null) { |
|
248 |
testDate = new Date(System.currentTimeMillis()); |
|
249 |
} |
|
250 |
||
251 |
userCheckers = pkixParam.getCertPathCheckers(); |
|
252 |
sigProvider = pkixParam.getSigProvider(); |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
253 |
|
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
254 |
if (pkixParam.isRevocationEnabled()) { |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
255 |
// Examine OCSP security property |
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
256 |
ocspEnabled = AccessController.doPrivileged( |
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
257 |
new GetBooleanSecurityPropertyAction |
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
258 |
(OCSPChecker.OCSP_ENABLE_PROP)); |
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
259 |
onlyEECert = AccessController.doPrivileged( |
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
260 |
new GetBooleanSecurityPropertyAction |
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
261 |
("com.sun.security.onlyCheckRevocationOfEECert")); |
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
262 |
} |
2 | 263 |
} |
264 |
||
265 |
/** |
|
266 |
* Internal method to actually validate a constructed path. |
|
267 |
* |
|
268 |
* @return the valid policy tree |
|
269 |
*/ |
|
270 |
private PolicyNode doValidate( |
|
271 |
TrustAnchor anchor, CertPath cpOriginal, |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
272 |
ArrayList<X509Certificate> certList, PKIXParameters pkixParam, |
2 | 273 |
PolicyNodeImpl rootNode) throws CertPathValidatorException |
274 |
{ |
|
275 |
int certPathLen = certList.size(); |
|
276 |
||
277 |
basicChecker = new BasicChecker(anchor, testDate, sigProvider, false); |
|
278 |
KeyChecker keyChecker = new KeyChecker(certPathLen, |
|
279 |
pkixParam.getTargetCertConstraints()); |
|
280 |
ConstraintsChecker constraintsChecker = |
|
281 |
new ConstraintsChecker(certPathLen); |
|
282 |
||
283 |
PolicyChecker policyChecker = |
|
284 |
new PolicyChecker(pkixParam.getInitialPolicies(), certPathLen, |
|
285 |
pkixParam.isExplicitPolicyRequired(), |
|
286 |
pkixParam.isPolicyMappingInhibited(), |
|
287 |
pkixParam.isAnyPolicyInhibited(), |
|
288 |
pkixParam.getPolicyQualifiersRejected(), |
|
289 |
rootNode); |
|
290 |
||
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
291 |
ArrayList<PKIXCertPathChecker> certPathCheckers = |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
292 |
new ArrayList<PKIXCertPathChecker>(); |
2 | 293 |
// add standard checkers that we will be using |
294 |
certPathCheckers.add(keyChecker); |
|
295 |
certPathCheckers.add(constraintsChecker); |
|
296 |
certPathCheckers.add(policyChecker); |
|
297 |
certPathCheckers.add(basicChecker); |
|
298 |
||
299 |
// only add a revocationChecker if revocation is enabled |
|
300 |
if (pkixParam.isRevocationEnabled()) { |
|
301 |
||
302 |
// Use OCSP if it has been enabled |
|
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
303 |
if (ocspEnabled) { |
2 | 304 |
OCSPChecker ocspChecker = |
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
305 |
new OCSPChecker(cpOriginal, pkixParam, onlyEECert); |
2 | 306 |
certPathCheckers.add(ocspChecker); |
307 |
} |
|
308 |
||
309 |
// Always use CRLs |
|
3841
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
310 |
CrlRevocationChecker revocationChecker = new |
6738c111d48f
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents:
1238
diff
changeset
|
311 |
CrlRevocationChecker(anchor, pkixParam, certList, onlyEECert); |
2 | 312 |
certPathCheckers.add(revocationChecker); |
313 |
} |
|
314 |
||
315 |
// add user-specified checkers |
|
316 |
certPathCheckers.addAll(userCheckers); |
|
317 |
||
318 |
PKIXMasterCertPathValidator masterValidator = |
|
319 |
new PKIXMasterCertPathValidator(certPathCheckers); |
|
320 |
||
321 |
masterValidator.validate(cpOriginal, certList); |
|
322 |
||
323 |
return policyChecker.getPolicyTree(); |
|
324 |
} |
|
325 |
} |