FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / security / cert / Certificate.java
blob25e8aadf1918dae9b69430a8c7cff1010374d703
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)
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;
40 import java.security.PublicKey;
41 import java.security.NoSuchAlgorithmException;
42 import java.security.InvalidKeyException;
43 import java.security.NoSuchProviderException;
44 import java.security.SignatureException;
45 import java.io.ObjectInputStream;
46 import java.io.ByteArrayInputStream;
47 import java.io.ObjectStreamException;
49 /**
50 The Certificate class is an abstract class used to manage
51 identity certificates. An identity certificate is a
52 combination of a principal and a public key which is
53 certified by another principal. This is the puprose of
54 Certificate Authorities (CA).
56 This class is used to manage different types of certificates
57 but have important common puposes. Different types of
58 certificates like X.509 and OpenPGP share general certificate
59 functions (like encoding and verifying) and information like
60 public keys.
62 X.509, OpenPGP, and SDSI can be implemented by subclassing this
63 class even though they differ in storage methods and information
64 stored.
66 @since JDK 1.2
68 @author Mark Benvenuto
70 public abstract class Certificate
72 static final long serialVersionUID = -6751606818319535583L;
74 private String type;
75 /**
76 Constructs a new certificate of the specified type. An example
77 is "X.509".
79 @param type a valid standard name for a certificate.
81 protected Certificate(String type)
83 this.type = type;
86 /**
87 Returns the Certificate type.
89 @return a string representing the Certificate type
91 public final String getType()
93 return type;
96 /**
97 Compares this Certificate to other. It checks if the
98 object if instanceOf Certificate and then checks if
99 the encoded form matches.
101 @param other An Object to test for equality
103 @return true if equal, false otherwise
105 public boolean equals(Object other)
107 if( other instanceof Certificate ) {
108 try {
109 Certificate x = (Certificate) other;
110 if( getEncoded().length != x.getEncoded().length )
111 return false;
113 byte b1[] = getEncoded();
114 byte b2[] = x.getEncoded();
116 for( int i = 0; i < b1.length; i++ )
117 if( b1[i] != b2[i] )
118 return false;
120 } catch( CertificateEncodingException cee ) {
121 return false;
123 return true;
125 return false;
129 Returns a hash code for this Certificate in its encoded
130 form.
132 @return A hash code of this class
134 public int hashCode()
136 return super.hashCode();
140 Gets the DER ASN.1 encoded format for this Certificate.
141 It assumes each certificate has only one encoding format.
142 Ex: X.509 is encoded as ASN.1 DER
144 @return byte array containg encoded form
146 @throws CertificateEncodingException if an error occurs
148 public abstract byte[] getEncoded() throws CertificateEncodingException;
151 Verifies that this Certificate was properly signed with the
152 PublicKey that corresponds to its private key.
154 @param key PublicKey to verify with
156 @throws CertificateException encoding error
157 @throws NoSuchAlgorithmException unsupported algorithm
158 @throws InvalidKeyException incorrect key
159 @throws NoSuchProviderException no provider
160 @throws SignatureException signature error
162 public abstract void verify(PublicKey key)
163 throws CertificateException,
164 NoSuchAlgorithmException,
165 InvalidKeyException,
166 NoSuchProviderException,
167 SignatureException;
170 Verifies that this Certificate was properly signed with the
171 PublicKey that corresponds to its private key and uses
172 the signature engine provided by the provider.
174 @param key PublicKey to verify with
175 @param sigProvider Provider to use for signature algorithm
177 @throws CertificateException encoding error
178 @throws NoSuchAlgorithmException unsupported algorithm
179 @throws InvalidKeyException incorrect key
180 @throws NoSuchProviderException incorrect provider
181 @throws SignatureException signature error
183 public abstract void verify(PublicKey key,
184 String sigProvider)
185 throws CertificateException,
186 NoSuchAlgorithmException,
187 InvalidKeyException,
188 NoSuchProviderException,
189 SignatureException;
192 Returns a string representing the Certificate.
194 @return a string representing the Certificate.
196 public abstract String toString();
200 Returns the public key stored in the Certificate.
202 @return The public key
204 public abstract PublicKey getPublicKey();
207 /* INNER CLASS */
209 Certificate.CertificateRep is an inner class used to provide an alternate
210 storage mechanism for serialized Certificates.
212 protected static class CertificateRep implements java.io.Serializable
214 private String type;
215 private byte[] data;
218 Create an alternate Certificate class to store a serialized Certificate
220 @param type the name of certificate type
221 @param data the certificate data
223 protected CertificateRep(String type,
224 byte[] data)
226 this.type = type;
227 this.data = data;
231 Return the stored Certificate
233 @return the stored certificate
235 @throws ObjectStreamException if certificate cannot be resolved
237 protected Object readResolve()
238 throws ObjectStreamException
240 try {
241 return new ObjectInputStream( new ByteArrayInputStream( data ) ).readObject();
242 } catch ( Exception e ) {
243 e.printStackTrace();
244 throw new RuntimeException ( e.toString() );