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 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.security.cert;
40 import java.security.NoSuchProviderException;
41 import java.security.Provider;
42 import java.security.Security;
43 import java.io.InputStream;
44 import java.util.Collection;
47 This class implments the CertificateFactory class interface
48 used to generate certificates and certificate revocation
49 list (CRL) objects from their encodings.
51 A certifcate factory for X.509 returns certificates of the
52 java.security.cert.X509Certificate class, and CRLs of the
53 java.security.cert.X509CRL class.
57 @author Mark Benvenuto
59 public class CertificateFactory
62 private CertificateFactorySpi certFacSpi;
63 private Provider provider;
67 Creates an instance of CertificateFactory
69 @param certFacSpi A CertificateFactory engine to use
70 @param provider A provider to use
71 @param type The type of Certificate
73 protected CertificateFactory(CertificateFactorySpi certFacSpi, Provider provider, String type)
75 this.certFacSpi = certFacSpi;
76 this.provider = provider;
82 Gets an instance of the CertificateFactory class representing
83 the specified certificate factory. If the type is not
84 found then, it throws CertificateException.
86 @param type the type of certificate to choose
88 @return a CertificateFactory repesenting the desired type
90 @throws CertificateException if the type of certificate is not implemented by providers
92 public static final CertificateFactory getInstance(String type) throws CertificateException
94 Provider[] p = Security.getProviders ();
96 for (int i = 0; i < p.length; i++)
98 String classname = p[i].getProperty ("CertificateFactory." + type);
99 if (classname != null)
100 return getInstance (classname, type, p[i]);
103 throw new CertificateException(type);
109 Gets an instance of the CertificateFactory class representing
110 the specified certificate factory from the specified provider.
111 If the type is not found then, it throws CertificateException.
112 If the provider is not found, then it throws
113 NoSuchProviderException.
115 @param type the type of certificate to choose
117 @return a CertificateFactory repesenting the desired type
119 @throws CertificateException if the type of certificate is not implemented by providers
120 @throws NoSuchProviderException if the provider is not found
122 public static final CertificateFactory getInstance(String type, String provider)
123 throws CertificateException, NoSuchProviderException
125 Provider p = Security.getProvider(provider);
127 throw new NoSuchProviderException();
129 return getInstance (p.getProperty ("CertificateFactory." + type),
133 private static CertificateFactory getInstance (String classname,
136 throws CertificateException
139 return new CertificateFactory( (CertificateFactorySpi)Class.forName( classname ).newInstance(), provider, type );
140 } catch( ClassNotFoundException cnfe) {
141 throw new CertificateException("Class not found");
142 } catch( InstantiationException ie) {
143 throw new CertificateException("Class instantiation failed");
144 } catch( IllegalAccessException iae) {
145 throw new CertificateException("Illegal Access");
151 Gets the provider that the class is from.
153 @return the provider of this class
155 public final Provider getProvider()
161 Returns the type of the certificate supported
163 @return A string with the type of certificate
165 public final String getType()
171 Generates a Certificate based on the encoded data read
172 from the InputStream.
174 The input stream must contain only one certificate.
176 If there exists a specialized certificate class for the
177 certificate format handled by the certificate factory
178 then the return Ceritificate should be a typecast of it.
179 Ex: A X.509 CertificateFactory should return X509Certificate.
181 For X.509 certificates, the certificate in inStream must be
182 DER encoded and supplied in binary or printable (Base64)
183 encoding. If the certificate is in Base64 encoding, it must be
184 bounded by -----BEGINCERTIFICATE-----, and
185 -----END CERTIFICATE-----.
187 @param inStream an input stream containing the certificate data
189 @return a certificate initialized with InputStream data.
191 @throws CertificateException Certificate parsing error
193 public final Certificate generateCertificate(InputStream inStream)
194 throws CertificateException
196 return certFacSpi.engineGenerateCertificate( inStream );
200 Returns a collection of certificates that were read from the
201 input stream. It may be empty, have only one, or have
202 multiple certificates.
204 For a X.509 certificate factory, the stream may contain a
205 single DER encoded certificate or a PKCS#7 certificate
206 chain. This is a PKCS#7 <I>SignedData</I> object with the
207 most significant field being <I>certificates</I>. If no
208 CRLs are present, then an empty collection is returned.
210 @param inStream an input stream containing the certificates
212 @return a collection of certificates initialized with
213 the InputStream data.
215 @throws CertificateException Certificate parsing error
217 public final Collection generateCertificates(InputStream inStream)
218 throws CertificateException
220 return certFacSpi.engineGenerateCertificates( inStream );
224 Generates a CRL based on the encoded data read
225 from the InputStream.
227 The input stream must contain only one CRL.
229 If there exists a specialized CRL class for the
230 CRL format handled by the certificate factory
231 then the return CRL should be a typecast of it.
232 Ex: A X.509 CertificateFactory should return X509CRL.
234 @param inStream an input stream containing the CRL data
236 @return a CRL initialized with InputStream data.
238 @throws CRLException CRL parsing error
240 public final CRL generateCRL(InputStream inStream)
243 return certFacSpi.engineGenerateCRL( inStream );
248 Generates CRLs based on the encoded data read
249 from the InputStream.
251 For a X.509 certificate factory, the stream may contain a
252 single DER encoded CRL or a PKCS#7 CRL set. This is a
253 PKCS#7 <I>SignedData</I> object with the most significant
254 field being <I>crls</I>. If no CRLs are present, then an
255 empty collection is returned.
257 @param inStream an input stream containing the CRLs
259 @return a collection of CRLs initialized with
260 the InputStream data.
262 @throws CRLException CRL parsing error
264 public final Collection generateCRLs(InputStream inStream)
267 return certFacSpi.engineGenerateCRLs( inStream );