1 /* Certificate.java --- Certificate class
2 Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
28 package java.security.cert;
29 import java.security.PublicKey;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.InvalidKeyException;
32 import java.security.NoSuchProviderException;
33 import java.security.SignatureException;
34 import java.io.ObjectInputStream;
35 import java.io.ByteArrayInputStream;
36 import java.io.ObjectStreamException;
39 The Certificate class is an abstract class used to manage
40 identity certificates. An identity certificate is a
41 combination of a principal and a public key which is
42 certified by another principal. This is the puprose of
43 Certificate Authorities (CA).
45 This class is used to manage different types of certificates
46 but have important common puposes. Different types of
47 certificates like X.509 and OpenPGP share general certificate
48 functions (like encoding and verifying) and information like
51 X.509, OpenPGP, and SDSI can be implemented by subclassing this
52 class even though they differ in storage methods and information
57 @author Mark Benvenuto
59 public abstract class Certificate
64 Constructs a new certificate of the specified type. An example
67 @param type a valid standard name for a certificate.
69 protected Certificate(String type)
75 Returns the Certificate type.
77 @return a string representing the Certificate type
79 public final String getType()
85 Compares this Certificate to other. It checks if the
86 object if instanceOf Certificate and then checks if
87 the encoded form matches.
89 @param other An Object to test for equality
91 @return true if equal, false otherwise
93 public boolean equals(Object other)
95 if( other instanceof Certificate ) {
97 Certificate x = (Certificate) other;
98 if( getEncoded().length != x.getEncoded().length )
101 byte b1[] = getEncoded();
102 byte b2[] = x.getEncoded();
104 for( int i = 0; i < b1.length; i++ )
108 } catch( CertificateEncodingException cee ) {
117 Returns a hash code for this Certificate in its encoded
120 @return A hash code of this class
122 public int hashCode()
124 return super.hashCode();
128 Gets the DER ASN.1 encoded format for this Certificate.
129 It assumes each certificate has only one encoding format.
130 Ex: X.509 is encoded as ASN.1 DER
132 @return byte array containg encoded form
134 @throws CertificateEncodingException if an error occurs
136 public abstract byte[] getEncoded() throws CertificateEncodingException;
139 Verifies that this Certificate was properly signed with the
140 PublicKey that corresponds to its private key.
142 @param key PublicKey to verify with
144 @throws CertificateException encoding error
145 @throws NoSuchAlgorithmException unsupported algorithm
146 @throws InvalidKeyException incorrect key
147 @throws NoSuchProviderException no provider
148 @throws SignatureException signature error
150 public abstract void verify(PublicKey key)
151 throws CertificateException,
152 NoSuchAlgorithmException,
154 NoSuchProviderException,
158 Verifies that this Certificate was properly signed with the
159 PublicKey that corresponds to its private key and uses
160 the signature engine provided by the provider.
162 @param key PublicKey to verify with
163 @param sigProvider Provider to use for signature algorithm
165 @throws CertificateException encoding error
166 @throws NoSuchAlgorithmException unsupported algorithm
167 @throws InvalidKeyException incorrect key
168 @throws NoSuchProviderException incorrect provider
169 @throws SignatureException signature error
171 public abstract void verify(PublicKey key,
173 throws CertificateException,
174 NoSuchAlgorithmException,
176 NoSuchProviderException,
180 Returns a string representing the Certificate.
182 @return a string representing the Certificate.
184 public abstract String toString();
188 Returns the public key stored in the Certificate.
190 @return The public key
192 public abstract PublicKey getPublicKey();
197 Certificate.CertificateRep is an inner class used to provide an alternate
198 storage mechanism for serialized Certificates.
200 protected static class CertificateRep implements java.io.Serializable
206 Create an alternate Certificate class to store a serialized Certificate
208 @param type the name of certificate type
209 @param data the certificate data
211 protected CertificateRep(String type,
219 Return the stored Certificate
221 @return the stored certificate
223 @throws ObjectStreamException if certificate cannot be resolved
225 protected Object readResolve()
226 throws ObjectStreamException
229 return new ObjectInputStream( new ByteArrayInputStream( data ) ).readObject();
230 } catch ( Exception e ) {
232 throw new RuntimeException ( e.toString() );