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