001 /*
002 * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.security.cert;
027
028 import java.security.KeyStore;
029 import java.security.KeyStoreException;
030 import java.security.InvalidAlgorithmParameterException;
031 import java.security.InvalidParameterException;
032 import java.util.Set;
033
034 /**
035 * Parameters used as input for the PKIX <code>CertPathBuilder</code>
036 * algorithm.
037 * <p>
038 * A PKIX <code>CertPathBuilder</code> uses these parameters to {@link
039 * CertPathBuilder#build build} a <code>CertPath</code> which has been
040 * validated according to the PKIX certification path validation algorithm.
041 *
042 * <p>To instantiate a <code>PKIXBuilderParameters</code> object, an
043 * application must specify one or more <i>most-trusted CAs</i> as defined by
044 * the PKIX certification path validation algorithm. The most-trusted CA
045 * can be specified using one of two constructors. An application
046 * can call {@link #PKIXBuilderParameters(Set, CertSelector)
047 * PKIXBuilderParameters(Set, CertSelector)}, specifying a
048 * <code>Set</code> of <code>TrustAnchor</code> objects, each of which
049 * identifies a most-trusted CA. Alternatively, an application can call
050 * {@link #PKIXBuilderParameters(KeyStore, CertSelector)
051 * PKIXBuilderParameters(KeyStore, CertSelector)}, specifying a
052 * <code>KeyStore</code> instance containing trusted certificate entries, each
053 * of which will be considered as a most-trusted CA.
054 *
055 * <p>In addition, an application must specify constraints on the target
056 * certificate that the <code>CertPathBuilder</code> will attempt
057 * to build a path to. The constraints are specified as a
058 * <code>CertSelector</code> object. These constraints should provide the
059 * <code>CertPathBuilder</code> with enough search criteria to find the target
060 * certificate. Minimal criteria for an <code>X509Certificate</code> usually
061 * include the subject name and/or one or more subject alternative names.
062 * If enough criteria is not specified, the <code>CertPathBuilder</code>
063 * may throw a <code>CertPathBuilderException</code>.
064 * <p>
065 * <b>Concurrent Access</b>
066 * <p>
067 * Unless otherwise specified, the methods defined in this class are not
068 * thread-safe. Multiple threads that need to access a single
069 * object concurrently should synchronize amongst themselves and
070 * provide the necessary locking. Multiple threads each manipulating
071 * separate objects need not synchronize.
072 *
073 * @see CertPathBuilder
074 *
075 * @version 1.22 05/05/07
076 * @since 1.4
077 * @author Sean Mullan
078 */
079 public class PKIXBuilderParameters extends PKIXParameters {
080
081 private int maxPathLength = 5;
082
083 /**
084 * Creates an instance of <code>PKIXBuilderParameters</code> with
085 * the specified <code>Set</code> of most-trusted CAs.
086 * Each element of the set is a {@link TrustAnchor TrustAnchor}.
087 *
088 * <p>Note that the <code>Set</code> is copied to protect against
089 * subsequent modifications.
090 *
091 * @param trustAnchors a <code>Set</code> of <code>TrustAnchor</code>s
092 * @param targetConstraints a <code>CertSelector</code> specifying the
093 * constraints on the target certificate
094 * @throws InvalidAlgorithmParameterException if <code>trustAnchors</code>
095 * is empty <code>(trustAnchors.isEmpty() == true)</code>
096 * @throws NullPointerException if <code>trustAnchors</code> is
097 * <code>null</code>
098 * @throws ClassCastException if any of the elements of
099 * <code>trustAnchors</code> are not of type
100 * <code>java.security.cert.TrustAnchor</code>
101 */
102 public PKIXBuilderParameters(Set<TrustAnchor> trustAnchors,
103 CertSelector targetConstraints)
104 throws InvalidAlgorithmParameterException {
105 super (trustAnchors);
106 setTargetCertConstraints(targetConstraints);
107 }
108
109 /**
110 * Creates an instance of <code>PKIXBuilderParameters</code> that
111 * populates the set of most-trusted CAs from the trusted
112 * certificate entries contained in the specified <code>KeyStore</code>.
113 * Only keystore entries that contain trusted <code>X509Certificate</code>s
114 * are considered; all other certificate types are ignored.
115 *
116 * @param keystore a <code>KeyStore</code> from which the set of
117 * most-trusted CAs will be populated
118 * @param targetConstraints a <code>CertSelector</code> specifying the
119 * constraints on the target certificate
120 * @throws KeyStoreException if <code>keystore</code> has not been
121 * initialized
122 * @throws InvalidAlgorithmParameterException if <code>keystore</code> does
123 * not contain at least one trusted certificate entry
124 * @throws NullPointerException if <code>keystore</code> is
125 * <code>null</code>
126 */
127 public PKIXBuilderParameters(KeyStore keystore,
128 CertSelector targetConstraints) throws KeyStoreException,
129 InvalidAlgorithmParameterException {
130 super (keystore);
131 setTargetCertConstraints(targetConstraints);
132 }
133
134 /**
135 * Sets the value of the maximum number of non-self-issued intermediate
136 * certificates that may exist in a certification path. A certificate
137 * is self-issued if the DNs that appear in the subject and issuer
138 * fields are identical and are not empty. Note that the last certificate
139 * in a certification path is not an intermediate certificate, and is not
140 * included in this limit. Usually the last certificate is an end entity
141 * certificate, but it can be a CA certificate. A PKIX
142 * <code>CertPathBuilder</code> instance must not build
143 * paths longer than the length specified.
144 *
145 * <p> A value of 0 implies that the path can only contain
146 * a single certificate. A value of -1 implies that the
147 * path length is unconstrained (i.e. there is no maximum).
148 * The default maximum path length, if not specified, is 5.
149 * Setting a value less than -1 will cause an exception to be thrown.
150 *
151 * <p> If any of the CA certificates contain the
152 * <code>BasicConstraintsExtension</code>, the value of the
153 * <code>pathLenConstraint</code> field of the extension overrides
154 * the maximum path length parameter whenever the result is a
155 * certification path of smaller length.
156 *
157 * @param maxPathLength the maximum number of non-self-issued intermediate
158 * certificates that may exist in a certification path
159 * @throws InvalidParameterException if <code>maxPathLength</code> is set
160 * to a value less than -1
161 *
162 * @see #getMaxPathLength
163 */
164 public void setMaxPathLength(int maxPathLength) {
165 if (maxPathLength < -1) {
166 throw new InvalidParameterException("the maximum path "
167 + "length parameter can not be less than -1");
168 }
169 this .maxPathLength = maxPathLength;
170 }
171
172 /**
173 * Returns the value of the maximum number of intermediate non-self-issued
174 * certificates that may exist in a certification path. See
175 * the {@link #setMaxPathLength} method for more details.
176 *
177 * @return the maximum number of non-self-issued intermediate certificates
178 * that may exist in a certification path, or -1 if there is no limit
179 *
180 * @see #setMaxPathLength
181 */
182 public int getMaxPathLength() {
183 return maxPathLength;
184 }
185
186 /**
187 * Returns a formatted string describing the parameters.
188 *
189 * @return a formatted string describing the parameters
190 */
191 public String toString() {
192 StringBuffer sb = new StringBuffer();
193 sb.append("[\n");
194 sb.append(super .toString());
195 sb.append(" Maximum Path Length: " + maxPathLength + "\n");
196 sb.append("]\n");
197 return sb.toString();
198 }
199 }
|