CertificateFactory.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* CertificateFactory.java -- Certificate Factory Class
  2. Copyright (C) 1999, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security.cert;
  32. import java.security.NoSuchProviderException;
  33. import java.security.Provider;
  34. import java.security.Security;
  35. import java.io.InputStream;
  36. import java.util.Collection;
  37. /**
  38. This class implments the CertificateFactory class interface
  39. used to generate certificates and certificate revocation
  40. list (CRL) objects from their encodings.
  41. A certifcate factory for X.509 returns certificates of the
  42. java.security.cert.X509Certificate class, and CRLs of the
  43. java.security.cert.X509CRL class.
  44. @author Mark Benvenuto
  45. @since JDK 1.2
  46. @status still missing full 1.4 support
  47. */
  48. public class CertificateFactory
  49. {
  50. private CertificateFactorySpi certFacSpi;
  51. private Provider provider;
  52. private String type;
  53. /**
  54. Creates an instance of CertificateFactory
  55. @param certFacSpi A CertificateFactory engine to use
  56. @param provider A provider to use
  57. @param type The type of Certificate
  58. */
  59. protected CertificateFactory(CertificateFactorySpi certFacSpi, Provider provider, String type)
  60. {
  61. this.certFacSpi = certFacSpi;
  62. this.provider = provider;
  63. this.type = type;
  64. }
  65. /**
  66. Gets an instance of the CertificateFactory class representing
  67. the specified certificate factory. If the type is not
  68. found then, it throws CertificateException.
  69. @param type the type of certificate to choose
  70. @return a CertificateFactory repesenting the desired type
  71. @throws CertificateException if the type of certificate is not implemented by providers
  72. */
  73. public static final CertificateFactory getInstance(String type) throws CertificateException
  74. {
  75. Provider[] p = Security.getProviders ();
  76. for (int i = 0; i < p.length; i++)
  77. {
  78. String classname = p[i].getProperty ("CertificateFactory." + type);
  79. if (classname != null)
  80. return getInstance (classname, type, p[i]);
  81. }
  82. throw new CertificateException(type);
  83. }
  84. /**
  85. Gets an instance of the CertificateFactory class representing
  86. the specified certificate factory from the specified provider.
  87. If the type is not found then, it throws CertificateException.
  88. If the provider is not found, then it throws
  89. NoSuchProviderException.
  90. @param type the type of certificate to choose
  91. @return a CertificateFactory repesenting the desired type
  92. @throws CertificateException if the type of certificate is not implemented by providers
  93. @throws NoSuchProviderException if the provider is not found
  94. */
  95. public static final CertificateFactory getInstance(String type, String provider)
  96. throws CertificateException, NoSuchProviderException
  97. {
  98. Provider p = Security.getProvider(provider);
  99. if( p == null)
  100. throw new NoSuchProviderException();
  101. return getInstance (p.getProperty ("CertificateFactory." + type),
  102. type, p);
  103. }
  104. private static CertificateFactory getInstance (String classname,
  105. String type,
  106. Provider provider)
  107. throws CertificateException
  108. {
  109. try {
  110. return new CertificateFactory( (CertificateFactorySpi)Class.forName( classname ).newInstance(), provider, type );
  111. } catch( ClassNotFoundException cnfe) {
  112. throw new CertificateException("Class not found");
  113. } catch( InstantiationException ie) {
  114. throw new CertificateException("Class instantiation failed");
  115. } catch( IllegalAccessException iae) {
  116. throw new CertificateException("Illegal Access");
  117. }
  118. }
  119. /**
  120. Gets the provider that the class is from.
  121. @return the provider of this class
  122. */
  123. public final Provider getProvider()
  124. {
  125. return provider;
  126. }
  127. /**
  128. Returns the type of the certificate supported
  129. @return A string with the type of certificate
  130. */
  131. public final String getType()
  132. {
  133. return type;
  134. }
  135. /**
  136. Generates a Certificate based on the encoded data read
  137. from the InputStream.
  138. The input stream must contain only one certificate.
  139. If there exists a specialized certificate class for the
  140. certificate format handled by the certificate factory
  141. then the return Ceritificate should be a typecast of it.
  142. Ex: A X.509 CertificateFactory should return X509Certificate.
  143. For X.509 certificates, the certificate in inStream must be
  144. DER encoded and supplied in binary or printable (Base64)
  145. encoding. If the certificate is in Base64 encoding, it must be
  146. bounded by -----BEGINCERTIFICATE-----, and
  147. -----END CERTIFICATE-----.
  148. @param inStream an input stream containing the certificate data
  149. @return a certificate initialized with InputStream data.
  150. @throws CertificateException Certificate parsing error
  151. */
  152. public final Certificate generateCertificate(InputStream inStream)
  153. throws CertificateException
  154. {
  155. return certFacSpi.engineGenerateCertificate( inStream );
  156. }
  157. /**
  158. Returns a collection of certificates that were read from the
  159. input stream. It may be empty, have only one, or have
  160. multiple certificates.
  161. For a X.509 certificate factory, the stream may contain a
  162. single DER encoded certificate or a PKCS#7 certificate
  163. chain. This is a PKCS#7 <I>SignedData</I> object with the
  164. most significant field being <I>certificates</I>. If no
  165. CRLs are present, then an empty collection is returned.
  166. @param inStream an input stream containing the certificates
  167. @return a collection of certificates initialized with
  168. the InputStream data.
  169. @throws CertificateException Certificate parsing error
  170. */
  171. public final Collection generateCertificates(InputStream inStream)
  172. throws CertificateException
  173. {
  174. return certFacSpi.engineGenerateCertificates( inStream );
  175. }
  176. /**
  177. Generates a CRL based on the encoded data read
  178. from the InputStream.
  179. The input stream must contain only one CRL.
  180. If there exists a specialized CRL class for the
  181. CRL format handled by the certificate factory
  182. then the return CRL should be a typecast of it.
  183. Ex: A X.509 CertificateFactory should return X509CRL.
  184. @param inStream an input stream containing the CRL data
  185. @return a CRL initialized with InputStream data.
  186. @throws CRLException CRL parsing error
  187. */
  188. public final CRL generateCRL(InputStream inStream)
  189. throws CRLException
  190. {
  191. return certFacSpi.engineGenerateCRL( inStream );
  192. }
  193. /**
  194. Generates CRLs based on the encoded data read
  195. from the InputStream.
  196. For a X.509 certificate factory, the stream may contain a
  197. single DER encoded CRL or a PKCS#7 CRL set. This is a
  198. PKCS#7 <I>SignedData</I> object with the most significant
  199. field being <I>crls</I>. If no CRLs are present, then an
  200. empty collection is returned.
  201. @param inStream an input stream containing the CRLs
  202. @return a collection of CRLs initialized with
  203. the InputStream data.
  204. @throws CRLException CRL parsing error
  205. */
  206. public final Collection generateCRLs(InputStream inStream)
  207. throws CRLException
  208. {
  209. return certFacSpi.engineGenerateCRLs( inStream );
  210. }
  211. public final CertPath generateCertPath(InputStream inStream)
  212. throws CertificateException
  213. {
  214. throw new CertificateException("not implemented");
  215. }
  216. } // class CertificateFactory