1 |
/* Certificate.java --- Certificate class |
/* Certificate.java --- Certificate class |
2 |
Copyright (C) 1999 Free Software Foundation, Inc. |
Copyright (C) 1999,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.PublicKey; |
import java.security.PublicKey; |
42 |
import java.security.NoSuchAlgorithmException; |
import java.security.NoSuchAlgorithmException; |
43 |
import java.security.InvalidKeyException; |
import java.security.InvalidKeyException; |
45 |
import java.security.SignatureException; |
import java.security.SignatureException; |
46 |
import java.io.ObjectInputStream; |
import java.io.ObjectInputStream; |
47 |
import java.io.ByteArrayInputStream; |
import java.io.ByteArrayInputStream; |
48 |
|
import java.io.InvalidObjectException; |
49 |
import java.io.ObjectStreamException; |
import java.io.ObjectStreamException; |
50 |
|
|
51 |
/** |
/** |
52 |
The Certificate class is an abstract class used to manage |
* The Certificate class is an abstract class used to manage |
53 |
identity certificates. An identity certificate is a |
* identity certificates. An identity certificate is a |
54 |
combination of a principal and a public key which is |
* combination of a principal and a public key which is |
55 |
certified by another principal. This is the puprose of |
* certified by another principal. This is the puprose of |
56 |
Certificate Authorities (CA). |
* Certificate Authorities (CA). |
57 |
|
* |
58 |
This class is used to manage different types of certificates |
* <p>This class is used to manage different types of certificates |
59 |
but have important common puposes. Different types of |
* but have important common puposes. Different types of |
60 |
certificates like X.509 and OpenPGP share general certificate |
* certificates like X.509 and OpenPGP share general certificate |
61 |
functions (like encoding and verifying) and information like |
* functions (like encoding and verifying) and information like |
62 |
public keys. |
* public keys. |
63 |
|
* |
64 |
X.509, OpenPGP, and SDSI can be implemented by subclassing this |
* <p>X.509, OpenPGP, and SDSI can be implemented by subclassing this |
65 |
class even though they differ in storage methods and information |
* class even though they differ in storage methods and information |
66 |
stored. |
* stored. |
67 |
|
* |
68 |
@since JDK 1.2 |
* @see CertificateFactory |
69 |
|
* @see X509Certificate |
70 |
@author Mark Benvenuto |
* @since JDK 1.2 |
71 |
*/ |
* @author Mark Benvenuto |
72 |
|
* @author Casey Marshall |
73 |
|
*/ |
74 |
public abstract class Certificate |
public abstract class Certificate |
75 |
{ |
{ |
76 |
static final long serialVersionUID = -6751606818319535583L; |
static final long serialVersionUID = -6751606818319535583L; |
77 |
|
|
78 |
private String type; |
private String type; |
79 |
|
|
80 |
/** |
/** |
81 |
Constructs a new certificate of the specified type. An example |
Constructs a new certificate of the specified type. An example |
82 |
is "X.509". |
is "X.509". |
206 |
|
|
207 |
@return The public key |
@return The public key |
208 |
*/ |
*/ |
209 |
public abstract PublicKey getPublicKey(); |
public abstract PublicKey getPublicKey() |
210 |
|
throws SignatureException, |
211 |
|
InvalidKeyException, |
212 |
|
NoSuchAlgorithmException, |
213 |
|
CertificateException, |
214 |
|
NoSuchProviderException; |
215 |
|
|
216 |
|
|
217 |
|
// Protected methods. |
218 |
|
// ------------------------------------------------------------------------ |
219 |
|
|
220 |
|
/** |
221 |
|
* Returns a replacement for this certificate to be serialized. This |
222 |
|
* method returns the equivalent to the following for this class: |
223 |
|
* |
224 |
|
* <blockquote> |
225 |
|
* <pre>new CertificateRep(getType(), getEncoded());</pre> |
226 |
|
* </blockquote> |
227 |
|
* |
228 |
|
* <p>This thusly replaces the certificate with its name and its |
229 |
|
* encoded form, which can be deserialized later with the {@link |
230 |
|
* CertificateFactory} implementation for this certificate's type. |
231 |
|
* |
232 |
|
* @return The replacement object to be serialized. |
233 |
|
* @throws ObjectStreamException If the replacement could not be |
234 |
|
* created. |
235 |
|
*/ |
236 |
|
protected Object writeReplace() throws ObjectStreamException |
237 |
|
{ |
238 |
|
try |
239 |
|
{ |
240 |
|
return new CertificateRep(getType(), getEncoded()); |
241 |
|
} |
242 |
|
catch (CertificateEncodingException cee) |
243 |
|
{ |
244 |
|
throw new InvalidObjectException(cee.toString()); |
245 |
|
} |
246 |
|
} |
247 |
|
|
248 |
|
// Inner class. |
249 |
|
// ------------------------------------------------------------------------ |
250 |
|
|
|
/* INNER CLASS */ |
|
251 |
/** |
/** |
252 |
Certificate.CertificateRep is an inner class used to provide an alternate |
Certificate.CertificateRep is an inner class used to provide an alternate |
253 |
storage mechanism for serialized Certificates. |
storage mechanism for serialized Certificates. |
254 |
*/ |
*/ |
255 |
protected static class CertificateRep implements java.io.Serializable |
protected static class CertificateRep implements java.io.Serializable |
256 |
{ |
{ |
257 |
|
|
258 |
|
/** From JDK1.4. */ |
259 |
|
private static final long serialVersionUID = -8563758940495660020L; |
260 |
|
|
261 |
|
/** The certificate type, e.g. "X.509". */ |
262 |
private String type; |
private String type; |
263 |
|
|
264 |
|
/** The encoded certificate data. */ |
265 |
private byte[] data; |
private byte[] data; |
266 |
|
|
267 |
/** |
/** |
268 |
Create an alternate Certificate class to store a serialized Certificate |
* Create an alternative representation of this certificate. The |
269 |
|
* <code>(type, data)</code> pair is typically the certificate's |
270 |
@param type the name of certificate type |
* type as returned by {@link Certificate#getType()} (i.e. the |
271 |
@param data the certificate data |
* canonical name of the certificate type) and the encoded form as |
272 |
*/ |
* returned by {@link Certificate#getEncoded()}. |
273 |
protected CertificateRep(String type, |
* |
274 |
byte[] data) |
* <p>For example, X.509 certificates would create an instance of |
275 |
|
* this class with the parameters "X.509" and the ASN.1 |
276 |
|
* representation of the certificate, encoded as DER bytes. |
277 |
|
* |
278 |
|
* @param type The certificate type. |
279 |
|
* @param data The encoded certificate data. |
280 |
|
*/ |
281 |
|
protected CertificateRep(String type, byte[] data) |
282 |
{ |
{ |
283 |
this.type = type; |
this.type = type; |
284 |
this.data = data; |
this.data = data; |
285 |
} |
} |
286 |
|
|
287 |
/** |
/** |
288 |
Return the stored Certificate |
* Deserialize this certificate replacement into the appropriate |
289 |
|
* certificate object. That is, this method attempts to create a |
290 |
@return the stored certificate |
* {@link CertificateFactory} for this certificate's type, then |
291 |
|
* attempts to parse the encoded data with that factory, returning |
292 |
@throws ObjectStreamException if certificate cannot be resolved |
* the resulting certificate. |
293 |
*/ |
* |
294 |
protected Object readResolve() |
* @return The deserialized certificate. |
295 |
throws ObjectStreamException |
* @throws ObjectStreamException If there is no appropriate |
296 |
|
* certificate factory for the given type, or if the encoded form |
297 |
|
* cannot be parsed. |
298 |
|
*/ |
299 |
|
protected Object readResolve() throws ObjectStreamException |
300 |
{ |
{ |
301 |
try { |
try |
302 |
return new ObjectInputStream( new ByteArrayInputStream( data ) ).readObject(); |
{ |
303 |
} catch ( Exception e ) { |
CertificateFactory fact = CertificateFactory.getInstance(type); |
304 |
e.printStackTrace(); |
return fact.generateCertificate(new ByteArrayInputStream(data)); |
305 |
throw new RuntimeException ( e.toString() ); |
} |
306 |
} |
catch (Exception e) |
307 |
|
{ |
308 |
|
throw new InvalidObjectException(e.toString()); |
309 |
|
} |
310 |
} |
} |
311 |
} |
} |
|
|
|
312 |
} |
} |