0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.x509;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStream;
0N/Aimport java.security.Principal;
0N/Aimport java.security.cert.CertificateEncodingException;
0N/Aimport java.security.cert.CertificateException;
0N/Aimport java.security.cert.CertificateParsingException;
0N/Aimport java.security.cert.X509Certificate;
0N/Aimport java.util.*;
0N/A
0N/Aimport javax.security.auth.x500.X500Principal;
0N/A
0N/Aimport sun.security.util.*;
0N/Aimport sun.security.pkcs.PKCS9Attribute;
0N/A
0N/A/**
0N/A * This class defines the Name Constraints Extension.
0N/A * <p>
0N/A * The name constraints extension provides permitted and excluded
0N/A * subtrees that place restrictions on names that may be included within
0N/A * a certificate issued by a given CA. Restrictions may apply to the
0N/A * subject distinguished name or subject alternative names. Any name
0N/A * matching a restriction in the excluded subtrees field is invalid
0N/A * regardless of information appearing in the permitted subtrees.
0N/A * <p>
0N/A * The ASN.1 syntax for this is:
0N/A * <pre>
0N/A * NameConstraints ::= SEQUENCE {
0N/A * permittedSubtrees [0] GeneralSubtrees OPTIONAL,
0N/A * excludedSubtrees [1] GeneralSubtrees OPTIONAL
0N/A * }
0N/A * GeneralSubtrees ::= SEQUENCE SIZE (1..MAX) OF GeneralSubtree
0N/A * </pre>
0N/A *
0N/A * @author Amit Kapoor
0N/A * @author Hemma Prafullchandra
0N/A * @see Extension
0N/A * @see CertAttrSet
0N/A */
0N/Apublic class NameConstraintsExtension extends Extension
0N/Aimplements CertAttrSet<String>, Cloneable {
0N/A /**
0N/A * Identifier for this attribute, to be used with the
0N/A * get, set, delete methods of Certificate, x509 type.
0N/A */
0N/A public static final String IDENT = "x509.info.extensions.NameConstraints";
0N/A /**
0N/A * Attribute names.
0N/A */
0N/A public static final String NAME = "NameConstraints";
0N/A public static final String PERMITTED_SUBTREES = "permitted_subtrees";
0N/A public static final String EXCLUDED_SUBTREES = "excluded_subtrees";
0N/A
0N/A // Private data members
0N/A private static final byte TAG_PERMITTED = 0;
0N/A private static final byte TAG_EXCLUDED = 1;
0N/A
0N/A private GeneralSubtrees permitted = null;
0N/A private GeneralSubtrees excluded = null;
0N/A
0N/A private boolean hasMin;
0N/A private boolean hasMax;
0N/A private boolean minMaxValid = false;
0N/A
0N/A // Recalculate hasMin and hasMax flags.
0N/A private void calcMinMax() throws IOException {
0N/A hasMin = false;
0N/A hasMax = false;
0N/A if (excluded != null) {
0N/A for (int i = 0; i < excluded.size(); i++) {
0N/A GeneralSubtree subtree = excluded.get(i);
0N/A if (subtree.getMinimum() != 0)
0N/A hasMin = true;
0N/A if (subtree.getMaximum() != -1)
0N/A hasMax = true;
0N/A }
0N/A }
0N/A
0N/A if (permitted != null) {
0N/A for (int i = 0; i < permitted.size(); i++) {
0N/A GeneralSubtree subtree = permitted.get(i);
0N/A if (subtree.getMinimum() != 0)
0N/A hasMin = true;
0N/A if (subtree.getMaximum() != -1)
0N/A hasMax = true;
0N/A }
0N/A }
0N/A minMaxValid = true;
0N/A }
0N/A
0N/A // Encode this extension value.
0N/A private void encodeThis() throws IOException {
0N/A minMaxValid = false;
0N/A if (permitted == null && excluded == null) {
0N/A this.extensionValue = null;
0N/A return;
0N/A }
0N/A DerOutputStream seq = new DerOutputStream();
0N/A
0N/A DerOutputStream tagged = new DerOutputStream();
0N/A if (permitted != null) {
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A permitted.encode(tmp);
0N/A tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A true, TAG_PERMITTED), tmp);
0N/A }
0N/A if (excluded != null) {
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A excluded.encode(tmp);
0N/A tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A true, TAG_EXCLUDED), tmp);
0N/A }
0N/A seq.write(DerValue.tag_Sequence, tagged);
0N/A this.extensionValue = seq.toByteArray();
0N/A }
0N/A
0N/A /**
0N/A * The default constructor for this class. Both parameters
0N/A * are optional and can be set to null. The extension criticality
0N/A * is set to true.
0N/A *
0N/A * @param permitted the permitted GeneralSubtrees (null for optional).
0N/A * @param excluded the excluded GeneralSubtrees (null for optional).
0N/A */
0N/A public NameConstraintsExtension(GeneralSubtrees permitted,
0N/A GeneralSubtrees excluded)
0N/A throws IOException {
0N/A this.permitted = permitted;
0N/A this.excluded = excluded;
0N/A
0N/A this.extensionId = PKIXExtensions.NameConstraints_Id;
0N/A this.critical = true;
0N/A encodeThis();
0N/A }
0N/A
0N/A /**
0N/A * Create the extension from the passed DER encoded value.
0N/A *
0N/A * @param critical true if the extension is to be treated as critical.
0N/A * @param value an array of DER encoded bytes of the actual value.
0N/A * @exception ClassCastException if value is not an array of bytes
0N/A * @exception IOException on error.
0N/A */
0N/A public NameConstraintsExtension(Boolean critical, Object value)
0N/A throws IOException {
0N/A this.extensionId = PKIXExtensions.NameConstraints_Id;
0N/A this.critical = critical.booleanValue();
0N/A
0N/A this.extensionValue = (byte[]) value;
0N/A DerValue val = new DerValue(this.extensionValue);
0N/A if (val.tag != DerValue.tag_Sequence) {
0N/A throw new IOException("Invalid encoding for" +
0N/A " NameConstraintsExtension.");
0N/A }
0N/A
0N/A // NB. this is always encoded with the IMPLICIT tag
0N/A // The checks only make sense if we assume implicit tagging,
0N/A // with explicit tagging the form is always constructed.
0N/A // Note that all the fields in NameConstraints are defined as
0N/A // being OPTIONAL, i.e., there could be an empty SEQUENCE, resulting
0N/A // in val.data being null.
0N/A if (val.data == null)
0N/A return;
0N/A while (val.data.available() != 0) {
0N/A DerValue opt = val.data.getDerValue();
0N/A
0N/A if (opt.isContextSpecific(TAG_PERMITTED) && opt.isConstructed()) {
0N/A if (permitted != null) {
0N/A throw new IOException("Duplicate permitted " +
0N/A "GeneralSubtrees in NameConstraintsExtension.");
0N/A }
0N/A opt.resetTag(DerValue.tag_Sequence);
0N/A permitted = new GeneralSubtrees(opt);
0N/A
0N/A } else if (opt.isContextSpecific(TAG_EXCLUDED) &&
0N/A opt.isConstructed()) {
0N/A if (excluded != null) {
0N/A throw new IOException("Duplicate excluded " +
0N/A "GeneralSubtrees in NameConstraintsExtension.");
0N/A }
0N/A opt.resetTag(DerValue.tag_Sequence);
0N/A excluded = new GeneralSubtrees(opt);
0N/A } else
0N/A throw new IOException("Invalid encoding of " +
0N/A "NameConstraintsExtension.");
0N/A }
0N/A minMaxValid = false;
0N/A }
0N/A
0N/A /**
0N/A * Return the printable string.
0N/A */
0N/A public String toString() {
0N/A return (super.toString() + "NameConstraints: [" +
0N/A ((permitted == null) ? "" :
0N/A ("\n Permitted:" + permitted.toString())) +
0N/A ((excluded == null) ? "" :
0N/A ("\n Excluded:" + excluded.toString()))
0N/A + " ]\n");
0N/A }
0N/A
0N/A /**
0N/A * Write the extension to the OutputStream.
0N/A *
0N/A * @param out the OutputStream to write the extension to.
0N/A * @exception IOException on encoding errors.
0N/A */
0N/A public void encode(OutputStream out) throws IOException {
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A if (this.extensionValue == null) {
0N/A this.extensionId = PKIXExtensions.NameConstraints_Id;
0N/A this.critical = true;
0N/A encodeThis();
0N/A }
0N/A super.encode(tmp);
0N/A out.write(tmp.toByteArray());
0N/A }
0N/A
0N/A /**
0N/A * Set the attribute value.
0N/A */
0N/A public void set(String name, Object obj) throws IOException {
0N/A if (name.equalsIgnoreCase(PERMITTED_SUBTREES)) {
0N/A if (!(obj instanceof GeneralSubtrees)) {
0N/A throw new IOException("Attribute value should be"
0N/A + " of type GeneralSubtrees.");
0N/A }
0N/A permitted = (GeneralSubtrees)obj;
0N/A } else if (name.equalsIgnoreCase(EXCLUDED_SUBTREES)) {
0N/A if (!(obj instanceof GeneralSubtrees)) {
0N/A throw new IOException("Attribute value should be "
0N/A + "of type GeneralSubtrees.");
0N/A }
0N/A excluded = (GeneralSubtrees)obj;
0N/A } else {
0N/A throw new IOException("Attribute name not recognized by " +
0N/A "CertAttrSet:NameConstraintsExtension.");
0N/A }
0N/A encodeThis();
0N/A }
0N/A
0N/A /**
0N/A * Get the attribute value.
0N/A */
0N/A public Object get(String name) throws IOException {
0N/A if (name.equalsIgnoreCase(PERMITTED_SUBTREES)) {
0N/A return (permitted);
0N/A } else if (name.equalsIgnoreCase(EXCLUDED_SUBTREES)) {
0N/A return (excluded);
0N/A } else {
0N/A throw new IOException("Attribute name not recognized by " +
0N/A "CertAttrSet:NameConstraintsExtension.");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Delete the attribute value.
0N/A */
0N/A public void delete(String name) throws IOException {
0N/A if (name.equalsIgnoreCase(PERMITTED_SUBTREES)) {
0N/A permitted = null;
0N/A } else if (name.equalsIgnoreCase(EXCLUDED_SUBTREES)) {
0N/A excluded = null;
0N/A } else {
0N/A throw new IOException("Attribute name not recognized by " +
0N/A "CertAttrSet:NameConstraintsExtension.");
0N/A }
0N/A encodeThis();
0N/A }
0N/A
0N/A /**
0N/A * Return an enumeration of names of attributes existing within this
0N/A * attribute.
0N/A */
0N/A public Enumeration<String> getElements() {
0N/A AttributeNameEnumeration elements = new AttributeNameEnumeration();
0N/A elements.addElement(PERMITTED_SUBTREES);
0N/A elements.addElement(EXCLUDED_SUBTREES);
0N/A
0N/A return (elements.elements());
0N/A }
0N/A
0N/A /**
0N/A * Return the name of this attribute.
0N/A */
0N/A public String getName() {
0N/A return (NAME);
0N/A }
0N/A
0N/A /**
0N/A * Merge additional name constraints with existing ones.
0N/A * This function is used in certification path processing
0N/A * to accumulate name constraints from successive certificates
0N/A * in the path. Note that NameConstraints can never be
0N/A * expanded by a merge, just remain constant or become more
0N/A * limiting.
0N/A * <p>
0N/A * IETF RFC2459 specifies the processing of Name Constraints as
0N/A * follows:
0N/A * <p>
0N/A * (j) If permittedSubtrees is present in the certificate, set the
0N/A * constrained subtrees state variable to the intersection of its
0N/A * previous value and the value indicated in the extension field.
0N/A * <p>
0N/A * (k) If excludedSubtrees is present in the certificate, set the
0N/A * excluded subtrees state variable to the union of its previous
0N/A * value and the value indicated in the extension field.
0N/A * <p>
0N/A * @param newConstraints additional NameConstraints to be applied
0N/A * @throws IOException on error
0N/A */
0N/A public void merge(NameConstraintsExtension newConstraints)
0N/A throws IOException {
0N/A
0N/A if (newConstraints == null) {
0N/A // absence of any explicit constraints implies unconstrained
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * If excludedSubtrees is present in the certificate, set the
0N/A * excluded subtrees state variable to the union of its previous
0N/A * value and the value indicated in the extension field.
0N/A */
0N/A
0N/A GeneralSubtrees newExcluded =
0N/A (GeneralSubtrees)newConstraints.get(EXCLUDED_SUBTREES);
0N/A if (excluded == null) {
0N/A excluded = (newExcluded != null) ?
0N/A (GeneralSubtrees)newExcluded.clone() : null;
0N/A } else {
0N/A if (newExcluded != null) {
0N/A // Merge new excluded with current excluded (union)
0N/A excluded.union(newExcluded);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * If permittedSubtrees is present in the certificate, set the
0N/A * constrained subtrees state variable to the intersection of its
0N/A * previous value and the value indicated in the extension field.
0N/A */
0N/A
0N/A GeneralSubtrees newPermitted =
0N/A (GeneralSubtrees)newConstraints.get(PERMITTED_SUBTREES);
0N/A if (permitted == null) {
0N/A permitted = (newPermitted != null) ?
0N/A (GeneralSubtrees)newPermitted.clone() : null;
0N/A } else {
0N/A if (newPermitted != null) {
0N/A // Merge new permitted with current permitted (intersection)
0N/A newExcluded = permitted.intersect(newPermitted);
0N/A
0N/A // Merge new excluded subtrees to current excluded (union)
0N/A if (newExcluded != null) {
0N/A if (excluded != null) {
0N/A excluded.union(newExcluded);
0N/A } else {
0N/A excluded = (GeneralSubtrees)newExcluded.clone();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Optional optimization: remove permitted subtrees that are excluded.
0N/A // This is not necessary for algorithm correctness, but it makes
0N/A // subsequent operations on the NameConstraints faster and require
0N/A // less space.
0N/A if (permitted != null) {
0N/A permitted.reduce(excluded);
0N/A }
0N/A
0N/A // The NameConstraints have been changed, so re-encode them. Methods in
0N/A // this class assume that the encodings have already been done.
0N/A encodeThis();
0N/A
0N/A }
0N/A
0N/A /**
0N/A * check whether a certificate conforms to these NameConstraints.
0N/A * This involves verifying that the subject name and subjectAltName
0N/A * extension (critical or noncritical) is consistent with the permitted
0N/A * subtrees state variables. Also verify that the subject name and
0N/A * subjectAltName extension (critical or noncritical) is consistent with
0N/A * the excluded subtrees state variables.
0N/A *
0N/A * @param cert X509Certificate to be verified
0N/A * @returns true if certificate verifies successfully
0N/A * @throws IOException on error
0N/A */
0N/A public boolean verify(X509Certificate cert) throws IOException {
0N/A
0N/A if (cert == null) {
0N/A throw new IOException("Certificate is null");
0N/A }
0N/A
0N/A // Calculate hasMin and hasMax booleans (if necessary)
0N/A if (!minMaxValid) {
0N/A calcMinMax();
0N/A }
0N/A
0N/A if (hasMin) {
0N/A throw new IOException("Non-zero minimum BaseDistance in"
0N/A + " name constraints not supported");
0N/A }
0N/A
0N/A if (hasMax) {
0N/A throw new IOException("Maximum BaseDistance in"
0N/A + " name constraints not supported");
0N/A }
0N/A
0N/A X500Principal subjectPrincipal = cert.getSubjectX500Principal();
0N/A X500Name subject = X500Name.asX500Name(subjectPrincipal);
0N/A
0N/A if (subject.isEmpty() == false) {
0N/A if (verify(subject) == false) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A GeneralNames altNames = null;
0N/A // extract altNames
0N/A try {
0N/A // extract extensions, if any, from certInfo
0N/A // following returns null if certificate contains no extensions
0N/A X509CertImpl certImpl = X509CertImpl.toImpl(cert);
0N/A SubjectAlternativeNameExtension altNameExt =
0N/A certImpl.getSubjectAlternativeNameExtension();
0N/A if (altNameExt != null) {
0N/A // extract altNames from extension; this call does not
0N/A // return an IOException on null altnames
0N/A altNames = (GeneralNames)
0N/A (altNameExt.get(altNameExt.SUBJECT_NAME));
0N/A }
0N/A } catch (CertificateException ce) {
0N/A throw new IOException("Unable to extract extensions from " +
0N/A "certificate: " + ce.getMessage());
0N/A }
0N/A
0N/A // If there are no subjectAlternativeNames, perform the special-case
0N/A // check where if the subjectName contains any EMAILADDRESS
0N/A // attributes, they must be checked against RFC822 constraints.
0N/A // If that passes, we're fine.
0N/A if (altNames == null) {
0N/A return verifyRFC822SpecialCase(subject);
0N/A }
0N/A
0N/A // verify each subjectAltName
0N/A for (int i = 0; i < altNames.size(); i++) {
0N/A GeneralNameInterface altGNI = altNames.get(i).getName();
0N/A if (!verify(altGNI)) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A // All tests passed.
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * check whether a name conforms to these NameConstraints.
0N/A * This involves verifying that the name is consistent with the
0N/A * permitted and excluded subtrees variables.
0N/A *
0N/A * @param name GeneralNameInterface name to be verified
0N/A * @returns true if certificate verifies successfully
0N/A * @throws IOException on error
0N/A */
0N/A public boolean verify(GeneralNameInterface name) throws IOException {
0N/A if (name == null) {
0N/A throw new IOException("name is null");
0N/A }
0N/A
0N/A // Verify that the name is consistent with the excluded subtrees
0N/A if (excluded != null && excluded.size() > 0) {
0N/A
0N/A for (int i = 0; i < excluded.size(); i++) {
0N/A GeneralSubtree gs = excluded.get(i);
0N/A if (gs == null)
0N/A continue;
0N/A GeneralName gn = gs.getName();
0N/A if (gn == null)
0N/A continue;
0N/A GeneralNameInterface exName = gn.getName();
0N/A if (exName == null)
0N/A continue;
0N/A
0N/A // if name matches or narrows any excluded subtree,
0N/A // return false
0N/A switch (exName.constrains(name)) {
0N/A case GeneralNameInterface.NAME_DIFF_TYPE:
0N/A case GeneralNameInterface.NAME_WIDENS: // name widens excluded
0N/A case GeneralNameInterface.NAME_SAME_TYPE:
0N/A break;
0N/A case GeneralNameInterface.NAME_MATCH:
0N/A case GeneralNameInterface.NAME_NARROWS: // subject name excluded
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Verify that the name is consistent with the permitted subtrees
0N/A if (permitted != null && permitted.size() > 0) {
0N/A
0N/A boolean sameType = false;
0N/A
0N/A for (int i = 0; i < permitted.size(); i++) {
0N/A GeneralSubtree gs = permitted.get(i);
0N/A if (gs == null)
0N/A continue;
0N/A GeneralName gn = gs.getName();
0N/A if (gn == null)
0N/A continue;
0N/A GeneralNameInterface perName = gn.getName();
0N/A if (perName == null)
0N/A continue;
0N/A
0N/A // if Name matches any type in permitted,
0N/A // and Name does not match or narrow some permitted subtree,
0N/A // return false
0N/A switch (perName.constrains(name)) {
0N/A case GeneralNameInterface.NAME_DIFF_TYPE:
0N/A continue; // continue checking other permitted names
0N/A case GeneralNameInterface.NAME_WIDENS: // name widens permitted
0N/A case GeneralNameInterface.NAME_SAME_TYPE:
0N/A sameType = true;
0N/A continue; // continue to look for a match or narrow
0N/A case GeneralNameInterface.NAME_MATCH:
0N/A case GeneralNameInterface.NAME_NARROWS:
0N/A // name narrows permitted
0N/A return true; // name is definitely OK, so break out of loop
0N/A }
0N/A }
0N/A if (sameType) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Perform the RFC 822 special case check. We have a certificate
0N/A * that does not contain any subject alternative names. Check that
0N/A * any EMAILADDRESS attributes in its subject name conform to these
0N/A * NameConstraints.
0N/A *
0N/A * @param subject the certificate's subject name
0N/A * @returns true if certificate verifies successfully
0N/A * @throws IOException on error
0N/A */
0N/A public boolean verifyRFC822SpecialCase(X500Name subject) throws IOException {
0N/A for (Iterator t = subject.allAvas().iterator(); t.hasNext(); ) {
0N/A AVA ava = (AVA)t.next();
0N/A ObjectIdentifier attrOID = ava.getObjectIdentifier();
0N/A if (attrOID.equals(PKCS9Attribute.EMAIL_ADDRESS_OID)) {
0N/A String attrValue = ava.getValueString();
0N/A if (attrValue != null) {
0N/A RFC822Name emailName;
0N/A try {
0N/A emailName = new RFC822Name(attrValue);
0N/A } catch (IOException ioe) {
0N/A continue;
0N/A }
0N/A if (!verify(emailName)) {
0N/A return(false);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Clone all objects that may be modified during certificate validation.
0N/A */
0N/A public Object clone() {
0N/A try {
0N/A NameConstraintsExtension newNCE =
0N/A (NameConstraintsExtension) super.clone();
0N/A
0N/A if (permitted != null) {
0N/A newNCE.permitted = (GeneralSubtrees) permitted.clone();
0N/A }
0N/A if (excluded != null) {
0N/A newNCE.excluded = (GeneralSubtrees) excluded.clone();
0N/A }
0N/A return newNCE;
0N/A } catch (CloneNotSupportedException cnsee) {
0N/A throw new RuntimeException("CloneNotSupportedException while " +
0N/A "cloning NameConstraintsException. This should never happen.");
0N/A }
0N/A }
0N/A}