001 /* PKIXBuilderParameters.java -- parameters for PKIX cert path builders
002 Copyright (C) 2003 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.security.cert;
040
041 import java.security.InvalidAlgorithmParameterException;
042 import java.security.KeyStore;
043 import java.security.KeyStoreException;
044
045 import java.util.Set;
046
047 /**
048 * Parameters for building certificate paths using the PKIX algorithm.
049 *
050 * @see CertPathBuilder
051 * @since 1.4
052 */
053 public class PKIXBuilderParameters extends PKIXParameters
054 {
055
056 // Fields.
057 // ------------------------------------------------------------------------
058
059 /** The maximum path length. */
060 private int maxPathLength;
061
062 // Constructors.
063 // ------------------------------------------------------------------------
064
065 /**
066 * Create a new PKIXBuilderParameters object, populating the trusted
067 * certificates set with all X.509 certificates found in the given key
068 * store. All certificates found in the key store are assumed to be
069 * trusted by this constructor.
070 *
071 * @param keystore The key store.
072 * @param targetConstraints The target certificate constraints.
073 * @throws KeyStoreException If the certificates cannot be retrieved
074 * from the key store.
075 * @throws InvalidAlgorithmParameterException If there are no
076 * certificates in the key store.
077 * @throws NullPointerException If <i>keystore</i> is null.
078 */
079 public PKIXBuilderParameters(KeyStore keystore,
080 CertSelector targetConstraints)
081 throws KeyStoreException, InvalidAlgorithmParameterException
082 {
083 super(keystore);
084 setTargetCertConstraints(targetConstraints);
085 maxPathLength = 5;
086 }
087
088 /**
089 * Create a new PKIXBuilderParameters object, populating the trusted
090 * certificates set with the elements of the given set, each of which
091 * must be a {@link TrustAnchor}.
092 *
093 * @param trustAnchors The set of trust anchors.
094 * @param targetConstraints The target certificate constraints.
095 * @throws InvalidAlgorithmParameterException If there are no
096 * certificates in the set.
097 * @throws NullPointerException If <i>trustAnchors</i> is null.
098 * @throws ClassCastException If every element in <i>trustAnchors</i>
099 * is not a {@link TrustAnchor}.
100 */
101 public PKIXBuilderParameters(Set<TrustAnchor> trustAnchors,
102 CertSelector targetConstraints)
103 throws InvalidAlgorithmParameterException
104 {
105 super(trustAnchors);
106 setTargetCertConstraints(targetConstraints);
107 maxPathLength = 5;
108 }
109
110 // Instance methods.
111 // ------------------------------------------------------------------------
112
113 /**
114 * Returns the maximum length of certificate paths to build.
115 *
116 * <p>If this value is 0 it is taken to mean that the certificate path
117 * should contain only one certificate. A value of -1 means that the
118 * certificate path length is unconstrained. The default value is 5.
119 *
120 * @return The maximum path length.
121 */
122 public int getMaxPathLength()
123 {
124 return maxPathLength;
125 }
126
127 /**
128 * Sets the maximum length of certificate paths to build.
129 *
130 * @param maxPathLength The new path length.
131 * @throws IllegalArgumentException If <i>maxPathLength</i> is less
132 * than -1.
133 */
134 public void setMaxPathLength(int maxPathLength)
135 {
136 if (maxPathLength < -1)
137 throw new IllegalArgumentException();
138 this.maxPathLength = maxPathLength;
139 }
140
141 public String toString()
142 {
143 StringBuffer buf = new StringBuffer(super.toString());
144 buf.insert(buf.length() - 2, "; Max Path Length=" + maxPathLength);
145 return buf.toString();
146 }
147 }