2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / security / cert / CertificateFactory.java
blobe60695491580e478da7bf1fe49698d4086a7543c
1 /* CertificateFactory.java -- Certificate Factory Class
2 Copyright (C) 1999, 2002, 2003 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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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;
41 import java.security.NoSuchAlgorithmException;
42 import java.security.NoSuchProviderException;
43 import java.security.Provider;
44 import java.security.Security;
46 import java.io.InputStream;
48 import java.util.Collection;
49 import java.util.Iterator;
50 import java.util.List;
52 import gnu.java.security.Engine;
54 /**
55 * This class implements the CertificateFactory class interface used to
56 * generate certificates, certificate revocation lists (CRLs), and certificate
57 * paths objects from their encoded forms.
59 * @author Mark Benvenuto
60 * @author Casey Marshall
61 * @since JDK 1.2
62 * @status Fully compatible with JDK 1.4.
64 public class CertificateFactory
67 /** The service name for certificate factories. */
68 private static final String CERTIFICATE_FACTORY = "CertificateFactory";
70 private CertificateFactorySpi certFacSpi;
71 private Provider provider;
72 private String type;
74 /**
75 * Creates an instance of CertificateFactory.
77 * @param certFacSpi The underlying CertificateFactory engine.
78 * @param provider The provider of this implementation.
79 * @param type The type of Certificate this factory creates.
81 protected CertificateFactory(CertificateFactorySpi certFacSpi,
82 Provider provider, String type)
84 this.certFacSpi = certFacSpi;
85 this.provider = provider;
86 this.type = type;
89 \f// Class methods.
90 // ------------------------------------------------------------------------
92 /**
93 * Gets an instance of the CertificateFactory class representing
94 * the specified certificate factory. If the type is not
95 * found then, it throws CertificateException.
97 * @param type The type of certificate factory to create.
98 * @return a CertificateFactory repesenting the desired type
99 * @throws CertificateException If the type of certificate is not
100 * implemented by any installed provider.
102 public static final CertificateFactory getInstance(String type)
103 throws CertificateException
105 Provider[] p = Security.getProviders();
107 for (int i = 0; i < p.length; i++)
111 return getInstance(type, p[i]);
113 catch (CertificateException ignored)
118 throw new CertificateException(type);
121 /**
122 * Gets an instance of the CertificateFactory class representing
123 * the specified certificate factory from the specified provider.
124 * If the type is not found then, it throws {@link CertificateException}.
125 * If the provider is not found, then it throws
126 * {@link java.security.NoSuchProviderException}.
128 * @param type The type of certificate factory to create.
129 * @param provider The name of the provider from which to get the
130 * implementation.
131 * @return A CertificateFactory for the desired type.
132 * @throws CertificateException If the type of certificate is not
133 * implemented by the named provider.
134 * @throws NoSuchProviderException If the named provider is not installed.
136 public static final CertificateFactory getInstance(String type,
137 String provider)
138 throws CertificateException, NoSuchProviderException
140 Provider p = Security.getProvider(provider);
141 if( p == null)
142 throw new NoSuchProviderException();
144 return getInstance(type, p);
148 * Get a certificate factory for the given certificate type from the
149 * given provider.
151 * @param type The type of certificate factory to create.
152 * @param provider The provider from which to get the implementation.
153 * @return A CertificateFactory for the desired type.
154 * @throws CertificateException If the type of certificate is not
155 * implemented by the provider.
156 * @throws IllegalArgumentException If the provider is null.
158 public static final CertificateFactory getInstance(String type,
159 Provider provider)
160 throws CertificateException
162 if (provider == null)
163 throw new IllegalArgumentException("null provider");
167 return new CertificateFactory((CertificateFactorySpi)
168 Engine.getInstance(CERTIFICATE_FACTORY, type, provider),
169 provider, type);
171 catch (ClassCastException cce)
173 throw new CertificateException(type);
175 catch (java.lang.reflect.InvocationTargetException ite)
177 throw new CertificateException(type);
179 catch (NoSuchAlgorithmException nsae)
181 throw new CertificateException(nsae.getMessage());
185 \f// Instance methods.
186 // ------------------------------------------------------------------------
189 * Gets the provider of this implementation.
191 * @return The provider of this implementation.
193 public final Provider getProvider()
195 return provider;
199 * Returns the type of the certificate this factory creates.
201 * @return A string with the type of certificate
203 public final String getType()
205 return type;
209 * Generates a Certificate from the encoded data read
210 * from an InputStream.
212 * <p>The input stream must contain only one certificate.
214 * <p>If there exists a specialized certificate class for the
215 * certificate format handled by the certificate factory
216 * then the return Ceritificate should be a typecast of it.
217 * Ex: A X.509 CertificateFactory should return X509Certificate.
219 * <p>For X.509 certificates, the certificate in inStream must be
220 * DER encoded and supplied in binary or printable (Base64)
221 * encoding. If the certificate is in Base64 encoding, it must be
222 * bounded by -----BEGINCERTIFICATE-----, and
223 * -----END CERTIFICATE-----.
225 * @param inStream An input stream containing the certificate data.
226 * @return A certificate initialized from the decoded InputStream data.
227 * @throws CertificateException If an error occurs decoding the
228 * certificate.
230 public final Certificate generateCertificate(InputStream inStream)
231 throws CertificateException
233 return certFacSpi.engineGenerateCertificate(inStream);
237 * Returns a collection of certificates that were read from the
238 * input stream. It may be empty, have only one, or have
239 * multiple certificates.
241 * For a X.509 certificate factory, the stream may contain a
242 * single DER encoded certificate or a PKCS#7 certificate
243 * chain. This is a PKCS#7 <I>SignedData</I> object with the
244 * most significant field being <I>certificates</I>. If no
245 * CRLs are present, then an empty collection is returned.
247 * @param inStream An input stream containing the certificate data.
248 * @return A collection of certificates initialized from the decoded
249 * InputStream data.
250 * @throws CertificateException If an error occurs decoding the
251 * certificates.
253 public final Collection generateCertificates(InputStream inStream)
254 throws CertificateException
256 return certFacSpi.engineGenerateCertificates(inStream);
260 * Generates a CRL based on the encoded data read
261 * from the InputStream.
263 * <p>The input stream must contain only one CRL.
265 * <p>If there exists a specialized CRL class for the
266 * CRL format handled by the certificate factory
267 * then the return CRL should be a typecast of it.
268 * Ex: A X.509 CertificateFactory should return X509CRL.
270 * @param inStream An input stream containing the CRL data.
271 * @return A CRL initialized from the decoded InputStream data.
272 * @throws CRLException If an error occurs decoding the CRL.
274 public final CRL generateCRL(InputStream inStream)
275 throws CRLException
277 return certFacSpi.engineGenerateCRL(inStream);
281 * <p>Generates CRLs based on the encoded data read
282 * from the InputStream.
284 * <p>For a X.509 certificate factory, the stream may contain a
285 * single DER encoded CRL or a PKCS#7 CRL set. This is a
286 * PKCS#7 <I>SignedData</I> object with the most significant
287 * field being <I>crls</I>. If no CRLs are present, then an
288 * empty collection is returned.
290 * @param inStream an input stream containing the CRLs.
291 * @return a collection of CRLs initialized from the decoded
292 * InputStream data.
293 * @throws CRLException If an error occurs decoding the CRLs.
295 public final Collection generateCRLs(InputStream inStream)
296 throws CRLException
298 return certFacSpi.engineGenerateCRLs( inStream );
302 * Generate a {@link CertPath} and initialize it with data parsed from
303 * the input stream. The default encoding of this factory is used.
305 * @param inStream The InputStream containing the CertPath data.
306 * @return A CertPath initialized from the input stream data.
307 * @throws CertificateException If an error occurs decoding the
308 * CertPath.
310 public final CertPath generateCertPath(InputStream inStream)
311 throws CertificateException
313 return certFacSpi.engineGenerateCertPath(inStream);
317 * Generate a {@link CertPath} and initialize it with data parsed from
318 * the input stream, using the specified encoding.
320 * @param inStream The InputStream containing the CertPath data.
321 * @param encoding The encoding of the InputStream data.
322 * @return A CertPath initialized from the input stream data.
323 * @throws CertificateException If an error occurs decoding the
324 * CertPath.
326 public final CertPath generateCertPath(InputStream inStream, String encoding)
327 throws CertificateException
329 return certFacSpi.engineGenerateCertPath(inStream, encoding);
333 * Generate a {@link CertPath} and initialize it with the certificates
334 * in the {@link java.util.List} argument.
336 * @param certificates The list of certificates with which to create
337 * the CertPath.
338 * @return A CertPath initialized from the certificates.
339 * @throws CertificateException If an error occurs generating the
340 * CertPath.
342 public final CertPath generateCertPath(List certificates)
343 throws CertificateException
345 return certFacSpi.engineGenerateCertPath(certificates);
349 * Returns an Iterator of CertPath encodings supported by this
350 * factory, with the default encoding first. The returned Iterator
351 * cannot be modified.
353 * @return The Iterator of supported encodings.
355 public final Iterator getCertPathEncodings()
357 return certFacSpi.engineGetCertPathEncodings();
359 } // class CertificateFactory