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