1 /* CertificateFactory.java --- Certificate Factory Class
2 Copyright (C) 1999 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
28 package java.security.cert;
29 import java.security.NoSuchProviderException;
30 import java.security.Provider;
31 import java.security.Security;
32 import java.io.InputStream;
33 import java.util.Collection;
36 This class implments the CertificateFactory class interface
37 used to generate certificates and certificate revocation
38 list (CRL) objects from their encodings.
40 A certifcate factory for X.509 returns certificates of the
41 java.security.cert.X509Certificate class, and CRLs of the
42 java.security.cert.X509CRL class.
46 @author Mark Benvenuto
48 public class CertificateFactory
51 private CertificateFactorySpi certFacSpi;
52 private Provider provider;
56 Creates an instance of CertificateFactory
58 @param certFacSpi A CertificateFactory engine to use
59 @param provider A provider to use
60 @param type The type of Certificate
62 protected CertificateFactory(CertificateFactorySpi certFacSpi, Provider provider, String type)
64 this.certFacSpi = certFacSpi;
65 this.provider = provider;
71 Gets an instance of the CertificateFactory class representing
72 the specified certificate factory. If the type is not
73 found then, it throws CertificateException.
75 @param type the type of certificate to choose
77 @return a CertificateFactory repesenting the desired type
79 @throws CertificateException if the type of certificate is not implemented by providers
81 public static final CertificateFactory getInstance(String type) throws CertificateException
83 Provider[] p = Security.getProviders ();
85 for (int i = 0; i < p.length; i++)
87 String classname = p[i].getProperty ("CertificateFactory." + type);
88 if (classname != null)
89 return getInstance (classname, type, p[i]);
92 throw new CertificateException(type);
98 Gets an instance of the CertificateFactory class representing
99 the specified certificate factory from the specified provider.
100 If the type is not found then, it throws CertificateException.
101 If the provider is not found, then it throws
102 NoSuchProviderException.
104 @param type the type of certificate to choose
106 @return a CertificateFactory repesenting the desired type
108 @throws CertificateException if the type of certificate is not implemented by providers
109 @throws NoSuchProviderException if the provider is not found
111 public static final CertificateFactory getInstance(String type, String provider)
112 throws CertificateException, NoSuchProviderException
114 Provider p = Security.getProvider(provider);
116 throw new NoSuchProviderException();
118 return getInstance (p.getProperty ("CertificateFactory." + type),
122 private static CertificateFactory getInstance (String classname,
125 throws CertificateException
128 return new CertificateFactory( (CertificateFactorySpi)Class.forName( classname ).newInstance(), provider, type );
129 } catch( ClassNotFoundException cnfe) {
130 throw new CertificateException("Class not found");
131 } catch( InstantiationException ie) {
132 throw new CertificateException("Class instantiation failed");
133 } catch( IllegalAccessException iae) {
134 throw new CertificateException("Illegal Access");
140 Gets the provider that the class is from.
142 @return the provider of this class
144 public final Provider getProvider()
150 Returns the type of the certificate supported
152 @return A string with the type of certificate
154 public final String getType()
160 Generates a Certificate based on the encoded data read
161 from the InputStream.
163 The input stream must contain only one certificate.
165 If there exists a specialized certificate class for the
166 certificate format handled by the certificate factory
167 then the return Ceritificate should be a typecast of it.
168 Ex: A X.509 CertificateFactory should return X509Certificate.
170 For X.509 certificates, the certificate in inStream must be
171 DER encoded and supplied in binary or printable (Base64)
172 encoding. If the certificate is in Base64 encoding, it must be
173 bounded by -----BEGINCERTIFICATE-----, and
174 -----END CERTIFICATE-----.
176 @param inStream an input stream containing the certificate data
178 @return a certificate initialized with InputStream data.
180 @throws CertificateException Certificate parsing error
182 public final Certificate generateCertificate(InputStream inStream)
183 throws CertificateException
185 return certFacSpi.engineGenerateCertificate( inStream );
189 Returns a collection of certificates that were read from the
190 input stream. It may be empty, have only one, or have
191 multiple certificates.
193 For a X.509 certificate factory, the stream may contain a
194 single DER encoded certificate or a PKCS#7 certificate
195 chain. This is a PKCS#7 <I>SignedData</I> object with the
196 most significant field being <I>certificates</I>. If no
197 CRLs are present, then an empty collection is returned.
199 @param inStream an input stream containing the certificates
201 @return a collection of certificates initialized with
202 the InputStream data.
204 @throws CertificateException Certificate parsing error
206 public final Collection generateCertificates(InputStream inStream)
207 throws CertificateException
209 return certFacSpi.engineGenerateCertificates( inStream );
213 Generates a CRL based on the encoded data read
214 from the InputStream.
216 The input stream must contain only one CRL.
218 If there exists a specialized CRL class for the
219 CRL format handled by the certificate factory
220 then the return CRL should be a typecast of it.
221 Ex: A X.509 CertificateFactory should return X509CRL.
223 @param inStream an input stream containing the CRL data
225 @return a CRL initialized with InputStream data.
227 @throws CRLException CRL parsing error
229 public final CRL generateCRL(InputStream inStream)
232 return certFacSpi.engineGenerateCRL( inStream );
237 Generates CRLs based on the encoded data read
238 from the InputStream.
240 For a X.509 certificate factory, the stream may contain a
241 single DER encoded CRL or a PKCS#7 CRL set. This is a
242 PKCS#7 <I>SignedData</I> object with the most significant
243 field being <I>crls</I>. If no CRLs are present, then an
244 empty collection is returned.
246 @param inStream an input stream containing the CRLs
248 @return a collection of CRLs initialized with
249 the InputStream data.
251 @throws CRLException CRL parsing error
253 public final Collection generateCRLs(InputStream inStream)
256 return certFacSpi.engineGenerateCRLs( inStream );