1 |
/* CertificateFactory.java -- Certificate Factory Class |
/* CertificateFactory.java -- Certificate Factory Class |
2 |
Copyright (C) 1999, 2002 Free Software Foundation, Inc. |
Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
37 |
|
|
38 |
|
|
39 |
package java.security.cert; |
package java.security.cert; |
40 |
|
|
41 |
|
import java.security.NoSuchAlgorithmException; |
42 |
import java.security.NoSuchProviderException; |
import java.security.NoSuchProviderException; |
43 |
import java.security.Provider; |
import java.security.Provider; |
44 |
import java.security.Security; |
import java.security.Security; |
45 |
|
|
46 |
import java.io.InputStream; |
import java.io.InputStream; |
47 |
|
|
48 |
import java.util.Collection; |
import java.util.Collection; |
49 |
|
import java.util.Iterator; |
50 |
|
import java.util.List; |
51 |
|
|
52 |
|
import gnu.java.security.Engine; |
53 |
|
|
54 |
/** |
/** |
55 |
This class implments the CertificateFactory class interface |
* This class implments the CertificateFactory class interface used to |
56 |
used to generate certificates and certificate revocation |
* generate certificates, certificate revocation lists (CRLs), and certificate |
57 |
list (CRL) objects from their encodings. |
* paths objects from their encoded forms. |
58 |
|
* |
59 |
A certifcate factory for X.509 returns certificates of the |
* @author Mark Benvenuto |
60 |
java.security.cert.X509Certificate class, and CRLs of the |
* @author Casey Marshall |
61 |
java.security.cert.X509CRL class. |
* @since JDK 1.2 |
62 |
|
* @status Fully compatible with JDK 1.4. |
63 |
@author Mark Benvenuto |
*/ |
|
@since JDK 1.2 |
|
|
@status still missing full 1.4 support |
|
|
*/ |
|
64 |
public class CertificateFactory |
public class CertificateFactory |
65 |
{ |
{ |
66 |
|
|
67 |
|
/** The service name for certificate factories. */ |
68 |
|
private static final String CERTIFICATE_FACTORY = "CertificateFactory"; |
69 |
|
|
70 |
private CertificateFactorySpi certFacSpi; |
private CertificateFactorySpi certFacSpi; |
71 |
private Provider provider; |
private Provider provider; |
72 |
private String type; |
private String type; |
73 |
|
|
74 |
/** |
/** |
75 |
Creates an instance of CertificateFactory |
* Creates an instance of CertificateFactory. |
76 |
|
* |
77 |
@param certFacSpi A CertificateFactory engine to use |
* @param certFacSpi The underlying CertificateFactory engine. |
78 |
@param provider A provider to use |
* @param provider The provider of this implementation. |
79 |
@param type The type of Certificate |
* @param type The type of Certificate this factory creates. |
80 |
*/ |
*/ |
81 |
protected CertificateFactory(CertificateFactorySpi certFacSpi, Provider provider, String type) |
protected CertificateFactory(CertificateFactorySpi certFacSpi, |
82 |
|
Provider provider, String type) |
83 |
{ |
{ |
84 |
this.certFacSpi = certFacSpi; |
this.certFacSpi = certFacSpi; |
85 |
this.provider = provider; |
this.provider = provider; |
86 |
this.type = type; |
this.type = type; |
87 |
} |
} |
88 |
|
|
89 |
|
// Class methods. |
90 |
|
// ------------------------------------------------------------------------ |
91 |
|
|
92 |
/** |
/** |
93 |
Gets an instance of the CertificateFactory class representing |
* Gets an instance of the CertificateFactory class representing |
94 |
the specified certificate factory. If the type is not |
* the specified certificate factory. If the type is not |
95 |
found then, it throws CertificateException. |
* found then, it throws CertificateException. |
96 |
|
* |
97 |
@param type the type of certificate to choose |
* @param type The type of certificate factory to create. |
98 |
|
* @return a CertificateFactory repesenting the desired type |
99 |
@return a CertificateFactory repesenting the desired type |
* @throws CertificateException If the type of certificate is not |
100 |
|
* implemented by any installed provider. |
101 |
@throws CertificateException if the type of certificate is not implemented by providers |
*/ |
102 |
*/ |
public static final CertificateFactory getInstance(String type) |
103 |
public static final CertificateFactory getInstance(String type) throws CertificateException |
throws CertificateException |
104 |
{ |
{ |
105 |
Provider[] p = Security.getProviders (); |
Provider[] p = Security.getProviders(); |
106 |
|
|
107 |
for (int i = 0; i < p.length; i++) |
for (int i = 0; i < p.length; i++) |
108 |
{ |
{ |
109 |
String classname = p[i].getProperty ("CertificateFactory." + type); |
try |
110 |
if (classname != null) |
{ |
111 |
return getInstance (classname, type, p[i]); |
return getInstance(type, p[i]); |
112 |
|
} |
113 |
|
catch (CertificateException ignored) |
114 |
|
{ |
115 |
|
} |
116 |
} |
} |
117 |
|
|
118 |
throw new CertificateException(type); |
throw new CertificateException(type); |
119 |
} |
} |
120 |
|
|
|
|
|
|
|
|
121 |
/** |
/** |
122 |
Gets an instance of the CertificateFactory class representing |
* Gets an instance of the CertificateFactory class representing |
123 |
the specified certificate factory from the specified provider. |
* the specified certificate factory from the specified provider. |
124 |
If the type is not found then, it throws CertificateException. |
* If the type is not found then, it throws {@link CertificateException}. |
125 |
If the provider is not found, then it throws |
* If the provider is not found, then it throws |
126 |
NoSuchProviderException. |
* {@link java.security.NoSuchProviderException}. |
127 |
|
* |
128 |
@param type the type of certificate to choose |
* @param type The type of certificate factory to create. |
129 |
|
* @param provider The name of the provider from which to get the |
130 |
@return a CertificateFactory repesenting the desired type |
* implementation. |
131 |
|
* @return A CertificateFactory for the desired type. |
132 |
@throws CertificateException if the type of certificate is not implemented by providers |
* @throws CertificateException If the type of certificate is not |
133 |
@throws NoSuchProviderException if the provider is not found |
* implemented by the named provider. |
134 |
*/ |
* @throws NoSuchProviderException If the named provider is not installed. |
135 |
public static final CertificateFactory getInstance(String type, String provider) |
*/ |
136 |
|
public static final CertificateFactory getInstance(String type, |
137 |
|
String provider) |
138 |
throws CertificateException, NoSuchProviderException |
throws CertificateException, NoSuchProviderException |
139 |
{ |
{ |
140 |
Provider p = Security.getProvider(provider); |
Provider p = Security.getProvider(provider); |
141 |
if( p == null) |
if( p == null) |
142 |
throw new NoSuchProviderException(); |
throw new NoSuchProviderException(); |
143 |
|
|
144 |
return getInstance (p.getProperty ("CertificateFactory." + type), |
return getInstance(type, p); |
|
type, p); |
|
145 |
} |
} |
146 |
|
|
147 |
private static CertificateFactory getInstance (String classname, |
/** |
148 |
String type, |
* Get a certificate factory for the given certificate type from the |
149 |
Provider provider) |
* given provider. |
150 |
|
* |
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. |
157 |
|
*/ |
158 |
|
public static final CertificateFactory getInstance(String type, |
159 |
|
Provider provider) |
160 |
throws CertificateException |
throws CertificateException |
161 |
{ |
{ |
162 |
try { |
if (provider == null) |
163 |
return new CertificateFactory( (CertificateFactorySpi)Class.forName( classname ).newInstance(), provider, type ); |
throw new IllegalArgumentException("null provider"); |
164 |
} catch( ClassNotFoundException cnfe) { |
|
165 |
throw new CertificateException("Class not found"); |
try |
166 |
} catch( InstantiationException ie) { |
{ |
167 |
throw new CertificateException("Class instantiation failed"); |
return new CertificateFactory((CertificateFactorySpi) |
168 |
} catch( IllegalAccessException iae) { |
Engine.getInstance(CERTIFICATE_FACTORY, type, provider), |
169 |
throw new CertificateException("Illegal Access"); |
provider, type); |
170 |
} |
} |
171 |
|
catch (ClassCastException cce) |
172 |
|
{ |
173 |
|
throw new CertificateException(type); |
174 |
|
} |
175 |
|
catch (java.lang.reflect.InvocationTargetException ite) |
176 |
|
{ |
177 |
|
throw new CertificateException(type); |
178 |
|
} |
179 |
|
catch (NoSuchAlgorithmException nsae) |
180 |
|
{ |
181 |
|
throw new CertificateException(nsae.getMessage()); |
182 |
|
} |
183 |
} |
} |
184 |
|
|
185 |
|
// Instance methods. |
186 |
|
// ------------------------------------------------------------------------ |
187 |
|
|
188 |
/** |
/** |
189 |
Gets the provider that the class is from. |
* Gets the provider of this implementation. |
190 |
|
* |
191 |
@return the provider of this class |
* @return The provider of this implementation. |
192 |
*/ |
*/ |
193 |
public final Provider getProvider() |
public final Provider getProvider() |
194 |
{ |
{ |
195 |
return provider; |
return provider; |
196 |
} |
} |
197 |
|
|
198 |
/** |
/** |
199 |
Returns the type of the certificate supported |
* Returns the type of the certificate this factory creates. |
200 |
|
* |
201 |
@return A string with the type of certificate |
* @return A string with the type of certificate |
202 |
*/ |
*/ |
203 |
public final String getType() |
public final String getType() |
204 |
{ |
{ |
205 |
return type; |
return type; |
206 |
} |
} |
207 |
|
|
208 |
/** |
/** |
209 |
Generates a Certificate based on the encoded data read |
* Generates a Certificate from the encoded data read |
210 |
from the InputStream. |
* from an InputStream. |
211 |
|
* |
212 |
The input stream must contain only one certificate. |
* <p>The input stream must contain only one certificate. |
213 |
|
* |
214 |
If there exists a specialized certificate class for the |
* <p>If there exists a specialized certificate class for the |
215 |
certificate format handled by the certificate factory |
* certificate format handled by the certificate factory |
216 |
then the return Ceritificate should be a typecast of it. |
* then the return Ceritificate should be a typecast of it. |
217 |
Ex: A X.509 CertificateFactory should return X509Certificate. |
* Ex: A X.509 CertificateFactory should return X509Certificate. |
218 |
|
* |
219 |
For X.509 certificates, the certificate in inStream must be |
* <p>For X.509 certificates, the certificate in inStream must be |
220 |
DER encoded and supplied in binary or printable (Base64) |
* DER encoded and supplied in binary or printable (Base64) |
221 |
encoding. If the certificate is in Base64 encoding, it must be |
* encoding. If the certificate is in Base64 encoding, it must be |
222 |
bounded by -----BEGINCERTIFICATE-----, and |
* bounded by -----BEGINCERTIFICATE-----, and |
223 |
-----END CERTIFICATE-----. |
* -----END CERTIFICATE-----. |
224 |
|
* |
225 |
@param inStream an input stream containing the certificate data |
* @param inStream An input stream containing the certificate data. |
226 |
|
* @return A certificate initialized from the decoded InputStream data. |
227 |
@return a certificate initialized with InputStream data. |
* @throws CertificateException If an error occurs decoding the |
228 |
|
* certificate. |
229 |
@throws CertificateException Certificate parsing error |
*/ |
|
*/ |
|
230 |
public final Certificate generateCertificate(InputStream inStream) |
public final Certificate generateCertificate(InputStream inStream) |
231 |
throws CertificateException |
throws CertificateException |
232 |
{ |
{ |
233 |
return certFacSpi.engineGenerateCertificate( inStream ); |
return certFacSpi.engineGenerateCertificate(inStream); |
234 |
} |
} |
235 |
|
|
236 |
/** |
/** |
237 |
Returns a collection of certificates that were read from the |
* Returns a collection of certificates that were read from the |
238 |
input stream. It may be empty, have only one, or have |
* input stream. It may be empty, have only one, or have |
239 |
multiple certificates. |
* multiple certificates. |
240 |
|
* |
241 |
For a X.509 certificate factory, the stream may contain a |
* For a X.509 certificate factory, the stream may contain a |
242 |
single DER encoded certificate or a PKCS#7 certificate |
* single DER encoded certificate or a PKCS#7 certificate |
243 |
chain. This is a PKCS#7 <I>SignedData</I> object with the |
* chain. This is a PKCS#7 <I>SignedData</I> object with the |
244 |
most significant field being <I>certificates</I>. If no |
* most significant field being <I>certificates</I>. If no |
245 |
CRLs are present, then an empty collection is returned. |
* CRLs are present, then an empty collection is returned. |
246 |
|
* |
247 |
@param inStream an input stream containing the certificates |
* @param inStream An input stream containing the certificate data. |
248 |
|
* @return A collection of certificates initialized from the decoded |
249 |
@return a collection of certificates initialized with |
* InputStream data. |
250 |
the InputStream data. |
* @throws CertificateException If an error occurs decoding the |
251 |
|
* certificates. |
252 |
@throws CertificateException Certificate parsing error |
*/ |
|
*/ |
|
253 |
public final Collection generateCertificates(InputStream inStream) |
public final Collection generateCertificates(InputStream inStream) |
254 |
throws CertificateException |
throws CertificateException |
255 |
{ |
{ |
256 |
return certFacSpi.engineGenerateCertificates( inStream ); |
return certFacSpi.engineGenerateCertificates(inStream); |
257 |
} |
} |
258 |
|
|
259 |
/** |
/** |
260 |
Generates a CRL based on the encoded data read |
* Generates a CRL based on the encoded data read |
261 |
from the InputStream. |
* from the InputStream. |
262 |
|
* |
263 |
The input stream must contain only one CRL. |
* <p>The input stream must contain only one CRL. |
264 |
|
* |
265 |
If there exists a specialized CRL class for the |
* <p>If there exists a specialized CRL class for the |
266 |
CRL format handled by the certificate factory |
* CRL format handled by the certificate factory |
267 |
then the return CRL should be a typecast of it. |
* then the return CRL should be a typecast of it. |
268 |
Ex: A X.509 CertificateFactory should return X509CRL. |
* Ex: A X.509 CertificateFactory should return X509CRL. |
269 |
|
* |
270 |
@param inStream an input stream containing the CRL data |
* @param inStream An input stream containing the CRL data. |
271 |
|
* @return A CRL initialized from the decoded InputStream data. |
272 |
@return a CRL initialized with InputStream data. |
* @throws CRLException If an error occurs decoding the CRL. |
273 |
|
*/ |
|
@throws CRLException CRL parsing error |
|
|
*/ |
|
274 |
public final CRL generateCRL(InputStream inStream) |
public final CRL generateCRL(InputStream inStream) |
275 |
throws CRLException |
throws CRLException |
276 |
{ |
{ |
277 |
return certFacSpi.engineGenerateCRL( inStream ); |
return certFacSpi.engineGenerateCRL(inStream); |
278 |
} |
} |
279 |
|
|
|
|
|
280 |
/** |
/** |
281 |
Generates CRLs based on the encoded data read |
* <p>Generates CRLs based on the encoded data read |
282 |
from the InputStream. |
* from the InputStream. |
283 |
|
* |
284 |
For a X.509 certificate factory, the stream may contain a |
* <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 |
* single DER encoded CRL or a PKCS#7 CRL set. This is a |
286 |
PKCS#7 <I>SignedData</I> object with the most significant |
* PKCS#7 <I>SignedData</I> object with the most significant |
287 |
field being <I>crls</I>. If no CRLs are present, then an |
* field being <I>crls</I>. If no CRLs are present, then an |
288 |
empty collection is returned. |
* empty collection is returned. |
289 |
|
* |
290 |
@param inStream an input stream containing the CRLs |
* @param inStream an input stream containing the CRLs. |
291 |
|
* @return a collection of CRLs initialized from the decoded |
292 |
@return a collection of CRLs initialized with |
* InputStream data. |
293 |
the InputStream data. |
* @throws CRLException If an error occurs decoding the CRLs. |
294 |
|
*/ |
|
@throws CRLException CRL parsing error |
|
|
*/ |
|
295 |
public final Collection generateCRLs(InputStream inStream) |
public final Collection generateCRLs(InputStream inStream) |
296 |
throws CRLException |
throws CRLException |
297 |
{ |
{ |
298 |
return certFacSpi.engineGenerateCRLs( inStream ); |
return certFacSpi.engineGenerateCRLs( inStream ); |
299 |
} |
} |
300 |
|
|
301 |
|
/** |
302 |
|
* Generate a {@link CertPath} and initialize it with data parsed from |
303 |
|
* the input stream. The default encoding of this factory is used. |
304 |
|
* |
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. |
309 |
|
*/ |
310 |
public final CertPath generateCertPath(InputStream inStream) |
public final CertPath generateCertPath(InputStream inStream) |
311 |
throws CertificateException |
throws CertificateException |
312 |
{ |
{ |
313 |
throw new CertificateException("not implemented"); |
return certFacSpi.engineGenerateCertPath(inStream); |
314 |
|
} |
315 |
|
|
316 |
|
/** |
317 |
|
* Generate a {@link CertPath} and initialize it with data parsed from |
318 |
|
* the input stream, using the specified encoding. |
319 |
|
* |
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. |
325 |
|
*/ |
326 |
|
public final CertPath generateCertPath(InputStream inStream, String encoding) |
327 |
|
throws CertificateException |
328 |
|
{ |
329 |
|
return certFacSpi.engineGenerateCertPath(inStream, encoding); |
330 |
|
} |
331 |
|
|
332 |
|
/** |
333 |
|
* Generate a {@link CertPath} and initialize it with the certificates |
334 |
|
* in the {@link java.util.List} argument. |
335 |
|
* |
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. |
341 |
|
*/ |
342 |
|
public final CertPath generateCertPath(List certificates) |
343 |
|
throws CertificateException |
344 |
|
{ |
345 |
|
return certFacSpi.engineGenerateCertPath(certificates); |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
|
* Returns an Iterator of CertPath encodings supported by this |
350 |
|
* factory, with the default encoding first. The returned Iterator |
351 |
|
* cannot be modified. |
352 |
|
* |
353 |
|
* @return The Iterator of supported encodings. |
354 |
|
*/ |
355 |
|
public final Iterator getCertPathEncodings() |
356 |
|
{ |
357 |
|
return certFacSpi.engineGetCertPathEncodings(); |
358 |
} |
} |
359 |
} // class CertificateFactory |
} // class CertificateFactory |