|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.security.x509; |
|
|
|
import java.io.IOException; |
|
import java.io.OutputStream; |
|
import java.security.cert.CertificateException; |
|
import java.security.cert.X509Certificate; |
|
import java.util.*; |
|
|
|
import javax.security.auth.x500.X500Principal; |
|
|
|
import sun.net.util.IPAddressUtil; |
|
import sun.security.util.*; |
|
import sun.security.pkcs.PKCS9Attribute; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class NameConstraintsExtension extends Extension |
|
implements CertAttrSet<String>, Cloneable { |
|
|
|
|
|
|
|
*/ |
|
public static final String IDENT = "x509.info.extensions.NameConstraints"; |
|
|
|
|
|
*/ |
|
public static final String NAME = "NameConstraints"; |
|
public static final String PERMITTED_SUBTREES = "permitted_subtrees"; |
|
public static final String EXCLUDED_SUBTREES = "excluded_subtrees"; |
|
|
|
|
|
private static final byte TAG_PERMITTED = 0; |
|
private static final byte TAG_EXCLUDED = 1; |
|
|
|
private GeneralSubtrees permitted = null; |
|
private GeneralSubtrees excluded = null; |
|
|
|
private boolean hasMin; |
|
private boolean hasMax; |
|
private boolean minMaxValid = false; |
|
|
|
|
|
private void calcMinMax() throws IOException { |
|
hasMin = false; |
|
hasMax = false; |
|
if (excluded != null) { |
|
for (int i = 0; i < excluded.size(); i++) { |
|
GeneralSubtree subtree = excluded.get(i); |
|
if (subtree.getMinimum() != 0) |
|
hasMin = true; |
|
if (subtree.getMaximum() != -1) |
|
hasMax = true; |
|
} |
|
} |
|
|
|
if (permitted != null) { |
|
for (int i = 0; i < permitted.size(); i++) { |
|
GeneralSubtree subtree = permitted.get(i); |
|
if (subtree.getMinimum() != 0) |
|
hasMin = true; |
|
if (subtree.getMaximum() != -1) |
|
hasMax = true; |
|
} |
|
} |
|
minMaxValid = true; |
|
} |
|
|
|
|
|
private void encodeThis() throws IOException { |
|
minMaxValid = false; |
|
if (permitted == null && excluded == null) { |
|
this.extensionValue = null; |
|
return; |
|
} |
|
DerOutputStream seq = new DerOutputStream(); |
|
|
|
DerOutputStream tagged = new DerOutputStream(); |
|
if (permitted != null) { |
|
DerOutputStream tmp = new DerOutputStream(); |
|
permitted.encode(tmp); |
|
tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
true, TAG_PERMITTED), tmp); |
|
} |
|
if (excluded != null) { |
|
DerOutputStream tmp = new DerOutputStream(); |
|
excluded.encode(tmp); |
|
tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
true, TAG_EXCLUDED), tmp); |
|
} |
|
seq.write(DerValue.tag_Sequence, tagged); |
|
this.extensionValue = seq.toByteArray(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public NameConstraintsExtension(GeneralSubtrees permitted, |
|
GeneralSubtrees excluded) |
|
throws IOException { |
|
this.permitted = permitted; |
|
this.excluded = excluded; |
|
|
|
this.extensionId = PKIXExtensions.NameConstraints_Id; |
|
this.critical = true; |
|
encodeThis(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public NameConstraintsExtension(Boolean critical, Object value) |
|
throws IOException { |
|
this.extensionId = PKIXExtensions.NameConstraints_Id; |
|
this.critical = critical.booleanValue(); |
|
|
|
this.extensionValue = (byte[]) value; |
|
DerValue val = new DerValue(this.extensionValue); |
|
if (val.tag != DerValue.tag_Sequence) { |
|
throw new IOException("Invalid encoding for" + |
|
" NameConstraintsExtension."); |
|
} |
|
|
|
// NB. this is always encoded with the IMPLICIT tag |
|
// The checks only make sense if we assume implicit tagging, |
|
// with explicit tagging the form is always constructed. |
|
// Note that all the fields in NameConstraints are defined as |
|
// being OPTIONAL, i.e., there could be an empty SEQUENCE, resulting |
|
|
|
if (val.data == null) |
|
return; |
|
while (val.data.available() != 0) { |
|
DerValue opt = val.data.getDerValue(); |
|
|
|
if (opt.isContextSpecific(TAG_PERMITTED) && opt.isConstructed()) { |
|
if (permitted != null) { |
|
throw new IOException("Duplicate permitted " + |
|
"GeneralSubtrees in NameConstraintsExtension."); |
|
} |
|
opt.resetTag(DerValue.tag_Sequence); |
|
permitted = new GeneralSubtrees(opt); |
|
|
|
} else if (opt.isContextSpecific(TAG_EXCLUDED) && |
|
opt.isConstructed()) { |
|
if (excluded != null) { |
|
throw new IOException("Duplicate excluded " + |
|
"GeneralSubtrees in NameConstraintsExtension."); |
|
} |
|
opt.resetTag(DerValue.tag_Sequence); |
|
excluded = new GeneralSubtrees(opt); |
|
} else |
|
throw new IOException("Invalid encoding of " + |
|
"NameConstraintsExtension."); |
|
} |
|
minMaxValid = false; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String toString() { |
|
StringBuilder sb = new StringBuilder(); |
|
sb.append(super.toString()) |
|
.append("NameConstraints: ["); |
|
if (permitted != null) { |
|
sb.append("\n Permitted:") |
|
.append(permitted); |
|
} |
|
if (excluded != null) { |
|
sb.append("\n Excluded:") |
|
.append(excluded); |
|
} |
|
sb.append(" ]\n"); |
|
return sb.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void encode(OutputStream out) throws IOException { |
|
DerOutputStream tmp = new DerOutputStream(); |
|
if (this.extensionValue == null) { |
|
this.extensionId = PKIXExtensions.NameConstraints_Id; |
|
this.critical = true; |
|
encodeThis(); |
|
} |
|
super.encode(tmp); |
|
out.write(tmp.toByteArray()); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void set(String name, Object obj) throws IOException { |
|
if (name.equalsIgnoreCase(PERMITTED_SUBTREES)) { |
|
if (!(obj instanceof GeneralSubtrees)) { |
|
throw new IOException("Attribute value should be" |
|
+ " of type GeneralSubtrees."); |
|
} |
|
permitted = (GeneralSubtrees)obj; |
|
} else if (name.equalsIgnoreCase(EXCLUDED_SUBTREES)) { |
|
if (!(obj instanceof GeneralSubtrees)) { |
|
throw new IOException("Attribute value should be " |
|
+ "of type GeneralSubtrees."); |
|
} |
|
excluded = (GeneralSubtrees)obj; |
|
} else { |
|
throw new IOException("Attribute name not recognized by " + |
|
"CertAttrSet:NameConstraintsExtension."); |
|
} |
|
encodeThis(); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public GeneralSubtrees get(String name) throws IOException { |
|
if (name.equalsIgnoreCase(PERMITTED_SUBTREES)) { |
|
return (permitted); |
|
} else if (name.equalsIgnoreCase(EXCLUDED_SUBTREES)) { |
|
return (excluded); |
|
} else { |
|
throw new IOException("Attribute name not recognized by " + |
|
"CertAttrSet:NameConstraintsExtension."); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void delete(String name) throws IOException { |
|
if (name.equalsIgnoreCase(PERMITTED_SUBTREES)) { |
|
permitted = null; |
|
} else if (name.equalsIgnoreCase(EXCLUDED_SUBTREES)) { |
|
excluded = null; |
|
} else { |
|
throw new IOException("Attribute name not recognized by " + |
|
"CertAttrSet:NameConstraintsExtension."); |
|
} |
|
encodeThis(); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public Enumeration<String> getElements() { |
|
AttributeNameEnumeration elements = new AttributeNameEnumeration(); |
|
elements.addElement(PERMITTED_SUBTREES); |
|
elements.addElement(EXCLUDED_SUBTREES); |
|
|
|
return (elements.elements()); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String getName() { |
|
return (NAME); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void merge(NameConstraintsExtension newConstraints) |
|
throws IOException { |
|
|
|
if (newConstraints == null) { |
|
|
|
return; |
|
} |
|
|
|
/* |
|
* If excludedSubtrees is present in the certificate, set the |
|
* excluded subtrees state variable to the union of its previous |
|
* value and the value indicated in the extension field. |
|
*/ |
|
|
|
GeneralSubtrees newExcluded = newConstraints.get(EXCLUDED_SUBTREES); |
|
if (excluded == null) { |
|
excluded = (newExcluded != null) ? |
|
(GeneralSubtrees)newExcluded.clone() : null; |
|
} else { |
|
if (newExcluded != null) { |
|
|
|
excluded.union(newExcluded); |
|
} |
|
} |
|
|
|
/* |
|
* If permittedSubtrees is present in the certificate, set the |
|
* constrained subtrees state variable to the intersection of its |
|
* previous value and the value indicated in the extension field. |
|
*/ |
|
|
|
GeneralSubtrees newPermitted = newConstraints.get(PERMITTED_SUBTREES); |
|
if (permitted == null) { |
|
permitted = (newPermitted != null) ? |
|
(GeneralSubtrees)newPermitted.clone() : null; |
|
} else { |
|
if (newPermitted != null) { |
|
|
|
newExcluded = permitted.intersect(newPermitted); |
|
|
|
|
|
if (newExcluded != null) { |
|
if (excluded != null) { |
|
excluded.union(newExcluded); |
|
} else { |
|
excluded = (GeneralSubtrees)newExcluded.clone(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
// Optional optimization: remove permitted subtrees that are excluded. |
|
// This is not necessary for algorithm correctness, but it makes |
|
// subsequent operations on the NameConstraints faster and require |
|
|
|
if (permitted != null) { |
|
permitted.reduce(excluded); |
|
} |
|
|
|
// The NameConstraints have been changed, so re-encode them. Methods in |
|
|
|
encodeThis(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean verify(X509Certificate cert) throws IOException { |
|
|
|
if (cert == null) { |
|
throw new IOException("Certificate is null"); |
|
} |
|
|
|
|
|
if (!minMaxValid) { |
|
calcMinMax(); |
|
} |
|
|
|
if (hasMin) { |
|
throw new IOException("Non-zero minimum BaseDistance in" |
|
+ " name constraints not supported"); |
|
} |
|
|
|
if (hasMax) { |
|
throw new IOException("Maximum BaseDistance in" |
|
+ " name constraints not supported"); |
|
} |
|
|
|
X500Principal subjectPrincipal = cert.getSubjectX500Principal(); |
|
X500Name subject = X500Name.asX500Name(subjectPrincipal); |
|
|
|
|
|
if (subject.isEmpty() == false) { |
|
if (verify(subject) == false) { |
|
return false; |
|
} |
|
} |
|
|
|
GeneralNames altNames = null; |
|
|
|
try { |
|
// extract extensions, if any, from certInfo |
|
|
|
X509CertImpl certImpl = X509CertImpl.toImpl(cert); |
|
SubjectAlternativeNameExtension altNameExt = |
|
certImpl.getSubjectAlternativeNameExtension(); |
|
if (altNameExt != null) { |
|
// extract altNames from extension; this call does not |
|
|
|
altNames = altNameExt.get( |
|
SubjectAlternativeNameExtension.SUBJECT_NAME); |
|
} |
|
} catch (CertificateException ce) { |
|
throw new IOException("Unable to extract extensions from " + |
|
"certificate: " + ce.getMessage()); |
|
} |
|
|
|
if (altNames == null) { |
|
altNames = new GeneralNames(); |
|
|
|
// RFC 5280 4.2.1.10: |
|
// When constraints are imposed on the rfc822Name name form, |
|
// but the certificate does not include a subject alternative name, |
|
// the rfc822Name constraint MUST be applied to the attribute of |
|
|
|
for (AVA ava : subject.allAvas()) { |
|
ObjectIdentifier attrOID = ava.getObjectIdentifier(); |
|
if (attrOID.equals(PKCS9Attribute.EMAIL_ADDRESS_OID)) { |
|
String attrValue = ava.getValueString(); |
|
if (attrValue != null) { |
|
try { |
|
altNames.add(new GeneralName( |
|
new RFC822Name(attrValue))); |
|
} catch (IOException ioe) { |
|
continue; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
// If there is no IPAddressName or DNSName in subjectAlternativeNames, |
|
|
|
DerValue derValue = subject.findMostSpecificAttribute |
|
(X500Name.commonName_oid); |
|
String cn = derValue == null ? null : derValue.getAsString(); |
|
|
|
if (cn != null) { |
|
try { |
|
if (IPAddressUtil.isIPv4LiteralAddress(cn) || |
|
IPAddressUtil.isIPv6LiteralAddress(cn)) { |
|
if (!hasNameType(altNames, GeneralNameInterface.NAME_IP)) { |
|
altNames.add(new GeneralName(new IPAddressName(cn))); |
|
} |
|
} else { |
|
if (!hasNameType(altNames, GeneralNameInterface.NAME_DNS)) { |
|
altNames.add(new GeneralName(new DNSName(cn))); |
|
} |
|
} |
|
} catch (IOException ioe) { |
|
// OK, cn is neither IP nor DNS |
|
} |
|
} |
|
|
|
|
|
for (int i = 0; i < altNames.size(); i++) { |
|
GeneralNameInterface altGNI = altNames.get(i).getName(); |
|
if (!verify(altGNI)) { |
|
return false; |
|
} |
|
} |
|
|
|
|
|
return true; |
|
} |
|
|
|
private static boolean hasNameType(GeneralNames names, int type) { |
|
for (GeneralName name : names.names()) { |
|
if (name.getType() == type) { |
|
return true; |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean verify(GeneralNameInterface name) throws IOException { |
|
if (name == null) { |
|
throw new IOException("name is null"); |
|
} |
|
|
|
|
|
if (excluded != null && excluded.size() > 0) { |
|
|
|
for (int i = 0; i < excluded.size(); i++) { |
|
GeneralSubtree gs = excluded.get(i); |
|
if (gs == null) |
|
continue; |
|
GeneralName gn = gs.getName(); |
|
if (gn == null) |
|
continue; |
|
GeneralNameInterface exName = gn.getName(); |
|
if (exName == null) |
|
continue; |
|
|
|
// if name matches or narrows any excluded subtree, |
|
|
|
switch (exName.constrains(name)) { |
|
case GeneralNameInterface.NAME_DIFF_TYPE: |
|
case GeneralNameInterface.NAME_WIDENS: |
|
case GeneralNameInterface.NAME_SAME_TYPE: |
|
break; |
|
case GeneralNameInterface.NAME_MATCH: |
|
case GeneralNameInterface.NAME_NARROWS: |
|
return false; |
|
} |
|
} |
|
} |
|
|
|
|
|
if (permitted != null && permitted.size() > 0) { |
|
|
|
boolean sameType = false; |
|
|
|
for (int i = 0; i < permitted.size(); i++) { |
|
GeneralSubtree gs = permitted.get(i); |
|
if (gs == null) |
|
continue; |
|
GeneralName gn = gs.getName(); |
|
if (gn == null) |
|
continue; |
|
GeneralNameInterface perName = gn.getName(); |
|
if (perName == null) |
|
continue; |
|
|
|
// if Name matches any type in permitted, |
|
// and Name does not match or narrow some permitted subtree, |
|
|
|
switch (perName.constrains(name)) { |
|
case GeneralNameInterface.NAME_DIFF_TYPE: |
|
continue; |
|
case GeneralNameInterface.NAME_WIDENS: |
|
case GeneralNameInterface.NAME_SAME_TYPE: |
|
sameType = true; |
|
continue; |
|
case GeneralNameInterface.NAME_MATCH: |
|
case GeneralNameInterface.NAME_NARROWS: |
|
|
|
return true; |
|
} |
|
} |
|
if (sameType) { |
|
return false; |
|
} |
|
} |
|
return true; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public Object clone() { |
|
try { |
|
NameConstraintsExtension newNCE = |
|
(NameConstraintsExtension) super.clone(); |
|
|
|
if (permitted != null) { |
|
newNCE.permitted = (GeneralSubtrees) permitted.clone(); |
|
} |
|
if (excluded != null) { |
|
newNCE.excluded = (GeneralSubtrees) excluded.clone(); |
|
} |
|
return newNCE; |
|
} catch (CloneNotSupportedException cnsee) { |
|
throw new RuntimeException("CloneNotSupportedException while " + |
|
"cloning NameConstraintsException. This should never happen."); |
|
} |
|
} |
|
} |