src/java.base/share/classes/sun/security/x509/X500Name.java
author igerasim
Tue, 02 Oct 2018 10:19:07 -0700
changeset 51986 c1db377f6300
parent 47216 71c04702a3d5
child 52856 5f3b9b633731
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
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 1996, 2014, 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.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
    34
import java.util.StringJoiner;
2
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 javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Note:  As of 1.4, the public class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * javax.security.auth.x500.X500Principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * should be used when parsing, generating, and comparing X.500 DNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class contains other useful methods for checking name constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * and retrieving DNs by keyword.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p> X.500 names are used to identify entities, such as those which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * identified by X.509 certificates.  They are world-wide, hierarchical,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * and descriptive.  Entities can be identified by attributes, and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * some systems can be searched for according to those attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * The ASN.1 for this is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * GeneralName ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     directoryName                   [4]     Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Name ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   RDNSequence }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * RelativeDistinguishedName ::=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   SET OF AttributeTypeAndValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * AttributeTypeAndValue ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   type     AttributeType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   value    AttributeValue }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * AttributeType ::= OBJECT IDENTIFIER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * AttributeValue ::= ANY DEFINED BY AttributeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * DirectoryString ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *       teletexString           TeletexString (SIZE (1..MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *       printableString         PrintableString (SIZE (1..MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *       universalString         UniversalString (SIZE (1..MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *       utf8String              UTF8String (SIZE (1.. MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *       bmpString               BMPString (SIZE (1..MAX)) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * This specification requires only a subset of the name comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * functionality specified in the X.500 series of specifications.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * requirements for conforming implementations are as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <ol TYPE=a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <li>attribute values encoded in different types (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *    PrintableString and BMPString) may be assumed to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *    different strings;
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 30033
diff changeset
    88
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <li>attribute values in types other than PrintableString are case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *    sensitive (this permits matching of attribute values as binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *    objects);
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 30033
diff changeset
    92
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <li>attribute values in PrintableString are not case sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *    (e.g., "Marianne Swanson" is the same as "MARIANNE SWANSON"); and
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 30033
diff changeset
    95
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <li>attribute values in PrintableString are compared after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *    removing leading and trailing white space and converting internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *    substrings of one or more consecutive white space characters to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *    single space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * These name comparison rules permit a certificate user to validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * certificates issued using languages or encodings unfamiliar to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * certificate user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * In addition, implementations of this specification MAY use these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * comparison rules to process unfamiliar attribute types for name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * chaining. This allows implementations to process certificates with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * unfamiliar attributes in the issuer name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * Note that the comparison rules defined in the X.500 series of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * specifications indicate that the character sets used to encode data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * in distinguished names are irrelevant.  The characters themselves are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * compared without regard to encoding. Implementations of the profile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * are permitted to use the comparison algorithm defined in the X.500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * series.  Such an implementation will recognize a superset of name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * matches recognized by the algorithm specified above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * Note that instances of this class are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @see GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @see GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @see GeneralNameInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
public class X500Name implements GeneralNameInterface, Principal {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private String dn; // roughly RFC 1779 DN, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private String rfc1779Dn; // RFC 1779 compliant DN, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private String rfc2253Dn; // RFC 2253 DN, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private String canonicalDn; // canonical RFC 2253 DN or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private RDN[] names;        // RDNs (never null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private X500Principal x500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // cached immutable list of the RDNs and all the AVAs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private volatile List<RDN> rdnList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private volatile List<AVA> allAvaList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Constructs a name from a conventionally formatted string, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * as "CN=Dave, OU=JavaSoft, O=Sun Microsystems, C=US".
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   146
     * (RFC 1779, 2253, or 4514 style).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   148
     * @param dname the X.500 Distinguished Name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public X500Name(String dname) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this(dname, Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Constructs a name from a conventionally formatted string, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * as "CN=Dave, OU=JavaSoft, O=Sun Microsystems, C=US".
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   157
     * (RFC 1779, 2253, or 4514 style).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   159
     * @param dname the X.500 Distinguished Name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param keywordMap an additional keyword/OID map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public X500Name(String dname, Map<String, String> keywordMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        parseDN(dname, keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Constructs a name from a string formatted according to format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Currently, the formats DEFAULT and RFC2253 are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * DEFAULT is the default format used by the X500Name(String)
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   171
     * constructor. RFC2253 is the format strictly according to RFC2253
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * without extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   174
     * @param dname the X.500 Distinguished Name
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   175
     * @param format the specified format of the String DN
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public X500Name(String dname, String format) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (dname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new NullPointerException("Name must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (format.equalsIgnoreCase("RFC2253")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            parseRFC2253DN(dname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else if (format.equalsIgnoreCase("DEFAULT")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            parseDN(dname, Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new IOException("Unsupported format " + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Constructs a name from fields common in enterprise application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * environments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <P><EM><STRONG>NOTE:</STRONG>  The behaviour when any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * these strings contain characters outside the ASCII range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * is unspecified in currently relevant standards.</EM>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param commonName common name of a person, e.g. "Vivette Davis"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param organizationUnit small organization name, e.g. "Purchasing"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param organizationName large organization name, e.g. "Onizuka, Inc."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param country two letter country code, e.g. "CH"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public X500Name(String commonName, String organizationUnit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                     String organizationName, String country)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        names = new RDN[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * NOTE:  it's only on output that little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         * ordering is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        names[3] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        names[3].assertion[0] = new AVA(commonName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                new DerValue(commonName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        names[2] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        names[2].assertion[0] = new AVA(orgUnitName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                new DerValue(organizationUnit));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        names[1] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        names[1].assertion[0] = new AVA(orgName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                new DerValue(organizationName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        names[0] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        names[0].assertion[0] = new AVA(countryName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                new DerValue(country));
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
     * Constructs a name from fields common in Internet application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * environments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <P><EM><STRONG>NOTE:</STRONG>  The behaviour when any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * these strings contain characters outside the ASCII range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * is unspecified in currently relevant standards.</EM>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param commonName common name of a person, e.g. "Vivette Davis"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param organizationUnit small organization name, e.g. "Purchasing"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param organizationName large organization name, e.g. "Onizuka, Inc."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param localityName locality (city) name, e.g. "Palo Alto"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param stateName state name, e.g. "California"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param country two letter country code, e.g. "CH"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public X500Name(String commonName, String organizationUnit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    String organizationName, String localityName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    String stateName, String country)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        names = new RDN[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * NOTE:  it's only on output that little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * ordering is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        names[5] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        names[5].assertion[0] = new AVA(commonName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                new DerValue(commonName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        names[4] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        names[4].assertion[0] = new AVA(orgUnitName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                new DerValue(organizationUnit));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        names[3] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        names[3].assertion[0] = new AVA(orgName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                new DerValue(organizationName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        names[2] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        names[2].assertion[0] = new AVA(localityName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                new DerValue(localityName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        names[1] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        names[1].assertion[0] = new AVA(stateName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                new DerValue(stateName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        names[0] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        names[0].assertion[0] = new AVA(countryName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                new DerValue(country));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Constructs a name from an array of relative distinguished names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param rdnArray array of relative distinguished names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public X500Name(RDN[] rdnArray) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (rdnArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            names = new RDN[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            names = rdnArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                if (names[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    throw new IOException("Cannot create an X500Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Constructs a name from an ASN.1 encoded value.  The encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * of the name in the stream uses DER (a BER/1 subset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param value a DER-encoded value holding an X.500 name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public X500Name(DerValue value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        //Note that toDerInputStream uses only the buffer (data) and not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        //the tag, so an empty SEQUENCE (OF) will yield an empty DerInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        this(value.toDerInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Constructs a name from an ASN.1 encoded input stream.  The encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * of the name in the stream uses DER (a BER/1 subset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param in DER-encoded data holding an X.500 name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public X500Name(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        parseDER(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *  Constructs a name from an ASN.1 encoded byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param name DER-encoded byte array holding an X.500 name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public X500Name(byte[] name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        DerInputStream in = new DerInputStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        parseDER(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Return an immutable List of all RDNs in this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public List<RDN> rdns() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        List<RDN> list = rdnList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            list = Collections.unmodifiableList(Arrays.asList(names));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            rdnList = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Return the number of RDNs in this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26967
diff changeset
   340
     * Return an immutable List of the AVAs contained in all the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * RDNs of this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public List<AVA> allAvas() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        List<AVA> list = allAvaList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (list == null) {
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 28059
diff changeset
   346
            list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                list.addAll(names[i].avas());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 31538
diff changeset
   350
            list = Collections.unmodifiableList(list);
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 31538
diff changeset
   351
            allAvaList = list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Return the total number of AVAs contained in all the RDNs of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public int avaSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return allAvas().size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Return whether this X500Name is empty. An X500Name is not empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * if it has at least one RDN containing at least one AVA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        int n = names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (names[i].assertion.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Calculates a hash code value for the object.  Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * which are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return getRFC2253CanonicalName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Compares this name with another, for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @return true iff the names are identical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (obj instanceof X500Name == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        X500Name other = (X500Name)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // if we already have the canonical forms, compare now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if ((this.canonicalDn != null) && (other.canonicalDn != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return this.canonicalDn.equals(other.canonicalDn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // quick check that number of RDNs and AVAs match before canonicalizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        int n = this.names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (n != other.names.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            RDN r1 = this.names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            RDN r2 = other.names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (r1.assertion.length != r2.assertion.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // definite check via canonical form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        String thisCanonical = this.getRFC2253CanonicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        String otherCanonical = other.getRFC2253CanonicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return thisCanonical.equals(otherCanonical);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Returns the name component as a Java string, regardless of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * encoding restrictions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    private String getString(DerValue attribute) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (attribute == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        String  value = attribute.getAsString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throw new IOException("not a DER string encoding, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    + attribute.tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Return type of GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return (GeneralNameInterface.NAME_DIRECTORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Returns a "Country" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * such attribute exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @return "C=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public String getCountry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        DerValue attr = findAttribute(countryName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Returns an "Organization" name component.  If more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * one such attribute exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @return "O=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public String getOrganization() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        DerValue attr = findAttribute(orgName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Returns an "Organizational Unit" name component.  If more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * than one such attribute exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @return "OU=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public String getOrganizationalUnit() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        DerValue attr = findAttribute(orgUnitName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Returns a "Common Name" component.  If more than one such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * attribute exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @return "CN=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public String getCommonName() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        DerValue attr = findAttribute(commonName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Returns a "Locality" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * such component exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @return "L=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public String getLocality() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        DerValue attr = findAttribute(localityName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Returns a "State" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * such component exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @return "S=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public String getState() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
      DerValue attr = findAttribute(stateName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Returns a "Domain" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * such component exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @return "DC=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public String getDomain() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        DerValue attr = findAttribute(DOMAIN_COMPONENT_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Returns a "DN Qualifier" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * such component exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @return "DNQ=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public String getDNQualifier() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        DerValue attr = findAttribute(DNQUALIFIER_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * Returns a "Surname" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * such component exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @return "SURNAME=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    public String getSurname() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        DerValue attr = findAttribute(SURNAME_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Returns a "Given Name" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * such component exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @return "GIVENNAME=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    public String getGivenName() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
       DerValue attr = findAttribute(GIVENNAME_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
       return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Returns an "Initials" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * such component exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @return "INITIALS=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public String getInitials() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        DerValue attr = findAttribute(INITIALS_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
      * Returns a "Generation Qualifier" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
      * such component exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
      * @return "GENERATION=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public String getGeneration() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        DerValue attr = findAttribute(GENERATIONQUALIFIER_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Returns an "IP address" name component.  If more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * such component exists, the topmost one is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @return "IP=" component of the name, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    public String getIP() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        DerValue attr = findAttribute(ipAddress_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return getString(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * Returns a string form of the X.500 distinguished name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * The format of the string is from RFC 1779. The returned string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * may contain non-standardised keywords for more readability
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   608
     * (keywords from RFCs 1779, 2253, and 5280).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (dn == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            generateDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return dn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Returns a string form of the X.500 distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * using the algorithm defined in RFC 1779. Only standard attribute type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * keywords defined in RFC 1779 are emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public String getRFC1779Name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        return getRFC1779Name(Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Returns a string form of the X.500 distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * using the algorithm defined in RFC 1779. Attribute type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * keywords defined in RFC 1779 are emitted, as well as additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * keywords contained in the OID/keyword map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public String getRFC1779Name(Map<String, String> oidMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if (oidMap.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            // return cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if (rfc1779Dn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                return rfc1779Dn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                rfc1779Dn = generateRFC1779DN(oidMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                return rfc1779Dn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return generateRFC1779DN(oidMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Returns a string form of the X.500 distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * using the algorithm defined in RFC 2253. Only standard attribute type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * keywords defined in RFC 2253 are emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public String getRFC2253Name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return getRFC2253Name(Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Returns a string form of the X.500 distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * using the algorithm defined in RFC 2253. Attribute type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * keywords defined in RFC 2253 are emitted, as well as additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * keywords contained in the OID/keyword map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public String getRFC2253Name(Map<String, String> oidMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        /* check for and return cached name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (oidMap.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            if (rfc2253Dn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                return rfc2253Dn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                rfc2253Dn = generateRFC2253DN(oidMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                return rfc2253Dn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        return generateRFC2253DN(oidMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    private String generateRFC2253DN(Map<String, String> oidMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * Section 2.1 : if the RDNSequence is an empty sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * the result is the empty or zero length string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if (names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         * 2.1 (continued) : Otherwise, the output consists of the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         * encodings of each RelativeDistinguishedName in the RDNSequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         * (according to 2.2), starting with the last element of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
         * and moving backwards toward the first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
         * The encodings of adjoining RelativeDistinguishedNames are separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * by a comma character (',' ASCII 44).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         */
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   692
        StringJoiner sj = new StringJoiner(",");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        for (int i = names.length - 1; i >= 0; i--) {
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   694
            sj.add(names[i].toRFC2253String(oidMap));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   696
        return sj.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public String getRFC2253CanonicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        /* check for and return cached name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (canonicalDn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return canonicalDn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         * Section 2.1 : if the RDNSequence is an empty sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         * the result is the empty or zero length string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            canonicalDn = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return canonicalDn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
         * 2.1 (continued) : Otherwise, the output consists of the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
         * encodings of each RelativeDistinguishedName in the RDNSequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
         * (according to 2.2), starting with the last element of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         * and moving backwards toward the first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
         * The encodings of adjoining RelativeDistinguishedNames are separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
         * by a comma character (',' ASCII 44).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
         */
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   722
        StringJoiner sj = new StringJoiner(",");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        for (int i = names.length - 1; i >= 0; i--) {
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   724
            sj.add(names[i].toRFC2253String(true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   726
        canonicalDn = sj.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return canonicalDn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Returns the value of toString().  This call is needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * implement the java.security.Principal interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public String getName() { return toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Find the first instance of this attribute in a "top down"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * search of all the attributes in the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    private DerValue findAttribute(ObjectIdentifier attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                DerValue value = names[i].findAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Find the most specific ("last") attribute of the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    public DerValue findMostSpecificAttribute(ObjectIdentifier attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            for (int i = names.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                DerValue value = names[i].findAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    /****************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    private void parseDER(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        // X.500 names are a "SEQUENCE OF" RDNs, which means zero or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        // more and order matters.  We scan them in order, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // conventionally is big-endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        DerValue[] nameseq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        byte[] derBytes = in.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            nameseq = in.getSequence(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            if (derBytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                nameseq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                DerValue derVal = new DerValue(DerValue.tag_Sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                                           derBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                derBytes = derVal.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                nameseq = new DerInputStream(derBytes).getSequence(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (nameseq == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            names = new RDN[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            names = new RDN[nameseq.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            for (int i = 0; i < nameseq.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                names[i] = new RDN(nameseq[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * Encodes the name in DER-encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @deprecated Use encode() instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @param out where to put the DER-encoded X.500 name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    public void emit(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * Encodes the name in DER-encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @param out where to put the DER-encoded X.500 name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            names[i].encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * Returned the encoding as an uncloned byte array. Callers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * guarantee that they neither modify it not expose it to untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    public byte[] getEncodedInternal() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            DerOutputStream     out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            DerOutputStream     tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                names[i].encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            encoded = out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * Gets the name in DER-encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @return the DER encoded byte array of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    public byte[] getEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        return getEncodedInternal().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * Parses a Distinguished Name (DN) in printable representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * According to RFC 1779, RDNs in a DN are separated by comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * The following examples show both methods of quoting a comma, so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * is not considered a separator:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *     O="Sue, Grabbit and Runn" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *     O=Sue\, Grabbit and Runn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   863
     * This method can parse RFC 1779, 2253 or 4514 DNs and non-standard 5280
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   864
     * keywords. Additional keywords can be specified in the keyword/OID map.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    private void parseDN(String input, Map<String, String> keywordMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        if (input == null || input.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            names = new RDN[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   873
        List<RDN> dnVector = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        int dnOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        int rdnEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        String rdnString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        int quoteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        String dnString = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        int searchOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        int nextComma = dnString.indexOf(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        int nextSemiColon = dnString.indexOf(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        while (nextComma >=0 || nextSemiColon >=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            if (nextSemiColon < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                rdnEnd = nextComma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            } else if (nextComma < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                rdnEnd = nextSemiColon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                rdnEnd = Math.min(nextComma, nextSemiColon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            quoteCount += countQuotes(dnString, searchOffset, rdnEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
             * We have encountered an RDN delimiter (comma or a semicolon).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
             * If the comma or semicolon in the RDN under consideration is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
             * preceded by a backslash (escape), or by a double quote, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
             * is part of the RDN. Otherwise, it is used as a separator, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
             * delimit the RDN under consideration from any subsequent RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            if (rdnEnd >= 0 && quoteCount != 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                !escaped(rdnEnd, searchOffset, dnString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                 * Comma/semicolon is a separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                rdnString = dnString.substring(dnOffset, rdnEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                // Parse RDN, and store it in vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                RDN rdn = new RDN(rdnString, keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                dnVector.add(rdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                // Increase the offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                dnOffset = rdnEnd + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                // Set quote counter back to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                quoteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            searchOffset = rdnEnd + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            nextComma = dnString.indexOf(',', searchOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            nextSemiColon = dnString.indexOf(';', searchOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        // Parse last or only RDN, and store it in vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        rdnString = dnString.substring(dnOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        RDN rdn = new RDN(rdnString, keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        dnVector.add(rdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         * Store the vector elements as an array of RDNs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         * NOTE: It's only on output that little-endian ordering is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        Collections.reverse(dnVector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        names = dnVector.toArray(new RDN[dnVector.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    private void parseRFC2253DN(String dnString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if (dnString.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            names = new RDN[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            return;
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   943
         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   945
         List<RDN> dnVector = new ArrayList<>();
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   946
         int dnOffset = 0;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   947
         String rdnString;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   948
         int searchOffset = 0;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   949
         int rdnEnd = dnString.indexOf(',');
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   950
         while (rdnEnd >=0) {
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   951
             /*
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   952
              * We have encountered an RDN delimiter (comma).
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   953
              * If the comma in the RDN under consideration is
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   954
              * preceded by a backslash (escape), it
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   955
              * is part of the RDN. Otherwise, it is used as a separator, to
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   956
              * delimit the RDN under consideration from any subsequent RDNs.
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   957
              */
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   958
             if (rdnEnd > 0 && !escaped(rdnEnd, searchOffset, dnString)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   960
                 /*
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   961
                  * Comma is a separator
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   962
                  */
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   963
                 rdnString = dnString.substring(dnOffset, rdnEnd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   965
                 // Parse RDN, and store it in vector
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   966
                 RDN rdn = new RDN(rdnString, "RFC2253");
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   967
                 dnVector.add(rdn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   969
                 // Increase the offset
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   970
                 dnOffset = rdnEnd + 1;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   971
             }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   973
             searchOffset = rdnEnd + 1;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   974
             rdnEnd = dnString.indexOf(',', searchOffset);
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   975
         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   977
         // Parse last or only RDN, and store it in vector
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   978
         rdnString = dnString.substring(dnOffset);
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   979
         RDN rdn = new RDN(rdnString, "RFC2253");
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   980
         dnVector.add(rdn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   982
         /*
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   983
          * Store the vector elements as an array of RDNs
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   984
          * NOTE: It's only on output that little-endian ordering is used.
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   985
          */
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   986
         Collections.reverse(dnVector);
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   987
         names = dnVector.toArray(new RDN[dnVector.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Counts double quotes in string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * Escaped quotes are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    static int countQuotes(String string, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        for (int i = from; i < to; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            if ((string.charAt(i) == '"' && i == from) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                (string.charAt(i) == '"' && string.charAt(i-1) != '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    private static boolean escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                (int rdnEnd, int searchOffset, String dnString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        if (rdnEnd == 1 && dnString.charAt(rdnEnd - 1) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            //  case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            //  \,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        } else if (rdnEnd > 1 && dnString.charAt(rdnEnd - 1) == '\\' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                dnString.charAt(rdnEnd - 2) != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            //  case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            //  foo\,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        } else if (rdnEnd > 1 && dnString.charAt(rdnEnd - 1) == '\\' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                dnString.charAt(rdnEnd - 2) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            //  case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            //  foo\\\\\,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            rdnEnd--;   // back up to last backSlash
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            while (rdnEnd >= searchOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                if (dnString.charAt(rdnEnd) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                    count++;    // count consecutive backslashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                rdnEnd--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            // if count is odd, then rdnEnd is escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            return (count % 2) != 0 ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * Dump the printable form of a distinguished name.  Each relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * name is separated from the next by a ",", and assertions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * relative names have "label=value" syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * Uses RFC 1779 syntax (i.e. little-endian, comma separators)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    private void generateDN() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        if (names.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            dn = names[0].toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1061
        if (names == null) {
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1062
            dn = "";
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1063
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1065
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1066
        StringJoiner sj = new StringJoiner(", ");
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1067
        for (int i = names.length - 1; i >= 0; i--) {
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1068
            sj.add(names[i].toString());
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1069
        }
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1070
        dn = sj.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * Dump the printable form of a distinguished name.  Each relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * name is separated from the next by a ",", and assertions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * relative names have "label=value" syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * Uses RFC 1779 syntax (i.e. little-endian, comma separators)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * Valid keywords from RFC 1779 are used. Additional keywords can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * specified in the OID/keyword map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    private String generateRFC1779DN(Map<String, String> oidMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        if (names.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            return names[0].toRFC1779String(oidMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1087
        if (names == null) {
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1088
            return "";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1090
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1091
        StringJoiner sj = new StringJoiner(", ");
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1092
        for (int i = names.length - 1; i >= 0; i--) {
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1093
            sj.add(names[i].toRFC1779String(oidMap));
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1094
        }
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1095
        return sj.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /****************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * Maybe return a preallocated OID, to reduce storage costs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * and speed recognition of common X.500 attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    static ObjectIdentifier intern(ObjectIdentifier oid) {
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 31538
diff changeset
  1105
        ObjectIdentifier interned = internedOIDs.putIfAbsent(oid, oid);
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 31538
diff changeset
  1106
        return (interned == null) ? oid : interned;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    private static final Map<ObjectIdentifier,ObjectIdentifier> internedOIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                        = new HashMap<ObjectIdentifier,ObjectIdentifier>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * Selected OIDs from X.520
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1114
     * Includes all those specified in RFC 5280 as MUST or SHOULD
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * be recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1117
    private static final int[] commonName_data = { 2, 5, 4, 3 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1118
    private static final int[] SURNAME_DATA = { 2, 5, 4, 4 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1119
    private static final int[] SERIALNUMBER_DATA = { 2, 5, 4, 5 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1120
    private static final int[] countryName_data = { 2, 5, 4, 6 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1121
    private static final int[] localityName_data = { 2, 5, 4, 7 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1122
    private static final int[] stateName_data = { 2, 5, 4, 8 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1123
    private static final int[] streetAddress_data = { 2, 5, 4, 9 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1124
    private static final int[] orgName_data = { 2, 5, 4, 10 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1125
    private static final int[] orgUnitName_data = { 2, 5, 4, 11 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1126
    private static final int[] title_data = { 2, 5, 4, 12 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1127
    private static final int[] GIVENNAME_DATA = { 2, 5, 4, 42 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1128
    private static final int[] INITIALS_DATA = { 2, 5, 4, 43 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1129
    private static final int[] GENERATIONQUALIFIER_DATA = { 2, 5, 4, 44 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1130
    private static final int[] DNQUALIFIER_DATA = { 2, 5, 4, 46 };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1132
    private static final int[] ipAddress_data = { 1, 3, 6, 1, 4, 1, 42, 2, 11, 2, 1 };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1133
    private static final int[] DOMAIN_COMPONENT_DATA =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        { 0, 9, 2342, 19200300, 100, 1, 25 };
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30649
diff changeset
  1135
    private static final int[] userid_data =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        { 0, 9, 2342, 19200300, 100, 1, 1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    public static final ObjectIdentifier commonName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    public static final ObjectIdentifier countryName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    public static final ObjectIdentifier localityName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    public static final ObjectIdentifier orgName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    public static final ObjectIdentifier orgUnitName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    public static final ObjectIdentifier stateName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    public static final ObjectIdentifier streetAddress_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    public static final ObjectIdentifier title_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    public static final ObjectIdentifier DNQUALIFIER_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    public static final ObjectIdentifier SURNAME_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    public static final ObjectIdentifier GIVENNAME_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    public static final ObjectIdentifier INITIALS_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    public static final ObjectIdentifier GENERATIONQUALIFIER_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    public static final ObjectIdentifier ipAddress_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    public static final ObjectIdentifier DOMAIN_COMPONENT_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    public static final ObjectIdentifier userid_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    public static final ObjectIdentifier SERIALNUMBER_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /** OID for the "CN=" attribute, denoting a person's common name. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        commonName_oid = intern(ObjectIdentifier.newInternal(commonName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    /** OID for the "SERIALNUMBER=" attribute, denoting a serial number for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        a name. Do not confuse with PKCS#9 issuerAndSerialNumber or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        certificate serial number. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        SERIALNUMBER_OID = intern(ObjectIdentifier.newInternal(SERIALNUMBER_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    /** OID for the "C=" attribute, denoting a country. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        countryName_oid = intern(ObjectIdentifier.newInternal(countryName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    /** OID for the "L=" attribute, denoting a locality (such as a city) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        localityName_oid = intern(ObjectIdentifier.newInternal(localityName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    /** OID for the "O=" attribute, denoting an organization name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        orgName_oid = intern(ObjectIdentifier.newInternal(orgName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    /** OID for the "OU=" attribute, denoting an organizational unit name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        orgUnitName_oid = intern(ObjectIdentifier.newInternal(orgUnitName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    /** OID for the "S=" attribute, denoting a state (such as Delaware) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        stateName_oid = intern(ObjectIdentifier.newInternal(stateName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    /** OID for the "STREET=" attribute, denoting a street address. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        streetAddress_oid = intern(ObjectIdentifier.newInternal(streetAddress_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    /** OID for the "T=" attribute, denoting a person's title. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        title_oid = intern(ObjectIdentifier.newInternal(title_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    /** OID for the "DNQUALIFIER=" or "DNQ=" attribute, denoting DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        disambiguating information.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        DNQUALIFIER_OID = intern(ObjectIdentifier.newInternal(DNQUALIFIER_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    /** OID for the "SURNAME=" attribute, denoting a person's surname.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        SURNAME_OID = intern(ObjectIdentifier.newInternal(SURNAME_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    /** OID for the "GIVENNAME=" attribute, denoting a person's given name.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        GIVENNAME_OID = intern(ObjectIdentifier.newInternal(GIVENNAME_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    /** OID for the "INITIALS=" attribute, denoting a person's initials.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        INITIALS_OID = intern(ObjectIdentifier.newInternal(INITIALS_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    /** OID for the "GENERATION=" attribute, denoting Jr., II, etc.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        GENERATIONQUALIFIER_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            intern(ObjectIdentifier.newInternal(GENERATIONQUALIFIER_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * OIDs from other sources which show up in X.500 names we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * expect to deal with often
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /** OID for "IP=" IP address attributes, used with SKIP. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        ipAddress_oid = intern(ObjectIdentifier.newInternal(ipAddress_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    /*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1212
     * Domain component OID from RFC 1274, RFC 2247, RFC 5280
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * OID for "DC=" domain component attributes, used with DNS names in DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        DOMAIN_COMPONENT_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            intern(ObjectIdentifier.newInternal(DOMAIN_COMPONENT_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    /** OID for "UID=" denoting a user id, defined in RFCs 1274 & 2798. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        userid_oid = intern(ObjectIdentifier.newInternal(userid_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * Return constraint type:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     *   <li>NAME_DIFF_TYPE = -1: input name is different type from this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *       (i.e. does not constrain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *   <li>NAME_MATCH = 0: input name matches this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *   <li>NAME_NARROWS = 1: input name narrows this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *   <li>NAME_WIDENS = 2: input name widens this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow this name,
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 30033
diff changeset
  1234
     *       but is same type.
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 30033
diff changeset
  1235
     * </ul>
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 30033
diff changeset
  1236
     * These results are used in checking NameConstraints during
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * certification path verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @param inputName to be checked for being constrained
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 30033
diff changeset
  1240
     * @return constraint type above
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @throws UnsupportedOperationException if name is not exact match, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     *         narrowing and widening are not supported for this name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    public int constrains(GeneralNameInterface inputName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        int constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        if (inputName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        } else if (inputName.getType() != NAME_DIRECTORY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        } else { // type == NAME_DIRECTORY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            X500Name inputX500 = (X500Name)inputName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            if (inputX500.equals(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            } else if (inputX500.names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            } else if (this.names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            } else if (inputX500.isWithinSubtree(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            } else if (isWithinSubtree(inputX500)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        return constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * Compares this name with another and determines if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * it is within the subtree of the other. Useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * checking against the name constraints extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * @return true iff this name is within the subtree of other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    private boolean isWithinSubtree(X500Name other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        if (this == other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        if (other == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        if (other.names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        if (this.names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        if (names.length < other.names.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        for (int i = 0; i < other.names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            if (!names[i].equals(other.names[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * Return subtree depth of this name for purposes of determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * NameConstraints minimum and maximum bounds and for calculating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * path lengths in name subtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 30033
diff changeset
  1306
     * @return distance of name from root
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * @throws UnsupportedOperationException if not supported for this name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    public int subtreeDepth() throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        return names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * Return lowest common ancestor of this name and other name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * @param other another X500Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * @return X500Name of lowest common ancestor; null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    public X500Name commonAncestor(X500Name other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        if (other == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        int otherLen = other.names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        int thisLen = this.names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        if (thisLen == 0 || otherLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        int minLen = (thisLen < otherLen) ? thisLen: otherLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        //Compare names from highest RDN down the naming tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        //Note that these are stored in RDN[0]...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        for (; i < minLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            if (!names[i].equals(other.names[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        //Copy matching RDNs into new RDN array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        RDN[] ancestor = new RDN[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        for (int j=0; j < i; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            ancestor[j] = names[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        X500Name commonAncestor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            commonAncestor = new X500Name(ancestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        return commonAncestor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * Constructor object for use by asX500Principal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1362
    private static final Constructor<X500Principal> principalConstructor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * Field object for use by asX500Name().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    private static final Field principalField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * Retrieve the Constructor and Field we need for reflective access
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * and make them accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        PrivilegedExceptionAction<Object[]> pa =
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 28059
diff changeset
  1375
                new PrivilegedExceptionAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            public Object[] run() throws Exception {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1377
                Class<X500Principal> pClass = X500Principal.class;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1378
                Class<?>[] args = new Class<?>[] { X500Name.class };
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1379
                Constructor<X500Principal> cons = pClass.getDeclaredConstructor(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                cons.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                Field field = pClass.getDeclaredField("thisX500Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                field.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                return new Object[] {cons, field};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            Object[] result = AccessController.doPrivileged(pa);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1388
            @SuppressWarnings("unchecked")
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1389
            Constructor<X500Principal> constr =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1390
                    (Constructor<X500Principal>)result[0];
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1391
            principalConstructor = constr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            principalField = (Field)result[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        } catch (Exception e) {
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 10336
diff changeset
  1394
            throw new InternalError("Could not obtain X500Principal access", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * Get an X500Principal backed by this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * Note that we are using privileged reflection to access the hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * package private constructor in X500Principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    public X500Principal asX500Principal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        if (x500Principal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                Object[] args = new Object[] {this};
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1408
                x500Principal = principalConstructor.newInstance(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                throw new RuntimeException("Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        return x500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * Get the X500Name contained in the given X500Principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * Note that the X500Name is retrieved using reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    public static X500Name asX500Name(X500Principal p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            X500Name name = (X500Name)principalField.get(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            name.x500Principal = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            throw new RuntimeException("Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
}