1 /* Certificate.java --- Certificate class
2 Copyright (C) 1999, 2003, 2004 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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
.io
.ByteArrayInputStream
;
42 import java
.io
.InvalidObjectException
;
43 import java
.io
.ObjectStreamException
;
44 import java
.io
.Serializable
;
45 import java
.security
.InvalidKeyException
;
46 import java
.security
.NoSuchAlgorithmException
;
47 import java
.security
.NoSuchProviderException
;
48 import java
.security
.PublicKey
;
49 import java
.security
.SignatureException
;
52 * The Certificate class is an abstract class used to manage
53 * identity certificates. An identity certificate is a
54 * combination of a principal and a public key which is
55 * certified by another principal. This is the puprose of
56 * Certificate Authorities (CA).
58 * <p>This class is used to manage different types of certificates
59 * but have important common puposes. Different types of
60 * certificates like X.509 and OpenPGP share general certificate
61 * functions (like encoding and verifying) and information like
64 * <p>X.509, OpenPGP, and SDSI can be implemented by subclassing this
65 * class even though they differ in storage methods and information
68 * @see CertificateFactory
69 * @see X509Certificate
71 * @author Mark Benvenuto
72 * @author Casey Marshall
74 public abstract class Certificate
implements Serializable
76 private static final long serialVersionUID
= -3585440601605666277L;
81 Constructs a new certificate of the specified type. An example
84 @param type a valid standard name for a certificate.
86 protected Certificate(String type
)
92 Returns the Certificate type.
94 @return a string representing the Certificate type
96 public final String
getType()
102 Compares this Certificate to other. It checks if the
103 object if instanceOf Certificate and then checks if
104 the encoded form matches.
106 @param other An Object to test for equality
108 @return true if equal, false otherwise
110 public boolean equals(Object other
)
112 if( other
instanceof Certificate
) {
114 Certificate x
= (Certificate
) other
;
115 if( getEncoded().length
!= x
.getEncoded().length
)
118 byte[] b1
= getEncoded();
119 byte[] b2
= x
.getEncoded();
121 for( int i
= 0; i
< b1
.length
; i
++ )
125 } catch( CertificateEncodingException cee
) {
134 Returns a hash code for this Certificate in its encoded
137 @return A hash code of this class
139 public int hashCode()
141 return super.hashCode();
145 Gets the DER ASN.1 encoded format for this Certificate.
146 It assumes each certificate has only one encoding format.
147 Ex: X.509 is encoded as ASN.1 DER
149 @return byte array containg encoded form
151 @throws CertificateEncodingException if an error occurs
153 public abstract byte[] getEncoded() throws CertificateEncodingException
;
156 Verifies that this Certificate was properly signed with the
157 PublicKey that corresponds to its private key.
159 @param key PublicKey to verify with
161 @throws CertificateException encoding error
162 @throws NoSuchAlgorithmException unsupported algorithm
163 @throws InvalidKeyException incorrect key
164 @throws NoSuchProviderException no provider
165 @throws SignatureException signature error
167 public abstract void verify(PublicKey key
)
168 throws CertificateException
,
169 NoSuchAlgorithmException
,
171 NoSuchProviderException
,
175 Verifies that this Certificate was properly signed with the
176 PublicKey that corresponds to its private key and uses
177 the signature engine provided by the provider.
179 @param key PublicKey to verify with
180 @param sigProvider Provider to use for signature algorithm
182 @throws CertificateException encoding error
183 @throws NoSuchAlgorithmException unsupported algorithm
184 @throws InvalidKeyException incorrect key
185 @throws NoSuchProviderException incorrect provider
186 @throws SignatureException signature error
188 public abstract void verify(PublicKey key
,
190 throws CertificateException
,
191 NoSuchAlgorithmException
,
193 NoSuchProviderException
,
197 Returns a string representing the Certificate.
199 @return a string representing the Certificate.
201 public abstract String
toString();
205 Returns the public key stored in the Certificate.
207 @return The public key
209 public abstract PublicKey
getPublicKey();
211 // Protected methods.
212 // ------------------------------------------------------------------------
215 * Returns a replacement for this certificate to be serialized. This
216 * method returns the equivalent to the following for this class:
219 * <pre>new CertificateRep(getType(), getEncoded());</pre>
222 * <p>This thusly replaces the certificate with its name and its
223 * encoded form, which can be deserialized later with the {@link
224 * CertificateFactory} implementation for this certificate's type.
226 * @return The replacement object to be serialized.
227 * @throws ObjectStreamException If the replacement could not be
230 protected Object
writeReplace() throws ObjectStreamException
234 return new CertificateRep(getType(), getEncoded());
236 catch (CertificateEncodingException cee
)
238 throw new InvalidObjectException(cee
.toString());
243 // ------------------------------------------------------------------------
246 Certificate.CertificateRep is an inner class used to provide an alternate
247 storage mechanism for serialized Certificates.
249 protected static class CertificateRep
implements java
.io
.Serializable
253 private static final long serialVersionUID
= -8563758940495660020L;
255 /** The certificate type, e.g. "X.509". */
258 /** The encoded certificate data. */
262 * Create an alternative representation of this certificate. The
263 * <code>(type, data)</code> pair is typically the certificate's
264 * type as returned by {@link Certificate#getType()} (i.e. the
265 * canonical name of the certificate type) and the encoded form as
266 * returned by {@link Certificate#getEncoded()}.
268 * <p>For example, X.509 certificates would create an instance of
269 * this class with the parameters "X.509" and the ASN.1
270 * representation of the certificate, encoded as DER bytes.
272 * @param type The certificate type.
273 * @param data The encoded certificate data.
275 protected CertificateRep(String type
, byte[] data
)
282 * Deserialize this certificate replacement into the appropriate
283 * certificate object. That is, this method attempts to create a
284 * {@link CertificateFactory} for this certificate's type, then
285 * attempts to parse the encoded data with that factory, returning
286 * the resulting certificate.
288 * @return The deserialized certificate.
289 * @throws ObjectStreamException If there is no appropriate
290 * certificate factory for the given type, or if the encoded form
293 protected Object
readResolve() throws ObjectStreamException
297 CertificateFactory fact
= CertificateFactory
.getInstance(type
);
298 return fact
.generateCertificate(new ByteArrayInputStream(data
));
302 throw new InvalidObjectException(e
.toString());