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