OSDN Git Service

b5fe2e3518ed6d906185f41da5e24a12da09892e
[pf3gnuchains/gcc-fork.git] / libjava / java / security / cert / CertificateFactory.java
1 /* CertificateFactory.java --- Certificate Factory Class
2    Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10  
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.
15
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.
20
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. */
26
27
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;
34
35 /**
36    This class implments the CertificateFactory class interface
37    used to generate certificates and certificate revocation
38    list (CRL) objects from their encodings.
39    
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. 
43    
44    @since JDK 1.2
45
46    @author Mark Benvenuto
47 */
48 public class CertificateFactory
49 {
50
51   private CertificateFactorySpi certFacSpi;
52   private Provider provider;
53   private String type;
54
55   /**
56      Creates an instance of CertificateFactory
57
58      @param certFacSpi A CertificateFactory engine to use
59      @param provider A provider to use
60      @param type The type of Certificate
61   */
62   protected CertificateFactory(CertificateFactorySpi certFacSpi, Provider provider, String type)
63   {
64     this.certFacSpi = certFacSpi;
65     this.provider = provider;
66     this.type = type;
67   }
68
69
70   /** 
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.
74
75       @param type the type of certificate to choose
76
77       @return a CertificateFactory repesenting the desired type
78
79       @throws CertificateException if the type of certificate is not implemented by providers
80   */
81   public static final CertificateFactory getInstance(String type) throws CertificateException
82   {
83     Provider[] p = Security.getProviders ();
84
85     for (int i = 0; i < p.length; i++)
86       {
87         String classname = p[i].getProperty ("CertificateFactory." + type);
88         if (classname != null)
89           return getInstance (classname, type, p[i]);
90       }
91
92     throw new CertificateException(type);
93   }
94
95
96
97   /** 
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.
103
104       @param type the type of certificate to choose
105
106       @return a CertificateFactory repesenting the desired type
107
108       @throws CertificateException if the type of certificate is not implemented by providers
109       @throws NoSuchProviderException if the provider is not found
110   */
111   public static final CertificateFactory getInstance(String type, String provider) 
112     throws CertificateException, NoSuchProviderException
113   {
114     Provider p = Security.getProvider(provider);
115     if( p == null)
116       throw new NoSuchProviderException();
117
118     return getInstance (p.getProperty ("CertificateFactory." + type),
119                         type, p);
120   }
121
122   private static CertificateFactory getInstance (String classname,
123                                                  String type,
124                                                  Provider provider)
125     throws CertificateException
126   {
127     try {
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");
135     }
136   }
137
138
139   /**
140      Gets the provider that the class is from.
141
142      @return the provider of this class
143   */
144   public final Provider getProvider()
145   {
146     return provider;
147   }
148
149   /**
150      Returns the type of the certificate supported
151
152      @return A string with the type of certificate
153   */
154   public final String getType()
155   {
156     return type;
157   }
158
159   /**
160      Generates a Certificate based on the encoded data read
161      from the InputStream.
162
163      The input stream must contain only one certificate.
164
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.
169
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-----. 
175
176      @param inStream an input stream containing the certificate data
177
178      @return a certificate initialized with InputStream data.
179
180      @throws CertificateException Certificate parsing error
181   */
182   public final Certificate generateCertificate(InputStream inStream)
183     throws CertificateException
184   {
185     return certFacSpi.engineGenerateCertificate( inStream );
186   }
187
188   /**
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.
192
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.
198         
199      @param inStream an input stream containing the certificates
200
201      @return a collection of certificates initialized with 
202      the InputStream data.
203
204      @throws CertificateException Certificate parsing error
205   */
206   public final Collection generateCertificates(InputStream inStream)
207     throws CertificateException
208   {
209     return certFacSpi.engineGenerateCertificates( inStream );
210   }
211
212   /**
213      Generates a CRL based on the encoded data read
214      from the InputStream.
215
216      The input stream must contain only one CRL.
217
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.
222
223      @param inStream an input stream containing the CRL data
224
225      @return a CRL initialized with InputStream data.
226
227      @throws CRLException CRL parsing error
228   */
229   public final CRL generateCRL(InputStream inStream)
230     throws CRLException
231   {
232     return certFacSpi.engineGenerateCRL( inStream );
233   }
234
235
236   /**
237      Generates CRLs based on the encoded data read
238      from the InputStream.
239
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.
245
246      @param inStream an input stream containing the CRLs
247
248      @return a collection of CRLs initialized with 
249      the InputStream data.
250
251      @throws CRLException CRL parsing error
252   */
253   public final Collection generateCRLs(InputStream inStream)
254     throws CRLException
255   {
256     return certFacSpi.engineGenerateCRLs( inStream );
257   }
258
259 }