jdk/src/java.base/share/classes/sun/security/x509/X500Name.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
child 28059 e576535359cc
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Note:  As of 1.4, the public class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * javax.security.auth.x500.X500Principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * should be used when parsing, generating, and comparing X.500 DNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class contains other useful methods for checking name constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * and retrieving DNs by keyword.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> X.500 names are used to identify entities, such as those which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * identified by X.509 certificates.  They are world-wide, hierarchical,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * and descriptive.  Entities can be identified by attributes, and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * some systems can be searched for according to those attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The ASN.1 for this is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * GeneralName ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     directoryName                   [4]     Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Name ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   RDNSequence }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * RelativeDistinguishedName ::=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   SET OF AttributeTypeAndValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * AttributeTypeAndValue ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   type     AttributeType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   value    AttributeValue }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * AttributeType ::= OBJECT IDENTIFIER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * AttributeValue ::= ANY DEFINED BY AttributeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * DirectoryString ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *       teletexString           TeletexString (SIZE (1..MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *       printableString         PrintableString (SIZE (1..MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *       universalString         UniversalString (SIZE (1..MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *       utf8String              UTF8String (SIZE (1.. MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *       bmpString               BMPString (SIZE (1..MAX)) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * This specification requires only a subset of the name comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * functionality specified in the X.500 series of specifications.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * requirements for conforming implementations are as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <ol TYPE=a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <li>attribute values encoded in different types (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *    PrintableString and BMPString) may be assumed to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *    different strings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <li>attribute values in types other than PrintableString are case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *    sensitive (this permits matching of attribute values as binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *    objects);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <li>attribute values in PrintableString are not case sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *    (e.g., "Marianne Swanson" is the same as "MARIANNE SWANSON"); and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <li>attribute values in PrintableString are compared after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *    removing leading and trailing white space and converting internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *    substrings of one or more consecutive white space characters to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *    single space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * These name comparison rules permit a certificate user to validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * certificates issued using languages or encodings unfamiliar to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * certificate user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * In addition, implementations of this specification MAY use these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * comparison rules to process unfamiliar attribute types for name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * chaining. This allows implementations to process certificates with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * unfamiliar attributes in the issuer name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Note that the comparison rules defined in the X.500 series of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * specifications indicate that the character sets used to encode data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * in distinguished names are irrelevant.  The characters themselves are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * compared without regard to encoding. Implementations of the profile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * are permitted to use the comparison algorithm defined in the X.500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * series.  Such an implementation will recognize a superset of name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * matches recognized by the algorithm specified above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * Note that instances of this class are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @see GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @see GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @see GeneralNameInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
public class X500Name implements GeneralNameInterface, Principal {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private String dn; // roughly RFC 1779 DN, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private String rfc1779Dn; // RFC 1779 compliant DN, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private String rfc2253Dn; // RFC 2253 DN, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private String canonicalDn; // canonical RFC 2253 DN or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private RDN[] names;        // RDNs (never null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private X500Principal x500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // cached immutable list of the RDNs and all the AVAs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private volatile List<RDN> rdnList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private volatile List<AVA> allAvaList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Constructs a name from a conventionally formatted string, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * 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
   145
     * (RFC 1779, 2253, or 4514 style).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   147
     * @param dname the X.500 Distinguished Name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public X500Name(String dname) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this(dname, Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Constructs a name from a conventionally formatted string, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * 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
   156
     * (RFC 1779, 2253, or 4514 style).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   158
     * @param dname the X.500 Distinguished Name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param keywordMap an additional keyword/OID map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public X500Name(String dname, Map<String, String> keywordMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        parseDN(dname, keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Constructs a name from a string formatted according to format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Currently, the formats DEFAULT and RFC2253 are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * 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
   170
     * constructor. RFC2253 is the format strictly according to RFC2253
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * without extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   173
     * @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
   174
     * @param format the specified format of the String DN
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public X500Name(String dname, String format) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (dname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new NullPointerException("Name must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (format.equalsIgnoreCase("RFC2253")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            parseRFC2253DN(dname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else if (format.equalsIgnoreCase("DEFAULT")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            parseDN(dname, Collections.<String, String>emptyMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new IOException("Unsupported format " + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
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
     * Constructs a name from fields common in enterprise application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * environments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <P><EM><STRONG>NOTE:</STRONG>  The behaviour when any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * these strings contain characters outside the ASCII range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * is unspecified in currently relevant standards.</EM>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param commonName common name of a person, e.g. "Vivette Davis"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param organizationUnit small organization name, e.g. "Purchasing"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param organizationName large organization name, e.g. "Onizuka, Inc."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param country two letter country code, e.g. "CH"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public X500Name(String commonName, String organizationUnit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                     String organizationName, String country)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        names = new RDN[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * NOTE:  it's only on output that little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * ordering is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        names[3] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        names[3].assertion[0] = new AVA(commonName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                new DerValue(commonName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        names[2] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        names[2].assertion[0] = new AVA(orgUnitName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                new DerValue(organizationUnit));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        names[1] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        names[1].assertion[0] = new AVA(orgName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                new DerValue(organizationName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        names[0] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        names[0].assertion[0] = new AVA(countryName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                new DerValue(country));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Constructs a name from fields common in Internet application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * environments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <P><EM><STRONG>NOTE:</STRONG>  The behaviour when any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * these strings contain characters outside the ASCII range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * is unspecified in currently relevant standards.</EM>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param commonName common name of a person, e.g. "Vivette Davis"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param organizationUnit small organization name, e.g. "Purchasing"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param organizationName large organization name, e.g. "Onizuka, Inc."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param localityName locality (city) name, e.g. "Palo Alto"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param stateName state name, e.g. "California"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param country two letter country code, e.g. "CH"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public X500Name(String commonName, String organizationUnit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    String organizationName, String localityName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    String stateName, String country)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        names = new RDN[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * NOTE:  it's only on output that little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * ordering is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        names[5] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        names[5].assertion[0] = new AVA(commonName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                new DerValue(commonName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        names[4] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        names[4].assertion[0] = new AVA(orgUnitName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                new DerValue(organizationUnit));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        names[3] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        names[3].assertion[0] = new AVA(orgName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                new DerValue(organizationName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        names[2] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        names[2].assertion[0] = new AVA(localityName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                new DerValue(localityName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        names[1] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        names[1].assertion[0] = new AVA(stateName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                new DerValue(stateName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        names[0] = new RDN(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        names[0].assertion[0] = new AVA(countryName_oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                new DerValue(country));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Constructs a name from an array of relative distinguished names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param rdnArray array of relative distinguished names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public X500Name(RDN[] rdnArray) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (rdnArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            names = new RDN[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            names = rdnArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                if (names[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    throw new IOException("Cannot create an X500Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
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
     * Constructs a name from an ASN.1 encoded value.  The encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * of the name in the stream uses DER (a BER/1 subset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param value a DER-encoded value holding an X.500 name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public X500Name(DerValue value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        //Note that toDerInputStream uses only the buffer (data) and not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        //the tag, so an empty SEQUENCE (OF) will yield an empty DerInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        this(value.toDerInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Constructs a name from an ASN.1 encoded input stream.  The encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * of the name in the stream uses DER (a BER/1 subset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param in DER-encoded data holding an X.500 name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public X500Name(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        parseDER(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *  Constructs a name from an ASN.1 encoded byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param name DER-encoded byte array holding an X.500 name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public X500Name(byte[] name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        DerInputStream in = new DerInputStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        parseDER(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Return an immutable List of all RDNs in this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public List<RDN> rdns() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        List<RDN> list = rdnList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            list = Collections.unmodifiableList(Arrays.asList(names));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            rdnList = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Return the number of RDNs in this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Return an immutable List of the the AVAs contained in all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * RDNs of this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public List<AVA> allAvas() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        List<AVA> list = allAvaList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            list = new ArrayList<AVA>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                list.addAll(names[i].avas());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Return the total number of AVAs contained in all the RDNs of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public int avaSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return allAvas().size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Return whether this X500Name is empty. An X500Name is not empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * if it has at least one RDN containing at least one AVA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int n = names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
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
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        StringBuilder fullname = new StringBuilder(48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        for (int i = names.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            if (i < names.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                fullname.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            fullname.append(names[i].toRFC2253String(oidMap));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        return fullname.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public String getRFC2253CanonicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        /* check for and return cached name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if (canonicalDn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            return canonicalDn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         * Section 2.1 : if the RDNSequence is an empty sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         * the result is the empty or zero length string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            canonicalDn = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            return canonicalDn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         * 2.1 (continued) : Otherwise, the output consists of the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
         * encodings of each RelativeDistinguishedName in the RDNSequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
         * (according to 2.2), starting with the last element of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
         * and moving backwards toward the first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         * The encodings of adjoining RelativeDistinguishedNames are separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         * by a comma character (',' ASCII 44).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        StringBuilder fullname = new StringBuilder(48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        for (int i = names.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (i < names.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                fullname.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            fullname.append(names[i].toRFC2253String(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        canonicalDn = fullname.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        return canonicalDn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Returns the value of toString().  This call is needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * implement the java.security.Principal interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    public String getName() { return toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Find the first instance of this attribute in a "top down"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * search of all the attributes in the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    private DerValue findAttribute(ObjectIdentifier attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                DerValue value = names[i].findAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * Find the most specific ("last") attribute of the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    public DerValue findMostSpecificAttribute(ObjectIdentifier attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            for (int i = names.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                DerValue value = names[i].findAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /****************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    private void parseDER(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        // X.500 names are a "SEQUENCE OF" RDNs, which means zero or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        // more and order matters.  We scan them in order, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // conventionally is big-endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        DerValue[] nameseq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        byte[] derBytes = in.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            nameseq = in.getSequence(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            if (derBytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                nameseq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                DerValue derVal = new DerValue(DerValue.tag_Sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                                           derBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                derBytes = derVal.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                nameseq = new DerInputStream(derBytes).getSequence(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        if (nameseq == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            names = new RDN[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            names = new RDN[nameseq.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            for (int i = 0; i < nameseq.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                names[i] = new RDN(nameseq[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * Encodes the name in DER-encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @deprecated Use encode() instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @param out where to put the DER-encoded X.500 name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public void emit(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * Encodes the name in DER-encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @param out where to put the DER-encoded X.500 name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            names[i].encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * Returned the encoding as an uncloned byte array. Callers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * guarantee that they neither modify it not expose it to untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    public byte[] getEncodedInternal() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            DerOutputStream     out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            DerOutputStream     tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                names[i].encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            encoded = out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Gets the name in DER-encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @return the DER encoded byte array of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    public byte[] getEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        return getEncodedInternal().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * Parses a Distinguished Name (DN) in printable representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * According to RFC 1779, RDNs in a DN are separated by comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * The following examples show both methods of quoting a comma, so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * is not considered a separator:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *     O="Sue, Grabbit and Runn" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *     O=Sue\, Grabbit and Runn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   869
     * 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
   870
     * keywords. Additional keywords can be specified in the keyword/OID map.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    private void parseDN(String input, Map<String, String> keywordMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if (input == null || input.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            names = new RDN[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   879
        List<RDN> dnVector = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        int dnOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        int rdnEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        String rdnString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        int quoteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        String dnString = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        int searchOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        int nextComma = dnString.indexOf(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        int nextSemiColon = dnString.indexOf(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        while (nextComma >=0 || nextSemiColon >=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            if (nextSemiColon < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                rdnEnd = nextComma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            } else if (nextComma < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                rdnEnd = nextSemiColon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                rdnEnd = Math.min(nextComma, nextSemiColon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            quoteCount += countQuotes(dnString, searchOffset, rdnEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
             * We have encountered an RDN delimiter (comma or a semicolon).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
             * If the comma or semicolon in the RDN under consideration is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
             * preceded by a backslash (escape), or by a double quote, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
             * is part of the RDN. Otherwise, it is used as a separator, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
             * delimit the RDN under consideration from any subsequent RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            if (rdnEnd >= 0 && quoteCount != 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                !escaped(rdnEnd, searchOffset, dnString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                 * Comma/semicolon is a separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                rdnString = dnString.substring(dnOffset, rdnEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                // Parse RDN, and store it in vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                RDN rdn = new RDN(rdnString, keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                dnVector.add(rdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                // Increase the offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                dnOffset = rdnEnd + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                // Set quote counter back to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                quoteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            searchOffset = rdnEnd + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            nextComma = dnString.indexOf(',', searchOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            nextSemiColon = dnString.indexOf(';', searchOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        // Parse last or only RDN, and store it in vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        rdnString = dnString.substring(dnOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        RDN rdn = new RDN(rdnString, keywordMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        dnVector.add(rdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
         * Store the vector elements as an array of RDNs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
         * NOTE: It's only on output that little-endian ordering is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        Collections.reverse(dnVector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        names = dnVector.toArray(new RDN[dnVector.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    private void parseRFC2253DN(String dnString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        if (dnString.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            names = new RDN[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            return;
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   949
         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   951
         List<RDN> dnVector = new ArrayList<>();
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   952
         int dnOffset = 0;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   953
         String rdnString;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   954
         int searchOffset = 0;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   955
         int rdnEnd = dnString.indexOf(',');
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   956
         while (rdnEnd >=0) {
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
              * 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
   959
              * 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
   960
              * preceded by a backslash (escape), it
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   961
              * 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
   962
              * 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
   963
              */
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   964
             if (rdnEnd > 0 && !escaped(rdnEnd, searchOffset, dnString)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   966
                 /*
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   967
                  * Comma is a separator
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   968
                  */
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   969
                 rdnString = dnString.substring(dnOffset, rdnEnd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   971
                 // 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
   972
                 RDN rdn = new RDN(rdnString, "RFC2253");
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   973
                 dnVector.add(rdn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   975
                 // Increase the offset
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   976
                 dnOffset = rdnEnd + 1;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   977
             }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   979
             searchOffset = rdnEnd + 1;
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   980
             rdnEnd = dnString.indexOf(',', searchOffset);
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   981
         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   983
         // 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
   984
         rdnString = dnString.substring(dnOffset);
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   985
         RDN rdn = new RDN(rdnString, "RFC2253");
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   986
         dnVector.add(rdn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
10370
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   988
         /*
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   989
          * 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
   990
          * 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
   991
          */
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   992
         Collections.reverse(dnVector);
5db0cf452a50 7024771: "\\<>" in attribute value part of X500Principal constructor parameter makes strange effect
mullan
parents: 10336
diff changeset
   993
         names = dnVector.toArray(new RDN[dnVector.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * Counts double quotes in string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * Escaped quotes are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    static int countQuotes(String string, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        for (int i = from; i < to; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            if ((string.charAt(i) == '"' && i == from) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                (string.charAt(i) == '"' && string.charAt(i-1) != '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    private static boolean escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                (int rdnEnd, int searchOffset, String dnString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        if (rdnEnd == 1 && dnString.charAt(rdnEnd - 1) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            //  case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            //  \,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        } else if (rdnEnd > 1 && dnString.charAt(rdnEnd - 1) == '\\' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                dnString.charAt(rdnEnd - 2) != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            //  case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            //  foo\,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        } else if (rdnEnd > 1 && dnString.charAt(rdnEnd - 1) == '\\' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                dnString.charAt(rdnEnd - 2) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            //  case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            //  foo\\\\\,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            rdnEnd--;   // back up to last backSlash
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            while (rdnEnd >= searchOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                if (dnString.charAt(rdnEnd) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    count++;    // count consecutive backslashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                rdnEnd--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            // if count is odd, then rdnEnd is escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            return (count % 2) != 0 ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * Dump the printable form of a distinguished name.  Each relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * name is separated from the next by a ",", and assertions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * relative names have "label=value" syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * Uses RFC 1779 syntax (i.e. little-endian, comma separators)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    private void generateDN() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        if (names.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            dn = names[0].toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        StringBuilder sb = new StringBuilder(48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            for (int i = names.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                if (i != names.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                    sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                sb.append(names[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        dn = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * Dump the printable form of a distinguished name.  Each relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * name is separated from the next by a ",", and assertions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * relative names have "label=value" syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * Uses RFC 1779 syntax (i.e. little-endian, comma separators)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * Valid keywords from RFC 1779 are used. Additional keywords can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * specified in the OID/keyword map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    private String generateRFC1779DN(Map<String, String> oidMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if (names.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            return names[0].toRFC1779String(oidMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        StringBuilder sb = new StringBuilder(48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            for (int i = names.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                if (i != names.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                    sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                sb.append(names[i].toRFC1779String(oidMap));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    /****************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * Maybe return a preallocated OID, to reduce storage costs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * and speed recognition of common X.500 attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    static ObjectIdentifier intern(ObjectIdentifier oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        ObjectIdentifier interned = internedOIDs.get(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        if (interned != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            return interned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        internedOIDs.put(oid, oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        return oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    private static final Map<ObjectIdentifier,ObjectIdentifier> internedOIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                        = new HashMap<ObjectIdentifier,ObjectIdentifier>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * Selected OIDs from X.520
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1125
     * Includes all those specified in RFC 5280 as MUST or SHOULD
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * be recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    private static final int commonName_data[] = { 2, 5, 4, 3 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    private static final int SURNAME_DATA[] = { 2, 5, 4, 4 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    private static final int SERIALNUMBER_DATA[] = { 2, 5, 4, 5 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    private static final int countryName_data[] = { 2, 5, 4, 6 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    private static final int localityName_data[] = { 2, 5, 4, 7 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    private static final int stateName_data[] = { 2, 5, 4, 8 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    private static final int streetAddress_data[] = { 2, 5, 4, 9 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    private static final int orgName_data[] = { 2, 5, 4, 10 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    private static final int orgUnitName_data[] = { 2, 5, 4, 11 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    private static final int title_data[] = { 2, 5, 4, 12 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    private static final int GIVENNAME_DATA[] = { 2, 5, 4, 42 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    private static final int INITIALS_DATA[] = { 2, 5, 4, 43 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    private static final int GENERATIONQUALIFIER_DATA[] = { 2, 5, 4, 44 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    private static final int DNQUALIFIER_DATA[] = { 2, 5, 4, 46 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    private static final int ipAddress_data[] = { 1, 3, 6, 1, 4, 1, 42, 2, 11, 2, 1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    private static final int DOMAIN_COMPONENT_DATA[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        { 0, 9, 2342, 19200300, 100, 1, 25 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    private static final int userid_data[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        { 0, 9, 2342, 19200300, 100, 1, 1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    public static final ObjectIdentifier commonName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    public static final ObjectIdentifier countryName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    public static final ObjectIdentifier localityName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    public static final ObjectIdentifier orgName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    public static final ObjectIdentifier orgUnitName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    public static final ObjectIdentifier stateName_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    public static final ObjectIdentifier streetAddress_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    public static final ObjectIdentifier title_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    public static final ObjectIdentifier DNQUALIFIER_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    public static final ObjectIdentifier SURNAME_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    public static final ObjectIdentifier GIVENNAME_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    public static final ObjectIdentifier INITIALS_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    public static final ObjectIdentifier GENERATIONQUALIFIER_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    public static final ObjectIdentifier ipAddress_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    public static final ObjectIdentifier DOMAIN_COMPONENT_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    public static final ObjectIdentifier userid_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    public static final ObjectIdentifier SERIALNUMBER_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    /** OID for the "CN=" attribute, denoting a person's common name. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        commonName_oid = intern(ObjectIdentifier.newInternal(commonName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    /** OID for the "SERIALNUMBER=" attribute, denoting a serial number for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        a name. Do not confuse with PKCS#9 issuerAndSerialNumber or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        certificate serial number. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        SERIALNUMBER_OID = intern(ObjectIdentifier.newInternal(SERIALNUMBER_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    /** OID for the "C=" attribute, denoting a country. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        countryName_oid = intern(ObjectIdentifier.newInternal(countryName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    /** OID for the "L=" attribute, denoting a locality (such as a city) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        localityName_oid = intern(ObjectIdentifier.newInternal(localityName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /** OID for the "O=" attribute, denoting an organization name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        orgName_oid = intern(ObjectIdentifier.newInternal(orgName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    /** OID for the "OU=" attribute, denoting an organizational unit name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        orgUnitName_oid = intern(ObjectIdentifier.newInternal(orgUnitName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    /** OID for the "S=" attribute, denoting a state (such as Delaware) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        stateName_oid = intern(ObjectIdentifier.newInternal(stateName_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    /** OID for the "STREET=" attribute, denoting a street address. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        streetAddress_oid = intern(ObjectIdentifier.newInternal(streetAddress_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    /** OID for the "T=" attribute, denoting a person's title. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        title_oid = intern(ObjectIdentifier.newInternal(title_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    /** OID for the "DNQUALIFIER=" or "DNQ=" attribute, denoting DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        disambiguating information.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        DNQUALIFIER_OID = intern(ObjectIdentifier.newInternal(DNQUALIFIER_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    /** OID for the "SURNAME=" attribute, denoting a person's surname.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        SURNAME_OID = intern(ObjectIdentifier.newInternal(SURNAME_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    /** OID for the "GIVENNAME=" attribute, denoting a person's given name.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        GIVENNAME_OID = intern(ObjectIdentifier.newInternal(GIVENNAME_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /** OID for the "INITIALS=" attribute, denoting a person's initials.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        INITIALS_OID = intern(ObjectIdentifier.newInternal(INITIALS_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    /** OID for the "GENERATION=" attribute, denoting Jr., II, etc.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        GENERATIONQUALIFIER_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            intern(ObjectIdentifier.newInternal(GENERATIONQUALIFIER_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * OIDs from other sources which show up in X.500 names we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * expect to deal with often
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    /** OID for "IP=" IP address attributes, used with SKIP. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        ipAddress_oid = intern(ObjectIdentifier.newInternal(ipAddress_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    /*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1223
     * Domain component OID from RFC 1274, RFC 2247, RFC 5280
2
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
     * OID for "DC=" domain component attributes, used with DNS names in DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        DOMAIN_COMPONENT_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            intern(ObjectIdentifier.newInternal(DOMAIN_COMPONENT_DATA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    /** OID for "UID=" denoting a user id, defined in RFCs 1274 & 2798. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        userid_oid = intern(ObjectIdentifier.newInternal(userid_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * Return constraint type:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     *   <li>NAME_DIFF_TYPE = -1: input name is different type from this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *       (i.e. does not constrain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *   <li>NAME_MATCH = 0: input name matches this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     *   <li>NAME_NARROWS = 1: input name narrows this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     *   <li>NAME_WIDENS = 2: input name widens this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow this name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     &       but is same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * </ul>.  These results are used in checking NameConstraints during
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * certification path verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * @param inputName to be checked for being constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * @returns constraint type above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * @throws UnsupportedOperationException if name is not exact match, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     *         narrowing and widening are not supported for this name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    public int constrains(GeneralNameInterface inputName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        int constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        if (inputName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        } else if (inputName.getType() != NAME_DIRECTORY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        } else { // type == NAME_DIRECTORY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            X500Name inputX500 = (X500Name)inputName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            if (inputX500.equals(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            } else if (inputX500.names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            } else if (this.names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            } else if (inputX500.isWithinSubtree(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            } else if (isWithinSubtree(inputX500)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        return constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * Compares this name with another and determines if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * it is within the subtree of the other. Useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * checking against the name constraints extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * @return true iff this name is within the subtree of other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    private boolean isWithinSubtree(X500Name other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        if (this == other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        if (other == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        if (other.names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        if (this.names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        if (names.length < other.names.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        for (int i = 0; i < other.names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            if (!names[i].equals(other.names[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * Return subtree depth of this name for purposes of determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * NameConstraints minimum and maximum bounds and for calculating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * path lengths in name subtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * @returns distance of name from root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * @throws UnsupportedOperationException if not supported for this name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    public int subtreeDepth() throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        return names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * Return lowest common ancestor of this name and other name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * @param other another X500Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * @return X500Name of lowest common ancestor; null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    public X500Name commonAncestor(X500Name other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        if (other == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        int otherLen = other.names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        int thisLen = this.names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        if (thisLen == 0 || otherLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        int minLen = (thisLen < otherLen) ? thisLen: otherLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        //Compare names from highest RDN down the naming tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        //Note that these are stored in RDN[0]...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        for (; i < minLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            if (!names[i].equals(other.names[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        //Copy matching RDNs into new RDN array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        RDN[] ancestor = new RDN[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        for (int j=0; j < i; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            ancestor[j] = names[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        X500Name commonAncestor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            commonAncestor = new X500Name(ancestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        return commonAncestor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * Constructor object for use by asX500Principal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1372
    private static final Constructor<X500Principal> principalConstructor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * Field object for use by asX500Name().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    private static final Field principalField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * Retrieve the Constructor and Field we need for reflective access
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * and make them accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        PrivilegedExceptionAction<Object[]> pa =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                new PrivilegedExceptionAction<Object[]>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            public Object[] run() throws Exception {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1387
                Class<X500Principal> pClass = X500Principal.class;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1388
                Class<?>[] args = new Class<?>[] { X500Name.class };
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1389
                Constructor<X500Principal> cons = pClass.getDeclaredConstructor(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                cons.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                Field field = pClass.getDeclaredField("thisX500Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                field.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                return new Object[] {cons, field};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            Object[] result = AccessController.doPrivileged(pa);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1398
            @SuppressWarnings("unchecked")
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1399
            Constructor<X500Principal> constr =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1400
                    (Constructor<X500Principal>)result[0];
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1401
            principalConstructor = constr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            principalField = (Field)result[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        } catch (Exception e) {
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 10336
diff changeset
  1404
            throw new InternalError("Could not obtain X500Principal access", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * Get an X500Principal backed by this X500Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * Note that we are using privileged reflection to access the hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * package private constructor in X500Principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    public X500Principal asX500Principal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        if (x500Principal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                Object[] args = new Object[] {this};
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1418
                x500Principal = principalConstructor.newInstance(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                throw new RuntimeException("Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        return x500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * Get the X500Name contained in the given X500Principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * Note that the X500Name is retrieved using reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    public static X500Name asX500Name(X500Principal p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            X500Name name = (X500Name)principalField.get(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            name.x500Principal = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            throw new RuntimeException("Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
}