src/java.base/share/classes/java/security/cert/TrustAnchor.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
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) 2001, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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 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
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
    43
 * specified in the form of a trusted {@code X509Certificate} or as
2
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>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
    47
 * <p>All {@code TrustAnchor} objects must be immutable and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * thread-safe. That is, multiple threads may concurrently invoke the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
    49
 * methods defined in this class on a single {@code TrustAnchor}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * object (or more than one) with no ill effects. Requiring
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
    51
 * {@code TrustAnchor} objects to be immutable and thread-safe
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * allows them to be passed around to various pieces of code without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * worrying about coordinating access. This stipulation applies to all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * public fields and methods of this class and any added or overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see PKIXParameters#PKIXParameters(Set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see PKIXBuilderParameters#PKIXBuilderParameters(Set, CertSelector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class TrustAnchor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final PublicKey pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final String caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final X500Principal caPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final X509Certificate trustedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private byte[] ncBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private NameConstraintsExtension nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
    73
     * Creates an instance of {@code TrustAnchor} with the specified
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
    74
     * {@code X509Certificate} and optional name constraints, which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * are intended to be used as additional constraints when validating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * an X.509 certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The name constraints are specified as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * should contain the DER encoded form of the name constraints, as they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * would appear in the NameConstraints structure defined in
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    81
     * <a href="http://tools.ietf.org/html/rfc5280">RFC 5280</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * and X.509. The ASN.1 definition of this structure appears below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
    84
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *  NameConstraints ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *       permittedSubtrees       [0]     GeneralSubtrees OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *       excludedSubtrees        [1]     GeneralSubtrees OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *  GeneralSubtrees ::= SEQUENCE SIZE (1..MAX) OF GeneralSubtree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *  GeneralSubtree ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *       base                    GeneralName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *       minimum         [0]     BaseDistance DEFAULT 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *       maximum         [1]     BaseDistance OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *  BaseDistance ::= INTEGER (0..MAX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *  GeneralName ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *       otherName                       [0]     OtherName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *       rfc822Name                      [1]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *       dNSName                         [2]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *       x400Address                     [3]     ORAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *       directoryName                   [4]     Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *       ediPartyName                    [5]     EDIPartyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *       uniformResourceIdentifier       [6]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *       iPAddress                       [7]     OCTET STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *       registeredID                    [8]     OBJECT IDENTIFIER}
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   108
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Note that the name constraints byte array supplied is cloned to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   113
     * @param trustedCert a trusted {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param nameConstraints a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * a NameConstraints extension to be used for checking name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Only the value of the extension is included, not the OID or criticality
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   117
     * flag. Specify {@code null} to omit the parameter.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws IllegalArgumentException if the name constraints cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @throws NullPointerException if the specified
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   121
     * {@code X509Certificate} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (trustedCert == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new NullPointerException("the trustedCert parameter must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                "be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.trustedCert = trustedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.pubKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.caName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.caPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        setNameConstraints(nameConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   136
     * Creates an instance of {@code TrustAnchor} where the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * most-trusted CA is specified as an X500Principal and public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Name constraints are an optional parameter, and are intended to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * as additional constraints when validating an X.509 certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * The name constraints are specified as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * contains the DER encoded form of the name constraints, as they
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   143
     * would appear in the NameConstraints structure defined in RFC 5280
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * and X.509. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * {@link #TrustAnchor(X509Certificate, byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints) }.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Note that the name constraints byte array supplied here is cloned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param caPrincipal the name of the most-trusted CA as X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param pubKey the public key of the most-trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param nameConstraints a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * a NameConstraints extension to be used for checking name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Only the value of the extension is included, not the OID or criticality
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   157
     * flag. Specify {@code null} to omit the parameter.
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   158
     * @throws NullPointerException if the specified {@code caPrincipal} or
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   159
     * {@code pubKey} parameter is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public TrustAnchor(X500Principal caPrincipal, PublicKey pubKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            byte[] nameConstraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if ((caPrincipal == null) || (pubKey == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        this.trustedCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this.caPrincipal = caPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        this.caName = caPrincipal.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        this.pubKey = pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        setNameConstraints(nameConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   175
     * Creates an instance of {@code TrustAnchor} where the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * most-trusted CA is specified as a distinguished name and public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Name constraints are an optional parameter, and are intended to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * as additional constraints when validating an X.509 certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * The name constraints are specified as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * contains the DER encoded form of the name constraints, as they
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   182
     * would appear in the NameConstraints structure defined in RFC 5280
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * and X.509. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * {@link #TrustAnchor(X509Certificate, byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints) }.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Note that the name constraints byte array supplied here is cloned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param caName the X.500 distinguished name of the most-trusted CA in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   193
     * {@code String} format
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param pubKey the public key of the most-trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param nameConstraints a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * a NameConstraints extension to be used for checking name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Only the value of the extension is included, not the OID or criticality
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   198
     * flag. Specify {@code null} to omit the parameter.
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   199
     * @throws IllegalArgumentException if the specified
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   200
     * {@code caName} parameter is empty {@code (caName.length() == 0)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * or incorrectly formatted or the name constraints cannot be decoded
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   202
     * @throws NullPointerException if the specified {@code caName} or
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   203
     * {@code pubKey} parameter is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public TrustAnchor(String caName, PublicKey pubKey, byte[] nameConstraints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (pubKey == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throw new NullPointerException("the pubKey parameter must be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                "non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (caName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throw new NullPointerException("the caName parameter must be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                "non-null");
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   213
        if (caName.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new IllegalArgumentException("the caName " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                "parameter must be a non-empty String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // check if caName is formatted correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this.caPrincipal = new X500Principal(caName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        this.pubKey = pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this.caName = caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.trustedCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        setNameConstraints(nameConstraints);
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
     * Returns the most-trusted CA certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   227
     * @return a trusted {@code X509Certificate} or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * if the trust anchor was not specified as a trusted certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public final X509Certificate getTrustedCert() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return this.trustedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Returns the name of the most-trusted CA as an X500Principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return the X.500 distinguished name of the most-trusted CA, or
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   238
     * {@code null} if the trust anchor was not specified as a trusted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * public key and name or X500Principal pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public final X500Principal getCA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return this.caPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   247
     * Returns the name of the most-trusted CA in RFC 2253 {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return the X.500 distinguished name of the most-trusted CA, or
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   251
     * {@code null} if the trust anchor was not specified as a trusted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * public key and name or X500Principal pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public final String getCAName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return this.caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Returns the public key of the most-trusted CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   261
     * @return the public key of the most-trusted CA, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * if the trust anchor was not specified as a trusted public key and name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * or X500Principal pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public final PublicKey getCAPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return this.pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Decode the name constraints and clone them if not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private void setNameConstraints(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            ncBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            nc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   277
            ncBytes = bytes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // validate DER encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                nc = new NameConstraintsExtension(Boolean.FALSE, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                IllegalArgumentException iae =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    new IllegalArgumentException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                iae.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                throw iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns the name constraints parameter. The specified name constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * are associated with this trust anchor and are intended to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * as additional constraints when validating an X.509 certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * The name constraints are returned as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * contains the DER encoded form of the name constraints, as they
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   297
     * would appear in the NameConstraints structure defined in RFC 5280
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * and X.509. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * {@link #TrustAnchor(X509Certificate, byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints) }.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Note that the byte array returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *         a NameConstraints extension used for checking name constraints,
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   308
     *         or {@code null} if not set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public final byte [] getNameConstraints() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   311
        return ncBytes == null ? null : ncBytes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   315
     * Returns a formatted string describing the {@code TrustAnchor}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 5506
diff changeset
   317
     * @return a formatted string describing the {@code TrustAnchor}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 21334
diff changeset
   320
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        sb.append("[\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (pubKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            sb.append("  Trusted CA Public Key: " + pubKey.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            sb.append("  Trusted CA Issuer Name: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                + String.valueOf(caName) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            sb.append("  Trusted CA cert: " + trustedCert.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (nc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            sb.append("  Name Constraints: " + nc.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
}