1. /*
  2. * @(#)CertPathBuilder.java 1.9 04/06/28
  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.AccessController;
  9. import java.security.InvalidAlgorithmParameterException;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.security.NoSuchProviderException;
  12. import java.security.PrivilegedAction;
  13. import java.security.Provider;
  14. import java.security.Security;
  15. import sun.security.util.Debug;
  16. import sun.security.jca.*;
  17. import sun.security.jca.GetInstance.Instance;
  18. /**
  19. * A class for building certification paths (also known as certificate chains).
  20. * <p>
  21. * This class uses a provider-based architecture, as described in the Java
  22. * Cryptography Architecture. To create a <code>CertPathBuilder</code>, call
  23. * one of the static <code>getInstance</code> methods, passing in the
  24. * algorithm name of the <code>CertPathBuilder</code> desired and optionally
  25. * the name of the provider desired.
  26. * <p>
  27. * Once a <code>CertPathBuilder</code> object has been created, certification
  28. * paths can be constructed by calling the {@link #build build} method and
  29. * passing it an algorithm-specific set of parameters. If successful, the
  30. * result (including the <code>CertPath</code> that was built) is returned
  31. * in an object that implements the <code>CertPathBuilderResult</code>
  32. * interface.
  33. * <p>
  34. * <b>Concurrent Access</b>
  35. * <p>
  36. * The static methods of this class are guaranteed to be thread-safe.
  37. * Multiple threads may concurrently invoke the static methods defined in
  38. * this class with no ill effects.
  39. * <p>
  40. * However, this is not true for the non-static methods defined by this class.
  41. * Unless otherwise documented by a specific provider, threads that need to
  42. * access a single <code>CertPathBuilder</code> instance concurrently should
  43. * synchronize amongst themselves and provide the necessary locking. Multiple
  44. * threads each manipulating a different <code>CertPathBuilder</code> instance
  45. * need not synchronize.
  46. *
  47. * @see CertPath
  48. *
  49. * @version 1.9 06/28/04
  50. * @since 1.4
  51. * @author Sean Mullan
  52. * @author Yassir Elley
  53. */
  54. public class CertPathBuilder {
  55. /*
  56. * Constant to lookup in the Security properties file to determine
  57. * the default certpathbuilder type. In the Security properties file,
  58. * the default certpathbuilder type is given as:
  59. * <pre>
  60. * certpathbuilder.type=PKIX
  61. * </pre>
  62. */
  63. private static final String CPB_TYPE = "certpathbuilder.type";
  64. private static final Debug debug = Debug.getInstance("certpath");
  65. private CertPathBuilderSpi builderSpi;
  66. private Provider provider;
  67. private String algorithm;
  68. /**
  69. * Creates a <code>CertPathBuilder</code> object of the given algorithm,
  70. * and encapsulates the given provider implementation (SPI object) in it.
  71. *
  72. * @param builderSpi the provider implementation
  73. * @param provider the provider
  74. * @param algorithm the algorithm name
  75. */
  76. protected CertPathBuilder(CertPathBuilderSpi builderSpi, Provider provider,
  77. String algorithm)
  78. {
  79. this.builderSpi = builderSpi;
  80. this.provider = provider;
  81. this.algorithm = algorithm;
  82. }
  83. /**
  84. * Returns a <code>CertPathBuilder</code> object that implements the
  85. * specified algorithm.
  86. * <p>
  87. * If the default provider package provides an implementation of the
  88. * specified <code>CertPathBuilder</code> algorithm, an instance of
  89. * <code>CertPathBuilder</code> containing that implementation is returned.
  90. * If the requested algorithm is not available in the default package,
  91. * other packages are searched.
  92. *
  93. * @param algorithm the name of the requested <code>CertPathBuilder</code>
  94. * algorithm
  95. * @return a <code>CertPathBuilder</code> object that implements the
  96. * specified algorithm
  97. * @throws NoSuchAlgorithmException if the requested algorithm is
  98. * not available in the default provider package or any of the other
  99. * provider packages that were searched
  100. */
  101. public static CertPathBuilder getInstance(String algorithm)
  102. throws NoSuchAlgorithmException {
  103. Instance instance = GetInstance.getInstance("CertPathBuilder",
  104. CertPathBuilderSpi.class, algorithm);
  105. return new CertPathBuilder((CertPathBuilderSpi)instance.impl,
  106. instance.provider, algorithm);
  107. }
  108. /**
  109. * Returns a <code>CertPathBuilder</code> object that implements the
  110. * specified algorithm, as supplied by the specified provider.
  111. *
  112. * @param algorithm the name of the requested <code>CertPathBuilder</code>
  113. * algorithm
  114. * @param provider the name of the provider
  115. * @return a <code>CertPathBuilder</code> object that implements the
  116. * specified algorithm, as supplied by the specified provider
  117. * @throws NoSuchAlgorithmException if the requested algorithm is
  118. * not available from the specified provider
  119. * @throws NoSuchProviderException if the provider has not been configured
  120. * @exception IllegalArgumentException if the <code>provider</code> is
  121. * null
  122. */
  123. public static CertPathBuilder getInstance(String algorithm, String provider)
  124. throws NoSuchAlgorithmException, NoSuchProviderException {
  125. Instance instance = GetInstance.getInstance("CertPathBuilder",
  126. CertPathBuilderSpi.class, algorithm, provider);
  127. return new CertPathBuilder((CertPathBuilderSpi)instance.impl,
  128. instance.provider, algorithm);
  129. }
  130. /**
  131. * Returns a <code>CertPathBuilder</code> object that implements the
  132. * specified algorithm, as supplied by the specified provider.
  133. * Note: the <code>provider</code> doesn't have to be registered.
  134. *
  135. * @param algorithm the name of the requested <code>CertPathBuilder</code>
  136. * algorithm
  137. * @param provider the provider
  138. * @return a <code>CertPathBuilder</code> object that implements the
  139. * specified algorithm, as supplied by the specified provider
  140. * @exception NoSuchAlgorithmException if the requested algorithm is
  141. * not available from the specified provider
  142. * @exception IllegalArgumentException if the <code>provider</code> is
  143. * null.
  144. */
  145. public static CertPathBuilder getInstance(String algorithm,
  146. Provider provider) throws NoSuchAlgorithmException {
  147. Instance instance = GetInstance.getInstance("CertPathBuilder",
  148. CertPathBuilderSpi.class, algorithm, provider);
  149. return new CertPathBuilder((CertPathBuilderSpi)instance.impl,
  150. instance.provider, algorithm);
  151. }
  152. /**
  153. * Returns the provider of this <code>CertPathBuilder</code>.
  154. *
  155. * @return the provider of this <code>CertPathBuilder</code>
  156. */
  157. public final Provider getProvider() {
  158. return this.provider;
  159. }
  160. /**
  161. * Returns the name of the algorithm of this <code>CertPathBuilder</code>.
  162. *
  163. * @return the name of the algorithm of this <code>CertPathBuilder</code>
  164. */
  165. public final String getAlgorithm() {
  166. return this.algorithm;
  167. }
  168. /**
  169. * Attempts to build a certification path using the specified algorithm
  170. * parameter set.
  171. *
  172. * @param params the algorithm parameters
  173. * @return the result of the build algorithm
  174. * @throws CertPathBuilderException if the builder is unable to construct
  175. * a certification path that satisfies the specified parameters
  176. * @throws InvalidAlgorithmParameterException if the specified parameters
  177. * are inappropriate for this <code>CertPathBuilder</code>
  178. */
  179. public final CertPathBuilderResult build(CertPathParameters params)
  180. throws CertPathBuilderException, InvalidAlgorithmParameterException
  181. {
  182. return builderSpi.engineBuild(params);
  183. }
  184. /**
  185. * Returns the default <code>CertPathBuilder</code> type as specified in
  186. * the Java security properties file, or the string "PKIX"
  187. * if no such property exists. The Java security properties file is
  188. * located in the file named <JAVA_HOME>/lib/security/java.security,
  189. * where <JAVA_HOME> refers to the directory where the JDK was
  190. * installed.
  191. *
  192. * <p>The default <code>CertPathBuilder</code> type can be used by
  193. * applications that do not want to use a hard-coded type when calling one
  194. * of the <code>getInstance</code> methods, and want to provide a default
  195. * type in case a user does not specify its own.
  196. *
  197. * <p>The default <code>CertPathBuilder</code> type can be changed by
  198. * setting the value of the "certpathbuilder.type" security property
  199. * (in the Java security properties file) to the desired type.
  200. *
  201. * @return the default <code>CertPathBuilder</code> type as specified
  202. * in the Java security properties file, or the string "PKIX"
  203. * if no such property exists.
  204. */
  205. public final static String getDefaultType() {
  206. String cpbtype;
  207. cpbtype = (String)AccessController.doPrivileged(new PrivilegedAction() {
  208. public Object run() {
  209. return Security.getProperty(CPB_TYPE);
  210. }
  211. });
  212. if (cpbtype == null) {
  213. cpbtype = "PKIX";
  214. }
  215. return cpbtype;
  216. }
  217. }