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