2
|
1 |
/*
|
|
2 |
* Copyright 1997-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.x509;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.OutputStream;
|
|
30 |
import java.security.Principal;
|
|
31 |
import java.security.cert.CertificateEncodingException;
|
|
32 |
import java.security.cert.CertificateException;
|
|
33 |
import java.security.cert.CertificateParsingException;
|
|
34 |
import java.security.cert.X509Certificate;
|
|
35 |
import java.util.*;
|
|
36 |
|
|
37 |
import javax.security.auth.x500.X500Principal;
|
|
38 |
|
|
39 |
import sun.security.util.*;
|
|
40 |
import sun.security.pkcs.PKCS9Attribute;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* This class defines the Name Constraints Extension.
|
|
44 |
* <p>
|
|
45 |
* The name constraints extension provides permitted and excluded
|
|
46 |
* subtrees that place restrictions on names that may be included within
|
|
47 |
* a certificate issued by a given CA. Restrictions may apply to the
|
|
48 |
* subject distinguished name or subject alternative names. Any name
|
|
49 |
* matching a restriction in the excluded subtrees field is invalid
|
|
50 |
* regardless of information appearing in the permitted subtrees.
|
|
51 |
* <p>
|
|
52 |
* The ASN.1 syntax for this is:
|
|
53 |
* <pre>
|
|
54 |
* NameConstraints ::= SEQUENCE {
|
|
55 |
* permittedSubtrees [0] GeneralSubtrees OPTIONAL,
|
|
56 |
* excludedSubtrees [1] GeneralSubtrees OPTIONAL
|
|
57 |
* }
|
|
58 |
* GeneralSubtrees ::= SEQUENCE SIZE (1..MAX) OF GeneralSubtree
|
|
59 |
* </pre>
|
|
60 |
*
|
|
61 |
* @author Amit Kapoor
|
|
62 |
* @author Hemma Prafullchandra
|
|
63 |
* @see Extension
|
|
64 |
* @see CertAttrSet
|
|
65 |
*/
|
|
66 |
public class NameConstraintsExtension extends Extension
|
|
67 |
implements CertAttrSet<String>, Cloneable {
|
|
68 |
/**
|
|
69 |
* Identifier for this attribute, to be used with the
|
|
70 |
* get, set, delete methods of Certificate, x509 type.
|
|
71 |
*/
|
|
72 |
public static final String IDENT = "x509.info.extensions.NameConstraints";
|
|
73 |
/**
|
|
74 |
* Attribute names.
|
|
75 |
*/
|
|
76 |
public static final String NAME = "NameConstraints";
|
|
77 |
public static final String PERMITTED_SUBTREES = "permitted_subtrees";
|
|
78 |
public static final String EXCLUDED_SUBTREES = "excluded_subtrees";
|
|
79 |
|
|
80 |
// Private data members
|
|
81 |
private static final byte TAG_PERMITTED = 0;
|
|
82 |
private static final byte TAG_EXCLUDED = 1;
|
|
83 |
|
|
84 |
private GeneralSubtrees permitted = null;
|
|
85 |
private GeneralSubtrees excluded = null;
|
|
86 |
|
|
87 |
private boolean hasMin;
|
|
88 |
private boolean hasMax;
|
|
89 |
private boolean minMaxValid = false;
|
|
90 |
|
|
91 |
// Recalculate hasMin and hasMax flags.
|
|
92 |
private void calcMinMax() throws IOException {
|
|
93 |
hasMin = false;
|
|
94 |
hasMax = false;
|
|
95 |
if (excluded != null) {
|
|
96 |
for (int i = 0; i < excluded.size(); i++) {
|
|
97 |
GeneralSubtree subtree = excluded.get(i);
|
|
98 |
if (subtree.getMinimum() != 0)
|
|
99 |
hasMin = true;
|
|
100 |
if (subtree.getMaximum() != -1)
|
|
101 |
hasMax = true;
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
if (permitted != null) {
|
|
106 |
for (int i = 0; i < permitted.size(); i++) {
|
|
107 |
GeneralSubtree subtree = permitted.get(i);
|
|
108 |
if (subtree.getMinimum() != 0)
|
|
109 |
hasMin = true;
|
|
110 |
if (subtree.getMaximum() != -1)
|
|
111 |
hasMax = true;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
minMaxValid = true;
|
|
115 |
}
|
|
116 |
|
|
117 |
// Encode this extension value.
|
|
118 |
private void encodeThis() throws IOException {
|
|
119 |
minMaxValid = false;
|
|
120 |
if (permitted == null && excluded == null) {
|
|
121 |
this.extensionValue = null;
|
|
122 |
return;
|
|
123 |
}
|
|
124 |
DerOutputStream seq = new DerOutputStream();
|
|
125 |
|
|
126 |
DerOutputStream tagged = new DerOutputStream();
|
|
127 |
if (permitted != null) {
|
|
128 |
DerOutputStream tmp = new DerOutputStream();
|
|
129 |
permitted.encode(tmp);
|
|
130 |
tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
131 |
true, TAG_PERMITTED), tmp);
|
|
132 |
}
|
|
133 |
if (excluded != null) {
|
|
134 |
DerOutputStream tmp = new DerOutputStream();
|
|
135 |
excluded.encode(tmp);
|
|
136 |
tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
137 |
true, TAG_EXCLUDED), tmp);
|
|
138 |
}
|
|
139 |
seq.write(DerValue.tag_Sequence, tagged);
|
|
140 |
this.extensionValue = seq.toByteArray();
|
|
141 |
}
|
|
142 |
|
|
143 |
/**
|
|
144 |
* The default constructor for this class. Both parameters
|
|
145 |
* are optional and can be set to null. The extension criticality
|
|
146 |
* is set to true.
|
|
147 |
*
|
|
148 |
* @param permitted the permitted GeneralSubtrees (null for optional).
|
|
149 |
* @param excluded the excluded GeneralSubtrees (null for optional).
|
|
150 |
*/
|
|
151 |
public NameConstraintsExtension(GeneralSubtrees permitted,
|
|
152 |
GeneralSubtrees excluded)
|
|
153 |
throws IOException {
|
|
154 |
this.permitted = permitted;
|
|
155 |
this.excluded = excluded;
|
|
156 |
|
|
157 |
this.extensionId = PKIXExtensions.NameConstraints_Id;
|
|
158 |
this.critical = true;
|
|
159 |
encodeThis();
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
|
163 |
* Create the extension from the passed DER encoded value.
|
|
164 |
*
|
|
165 |
* @param critical true if the extension is to be treated as critical.
|
|
166 |
* @param value an array of DER encoded bytes of the actual value.
|
|
167 |
* @exception ClassCastException if value is not an array of bytes
|
|
168 |
* @exception IOException on error.
|
|
169 |
*/
|
|
170 |
public NameConstraintsExtension(Boolean critical, Object value)
|
|
171 |
throws IOException {
|
|
172 |
this.extensionId = PKIXExtensions.NameConstraints_Id;
|
|
173 |
this.critical = critical.booleanValue();
|
|
174 |
|
|
175 |
this.extensionValue = (byte[]) value;
|
|
176 |
DerValue val = new DerValue(this.extensionValue);
|
|
177 |
if (val.tag != DerValue.tag_Sequence) {
|
|
178 |
throw new IOException("Invalid encoding for" +
|
|
179 |
" NameConstraintsExtension.");
|
|
180 |
}
|
|
181 |
|
|
182 |
// NB. this is always encoded with the IMPLICIT tag
|
|
183 |
// The checks only make sense if we assume implicit tagging,
|
|
184 |
// with explicit tagging the form is always constructed.
|
|
185 |
// Note that all the fields in NameConstraints are defined as
|
|
186 |
// being OPTIONAL, i.e., there could be an empty SEQUENCE, resulting
|
|
187 |
// in val.data being null.
|
|
188 |
if (val.data == null)
|
|
189 |
return;
|
|
190 |
while (val.data.available() != 0) {
|
|
191 |
DerValue opt = val.data.getDerValue();
|
|
192 |
|
|
193 |
if (opt.isContextSpecific(TAG_PERMITTED) && opt.isConstructed()) {
|
|
194 |
if (permitted != null) {
|
|
195 |
throw new IOException("Duplicate permitted " +
|
|
196 |
"GeneralSubtrees in NameConstraintsExtension.");
|
|
197 |
}
|
|
198 |
opt.resetTag(DerValue.tag_Sequence);
|
|
199 |
permitted = new GeneralSubtrees(opt);
|
|
200 |
|
|
201 |
} else if (opt.isContextSpecific(TAG_EXCLUDED) &&
|
|
202 |
opt.isConstructed()) {
|
|
203 |
if (excluded != null) {
|
|
204 |
throw new IOException("Duplicate excluded " +
|
|
205 |
"GeneralSubtrees in NameConstraintsExtension.");
|
|
206 |
}
|
|
207 |
opt.resetTag(DerValue.tag_Sequence);
|
|
208 |
excluded = new GeneralSubtrees(opt);
|
|
209 |
} else
|
|
210 |
throw new IOException("Invalid encoding of " +
|
|
211 |
"NameConstraintsExtension.");
|
|
212 |
}
|
|
213 |
minMaxValid = false;
|
|
214 |
}
|
|
215 |
|
|
216 |
/**
|
|
217 |
* Return the printable string.
|
|
218 |
*/
|
|
219 |
public String toString() {
|
|
220 |
return (super.toString() + "NameConstraints: [" +
|
|
221 |
((permitted == null) ? "" :
|
|
222 |
("\n Permitted:" + permitted.toString())) +
|
|
223 |
((excluded == null) ? "" :
|
|
224 |
("\n Excluded:" + excluded.toString()))
|
|
225 |
+ " ]\n");
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* Write the extension to the OutputStream.
|
|
230 |
*
|
|
231 |
* @param out the OutputStream to write the extension to.
|
|
232 |
* @exception IOException on encoding errors.
|
|
233 |
*/
|
|
234 |
public void encode(OutputStream out) throws IOException {
|
|
235 |
DerOutputStream tmp = new DerOutputStream();
|
|
236 |
if (this.extensionValue == null) {
|
|
237 |
this.extensionId = PKIXExtensions.NameConstraints_Id;
|
|
238 |
this.critical = true;
|
|
239 |
encodeThis();
|
|
240 |
}
|
|
241 |
super.encode(tmp);
|
|
242 |
out.write(tmp.toByteArray());
|
|
243 |
}
|
|
244 |
|
|
245 |
/**
|
|
246 |
* Set the attribute value.
|
|
247 |
*/
|
|
248 |
public void set(String name, Object obj) throws IOException {
|
|
249 |
if (name.equalsIgnoreCase(PERMITTED_SUBTREES)) {
|
|
250 |
if (!(obj instanceof GeneralSubtrees)) {
|
|
251 |
throw new IOException("Attribute value should be"
|
|
252 |
+ " of type GeneralSubtrees.");
|
|
253 |
}
|
|
254 |
permitted = (GeneralSubtrees)obj;
|
|
255 |
} else if (name.equalsIgnoreCase(EXCLUDED_SUBTREES)) {
|
|
256 |
if (!(obj instanceof GeneralSubtrees)) {
|
|
257 |
throw new IOException("Attribute value should be "
|
|
258 |
+ "of type GeneralSubtrees.");
|
|
259 |
}
|
|
260 |
excluded = (GeneralSubtrees)obj;
|
|
261 |
} else {
|
|
262 |
throw new IOException("Attribute name not recognized by " +
|
|
263 |
"CertAttrSet:NameConstraintsExtension.");
|
|
264 |
}
|
|
265 |
encodeThis();
|
|
266 |
}
|
|
267 |
|
|
268 |
/**
|
|
269 |
* Get the attribute value.
|
|
270 |
*/
|
|
271 |
public Object get(String name) throws IOException {
|
|
272 |
if (name.equalsIgnoreCase(PERMITTED_SUBTREES)) {
|
|
273 |
return (permitted);
|
|
274 |
} else if (name.equalsIgnoreCase(EXCLUDED_SUBTREES)) {
|
|
275 |
return (excluded);
|
|
276 |
} else {
|
|
277 |
throw new IOException("Attribute name not recognized by " +
|
|
278 |
"CertAttrSet:NameConstraintsExtension.");
|
|
279 |
}
|
|
280 |
}
|
|
281 |
|
|
282 |
/**
|
|
283 |
* Delete the attribute value.
|
|
284 |
*/
|
|
285 |
public void delete(String name) throws IOException {
|
|
286 |
if (name.equalsIgnoreCase(PERMITTED_SUBTREES)) {
|
|
287 |
permitted = null;
|
|
288 |
} else if (name.equalsIgnoreCase(EXCLUDED_SUBTREES)) {
|
|
289 |
excluded = null;
|
|
290 |
} else {
|
|
291 |
throw new IOException("Attribute name not recognized by " +
|
|
292 |
"CertAttrSet:NameConstraintsExtension.");
|
|
293 |
}
|
|
294 |
encodeThis();
|
|
295 |
}
|
|
296 |
|
|
297 |
/**
|
|
298 |
* Return an enumeration of names of attributes existing within this
|
|
299 |
* attribute.
|
|
300 |
*/
|
|
301 |
public Enumeration<String> getElements() {
|
|
302 |
AttributeNameEnumeration elements = new AttributeNameEnumeration();
|
|
303 |
elements.addElement(PERMITTED_SUBTREES);
|
|
304 |
elements.addElement(EXCLUDED_SUBTREES);
|
|
305 |
|
|
306 |
return (elements.elements());
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* Return the name of this attribute.
|
|
311 |
*/
|
|
312 |
public String getName() {
|
|
313 |
return (NAME);
|
|
314 |
}
|
|
315 |
|
|
316 |
/**
|
|
317 |
* Merge additional name constraints with existing ones.
|
|
318 |
* This function is used in certification path processing
|
|
319 |
* to accumulate name constraints from successive certificates
|
|
320 |
* in the path. Note that NameConstraints can never be
|
|
321 |
* expanded by a merge, just remain constant or become more
|
|
322 |
* limiting.
|
|
323 |
* <p>
|
|
324 |
* IETF RFC2459 specifies the processing of Name Constraints as
|
|
325 |
* follows:
|
|
326 |
* <p>
|
|
327 |
* (j) If permittedSubtrees is present in the certificate, set the
|
|
328 |
* constrained subtrees state variable to the intersection of its
|
|
329 |
* previous value and the value indicated in the extension field.
|
|
330 |
* <p>
|
|
331 |
* (k) If excludedSubtrees is present in the certificate, set the
|
|
332 |
* excluded subtrees state variable to the union of its previous
|
|
333 |
* value and the value indicated in the extension field.
|
|
334 |
* <p>
|
|
335 |
* @param newConstraints additional NameConstraints to be applied
|
|
336 |
* @throws IOException on error
|
|
337 |
*/
|
|
338 |
public void merge(NameConstraintsExtension newConstraints)
|
|
339 |
throws IOException {
|
|
340 |
|
|
341 |
if (newConstraints == null) {
|
|
342 |
// absence of any explicit constraints implies unconstrained
|
|
343 |
return;
|
|
344 |
}
|
|
345 |
|
|
346 |
/*
|
|
347 |
* If excludedSubtrees is present in the certificate, set the
|
|
348 |
* excluded subtrees state variable to the union of its previous
|
|
349 |
* value and the value indicated in the extension field.
|
|
350 |
*/
|
|
351 |
|
|
352 |
GeneralSubtrees newExcluded =
|
|
353 |
(GeneralSubtrees)newConstraints.get(EXCLUDED_SUBTREES);
|
|
354 |
if (excluded == null) {
|
|
355 |
excluded = (newExcluded != null) ?
|
|
356 |
(GeneralSubtrees)newExcluded.clone() : null;
|
|
357 |
} else {
|
|
358 |
if (newExcluded != null) {
|
|
359 |
// Merge new excluded with current excluded (union)
|
|
360 |
excluded.union(newExcluded);
|
|
361 |
}
|
|
362 |
}
|
|
363 |
|
|
364 |
/*
|
|
365 |
* If permittedSubtrees is present in the certificate, set the
|
|
366 |
* constrained subtrees state variable to the intersection of its
|
|
367 |
* previous value and the value indicated in the extension field.
|
|
368 |
*/
|
|
369 |
|
|
370 |
GeneralSubtrees newPermitted =
|
|
371 |
(GeneralSubtrees)newConstraints.get(PERMITTED_SUBTREES);
|
|
372 |
if (permitted == null) {
|
|
373 |
permitted = (newPermitted != null) ?
|
|
374 |
(GeneralSubtrees)newPermitted.clone() : null;
|
|
375 |
} else {
|
|
376 |
if (newPermitted != null) {
|
|
377 |
// Merge new permitted with current permitted (intersection)
|
|
378 |
newExcluded = permitted.intersect(newPermitted);
|
|
379 |
|
|
380 |
// Merge new excluded subtrees to current excluded (union)
|
|
381 |
if (newExcluded != null) {
|
|
382 |
if (excluded != null) {
|
|
383 |
excluded.union(newExcluded);
|
|
384 |
} else {
|
|
385 |
excluded = (GeneralSubtrees)newExcluded.clone();
|
|
386 |
}
|
|
387 |
}
|
|
388 |
}
|
|
389 |
}
|
|
390 |
|
|
391 |
// Optional optimization: remove permitted subtrees that are excluded.
|
|
392 |
// This is not necessary for algorithm correctness, but it makes
|
|
393 |
// subsequent operations on the NameConstraints faster and require
|
|
394 |
// less space.
|
|
395 |
if (permitted != null) {
|
|
396 |
permitted.reduce(excluded);
|
|
397 |
}
|
|
398 |
|
|
399 |
// The NameConstraints have been changed, so re-encode them. Methods in
|
|
400 |
// this class assume that the encodings have already been done.
|
|
401 |
encodeThis();
|
|
402 |
|
|
403 |
}
|
|
404 |
|
|
405 |
/**
|
|
406 |
* check whether a certificate conforms to these NameConstraints.
|
|
407 |
* This involves verifying that the subject name and subjectAltName
|
|
408 |
* extension (critical or noncritical) is consistent with the permitted
|
|
409 |
* subtrees state variables. Also verify that the subject name and
|
|
410 |
* subjectAltName extension (critical or noncritical) is consistent with
|
|
411 |
* the excluded subtrees state variables.
|
|
412 |
*
|
|
413 |
* @param cert X509Certificate to be verified
|
|
414 |
* @returns true if certificate verifies successfully
|
|
415 |
* @throws IOException on error
|
|
416 |
*/
|
|
417 |
public boolean verify(X509Certificate cert) throws IOException {
|
|
418 |
|
|
419 |
if (cert == null) {
|
|
420 |
throw new IOException("Certificate is null");
|
|
421 |
}
|
|
422 |
|
|
423 |
// Calculate hasMin and hasMax booleans (if necessary)
|
|
424 |
if (!minMaxValid) {
|
|
425 |
calcMinMax();
|
|
426 |
}
|
|
427 |
|
|
428 |
if (hasMin) {
|
|
429 |
throw new IOException("Non-zero minimum BaseDistance in"
|
|
430 |
+ " name constraints not supported");
|
|
431 |
}
|
|
432 |
|
|
433 |
if (hasMax) {
|
|
434 |
throw new IOException("Maximum BaseDistance in"
|
|
435 |
+ " name constraints not supported");
|
|
436 |
}
|
|
437 |
|
|
438 |
X500Principal subjectPrincipal = cert.getSubjectX500Principal();
|
|
439 |
X500Name subject = X500Name.asX500Name(subjectPrincipal);
|
|
440 |
|
|
441 |
if (subject.isEmpty() == false) {
|
|
442 |
if (verify(subject) == false) {
|
|
443 |
return false;
|
|
444 |
}
|
|
445 |
}
|
|
446 |
|
|
447 |
GeneralNames altNames = null;
|
|
448 |
// extract altNames
|
|
449 |
try {
|
|
450 |
// extract extensions, if any, from certInfo
|
|
451 |
// following returns null if certificate contains no extensions
|
|
452 |
X509CertImpl certImpl = X509CertImpl.toImpl(cert);
|
|
453 |
SubjectAlternativeNameExtension altNameExt =
|
|
454 |
certImpl.getSubjectAlternativeNameExtension();
|
|
455 |
if (altNameExt != null) {
|
|
456 |
// extract altNames from extension; this call does not
|
|
457 |
// return an IOException on null altnames
|
|
458 |
altNames = (GeneralNames)
|
|
459 |
(altNameExt.get(altNameExt.SUBJECT_NAME));
|
|
460 |
}
|
|
461 |
} catch (CertificateException ce) {
|
|
462 |
throw new IOException("Unable to extract extensions from " +
|
|
463 |
"certificate: " + ce.getMessage());
|
|
464 |
}
|
|
465 |
|
|
466 |
// If there are no subjectAlternativeNames, perform the special-case
|
|
467 |
// check where if the subjectName contains any EMAILADDRESS
|
|
468 |
// attributes, they must be checked against RFC822 constraints.
|
|
469 |
// If that passes, we're fine.
|
|
470 |
if (altNames == null) {
|
|
471 |
return verifyRFC822SpecialCase(subject);
|
|
472 |
}
|
|
473 |
|
|
474 |
// verify each subjectAltName
|
|
475 |
for (int i = 0; i < altNames.size(); i++) {
|
|
476 |
GeneralNameInterface altGNI = altNames.get(i).getName();
|
|
477 |
if (!verify(altGNI)) {
|
|
478 |
return false;
|
|
479 |
}
|
|
480 |
}
|
|
481 |
|
|
482 |
// All tests passed.
|
|
483 |
return true;
|
|
484 |
}
|
|
485 |
|
|
486 |
/**
|
|
487 |
* check whether a name conforms to these NameConstraints.
|
|
488 |
* This involves verifying that the name is consistent with the
|
|
489 |
* permitted and excluded subtrees variables.
|
|
490 |
*
|
|
491 |
* @param name GeneralNameInterface name to be verified
|
|
492 |
* @returns true if certificate verifies successfully
|
|
493 |
* @throws IOException on error
|
|
494 |
*/
|
|
495 |
public boolean verify(GeneralNameInterface name) throws IOException {
|
|
496 |
if (name == null) {
|
|
497 |
throw new IOException("name is null");
|
|
498 |
}
|
|
499 |
|
|
500 |
// Verify that the name is consistent with the excluded subtrees
|
|
501 |
if (excluded != null && excluded.size() > 0) {
|
|
502 |
|
|
503 |
for (int i = 0; i < excluded.size(); i++) {
|
|
504 |
GeneralSubtree gs = excluded.get(i);
|
|
505 |
if (gs == null)
|
|
506 |
continue;
|
|
507 |
GeneralName gn = gs.getName();
|
|
508 |
if (gn == null)
|
|
509 |
continue;
|
|
510 |
GeneralNameInterface exName = gn.getName();
|
|
511 |
if (exName == null)
|
|
512 |
continue;
|
|
513 |
|
|
514 |
// if name matches or narrows any excluded subtree,
|
|
515 |
// return false
|
|
516 |
switch (exName.constrains(name)) {
|
|
517 |
case GeneralNameInterface.NAME_DIFF_TYPE:
|
|
518 |
case GeneralNameInterface.NAME_WIDENS: // name widens excluded
|
|
519 |
case GeneralNameInterface.NAME_SAME_TYPE:
|
|
520 |
break;
|
|
521 |
case GeneralNameInterface.NAME_MATCH:
|
|
522 |
case GeneralNameInterface.NAME_NARROWS: // subject name excluded
|
|
523 |
return false;
|
|
524 |
}
|
|
525 |
}
|
|
526 |
}
|
|
527 |
|
|
528 |
// Verify that the name is consistent with the permitted subtrees
|
|
529 |
if (permitted != null && permitted.size() > 0) {
|
|
530 |
|
|
531 |
boolean sameType = false;
|
|
532 |
|
|
533 |
for (int i = 0; i < permitted.size(); i++) {
|
|
534 |
GeneralSubtree gs = permitted.get(i);
|
|
535 |
if (gs == null)
|
|
536 |
continue;
|
|
537 |
GeneralName gn = gs.getName();
|
|
538 |
if (gn == null)
|
|
539 |
continue;
|
|
540 |
GeneralNameInterface perName = gn.getName();
|
|
541 |
if (perName == null)
|
|
542 |
continue;
|
|
543 |
|
|
544 |
// if Name matches any type in permitted,
|
|
545 |
// and Name does not match or narrow some permitted subtree,
|
|
546 |
// return false
|
|
547 |
switch (perName.constrains(name)) {
|
|
548 |
case GeneralNameInterface.NAME_DIFF_TYPE:
|
|
549 |
continue; // continue checking other permitted names
|
|
550 |
case GeneralNameInterface.NAME_WIDENS: // name widens permitted
|
|
551 |
case GeneralNameInterface.NAME_SAME_TYPE:
|
|
552 |
sameType = true;
|
|
553 |
continue; // continue to look for a match or narrow
|
|
554 |
case GeneralNameInterface.NAME_MATCH:
|
|
555 |
case GeneralNameInterface.NAME_NARROWS:
|
|
556 |
// name narrows permitted
|
|
557 |
return true; // name is definitely OK, so break out of loop
|
|
558 |
}
|
|
559 |
}
|
|
560 |
if (sameType) {
|
|
561 |
return false;
|
|
562 |
}
|
|
563 |
}
|
|
564 |
return true;
|
|
565 |
}
|
|
566 |
|
|
567 |
/**
|
|
568 |
* Perform the RFC 822 special case check. We have a certificate
|
|
569 |
* that does not contain any subject alternative names. Check that
|
|
570 |
* any EMAILADDRESS attributes in its subject name conform to these
|
|
571 |
* NameConstraints.
|
|
572 |
*
|
|
573 |
* @param subject the certificate's subject name
|
|
574 |
* @returns true if certificate verifies successfully
|
|
575 |
* @throws IOException on error
|
|
576 |
*/
|
|
577 |
public boolean verifyRFC822SpecialCase(X500Name subject) throws IOException {
|
|
578 |
for (Iterator t = subject.allAvas().iterator(); t.hasNext(); ) {
|
|
579 |
AVA ava = (AVA)t.next();
|
|
580 |
ObjectIdentifier attrOID = ava.getObjectIdentifier();
|
|
581 |
if (attrOID.equals(PKCS9Attribute.EMAIL_ADDRESS_OID)) {
|
|
582 |
String attrValue = ava.getValueString();
|
|
583 |
if (attrValue != null) {
|
|
584 |
RFC822Name emailName;
|
|
585 |
try {
|
|
586 |
emailName = new RFC822Name(attrValue);
|
|
587 |
} catch (IOException ioe) {
|
|
588 |
continue;
|
|
589 |
}
|
|
590 |
if (!verify(emailName)) {
|
|
591 |
return(false);
|
|
592 |
}
|
|
593 |
}
|
|
594 |
}
|
|
595 |
}
|
|
596 |
return true;
|
|
597 |
}
|
|
598 |
|
|
599 |
/**
|
|
600 |
* Clone all objects that may be modified during certificate validation.
|
|
601 |
*/
|
|
602 |
public Object clone() {
|
|
603 |
try {
|
|
604 |
NameConstraintsExtension newNCE =
|
|
605 |
(NameConstraintsExtension) super.clone();
|
|
606 |
|
|
607 |
if (permitted != null) {
|
|
608 |
newNCE.permitted = (GeneralSubtrees) permitted.clone();
|
|
609 |
}
|
|
610 |
if (excluded != null) {
|
|
611 |
newNCE.excluded = (GeneralSubtrees) excluded.clone();
|
|
612 |
}
|
|
613 |
return newNCE;
|
|
614 |
} catch (CloneNotSupportedException cnsee) {
|
|
615 |
throw new RuntimeException("CloneNotSupportedException while " +
|
|
616 |
"cloning NameConstraintsException. This should never happen.");
|
|
617 |
}
|
|
618 |
}
|
|
619 |
}
|