OSDN Git Service

Add license clarification.
[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 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.
25
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. */
37
38
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;
45
46 /**
47    This class implments the CertificateFactory class interface
48    used to generate certificates and certificate revocation
49    list (CRL) objects from their encodings.
50    
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. 
54    
55    @since JDK 1.2
56
57    @author Mark Benvenuto
58 */
59 public class CertificateFactory
60 {
61
62   private CertificateFactorySpi certFacSpi;
63   private Provider provider;
64   private String type;
65
66   /**
67      Creates an instance of CertificateFactory
68
69      @param certFacSpi A CertificateFactory engine to use
70      @param provider A provider to use
71      @param type The type of Certificate
72   */
73   protected CertificateFactory(CertificateFactorySpi certFacSpi, Provider provider, String type)
74   {
75     this.certFacSpi = certFacSpi;
76     this.provider = provider;
77     this.type = type;
78   }
79
80
81   /** 
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.
85
86       @param type the type of certificate to choose
87
88       @return a CertificateFactory repesenting the desired type
89
90       @throws CertificateException if the type of certificate is not implemented by providers
91   */
92   public static final CertificateFactory getInstance(String type) throws CertificateException
93   {
94     Provider[] p = Security.getProviders ();
95
96     for (int i = 0; i < p.length; i++)
97       {
98         String classname = p[i].getProperty ("CertificateFactory." + type);
99         if (classname != null)
100           return getInstance (classname, type, p[i]);
101       }
102
103     throw new CertificateException(type);
104   }
105
106
107
108   /** 
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.
114
115       @param type the type of certificate to choose
116
117       @return a CertificateFactory repesenting the desired type
118
119       @throws CertificateException if the type of certificate is not implemented by providers
120       @throws NoSuchProviderException if the provider is not found
121   */
122   public static final CertificateFactory getInstance(String type, String provider) 
123     throws CertificateException, NoSuchProviderException
124   {
125     Provider p = Security.getProvider(provider);
126     if( p == null)
127       throw new NoSuchProviderException();
128
129     return getInstance (p.getProperty ("CertificateFactory." + type),
130                         type, p);
131   }
132
133   private static CertificateFactory getInstance (String classname,
134                                                  String type,
135                                                  Provider provider)
136     throws CertificateException
137   {
138     try {
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");
146     }
147   }
148
149
150   /**
151      Gets the provider that the class is from.
152
153      @return the provider of this class
154   */
155   public final Provider getProvider()
156   {
157     return provider;
158   }
159
160   /**
161      Returns the type of the certificate supported
162
163      @return A string with the type of certificate
164   */
165   public final String getType()
166   {
167     return type;
168   }
169
170   /**
171      Generates a Certificate based on the encoded data read
172      from the InputStream.
173
174      The input stream must contain only one certificate.
175
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.
180
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-----. 
186
187      @param inStream an input stream containing the certificate data
188
189      @return a certificate initialized with InputStream data.
190
191      @throws CertificateException Certificate parsing error
192   */
193   public final Certificate generateCertificate(InputStream inStream)
194     throws CertificateException
195   {
196     return certFacSpi.engineGenerateCertificate( inStream );
197   }
198
199   /**
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.
203
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.
209         
210      @param inStream an input stream containing the certificates
211
212      @return a collection of certificates initialized with 
213      the InputStream data.
214
215      @throws CertificateException Certificate parsing error
216   */
217   public final Collection generateCertificates(InputStream inStream)
218     throws CertificateException
219   {
220     return certFacSpi.engineGenerateCertificates( inStream );
221   }
222
223   /**
224      Generates a CRL based on the encoded data read
225      from the InputStream.
226
227      The input stream must contain only one CRL.
228
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.
233
234      @param inStream an input stream containing the CRL data
235
236      @return a CRL initialized with InputStream data.
237
238      @throws CRLException CRL parsing error
239   */
240   public final CRL generateCRL(InputStream inStream)
241     throws CRLException
242   {
243     return certFacSpi.engineGenerateCRL( inStream );
244   }
245
246
247   /**
248      Generates CRLs based on the encoded data read
249      from the InputStream.
250
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.
256
257      @param inStream an input stream containing the CRLs
258
259      @return a collection of CRLs initialized with 
260      the InputStream data.
261
262      @throws CRLException CRL parsing error
263   */
264   public final Collection generateCRLs(InputStream inStream)
265     throws CRLException
266   {
267     return certFacSpi.engineGenerateCRLs( inStream );
268   }
269
270 }