author | sherman |
Tue, 30 Aug 2011 11:53:11 -0700 | |
changeset 10419 | 12c063b39232 |
parent 10336 | 0bb1999251f8 |
child 12860 | 9ffbd4e43413 |
child 11902 | a94ba35d9c4a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
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.security.PublicKey; |
|
30 |
import java.security.cert.CertificateException; |
|
31 |
import java.security.cert.CertPathValidatorException; |
|
32 |
import java.security.cert.PKIXCertPathChecker; |
|
33 |
import java.security.cert.TrustAnchor; |
|
34 |
import java.security.cert.X509Certificate; |
|
35 |
import java.security.interfaces.DSAPublicKey; |
|
36 |
import java.util.ArrayList; |
|
37 |
import java.util.HashSet; |
|
38 |
import java.util.List; |
|
39 |
import java.util.ListIterator; |
|
40 |
import java.util.Set; |
|
41 |
import javax.security.auth.x500.X500Principal; |
|
42 |
||
43 |
import sun.security.util.Debug; |
|
44 |
import sun.security.x509.NameConstraintsExtension; |
|
45 |
import sun.security.x509.SubjectKeyIdentifierExtension; |
|
46 |
import sun.security.x509.X509CertImpl; |
|
47 |
||
48 |
/** |
|
49 |
* A specification of a reverse PKIX validation state |
|
50 |
* which is initialized by each build and updated each time a |
|
51 |
* certificate is added to the current path. |
|
52 |
* @since 1.4 |
|
53 |
* @author Sean Mullan |
|
54 |
* @author Yassir Elley |
|
55 |
*/ |
|
56 |
||
57 |
class ReverseState implements State { |
|
58 |
||
59 |
private static final Debug debug = Debug.getInstance("certpath"); |
|
60 |
||
61 |
/* The subject DN of the last cert in the path */ |
|
62 |
X500Principal subjectDN; |
|
63 |
||
64 |
/* The subject public key of the last cert */ |
|
65 |
PublicKey pubKey; |
|
66 |
||
67 |
/* The subject key identifier extension (if any) of the last cert */ |
|
68 |
SubjectKeyIdentifierExtension subjKeyId; |
|
69 |
||
70 |
/* The PKIX constrained/excluded subtrees state variable */ |
|
71 |
NameConstraintsExtension nc; |
|
72 |
||
73 |
/* The PKIX explicit policy, policy mapping, and inhibit_any-policy |
|
74 |
state variables */ |
|
75 |
int explicitPolicy; |
|
76 |
int policyMapping; |
|
77 |
int inhibitAnyPolicy; |
|
78 |
int certIndex; |
|
79 |
PolicyNodeImpl rootNode; |
|
80 |
||
81 |
/* The number of remaining CA certs which may follow in the path. |
|
82 |
* -1: previous cert was an EE cert |
|
83 |
* 0: only EE certs may follow. |
|
84 |
* >0 and <Integer.MAX_VALUE:no more than this number of CA certs may follow |
|
85 |
* Integer.MAX_VALUE: unlimited |
|
86 |
*/ |
|
87 |
int remainingCACerts; |
|
88 |
||
89 |
/* The list of user-defined checkers retrieved from the PKIXParameters |
|
90 |
* instance */ |
|
91 |
ArrayList<PKIXCertPathChecker> userCheckers; |
|
92 |
||
93 |
/* Flag indicating if state is initial (path is just starting) */ |
|
94 |
private boolean init = true; |
|
95 |
||
96 |
/* the checker used for revocation status */ |
|
97 |
public CrlRevocationChecker crlChecker; |
|
98 |
||
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
99 |
/* the algorithm checker */ |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
100 |
AlgorithmChecker algorithmChecker; |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
101 |
|
2 | 102 |
/* the trust anchor used to validate the path */ |
103 |
TrustAnchor trustAnchor; |
|
104 |
||
105 |
/* Flag indicating if current cert can vouch for the CRL for |
|
106 |
* the next cert |
|
107 |
*/ |
|
108 |
public boolean crlSign = true; |
|
109 |
||
110 |
/** |
|
111 |
* Returns a boolean flag indicating if the state is initial |
|
112 |
* (just starting) |
|
113 |
* |
|
114 |
* @return boolean flag indicating if the state is initial (just starting) |
|
115 |
*/ |
|
116 |
public boolean isInitial() { |
|
117 |
return init; |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Display state for debugging purposes |
|
122 |
*/ |
|
123 |
public String toString() { |
|
124 |
StringBuffer sb = new StringBuffer(); |
|
125 |
try { |
|
126 |
sb.append("State ["); |
|
127 |
sb.append("\n subjectDN of last cert: " + subjectDN); |
|
128 |
sb.append("\n subjectKeyIdentifier: " + String.valueOf(subjKeyId)); |
|
129 |
sb.append("\n nameConstraints: " + String.valueOf(nc)); |
|
130 |
sb.append("\n certIndex: " + certIndex); |
|
131 |
sb.append("\n explicitPolicy: " + explicitPolicy); |
|
132 |
sb.append("\n policyMapping: " + policyMapping); |
|
133 |
sb.append("\n inhibitAnyPolicy: " + inhibitAnyPolicy); |
|
134 |
sb.append("\n rootNode: " + rootNode); |
|
135 |
sb.append("\n remainingCACerts: " + remainingCACerts); |
|
136 |
sb.append("\n crlSign: " + crlSign); |
|
137 |
sb.append("\n init: " + init); |
|
138 |
sb.append("\n]\n"); |
|
139 |
} catch (Exception e) { |
|
140 |
if (debug != null) { |
|
141 |
debug.println("ReverseState.toString() unexpected exception"); |
|
142 |
e.printStackTrace(); |
|
143 |
} |
|
144 |
} |
|
145 |
return sb.toString(); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Initialize the state. |
|
150 |
* |
|
151 |
* @param maxPathLen The maximum number of CA certs in a path, where -1 |
|
152 |
* means unlimited and 0 means only a single EE cert is allowed. |
|
153 |
* @param explicitPolicyRequired True, if explicit policy is required. |
|
154 |
* @param policyMappingInhibited True, if policy mapping is inhibited. |
|
155 |
* @param anyPolicyInhibited True, if any policy is inhibited. |
|
156 |
* @param certPathCheckers the list of user-defined PKIXCertPathCheckers |
|
157 |
*/ |
|
158 |
public void initState(int maxPathLen, boolean explicitPolicyRequired, |
|
159 |
boolean policyMappingInhibited, boolean anyPolicyInhibited, |
|
160 |
List<PKIXCertPathChecker> certPathCheckers) |
|
161 |
throws CertPathValidatorException |
|
162 |
{ |
|
163 |
/* |
|
164 |
* Initialize number of remainingCACerts. |
|
165 |
* Note that -1 maxPathLen implies unlimited. |
|
166 |
* 0 implies only an EE cert is acceptable. |
|
167 |
*/ |
|
168 |
remainingCACerts = (maxPathLen == -1 ? Integer.MAX_VALUE : maxPathLen); |
|
169 |
||
170 |
/* Initialize explicit policy state variable */ |
|
171 |
if (explicitPolicyRequired) { |
|
172 |
explicitPolicy = 0; |
|
173 |
} else { |
|
174 |
// unconstrained if maxPathLen is -1, |
|
175 |
// otherwise, we want to initialize this to the value of the |
|
176 |
// longest possible path + 1 (i.e. maxpathlen + finalcert + 1) |
|
177 |
explicitPolicy = (maxPathLen == -1) |
|
178 |
? maxPathLen |
|
179 |
: maxPathLen + 2; |
|
180 |
} |
|
181 |
||
182 |
/* Initialize policy mapping state variable */ |
|
183 |
if (policyMappingInhibited) { |
|
184 |
policyMapping = 0; |
|
185 |
} else { |
|
186 |
policyMapping = (maxPathLen == -1) |
|
187 |
? maxPathLen |
|
188 |
: maxPathLen + 2; |
|
189 |
} |
|
190 |
||
191 |
/* Initialize inhibit any policy state variable */ |
|
192 |
if (anyPolicyInhibited) { |
|
193 |
inhibitAnyPolicy = 0; |
|
194 |
} else { |
|
195 |
inhibitAnyPolicy = (maxPathLen == -1) |
|
196 |
? maxPathLen |
|
197 |
: maxPathLen + 2; |
|
198 |
} |
|
199 |
||
200 |
/* Initialize certIndex */ |
|
201 |
certIndex = 1; |
|
202 |
||
203 |
/* Initialize policy tree */ |
|
204 |
Set<String> initExpPolSet = new HashSet<String>(1); |
|
205 |
initExpPolSet.add(PolicyChecker.ANY_POLICY); |
|
206 |
||
207 |
rootNode = new PolicyNodeImpl |
|
208 |
(null, PolicyChecker.ANY_POLICY, null, false, initExpPolSet, false); |
|
209 |
||
210 |
/* |
|
211 |
* Initialize each user-defined checker |
|
212 |
*/ |
|
213 |
if (certPathCheckers != null) { |
|
214 |
/* Shallow copy the checkers */ |
|
215 |
userCheckers = new ArrayList<PKIXCertPathChecker>(certPathCheckers); |
|
216 |
/* initialize each checker (just in case) */ |
|
217 |
for (PKIXCertPathChecker checker : certPathCheckers) { |
|
218 |
checker.init(false); |
|
219 |
} |
|
220 |
} else { |
|
221 |
userCheckers = new ArrayList<PKIXCertPathChecker>(); |
|
222 |
} |
|
223 |
||
224 |
/* Start by trusting the cert to sign CRLs */ |
|
225 |
crlSign = true; |
|
226 |
||
227 |
init = true; |
|
228 |
} |
|
229 |
||
230 |
/** |
|
231 |
* Update the state with the specified trust anchor. |
|
232 |
* |
|
233 |
* @param anchor the most-trusted CA |
|
234 |
*/ |
|
235 |
public void updateState(TrustAnchor anchor) |
|
236 |
throws CertificateException, IOException, CertPathValidatorException |
|
237 |
{ |
|
238 |
trustAnchor = anchor; |
|
239 |
X509Certificate trustedCert = anchor.getTrustedCert(); |
|
240 |
if (trustedCert != null) { |
|
241 |
updateState(trustedCert); |
|
242 |
} else { |
|
243 |
X500Principal caName = anchor.getCA(); |
|
244 |
updateState(anchor.getCAPublicKey(), caName); |
|
245 |
} |
|
246 |
||
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
247 |
// The user specified AlgorithmChecker may not be |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
248 |
// able to set the trust anchor until now. |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
249 |
for (PKIXCertPathChecker checker : userCheckers) { |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
250 |
if (checker instanceof AlgorithmChecker) { |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
251 |
((AlgorithmChecker)checker).trySetTrustAnchor(anchor); |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
252 |
} |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
253 |
} |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5506
diff
changeset
|
254 |
|
2 | 255 |
init = false; |
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Update the state. This method is used when the most-trusted CA is |
|
260 |
* a trusted public-key and caName, instead of a trusted cert. |
|
261 |
* |
|
262 |
* @param pubKey the public key of the trusted CA |
|
263 |
* @param subjectDN the subject distinguished name of the trusted CA |
|
264 |
*/ |
|
265 |
private void updateState(PublicKey pubKey, X500Principal subjectDN) { |
|
266 |
||
267 |
/* update subject DN */ |
|
268 |
this.subjectDN = subjectDN; |
|
269 |
||
270 |
/* update subject public key */ |
|
271 |
this.pubKey = pubKey; |
|
272 |
} |
|
273 |
||
274 |
/** |
|
275 |
* Update the state with the next certificate added to the path. |
|
276 |
* |
|
277 |
* @param cert the certificate which is used to update the state |
|
278 |
*/ |
|
279 |
public void updateState(X509Certificate cert) |
|
280 |
throws CertificateException, IOException, CertPathValidatorException { |
|
281 |
||
282 |
if (cert == null) { |
|
283 |
return; |
|
284 |
} |
|
285 |
||
286 |
/* update subject DN */ |
|
287 |
subjectDN = cert.getSubjectX500Principal(); |
|
288 |
||
289 |
/* check for key needing to inherit alg parameters */ |
|
290 |
X509CertImpl icert = X509CertImpl.toImpl(cert); |
|
291 |
PublicKey newKey = cert.getPublicKey(); |
|
292 |
if (newKey instanceof DSAPublicKey && |
|
293 |
(((DSAPublicKey)newKey).getParams() == null)) { |
|
294 |
newKey = BasicChecker.makeInheritedParamsKey(newKey, pubKey); |
|
295 |
} |
|
296 |
||
297 |
/* update subject public key */ |
|
298 |
pubKey = newKey; |
|
299 |
||
300 |
/* |
|
301 |
* if this is a trusted cert (init == true), then we |
|
302 |
* don't update any of the remaining fields |
|
303 |
*/ |
|
304 |
if (init) { |
|
305 |
init = false; |
|
306 |
return; |
|
307 |
} |
|
308 |
||
309 |
/* update subject key identifier */ |
|
310 |
subjKeyId = icert.getSubjectKeyIdentifierExtension(); |
|
311 |
||
312 |
/* update crlSign */ |
|
313 |
crlSign = CrlRevocationChecker.certCanSignCrl(cert); |
|
314 |
||
315 |
/* update current name constraints */ |
|
316 |
if (nc != null) { |
|
317 |
nc.merge(icert.getNameConstraintsExtension()); |
|
318 |
} else { |
|
319 |
nc = icert.getNameConstraintsExtension(); |
|
320 |
if (nc != null) { |
|
321 |
// Make sure we do a clone here, because we're probably |
|
322 |
// going to modify this object later and we don't want to |
|
323 |
// be sharing it with a Certificate object! |
|
324 |
nc = (NameConstraintsExtension) nc.clone(); |
|
325 |
} |
|
326 |
} |
|
327 |
||
328 |
/* update policy state variables */ |
|
329 |
explicitPolicy = |
|
330 |
PolicyChecker.mergeExplicitPolicy(explicitPolicy, icert, false); |
|
331 |
policyMapping = |
|
332 |
PolicyChecker.mergePolicyMapping(policyMapping, icert); |
|
333 |
inhibitAnyPolicy = |
|
334 |
PolicyChecker.mergeInhibitAnyPolicy(inhibitAnyPolicy, icert); |
|
335 |
certIndex++; |
|
336 |
||
337 |
/* |
|
338 |
* Update remaining CA certs |
|
339 |
*/ |
|
340 |
remainingCACerts = |
|
341 |
ConstraintsChecker.mergeBasicConstraints(cert, remainingCACerts); |
|
342 |
||
343 |
init = false; |
|
344 |
} |
|
345 |
||
346 |
/** |
|
347 |
* Returns a boolean flag indicating if a key lacking necessary key |
|
348 |
* algorithm parameters has been encountered. |
|
349 |
* |
|
350 |
* @return boolean flag indicating if key lacking parameters encountered. |
|
351 |
*/ |
|
352 |
public boolean keyParamsNeeded() { |
|
353 |
/* when building in reverse, we immediately get parameters needed |
|
354 |
* or else throw an exception |
|
355 |
*/ |
|
356 |
return false; |
|
357 |
} |
|
358 |
||
359 |
/* |
|
360 |
* Clone current state. The state is cloned as each cert is |
|
361 |
* added to the path. This is necessary if backtracking occurs, |
|
362 |
* and a prior state needs to be restored. |
|
363 |
* |
|
364 |
* Note that this is a SMART clone. Not all fields are fully copied, |
|
365 |
* because some of them (e.g., subjKeyId) will |
|
366 |
* not have their contents modified by subsequent calls to updateState. |
|
367 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7040
diff
changeset
|
368 |
@SuppressWarnings("unchecked") // Safe casts assuming clone() works correctly |
2 | 369 |
public Object clone() { |
370 |
try { |
|
371 |
ReverseState clonedState = (ReverseState) super.clone(); |
|
372 |
||
373 |
/* clone checkers, if cloneable */ |
|
374 |
clonedState.userCheckers = |
|
375 |
(ArrayList<PKIXCertPathChecker>)userCheckers.clone(); |
|
376 |
ListIterator<PKIXCertPathChecker> li = |
|
377 |
clonedState.userCheckers.listIterator(); |
|
378 |
while (li.hasNext()) { |
|
379 |
PKIXCertPathChecker checker = li.next(); |
|
380 |
if (checker instanceof Cloneable) { |
|
381 |
li.set((PKIXCertPathChecker)checker.clone()); |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
/* make copy of name constraints */ |
|
386 |
if (nc != null) { |
|
387 |
clonedState.nc = (NameConstraintsExtension) nc.clone(); |
|
388 |
} |
|
389 |
||
390 |
/* make copy of policy tree */ |
|
391 |
if (rootNode != null) { |
|
392 |
clonedState.rootNode = rootNode.copyTree(); |
|
393 |
} |
|
394 |
||
395 |
return clonedState; |
|
396 |
} catch (CloneNotSupportedException e) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10336
diff
changeset
|
397 |
throw new InternalError(e.toString(), e); |
2 | 398 |
} |
399 |
} |
|
400 |
} |