jdk/src/share/classes/java/security/cert/PKIXBuilderParameters.java
author xuelei
Wed, 20 Jan 2010 21:38:37 +0800
changeset 4807 2521b7dcf505
parent 2 90ce3da70b43
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2003 Sun Microsystems, Inc.  All Rights Reserved.
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.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.InvalidParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Parameters used as input for the PKIX <code>CertPathBuilder</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A PKIX <code>CertPathBuilder</code> uses these parameters to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * CertPathBuilder#build build} a <code>CertPath</code> which has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * validated according to the PKIX certification path validation algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>To instantiate a <code>PKIXBuilderParameters</code> object, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * application must specify one or more <i>most-trusted CAs</i> as defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the PKIX certification path validation algorithm. The most-trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * can be specified using one of two constructors. An application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * can call {@link #PKIXBuilderParameters(Set, CertSelector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * PKIXBuilderParameters(Set, CertSelector)}, specifying a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>Set</code> of <code>TrustAnchor</code> objects, each of which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * identifies a most-trusted CA. Alternatively, an application can call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * {@link #PKIXBuilderParameters(KeyStore, CertSelector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * PKIXBuilderParameters(KeyStore, CertSelector)}, specifying a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>KeyStore</code> instance containing trusted certificate entries, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of which will be considered as a most-trusted CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>In addition, an application must specify constraints on the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * certificate that the <code>CertPathBuilder</code> will attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * to build a path to. The constraints are specified as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>CertSelector</code> object. These constraints should provide the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <code>CertPathBuilder</code> with enough search criteria to find the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * certificate. Minimal criteria for an <code>X509Certificate</code> usually
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * include the subject name and/or one or more subject alternative names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * If enough criteria is not specified, the <code>CertPathBuilder</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * may throw a <code>CertPathBuilderException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Unless otherwise specified, the methods defined in this class are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * thread-safe. Multiple threads that need to access a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * object concurrently should synchronize amongst themselves and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * provide the necessary locking. Multiple threads each manipulating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * separate objects need not synchronize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @see CertPathBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public class PKIXBuilderParameters extends PKIXParameters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private int maxPathLength = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Creates an instance of <code>PKIXBuilderParameters</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * the specified <code>Set</code> of most-trusted CAs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Each element of the set is a {@link TrustAnchor TrustAnchor}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p>Note that the <code>Set</code> is copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param trustAnchors a <code>Set</code> of <code>TrustAnchor</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param targetConstraints a <code>CertSelector</code> specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * constraints on the target certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @throws InvalidAlgorithmParameterException if <code>trustAnchors</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * is empty <code>(trustAnchors.isEmpty() == true)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @throws NullPointerException if <code>trustAnchors</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @throws ClassCastException if any of the elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <code>trustAnchors</code> are not of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <code>java.security.cert.TrustAnchor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public PKIXBuilderParameters(Set<TrustAnchor> trustAnchors, CertSelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        targetConstraints) throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        super(trustAnchors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        setTargetCertConstraints(targetConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Creates an instance of <code>PKIXBuilderParameters</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * populates the set of most-trusted CAs from the trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * certificate entries contained in the specified <code>KeyStore</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Only keystore entries that contain trusted <code>X509Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * are considered; all other certificate types are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param keystore a <code>KeyStore</code> from which the set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * most-trusted CAs will be populated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param targetConstraints a <code>CertSelector</code> specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * constraints on the target certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws KeyStoreException if <code>keystore</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws InvalidAlgorithmParameterException if <code>keystore</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * not contain at least one trusted certificate entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws NullPointerException if <code>keystore</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public PKIXBuilderParameters(KeyStore keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        CertSelector targetConstraints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        throws KeyStoreException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        super(keystore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        setTargetCertConstraints(targetConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Sets the value of the maximum number of non-self-issued intermediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * certificates that may exist in a certification path. A certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * is self-issued if the DNs that appear in the subject and issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * fields are identical and are not empty. Note that the last certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * in a certification path is not an intermediate certificate, and is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * included in this limit. Usually the last certificate is an end entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * certificate, but it can be a CA certificate. A PKIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <code>CertPathBuilder</code> instance must not build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * paths longer than the length specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p> A value of 0 implies that the path can only contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * a single certificate. A value of -1 implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * path length is unconstrained (i.e. there is no maximum).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The default maximum path length, if not specified, is 5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Setting a value less than -1 will cause an exception to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <p> If any of the CA certificates contain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <code>BasicConstraintsExtension</code>, the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <code>pathLenConstraint</code> field of the extension overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * the maximum path length parameter whenever the result is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * certification path of smaller length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param maxPathLength the maximum number of non-self-issued intermediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *  certificates that may exist in a certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws InvalidParameterException if <code>maxPathLength</code> is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *  to a value less than -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see #getMaxPathLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void setMaxPathLength(int maxPathLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (maxPathLength < -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new InvalidParameterException("the maximum path "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                + "length parameter can not be less than -1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        this.maxPathLength = maxPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Returns the value of the maximum number of intermediate non-self-issued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * certificates that may exist in a certification path. See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * the {@link #setMaxPathLength} method for more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return the maximum number of non-self-issued intermediate certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *  that may exist in a certification path, or -1 if there is no limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see #setMaxPathLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public int getMaxPathLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return maxPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns a formatted string describing the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return a formatted string describing the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        sb.append("[\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        sb.append(super.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        sb.append("  Maximum Path Length: " + maxPathLength + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        sb.append("]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}