jdk/src/share/classes/java/security/cert/TrustAnchor.java
author xuelei
Wed, 20 Jan 2010 21:38:37 +0800
changeset 4807 2521b7dcf505
parent 715 f16baef3a20e
child 5506 202f599c92aa
permissions -rw-r--r--
6862064: incorrect implementation of PKIXParameters.clone() Reviewed-by: weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security.cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.x509.NameConstraintsExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A trust anchor or most-trusted Certification Authority (CA).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class represents a "most-trusted CA", which is used as a trust anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * for validating X.509 certification paths. A most-trusted CA includes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * public key of the CA, the CA's name, and any constraints upon the set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * paths which may be validated using this key. These parameters can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * specified in the form of a trusted <code>X509Certificate</code> or as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * individual parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>All <code>TrustAnchor</code> objects must be immutable and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * thread-safe. That is, multiple threads may concurrently invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * methods defined in this class on a single <code>TrustAnchor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * object (or more than one) with no ill effects. Requiring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>TrustAnchor</code> objects to be immutable and thread-safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * allows them to be passed around to various pieces of code without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * worrying about coordinating access. This stipulation applies to all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * public fields and methods of this class and any added or overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see PKIXParameters#PKIXParameters(Set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see PKIXBuilderParameters#PKIXBuilderParameters(Set, CertSelector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class TrustAnchor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final PublicKey pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final String caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final X500Principal caPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final X509Certificate trustedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private byte[] ncBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private NameConstraintsExtension nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Creates an instance of <code>TrustAnchor</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <code>X509Certificate</code> and optional name constraints, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * are intended to be used as additional constraints when validating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * an X.509 certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The name constraints are specified as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * should contain the DER encoded form of the name constraints, as they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * would appear in the NameConstraints structure defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <a href="http://www.ietf.org/rfc/rfc3280">RFC 3280</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * and X.509. The ASN.1 definition of this structure appears below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *  NameConstraints ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *       permittedSubtrees       [0]     GeneralSubtrees OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *       excludedSubtrees        [1]     GeneralSubtrees OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *  GeneralSubtrees ::= SEQUENCE SIZE (1..MAX) OF GeneralSubtree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *  GeneralSubtree ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *       base                    GeneralName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *       minimum         [0]     BaseDistance DEFAULT 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *       maximum         [1]     BaseDistance OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *  BaseDistance ::= INTEGER (0..MAX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *  GeneralName ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *       otherName                       [0]     OtherName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *       rfc822Name                      [1]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *       dNSName                         [2]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *       x400Address                     [3]     ORAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *       directoryName                   [4]     Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *       ediPartyName                    [5]     EDIPartyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *       uniformResourceIdentifier       [6]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *       iPAddress                       [7]     OCTET STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *       registeredID                    [8]     OBJECT IDENTIFIER}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Note that the name constraints byte array supplied is cloned to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param trustedCert a trusted <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param nameConstraints a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * a NameConstraints extension to be used for checking name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Only the value of the extension is included, not the OID or criticality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * flag. Specify <code>null</code> to omit the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws IllegalArgumentException if the name constraints cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws NullPointerException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <code>X509Certificate</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (trustedCert == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new NullPointerException("the trustedCert parameter must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                "be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.trustedCert = trustedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.pubKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.caName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.caPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        setNameConstraints(nameConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Creates an instance of <code>TrustAnchor</code> where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * most-trusted CA is specified as an X500Principal and public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Name constraints are an optional parameter, and are intended to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * as additional constraints when validating an X.509 certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The name constraints are specified as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * contains the DER encoded form of the name constraints, as they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * would appear in the NameConstraints structure defined in RFC 3280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * and X.509. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * {@link #TrustAnchor(X509Certificate, byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints) }.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Note that the name constraints byte array supplied here is cloned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param caPrincipal the name of the most-trusted CA as X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param pubKey the public key of the most-trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param nameConstraints a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * a NameConstraints extension to be used for checking name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Only the value of the extension is included, not the OID or criticality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * flag. Specify <code>null</code> to omit the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws NullPointerException if the specified <code>caPrincipal</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <code>pubKey</code> parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public TrustAnchor(X500Principal caPrincipal, PublicKey pubKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            byte[] nameConstraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if ((caPrincipal == null) || (pubKey == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this.trustedCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        this.caPrincipal = caPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        this.caName = caPrincipal.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this.pubKey = pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        setNameConstraints(nameConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Creates an instance of <code>TrustAnchor</code> where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * most-trusted CA is specified as a distinguished name and public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Name constraints are an optional parameter, and are intended to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * as additional constraints when validating an X.509 certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The name constraints are specified as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * contains the DER encoded form of the name constraints, as they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * would appear in the NameConstraints structure defined in RFC 3280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * and X.509. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * {@link #TrustAnchor(X509Certificate, byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints) }.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Note that the name constraints byte array supplied here is cloned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param caName the X.500 distinguished name of the most-trusted CA in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <code>String</code> format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param pubKey the public key of the most-trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param nameConstraints a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * a NameConstraints extension to be used for checking name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Only the value of the extension is included, not the OID or criticality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * flag. Specify <code>null</code> to omit the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @throws IllegalArgumentException if the specified <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * caName</code> parameter is empty <code>(caName.length() == 0)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * or incorrectly formatted or the name constraints cannot be decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @throws NullPointerException if the specified <code>caName</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <code>pubKey</code> parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public TrustAnchor(String caName, PublicKey pubKey, byte[] nameConstraints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (pubKey == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new NullPointerException("the pubKey parameter must be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                "non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (caName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new NullPointerException("the caName parameter must be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                "non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (caName.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new IllegalArgumentException("the caName " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                "parameter must be a non-empty String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // check if caName is formatted correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        this.caPrincipal = new X500Principal(caName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this.pubKey = pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.caName = caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        this.trustedCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        setNameConstraints(nameConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns the most-trusted CA certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return a trusted <code>X509Certificate</code> or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * if the trust anchor was not specified as a trusted certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public final X509Certificate getTrustedCert() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return this.trustedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Returns the name of the most-trusted CA as an X500Principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return the X.500 distinguished name of the most-trusted CA, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <code>null</code> if the trust anchor was not specified as a trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * public key and name or X500Principal pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public final X500Principal getCA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return this.caPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Returns the name of the most-trusted CA in RFC 2253 <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @return the X.500 distinguished name of the most-trusted CA, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <code>null</code> if the trust anchor was not specified as a trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * public key and name or X500Principal pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public final String getCAName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return this.caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Returns the public key of the most-trusted CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return the public key of the most-trusted CA, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * if the trust anchor was not specified as a trusted public key and name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * or X500Principal pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public final PublicKey getCAPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return this.pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Decode the name constraints and clone them if not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private void setNameConstraints(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            ncBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            nc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   278
            ncBytes = bytes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // validate DER encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                nc = new NameConstraintsExtension(Boolean.FALSE, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                IllegalArgumentException iae =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    new IllegalArgumentException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                iae.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                throw iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Returns the name constraints parameter. The specified name constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * are associated with this trust anchor and are intended to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * as additional constraints when validating an X.509 certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * The name constraints are returned as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * contains the DER encoded form of the name constraints, as they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * would appear in the NameConstraints structure defined in RFC 3280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * and X.509. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * {@link #TrustAnchor(X509Certificate, byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints) }.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Note that the byte array returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *         a NameConstraints extension used for checking name constraints,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *         or <code>null</code> if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public final byte [] getNameConstraints() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   312
        return ncBytes == null ? null : ncBytes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns a formatted string describing the <code>TrustAnchor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return a formatted string describing the <code>TrustAnchor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        sb.append("[\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (pubKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            sb.append("  Trusted CA Public Key: " + pubKey.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            sb.append("  Trusted CA Issuer Name: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                + String.valueOf(caName) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            sb.append("  Trusted CA cert: " + trustedCert.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (nc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            sb.append("  Name Constraints: " + nc.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}