2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / security / cert / Certificate.java
blobf4aff41d35ea060106f12fa7001d79523985ad04
1 /* Certificate.java --- Certificate class
2 Copyright (C) 1999,2003 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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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.security.PublicKey;
42 import java.security.NoSuchAlgorithmException;
43 import java.security.InvalidKeyException;
44 import java.security.NoSuchProviderException;
45 import java.security.SignatureException;
46 import java.io.ObjectInputStream;
47 import java.io.ByteArrayInputStream;
48 import java.io.InvalidObjectException;
49 import java.io.ObjectStreamException;
50 import java.io.Serializable;
52 /**
53 * The Certificate class is an abstract class used to manage
54 * identity certificates. An identity certificate is a
55 * combination of a principal and a public key which is
56 * certified by another principal. This is the puprose of
57 * Certificate Authorities (CA).
59 * <p>This class is used to manage different types of certificates
60 * but have important common puposes. Different types of
61 * certificates like X.509 and OpenPGP share general certificate
62 * functions (like encoding and verifying) and information like
63 * public keys.
65 * <p>X.509, OpenPGP, and SDSI can be implemented by subclassing this
66 * class even though they differ in storage methods and information
67 * stored.
69 * @see CertificateFactory
70 * @see X509Certificate
71 * @since JDK 1.2
72 * @author Mark Benvenuto
73 * @author Casey Marshall
75 public abstract class Certificate implements Serializable
77 private static final long serialVersionUID = -6751606818319535583L;
79 private String type;
81 /**
82 Constructs a new certificate of the specified type. An example
83 is "X.509".
85 @param type a valid standard name for a certificate.
87 protected Certificate(String type)
89 this.type = type;
92 /**
93 Returns the Certificate type.
95 @return a string representing the Certificate type
97 public final String getType()
99 return type;
103 Compares this Certificate to other. It checks if the
104 object if instanceOf Certificate and then checks if
105 the encoded form matches.
107 @param other An Object to test for equality
109 @return true if equal, false otherwise
111 public boolean equals(Object other)
113 if( other instanceof Certificate ) {
114 try {
115 Certificate x = (Certificate) other;
116 if( getEncoded().length != x.getEncoded().length )
117 return false;
119 byte b1[] = getEncoded();
120 byte b2[] = x.getEncoded();
122 for( int i = 0; i < b1.length; i++ )
123 if( b1[i] != b2[i] )
124 return false;
126 } catch( CertificateEncodingException cee ) {
127 return false;
129 return true;
131 return false;
135 Returns a hash code for this Certificate in its encoded
136 form.
138 @return A hash code of this class
140 public int hashCode()
142 return super.hashCode();
146 Gets the DER ASN.1 encoded format for this Certificate.
147 It assumes each certificate has only one encoding format.
148 Ex: X.509 is encoded as ASN.1 DER
150 @return byte array containg encoded form
152 @throws CertificateEncodingException if an error occurs
154 public abstract byte[] getEncoded() throws CertificateEncodingException;
157 Verifies that this Certificate was properly signed with the
158 PublicKey that corresponds to its private key.
160 @param key PublicKey to verify with
162 @throws CertificateException encoding error
163 @throws NoSuchAlgorithmException unsupported algorithm
164 @throws InvalidKeyException incorrect key
165 @throws NoSuchProviderException no provider
166 @throws SignatureException signature error
168 public abstract void verify(PublicKey key)
169 throws CertificateException,
170 NoSuchAlgorithmException,
171 InvalidKeyException,
172 NoSuchProviderException,
173 SignatureException;
176 Verifies that this Certificate was properly signed with the
177 PublicKey that corresponds to its private key and uses
178 the signature engine provided by the provider.
180 @param key PublicKey to verify with
181 @param sigProvider Provider to use for signature algorithm
183 @throws CertificateException encoding error
184 @throws NoSuchAlgorithmException unsupported algorithm
185 @throws InvalidKeyException incorrect key
186 @throws NoSuchProviderException incorrect provider
187 @throws SignatureException signature error
189 public abstract void verify(PublicKey key,
190 String sigProvider)
191 throws CertificateException,
192 NoSuchAlgorithmException,
193 InvalidKeyException,
194 NoSuchProviderException,
195 SignatureException;
198 Returns a string representing the Certificate.
200 @return a string representing the Certificate.
202 public abstract String toString();
206 Returns the public key stored in the Certificate.
208 @return The public key
210 public abstract PublicKey getPublicKey();
212 // Protected methods.
213 // ------------------------------------------------------------------------
216 * Returns a replacement for this certificate to be serialized. This
217 * method returns the equivalent to the following for this class:
219 * <blockquote>
220 * <pre>new CertificateRep(getType(), getEncoded());</pre>
221 * </blockquote>
223 * <p>This thusly replaces the certificate with its name and its
224 * encoded form, which can be deserialized later with the {@link
225 * CertificateFactory} implementation for this certificate's type.
227 * @return The replacement object to be serialized.
228 * @throws ObjectStreamException If the replacement could not be
229 * created.
231 protected Object writeReplace() throws ObjectStreamException
235 return new CertificateRep(getType(), getEncoded());
237 catch (CertificateEncodingException cee)
239 throw new InvalidObjectException(cee.toString());
243 // Inner class.
244 // ------------------------------------------------------------------------
247 Certificate.CertificateRep is an inner class used to provide an alternate
248 storage mechanism for serialized Certificates.
250 protected static class CertificateRep implements java.io.Serializable
253 /** From JDK1.4. */
254 private static final long serialVersionUID = -8563758940495660020L;
256 /** The certificate type, e.g. "X.509". */
257 private String type;
259 /** The encoded certificate data. */
260 private byte[] data;
263 * Create an alternative representation of this certificate. The
264 * <code>(type, data)</code> pair is typically the certificate's
265 * type as returned by {@link Certificate#getType()} (i.e. the
266 * canonical name of the certificate type) and the encoded form as
267 * returned by {@link Certificate#getEncoded()}.
269 * <p>For example, X.509 certificates would create an instance of
270 * this class with the parameters "X.509" and the ASN.1
271 * representation of the certificate, encoded as DER bytes.
273 * @param type The certificate type.
274 * @param data The encoded certificate data.
276 protected CertificateRep(String type, byte[] data)
278 this.type = type;
279 this.data = data;
283 * Deserialize this certificate replacement into the appropriate
284 * certificate object. That is, this method attempts to create a
285 * {@link CertificateFactory} for this certificate's type, then
286 * attempts to parse the encoded data with that factory, returning
287 * the resulting certificate.
289 * @return The deserialized certificate.
290 * @throws ObjectStreamException If there is no appropriate
291 * certificate factory for the given type, or if the encoded form
292 * cannot be parsed.
294 protected Object readResolve() throws ObjectStreamException
298 CertificateFactory fact = CertificateFactory.getInstance(type);
299 return fact.generateCertificate(new ByteArrayInputStream(data));
301 catch (Exception e)
303 throw new InvalidObjectException(e.toString());