2
|
1 |
/*
|
|
2 |
* Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
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.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 |
|
|
99 |
/* the trust anchor used to validate the path */
|
|
100 |
TrustAnchor trustAnchor;
|
|
101 |
|
|
102 |
/* Flag indicating if current cert can vouch for the CRL for
|
|
103 |
* the next cert
|
|
104 |
*/
|
|
105 |
public boolean crlSign = true;
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Returns a boolean flag indicating if the state is initial
|
|
109 |
* (just starting)
|
|
110 |
*
|
|
111 |
* @return boolean flag indicating if the state is initial (just starting)
|
|
112 |
*/
|
|
113 |
public boolean isInitial() {
|
|
114 |
return init;
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Display state for debugging purposes
|
|
119 |
*/
|
|
120 |
public String toString() {
|
|
121 |
StringBuffer sb = new StringBuffer();
|
|
122 |
try {
|
|
123 |
sb.append("State [");
|
|
124 |
sb.append("\n subjectDN of last cert: " + subjectDN);
|
|
125 |
sb.append("\n subjectKeyIdentifier: " + String.valueOf(subjKeyId));
|
|
126 |
sb.append("\n nameConstraints: " + String.valueOf(nc));
|
|
127 |
sb.append("\n certIndex: " + certIndex);
|
|
128 |
sb.append("\n explicitPolicy: " + explicitPolicy);
|
|
129 |
sb.append("\n policyMapping: " + policyMapping);
|
|
130 |
sb.append("\n inhibitAnyPolicy: " + inhibitAnyPolicy);
|
|
131 |
sb.append("\n rootNode: " + rootNode);
|
|
132 |
sb.append("\n remainingCACerts: " + remainingCACerts);
|
|
133 |
sb.append("\n crlSign: " + crlSign);
|
|
134 |
sb.append("\n init: " + init);
|
|
135 |
sb.append("\n]\n");
|
|
136 |
} catch (Exception e) {
|
|
137 |
if (debug != null) {
|
|
138 |
debug.println("ReverseState.toString() unexpected exception");
|
|
139 |
e.printStackTrace();
|
|
140 |
}
|
|
141 |
}
|
|
142 |
return sb.toString();
|
|
143 |
}
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Initialize the state.
|
|
147 |
*
|
|
148 |
* @param maxPathLen The maximum number of CA certs in a path, where -1
|
|
149 |
* means unlimited and 0 means only a single EE cert is allowed.
|
|
150 |
* @param explicitPolicyRequired True, if explicit policy is required.
|
|
151 |
* @param policyMappingInhibited True, if policy mapping is inhibited.
|
|
152 |
* @param anyPolicyInhibited True, if any policy is inhibited.
|
|
153 |
* @param certPathCheckers the list of user-defined PKIXCertPathCheckers
|
|
154 |
*/
|
|
155 |
public void initState(int maxPathLen, boolean explicitPolicyRequired,
|
|
156 |
boolean policyMappingInhibited, boolean anyPolicyInhibited,
|
|
157 |
List<PKIXCertPathChecker> certPathCheckers)
|
|
158 |
throws CertPathValidatorException
|
|
159 |
{
|
|
160 |
/*
|
|
161 |
* Initialize number of remainingCACerts.
|
|
162 |
* Note that -1 maxPathLen implies unlimited.
|
|
163 |
* 0 implies only an EE cert is acceptable.
|
|
164 |
*/
|
|
165 |
remainingCACerts = (maxPathLen == -1 ? Integer.MAX_VALUE : maxPathLen);
|
|
166 |
|
|
167 |
/* Initialize explicit policy state variable */
|
|
168 |
if (explicitPolicyRequired) {
|
|
169 |
explicitPolicy = 0;
|
|
170 |
} else {
|
|
171 |
// unconstrained if maxPathLen is -1,
|
|
172 |
// otherwise, we want to initialize this to the value of the
|
|
173 |
// longest possible path + 1 (i.e. maxpathlen + finalcert + 1)
|
|
174 |
explicitPolicy = (maxPathLen == -1)
|
|
175 |
? maxPathLen
|
|
176 |
: maxPathLen + 2;
|
|
177 |
}
|
|
178 |
|
|
179 |
/* Initialize policy mapping state variable */
|
|
180 |
if (policyMappingInhibited) {
|
|
181 |
policyMapping = 0;
|
|
182 |
} else {
|
|
183 |
policyMapping = (maxPathLen == -1)
|
|
184 |
? maxPathLen
|
|
185 |
: maxPathLen + 2;
|
|
186 |
}
|
|
187 |
|
|
188 |
/* Initialize inhibit any policy state variable */
|
|
189 |
if (anyPolicyInhibited) {
|
|
190 |
inhibitAnyPolicy = 0;
|
|
191 |
} else {
|
|
192 |
inhibitAnyPolicy = (maxPathLen == -1)
|
|
193 |
? maxPathLen
|
|
194 |
: maxPathLen + 2;
|
|
195 |
}
|
|
196 |
|
|
197 |
/* Initialize certIndex */
|
|
198 |
certIndex = 1;
|
|
199 |
|
|
200 |
/* Initialize policy tree */
|
|
201 |
Set<String> initExpPolSet = new HashSet<String>(1);
|
|
202 |
initExpPolSet.add(PolicyChecker.ANY_POLICY);
|
|
203 |
|
|
204 |
rootNode = new PolicyNodeImpl
|
|
205 |
(null, PolicyChecker.ANY_POLICY, null, false, initExpPolSet, false);
|
|
206 |
|
|
207 |
/*
|
|
208 |
* Initialize each user-defined checker
|
|
209 |
*/
|
|
210 |
if (certPathCheckers != null) {
|
|
211 |
/* Shallow copy the checkers */
|
|
212 |
userCheckers = new ArrayList<PKIXCertPathChecker>(certPathCheckers);
|
|
213 |
/* initialize each checker (just in case) */
|
|
214 |
for (PKIXCertPathChecker checker : certPathCheckers) {
|
|
215 |
checker.init(false);
|
|
216 |
}
|
|
217 |
} else {
|
|
218 |
userCheckers = new ArrayList<PKIXCertPathChecker>();
|
|
219 |
}
|
|
220 |
|
|
221 |
/* Start by trusting the cert to sign CRLs */
|
|
222 |
crlSign = true;
|
|
223 |
|
|
224 |
init = true;
|
|
225 |
}
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Update the state with the specified trust anchor.
|
|
229 |
*
|
|
230 |
* @param anchor the most-trusted CA
|
|
231 |
*/
|
|
232 |
public void updateState(TrustAnchor anchor)
|
|
233 |
throws CertificateException, IOException, CertPathValidatorException
|
|
234 |
{
|
|
235 |
trustAnchor = anchor;
|
|
236 |
X509Certificate trustedCert = anchor.getTrustedCert();
|
|
237 |
if (trustedCert != null) {
|
|
238 |
updateState(trustedCert);
|
|
239 |
} else {
|
|
240 |
X500Principal caName = anchor.getCA();
|
|
241 |
updateState(anchor.getCAPublicKey(), caName);
|
|
242 |
}
|
|
243 |
|
|
244 |
init = false;
|
|
245 |
}
|
|
246 |
|
|
247 |
/**
|
|
248 |
* Update the state. This method is used when the most-trusted CA is
|
|
249 |
* a trusted public-key and caName, instead of a trusted cert.
|
|
250 |
*
|
|
251 |
* @param pubKey the public key of the trusted CA
|
|
252 |
* @param subjectDN the subject distinguished name of the trusted CA
|
|
253 |
*/
|
|
254 |
private void updateState(PublicKey pubKey, X500Principal subjectDN) {
|
|
255 |
|
|
256 |
/* update subject DN */
|
|
257 |
this.subjectDN = subjectDN;
|
|
258 |
|
|
259 |
/* update subject public key */
|
|
260 |
this.pubKey = pubKey;
|
|
261 |
}
|
|
262 |
|
|
263 |
/**
|
|
264 |
* Update the state with the next certificate added to the path.
|
|
265 |
*
|
|
266 |
* @param cert the certificate which is used to update the state
|
|
267 |
*/
|
|
268 |
public void updateState(X509Certificate cert)
|
|
269 |
throws CertificateException, IOException, CertPathValidatorException {
|
|
270 |
|
|
271 |
if (cert == null) {
|
|
272 |
return;
|
|
273 |
}
|
|
274 |
|
|
275 |
/* update subject DN */
|
|
276 |
subjectDN = cert.getSubjectX500Principal();
|
|
277 |
|
|
278 |
/* check for key needing to inherit alg parameters */
|
|
279 |
X509CertImpl icert = X509CertImpl.toImpl(cert);
|
|
280 |
PublicKey newKey = cert.getPublicKey();
|
|
281 |
if (newKey instanceof DSAPublicKey &&
|
|
282 |
(((DSAPublicKey)newKey).getParams() == null)) {
|
|
283 |
newKey = BasicChecker.makeInheritedParamsKey(newKey, pubKey);
|
|
284 |
}
|
|
285 |
|
|
286 |
/* update subject public key */
|
|
287 |
pubKey = newKey;
|
|
288 |
|
|
289 |
/*
|
|
290 |
* if this is a trusted cert (init == true), then we
|
|
291 |
* don't update any of the remaining fields
|
|
292 |
*/
|
|
293 |
if (init) {
|
|
294 |
init = false;
|
|
295 |
return;
|
|
296 |
}
|
|
297 |
|
|
298 |
/* update subject key identifier */
|
|
299 |
subjKeyId = icert.getSubjectKeyIdentifierExtension();
|
|
300 |
|
|
301 |
/* update crlSign */
|
|
302 |
crlSign = CrlRevocationChecker.certCanSignCrl(cert);
|
|
303 |
|
|
304 |
/* update current name constraints */
|
|
305 |
if (nc != null) {
|
|
306 |
nc.merge(icert.getNameConstraintsExtension());
|
|
307 |
} else {
|
|
308 |
nc = icert.getNameConstraintsExtension();
|
|
309 |
if (nc != null) {
|
|
310 |
// Make sure we do a clone here, because we're probably
|
|
311 |
// going to modify this object later and we don't want to
|
|
312 |
// be sharing it with a Certificate object!
|
|
313 |
nc = (NameConstraintsExtension) nc.clone();
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
/* update policy state variables */
|
|
318 |
explicitPolicy =
|
|
319 |
PolicyChecker.mergeExplicitPolicy(explicitPolicy, icert, false);
|
|
320 |
policyMapping =
|
|
321 |
PolicyChecker.mergePolicyMapping(policyMapping, icert);
|
|
322 |
inhibitAnyPolicy =
|
|
323 |
PolicyChecker.mergeInhibitAnyPolicy(inhibitAnyPolicy, icert);
|
|
324 |
certIndex++;
|
|
325 |
|
|
326 |
/*
|
|
327 |
* Update remaining CA certs
|
|
328 |
*/
|
|
329 |
remainingCACerts =
|
|
330 |
ConstraintsChecker.mergeBasicConstraints(cert, remainingCACerts);
|
|
331 |
|
|
332 |
init = false;
|
|
333 |
}
|
|
334 |
|
|
335 |
/**
|
|
336 |
* Returns a boolean flag indicating if a key lacking necessary key
|
|
337 |
* algorithm parameters has been encountered.
|
|
338 |
*
|
|
339 |
* @return boolean flag indicating if key lacking parameters encountered.
|
|
340 |
*/
|
|
341 |
public boolean keyParamsNeeded() {
|
|
342 |
/* when building in reverse, we immediately get parameters needed
|
|
343 |
* or else throw an exception
|
|
344 |
*/
|
|
345 |
return false;
|
|
346 |
}
|
|
347 |
|
|
348 |
/*
|
|
349 |
* Clone current state. The state is cloned as each cert is
|
|
350 |
* added to the path. This is necessary if backtracking occurs,
|
|
351 |
* and a prior state needs to be restored.
|
|
352 |
*
|
|
353 |
* Note that this is a SMART clone. Not all fields are fully copied,
|
|
354 |
* because some of them (e.g., subjKeyId) will
|
|
355 |
* not have their contents modified by subsequent calls to updateState.
|
|
356 |
*/
|
|
357 |
public Object clone() {
|
|
358 |
try {
|
|
359 |
ReverseState clonedState = (ReverseState) super.clone();
|
|
360 |
|
|
361 |
/* clone checkers, if cloneable */
|
|
362 |
clonedState.userCheckers =
|
|
363 |
(ArrayList<PKIXCertPathChecker>)userCheckers.clone();
|
|
364 |
ListIterator<PKIXCertPathChecker> li =
|
|
365 |
clonedState.userCheckers.listIterator();
|
|
366 |
while (li.hasNext()) {
|
|
367 |
PKIXCertPathChecker checker = li.next();
|
|
368 |
if (checker instanceof Cloneable) {
|
|
369 |
li.set((PKIXCertPathChecker)checker.clone());
|
|
370 |
}
|
|
371 |
}
|
|
372 |
|
|
373 |
/* make copy of name constraints */
|
|
374 |
if (nc != null) {
|
|
375 |
clonedState.nc = (NameConstraintsExtension) nc.clone();
|
|
376 |
}
|
|
377 |
|
|
378 |
/* make copy of policy tree */
|
|
379 |
if (rootNode != null) {
|
|
380 |
clonedState.rootNode = rootNode.copyTree();
|
|
381 |
}
|
|
382 |
|
|
383 |
return clonedState;
|
|
384 |
} catch (CloneNotSupportedException e) {
|
|
385 |
throw new InternalError(e.toString());
|
|
386 |
}
|
|
387 |
}
|
|
388 |
}
|