author | mullan |
Thu, 11 Sep 2008 14:05:16 -0400 | |
changeset 1238 | 6d1f4b722acd |
parent 790 | b91742db13e2 |
child 2941 | af92a83128fe |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
2 |
* Copyright 2000-2008 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.util.Collection; |
|
29 |
import java.util.Collections; |
|
30 |
import java.util.Set; |
|
31 |
import java.util.HashSet; |
|
32 |
import java.io.IOException; |
|
33 |
import java.security.cert.Certificate; |
|
34 |
import java.security.cert.CertificateException; |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
35 |
import java.security.cert.CertPathValidatorException; |
2 | 36 |
import java.security.cert.X509Certificate; |
37 |
import java.security.cert.PKIXCertPathChecker; |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
38 |
import java.security.cert.PKIXReason; |
2 | 39 |
import sun.security.util.Debug; |
40 |
import sun.security.x509.PKIXExtensions; |
|
41 |
import sun.security.x509.NameConstraintsExtension; |
|
42 |
import sun.security.x509.X509CertImpl; |
|
43 |
||
44 |
/** |
|
45 |
* ConstraintsChecker is a <code>PKIXCertPathChecker</code> that checks |
|
46 |
* constraints information on a PKIX certificate, namely basic constraints |
|
47 |
* and name constraints. |
|
48 |
* |
|
49 |
* @since 1.4 |
|
50 |
* @author Yassir Elley |
|
51 |
*/ |
|
52 |
class ConstraintsChecker extends PKIXCertPathChecker { |
|
53 |
||
54 |
private static final Debug debug = Debug.getInstance("certpath"); |
|
55 |
/* length of cert path */ |
|
56 |
private final int certPathLength; |
|
57 |
/* current maximum path length (as defined in PKIX) */ |
|
58 |
private int maxPathLength; |
|
59 |
/* current index of cert */ |
|
60 |
private int i; |
|
61 |
private NameConstraintsExtension prevNC; |
|
62 |
||
790
b91742db13e2
6673277: Thread unsafe lazy initialization code in sun.security.provider.certpath.*Checker classes
mullan
parents:
2
diff
changeset
|
63 |
private Set<String> supportedExts; |
2 | 64 |
|
65 |
/** |
|
66 |
* Creates a ConstraintsChecker. |
|
67 |
* |
|
68 |
* @param certPathLength the length of the certification path |
|
69 |
* @throws CertPathValidatorException if the checker cannot be initialized |
|
70 |
*/ |
|
71 |
ConstraintsChecker(int certPathLength) throws CertPathValidatorException { |
|
72 |
this.certPathLength = certPathLength; |
|
73 |
init(false); |
|
74 |
} |
|
75 |
||
76 |
public void init(boolean forward) throws CertPathValidatorException { |
|
77 |
if (!forward) { |
|
78 |
i = 0; |
|
79 |
maxPathLength = certPathLength; |
|
80 |
prevNC = null; |
|
81 |
} else { |
|
82 |
throw new CertPathValidatorException |
|
83 |
("forward checking not supported"); |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
public boolean isForwardCheckingSupported() { |
|
88 |
return false; |
|
89 |
} |
|
90 |
||
91 |
public Set<String> getSupportedExtensions() { |
|
92 |
if (supportedExts == null) { |
|
93 |
supportedExts = new HashSet<String>(); |
|
94 |
supportedExts.add(PKIXExtensions.BasicConstraints_Id.toString()); |
|
95 |
supportedExts.add(PKIXExtensions.NameConstraints_Id.toString()); |
|
96 |
supportedExts = Collections.unmodifiableSet(supportedExts); |
|
97 |
} |
|
98 |
return supportedExts; |
|
99 |
} |
|
100 |
||
101 |
/** |
|
102 |
* Performs the basic constraints and name constraints |
|
103 |
* checks on the certificate using its internal state. |
|
104 |
* |
|
105 |
* @param cert the <code>Certificate</code> to be checked |
|
106 |
* @param unresCritExts a <code>Collection</code> of OID strings |
|
107 |
* representing the current set of unresolved critical extensions |
|
108 |
* @throws CertPathValidatorException if the specified certificate |
|
109 |
* does not pass the check |
|
110 |
*/ |
|
111 |
public void check(Certificate cert, Collection<String> unresCritExts) |
|
112 |
throws CertPathValidatorException |
|
113 |
{ |
|
114 |
X509Certificate currCert = (X509Certificate) cert; |
|
115 |
||
116 |
i++; |
|
117 |
// MUST run NC check second, since it depends on BC check to |
|
118 |
// update remainingCerts |
|
119 |
checkBasicConstraints(currCert); |
|
120 |
verifyNameConstraints(currCert); |
|
121 |
||
122 |
if (unresCritExts != null && !unresCritExts.isEmpty()) { |
|
123 |
unresCritExts.remove(PKIXExtensions.BasicConstraints_Id.toString()); |
|
124 |
unresCritExts.remove(PKIXExtensions.NameConstraints_Id.toString()); |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Internal method to check the name constraints against a cert |
|
130 |
*/ |
|
131 |
private void verifyNameConstraints(X509Certificate currCert) |
|
132 |
throws CertPathValidatorException |
|
133 |
{ |
|
134 |
String msg = "name constraints"; |
|
135 |
if (debug != null) { |
|
136 |
debug.println("---checking " + msg + "..."); |
|
137 |
} |
|
138 |
||
139 |
// check name constraints only if there is a previous name constraint |
|
140 |
// and either the currCert is the final cert or the currCert is not |
|
141 |
// self-issued |
|
142 |
if (prevNC != null && ((i == certPathLength) || |
|
143 |
!X509CertImpl.isSelfIssued(currCert))) { |
|
144 |
if (debug != null) { |
|
145 |
debug.println("prevNC = " + prevNC); |
|
146 |
debug.println("currDN = " + currCert.getSubjectX500Principal()); |
|
147 |
} |
|
148 |
||
149 |
try { |
|
150 |
if (!prevNC.verify(currCert)) { |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
151 |
throw new CertPathValidatorException(msg + " check failed", |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
152 |
null, null, -1, PKIXReason.INVALID_NAME); |
2 | 153 |
} |
154 |
} catch (IOException ioe) { |
|
155 |
throw new CertPathValidatorException(ioe); |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
// merge name constraints regardless of whether cert is self-issued |
|
160 |
prevNC = mergeNameConstraints(currCert, prevNC); |
|
161 |
||
162 |
if (debug != null) |
|
163 |
debug.println(msg + " verified."); |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Helper to fold sets of name constraints together |
|
168 |
*/ |
|
169 |
static NameConstraintsExtension |
|
170 |
mergeNameConstraints(X509Certificate currCert, |
|
171 |
NameConstraintsExtension prevNC) throws CertPathValidatorException |
|
172 |
{ |
|
173 |
X509CertImpl currCertImpl; |
|
174 |
try { |
|
175 |
currCertImpl = X509CertImpl.toImpl(currCert); |
|
176 |
} catch (CertificateException ce) { |
|
177 |
throw new CertPathValidatorException(ce); |
|
178 |
} |
|
179 |
||
180 |
NameConstraintsExtension newConstraints = |
|
181 |
currCertImpl.getNameConstraintsExtension(); |
|
182 |
||
183 |
if (debug != null) { |
|
184 |
debug.println("prevNC = " + prevNC); |
|
185 |
debug.println("newNC = " + String.valueOf(newConstraints)); |
|
186 |
} |
|
187 |
||
188 |
// if there are no previous name constraints, we just return the |
|
189 |
// new name constraints. |
|
190 |
if (prevNC == null) { |
|
191 |
if (debug != null) { |
|
192 |
debug.println("mergedNC = " + String.valueOf(newConstraints)); |
|
193 |
} |
|
194 |
if (newConstraints == null) { |
|
195 |
return newConstraints; |
|
196 |
} else { |
|
197 |
// Make sure we do a clone here, because we're probably |
|
198 |
// going to modify this object later and we don't want to |
|
199 |
// be sharing it with a Certificate object! |
|
200 |
return (NameConstraintsExtension) newConstraints.clone(); |
|
201 |
} |
|
202 |
} else { |
|
203 |
try { |
|
204 |
// after merge, prevNC should contain the merged constraints |
|
205 |
prevNC.merge(newConstraints); |
|
206 |
} catch (IOException ioe) { |
|
207 |
throw new CertPathValidatorException(ioe); |
|
208 |
} |
|
209 |
if (debug != null) { |
|
210 |
debug.println("mergedNC = " + prevNC); |
|
211 |
} |
|
212 |
return prevNC; |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
/** |
|
217 |
* Internal method to check that a given cert meets basic constraints. |
|
218 |
*/ |
|
219 |
private void checkBasicConstraints(X509Certificate currCert) |
|
220 |
throws CertPathValidatorException |
|
221 |
{ |
|
222 |
String msg = "basic constraints"; |
|
223 |
if (debug != null) { |
|
224 |
debug.println("---checking " + msg + "..."); |
|
225 |
debug.println("i = " + i); |
|
226 |
debug.println("maxPathLength = " + maxPathLength); |
|
227 |
} |
|
228 |
||
229 |
/* check if intermediate cert */ |
|
230 |
if (i < certPathLength) { |
|
231 |
int pathLenConstraint = currCert.getBasicConstraints(); |
|
232 |
if (pathLenConstraint == -1) { |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
233 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
234 |
(msg + " check failed: this is not a CA certificate", null, |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
235 |
null, -1, PKIXReason.NOT_CA_CERT); |
2 | 236 |
} |
237 |
||
238 |
if (!X509CertImpl.isSelfIssued(currCert)) { |
|
239 |
if (maxPathLength <= 0) { |
|
240 |
throw new CertPathValidatorException |
|
241 |
(msg + " check failed: pathLenConstraint violated - " |
|
242 |
+ "this cert must be the last cert in the " |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
243 |
+ "certification path", null, null, -1, |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
790
diff
changeset
|
244 |
PKIXReason.PATH_TOO_LONG); |
2 | 245 |
} |
246 |
maxPathLength--; |
|
247 |
} |
|
248 |
if (pathLenConstraint < maxPathLength) |
|
249 |
maxPathLength = pathLenConstraint; |
|
250 |
} |
|
251 |
||
252 |
if (debug != null) { |
|
253 |
debug.println("after processing, maxPathLength = " + maxPathLength); |
|
254 |
debug.println(msg + " verified."); |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Merges the specified maxPathLength with the pathLenConstraint |
|
260 |
* obtained from the certificate. |
|
261 |
* |
|
262 |
* @param cert the <code>X509Certificate</code> |
|
263 |
* @param maxPathLength the previous maximum path length |
|
264 |
* @return the new maximum path length constraint (-1 means no more |
|
265 |
* certificates can follow, Integer.MAX_VALUE means path length is |
|
266 |
* unconstrained) |
|
267 |
*/ |
|
268 |
static int mergeBasicConstraints(X509Certificate cert, int maxPathLength) { |
|
269 |
||
270 |
int pathLenConstraint = cert.getBasicConstraints(); |
|
271 |
||
272 |
if (!X509CertImpl.isSelfIssued(cert)) { |
|
273 |
maxPathLength--; |
|
274 |
} |
|
275 |
||
276 |
if (pathLenConstraint < maxPathLength) { |
|
277 |
maxPathLength = pathLenConstraint; |
|
278 |
} |
|
279 |
||
280 |
return maxPathLength; |
|
281 |
} |
|
282 |
} |