1. /*
  2. * @(#)CertificateFactory.java 1.28 04/05/05
  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.io.InputStream;
  9. import java.util.Collection;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.security.Provider;
  13. import java.security.AccessController;
  14. import java.security.PrivilegedAction;
  15. import java.security.NoSuchAlgorithmException;
  16. import java.security.NoSuchProviderException;
  17. import sun.security.jca.*;
  18. import sun.security.jca.GetInstance.Instance;
  19. /**
  20. * This class defines the functionality of a certificate factory, which is
  21. * used to generate certificate, certification path (<code>CertPath</code>)
  22. * and certificate revocation list (CRL) objects from their encodings.
  23. *
  24. * <p>For encodings consisting of multiple certificates, use
  25. * <code>generateCertificates</code> when you want to
  26. * parse a collection of possibly unrelated certificates. Otherwise,
  27. * use <code>generateCertPath</code> when you want to generate
  28. * a <code>CertPath</code> (a certificate chain) and subsequently
  29. * validate it with a <code>CertPathValidator</code>.
  30. *
  31. * <p>A certificate factory for X.509 must return certificates that are an
  32. * instance of <code>java.security.cert.X509Certificate</code>, and CRLs
  33. * that are an instance of <code>java.security.cert.X509CRL</code>.
  34. *
  35. * <p>The following example reads a file with Base64 encoded certificates,
  36. * which are each bounded at the beginning by -----BEGIN CERTIFICATE-----, and
  37. * bounded at the end by -----END CERTIFICATE-----. We convert the
  38. * <code>FileInputStream</code> (which does not support <code>mark</code>
  39. * and <code>reset</code>) to a <code>BufferedInputStream</code> (which
  40. * supports those methods), so that each call to
  41. * <code>generateCertificate</code> consumes only one certificate, and the
  42. * read position of the input stream is positioned to the next certificate in
  43. * the file:<p>
  44. *
  45. * <pre>
  46. * FileInputStream fis = new FileInputStream(filename);
  47. * BufferedInputStream bis = new BufferedInputStream(fis);
  48. *
  49. * CertificateFactory cf = CertificateFactory.getInstance("X.509");
  50. *
  51. * while (bis.available() > 0) {
  52. * Certificate cert = cf.generateCertificate(bis);
  53. * System.out.println(cert.toString());
  54. * }
  55. * </pre>
  56. *
  57. * <p>The following example parses a PKCS#7-formatted certificate reply stored
  58. * in a file and extracts all the certificates from it:<p>
  59. *
  60. * <pre>
  61. * FileInputStream fis = new FileInputStream(filename);
  62. * CertificateFactory cf = CertificateFactory.getInstance("X.509");
  63. * Collection c = cf.generateCertificates(fis);
  64. * Iterator i = c.iterator();
  65. * while (i.hasNext()) {
  66. * Certificate cert = (Certificate)i.next();
  67. * System.out.println(cert);
  68. * }
  69. * </pre>
  70. *
  71. * @author Hemma Prafullchandra
  72. * @author Jan Luehe
  73. * @author Sean Mullan
  74. *
  75. * @version 1.28, 05/05/04
  76. *
  77. * @see Certificate
  78. * @see X509Certificate
  79. * @see CertPath
  80. * @see CRL
  81. * @see X509CRL
  82. *
  83. * @since 1.2
  84. */
  85. public class CertificateFactory {
  86. // The certificate type
  87. private String type;
  88. // The provider
  89. private Provider provider;
  90. // The provider implementation
  91. private CertificateFactorySpi certFacSpi;
  92. /**
  93. * Creates a CertificateFactory object of the given type, and encapsulates
  94. * the given provider implementation (SPI object) in it.
  95. *
  96. * @param certFacSpi the provider implementation.
  97. * @param provider the provider.
  98. * @param type the certificate type.
  99. */
  100. protected CertificateFactory(CertificateFactorySpi certFacSpi,
  101. Provider provider, String type)
  102. {
  103. this.certFacSpi = certFacSpi;
  104. this.provider = provider;
  105. this.type = type;
  106. }
  107. /**
  108. * Generates a certificate factory object that implements the
  109. * specified certificate type. If the default provider package
  110. * provides an implementation of the requested certificate type,
  111. * an instance of certificate factory containing that
  112. * implementation is returned.
  113. * If the type is not available in the default
  114. * package, other packages are searched.
  115. *
  116. * @param type the name of the requested certificate type.
  117. * See Appendix A in the <a href=
  118. * "../../../../guide/security/CryptoSpec.html#AppA">
  119. * Java Cryptography Architecture API Specification & Reference </a>
  120. * for information about standard certificate types.
  121. *
  122. * @return a certificate factory object for the specified type.
  123. *
  124. * @exception CertificateException if the requested certificate type is
  125. * not available in the default provider package or any of the other
  126. * provider packages that were searched.
  127. */
  128. public static final CertificateFactory getInstance(String type)
  129. throws CertificateException {
  130. try {
  131. Instance instance = GetInstance.getInstance("CertificateFactory",
  132. CertificateFactorySpi.class, type);
  133. return new CertificateFactory((CertificateFactorySpi)instance.impl,
  134. instance.provider, type);
  135. } catch (NoSuchAlgorithmException e) {
  136. throw new CertificateException(type + " not found", e);
  137. }
  138. }
  139. /**
  140. * Generates a certificate factory object for the specified
  141. * certificate type from the specified provider.
  142. *
  143. * @param type the certificate type
  144. * @param provider the name of the provider.
  145. *
  146. * @return a certificate factory object for the specified type.
  147. *
  148. * @exception CertificateException if the certificate type is
  149. * not available from the specified provider.
  150. *
  151. * @exception NoSuchProviderException if the provider has not been
  152. * configured.
  153. *
  154. * @see Provider
  155. */
  156. public static final CertificateFactory getInstance(String type,
  157. String provider) throws CertificateException,
  158. NoSuchProviderException {
  159. try {
  160. Instance instance = GetInstance.getInstance("CertificateFactory",
  161. CertificateFactorySpi.class, type, provider);
  162. return new CertificateFactory((CertificateFactorySpi)instance.impl,
  163. instance.provider, type);
  164. } catch (NoSuchAlgorithmException e) {
  165. throw new CertificateException(type + " not found", e);
  166. }
  167. }
  168. /**
  169. * Generates a certificate factory object for the specified
  170. * certificate type from the specified provider.
  171. * Note: the <code>provider</code> doesn't have to be registered.
  172. *
  173. * @param type the certificate type
  174. * @param provider the provider
  175. *
  176. * @return a certificate factory object for the specified type.
  177. *
  178. * @exception CertificateException if the certificate type is
  179. * not available from the specified provider.
  180. *
  181. * @exception IllegalArgumentException if the <code>provider</code> is
  182. * null.
  183. *
  184. * @see Provider
  185. *
  186. * @since 1.4
  187. */
  188. public static final CertificateFactory getInstance(String type,
  189. Provider provider) throws CertificateException {
  190. try {
  191. Instance instance = GetInstance.getInstance("CertificateFactory",
  192. CertificateFactorySpi.class, type, provider);
  193. return new CertificateFactory((CertificateFactorySpi)instance.impl,
  194. instance.provider, type);
  195. } catch (NoSuchAlgorithmException e) {
  196. throw new CertificateException(type + " not found", e);
  197. }
  198. }
  199. /**
  200. * Returns the provider of this certificate factory.
  201. *
  202. * @return the provider of this certificate factory.
  203. */
  204. public final Provider getProvider() {
  205. return this.provider;
  206. }
  207. /**
  208. * Returns the name of the certificate type associated with this
  209. * certificate factory.
  210. *
  211. * @return the name of the certificate type associated with this
  212. * certificate factory.
  213. */
  214. public final String getType() {
  215. return this.type;
  216. }
  217. /**
  218. * Generates a certificate object and initializes it with
  219. * the data read from the input stream <code>inStream</code>.
  220. *
  221. * <p>In order to take advantage of the specialized certificate format
  222. * supported by this certificate factory,
  223. * the returned certificate object can be typecast to the corresponding
  224. * certificate class. For example, if this certificate
  225. * factory implements X.509 certificates, the returned certificate object
  226. * can be typecast to the <code>X509Certificate</code> class.
  227. *
  228. * <p>In the case of a certificate factory for X.509 certificates, the
  229. * certificate provided in <code>inStream</code> must be DER-encoded and
  230. * may be supplied in binary or printable (Base64) encoding. If the
  231. * certificate is provided in Base64 encoding, it must be bounded at
  232. * the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at
  233. * the end by -----END CERTIFICATE-----.
  234. *
  235. * <p>Note that if the given input stream does not support
  236. * {@link java.io.InputStream#mark(int) mark} and
  237. * {@link java.io.InputStream#reset() reset}, this method will
  238. * consume the entire input stream. Otherwise, each call to this
  239. * method consumes one certificate and the read position of the
  240. * input stream is positioned to the next available byte after
  241. * the inherent end-of-certificate marker. If the data in the input stream
  242. * does not contain an inherent end-of-certificate marker (other
  243. * than EOF) and there is trailing data after the certificate is parsed, a
  244. * <code>CertificateException</code> is thrown.
  245. *
  246. * @param inStream an input stream with the certificate data.
  247. *
  248. * @return a certificate object initialized with the data
  249. * from the input stream.
  250. *
  251. * @exception CertificateException on parsing errors.
  252. */
  253. public final Certificate generateCertificate(InputStream inStream)
  254. throws CertificateException
  255. {
  256. return certFacSpi.engineGenerateCertificate(inStream);
  257. }
  258. /**
  259. * Returns an iteration of the <code>CertPath</code> encodings supported
  260. * by this certificate factory, with the default encoding first. See
  261. * Appendix A in the
  262. * <a href="../../../../guide/security/certpath/CertPathProgGuide.html#AppA">
  263. * Java Certification Path API Programmer's Guide</a> for information about
  264. * standard encoding names and their formats.
  265. * <p>
  266. * Attempts to modify the returned <code>Iterator</code> via its
  267. * <code>remove</code> method result in an
  268. * <code>UnsupportedOperationException</code>.
  269. *
  270. * @return an <code>Iterator</code> over the names of the supported
  271. * <code>CertPath</code> encodings (as <code>String</code>s)
  272. * @since 1.4
  273. */
  274. public final Iterator<String> getCertPathEncodings() {
  275. return(certFacSpi.engineGetCertPathEncodings());
  276. }
  277. /**
  278. * Generates a <code>CertPath</code> object and initializes it with
  279. * the data read from the <code>InputStream</code> inStream. The data
  280. * is assumed to be in the default encoding. The name of the default
  281. * encoding is the first element of the <code>Iterator</code> returned by
  282. * the {@link #getCertPathEncodings getCertPathEncodings} method.
  283. *
  284. * @param inStream an <code>InputStream</code> containing the data
  285. * @return a <code>CertPath</code> initialized with the data from the
  286. * <code>InputStream</code>
  287. * @exception CertificateException if an exception occurs while decoding
  288. * @since 1.4
  289. */
  290. public final CertPath generateCertPath(InputStream inStream)
  291. throws CertificateException
  292. {
  293. return(certFacSpi.engineGenerateCertPath(inStream));
  294. }
  295. /**
  296. * Generates a <code>CertPath</code> object and initializes it with
  297. * the data read from the <code>InputStream</code> inStream. The data
  298. * is assumed to be in the specified encoding. See Appendix A in the
  299. * <a href="../../../../guide/security/certpath/CertPathProgGuide.html#AppA">
  300. * Java Certification Path API Programmer's Guide</a>
  301. * for information about standard encoding names and their formats.
  302. *
  303. * @param inStream an <code>InputStream</code> containing the data
  304. * @param encoding the encoding used for the data
  305. * @return a <code>CertPath</code> initialized with the data from the
  306. * <code>InputStream</code>
  307. * @exception CertificateException if an exception occurs while decoding or
  308. * the encoding requested is not supported
  309. * @since 1.4
  310. */
  311. public final CertPath generateCertPath(InputStream inStream,
  312. String encoding) throws CertificateException
  313. {
  314. return(certFacSpi.engineGenerateCertPath(inStream, encoding));
  315. }
  316. /**
  317. * Generates a <code>CertPath</code> object and initializes it with
  318. * a <code>List</code> of <code>Certificate</code>s.
  319. * <p>
  320. * The certificates supplied must be of a type supported by the
  321. * <code>CertificateFactory</code>. They will be copied out of the supplied
  322. * <code>List</code> object.
  323. *
  324. * @param certificates a <code>List</code> of <code>Certificate</code>s
  325. * @return a <code>CertPath</code> initialized with the supplied list of
  326. * certificates
  327. * @exception CertificateException if an exception occurs
  328. * @since 1.4
  329. */
  330. public final CertPath
  331. generateCertPath(List<? extends Certificate> certificates)
  332. throws CertificateException
  333. {
  334. return(certFacSpi.engineGenerateCertPath(certificates));
  335. }
  336. /**
  337. * Returns a (possibly empty) collection view of the certificates read
  338. * from the given input stream <code>inStream</code>.
  339. *
  340. * <p>In order to take advantage of the specialized certificate format
  341. * supported by this certificate factory, each element in
  342. * the returned collection view can be typecast to the corresponding
  343. * certificate class. For example, if this certificate
  344. * factory implements X.509 certificates, the elements in the returned
  345. * collection can be typecast to the <code>X509Certificate</code> class.
  346. *
  347. * <p>In the case of a certificate factory for X.509 certificates,
  348. * <code>inStream</code> may contain a sequence of DER-encoded certificates
  349. * in the formats described for
  350. * {@link #generateCertificate(java.io.InputStream) generateCertificate}.
  351. * In addition, <code>inStream</code> may contain a PKCS#7 certificate
  352. * chain. This is a PKCS#7 <i>SignedData</i> object, with the only
  353. * significant field being <i>certificates</i>. In particular, the
  354. * signature and the contents are ignored. This format allows multiple
  355. * certificates to be downloaded at once. If no certificates are present,
  356. * an empty collection is returned.
  357. *
  358. * <p>Note that if the given input stream does not support
  359. * {@link java.io.InputStream#mark(int) mark} and
  360. * {@link java.io.InputStream#reset() reset}, this method will
  361. * consume the entire input stream.
  362. *
  363. * @param inStream the input stream with the certificates.
  364. *
  365. * @return a (possibly empty) collection view of
  366. * java.security.cert.Certificate objects
  367. * initialized with the data from the input stream.
  368. *
  369. * @exception CertificateException on parsing errors.
  370. */
  371. public final Collection<? extends Certificate> generateCertificates
  372. (InputStream inStream) throws CertificateException {
  373. return certFacSpi.engineGenerateCertificates(inStream);
  374. }
  375. /**
  376. * Generates a certificate revocation list (CRL) object and initializes it
  377. * with the data read from the input stream <code>inStream</code>.
  378. *
  379. * <p>In order to take advantage of the specialized CRL format
  380. * supported by this certificate factory,
  381. * the returned CRL object can be typecast to the corresponding
  382. * CRL class. For example, if this certificate
  383. * factory implements X.509 CRLs, the returned CRL object
  384. * can be typecast to the <code>X509CRL</code> class.
  385. *
  386. * <p>Note that if the given input stream does not support
  387. * {@link java.io.InputStream#mark(int) mark} and
  388. * {@link java.io.InputStream#reset() reset}, this method will
  389. * consume the entire input stream. Otherwise, each call to this
  390. * method consumes one CRL and the read position of the input stream
  391. * is positioned to the next available byte after the the inherent
  392. * end-of-CRL marker. If the data in the
  393. * input stream does not contain an inherent end-of-CRL marker (other
  394. * than EOF) and there is trailing data after the CRL is parsed, a
  395. * <code>CRLException</code> is thrown.
  396. *
  397. * @param inStream an input stream with the CRL data.
  398. *
  399. * @return a CRL object initialized with the data
  400. * from the input stream.
  401. *
  402. * @exception CRLException on parsing errors.
  403. */
  404. public final CRL generateCRL(InputStream inStream)
  405. throws CRLException
  406. {
  407. return certFacSpi.engineGenerateCRL(inStream);
  408. }
  409. /**
  410. * Returns a (possibly empty) collection view of the CRLs read
  411. * from the given input stream <code>inStream</code>.
  412. *
  413. * <p>In order to take advantage of the specialized CRL format
  414. * supported by this certificate factory, each element in
  415. * the returned collection view can be typecast to the corresponding
  416. * CRL class. For example, if this certificate
  417. * factory implements X.509 CRLs, the elements in the returned
  418. * collection can be typecast to the <code>X509CRL</code> class.
  419. *
  420. * <p>In the case of a certificate factory for X.509 CRLs,
  421. * <code>inStream</code> may contain a sequence of DER-encoded CRLs.
  422. * In addition, <code>inStream</code> may contain a PKCS#7 CRL
  423. * set. This is a PKCS#7 <i>SignedData</i> object, with the only
  424. * significant field being <i>crls</i>. In particular, the
  425. * signature and the contents are ignored. This format allows multiple
  426. * CRLs to be downloaded at once. If no CRLs are present,
  427. * an empty collection is returned.
  428. *
  429. * <p>Note that if the given input stream does not support
  430. * {@link java.io.InputStream#mark(int) mark} and
  431. * {@link java.io.InputStream#reset() reset}, this method will
  432. * consume the entire input stream.
  433. *
  434. * @param inStream the input stream with the CRLs.
  435. *
  436. * @return a (possibly empty) collection view of
  437. * java.security.cert.CRL objects initialized with the data from the input
  438. * stream.
  439. *
  440. * @exception CRLException on parsing errors.
  441. */
  442. public final Collection<? extends CRL> generateCRLs(InputStream inStream)
  443. throws CRLException {
  444. return certFacSpi.engineGenerateCRLs(inStream);
  445. }
  446. }