OSDN Git Service

2fb2a201f2163bfb4e1802cbcaab1b5b183f0d6b
[pf3gnuchains/gcc-fork.git] / libjava / java / security / cert / Certificate.java
1 /* Certificate.java --- Certificate class
2    Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10  
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.
15
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.
20
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. */
26
27
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;
37
38 /**
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).
44    
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
49    public keys.
50    
51    X.509, OpenPGP, and SDSI can be implemented by subclassing this
52    class even though they differ in storage methods and information
53    stored.
54    
55    @since JDK 1.2
56    
57    @author Mark Benvenuto
58 */
59 public abstract class Certificate
60 {
61
62   private String type;
63   /**
64      Constructs a new certificate of the specified type. An example
65      is "X.509".
66
67      @param type a valid standard name for a certificate.
68   */
69   protected Certificate(String type)
70   {
71     this.type = type;
72   }
73
74   /**
75      Returns the Certificate type.
76
77      @return a string representing the Certificate type
78   */
79   public final String getType()
80   {
81     return type;
82   }
83
84   /**
85      Compares this Certificate to other. It checks if the
86      object if instanceOf Certificate and then checks if
87      the encoded form matches.
88
89      @param other An Object to test for equality
90
91      @return true if equal, false otherwise
92   */
93   public boolean equals(Object other)
94   {
95     if( other instanceof Certificate ) {
96       try {
97         Certificate x = (Certificate) other;
98         if( getEncoded().length != x.getEncoded().length )
99           return false;
100
101         byte b1[] = getEncoded();
102         byte b2[] = x.getEncoded();
103
104         for( int i = 0; i < b1.length; i++ )
105           if( b1[i] != b2[i] )
106             return false;
107
108       } catch( CertificateEncodingException cee ) { 
109         return false;
110       }
111       return true;
112     }
113     return false;
114   }
115
116   /**
117      Returns a hash code for this Certificate in its encoded
118      form.
119
120      @return A hash code of this class
121   */
122   public int hashCode()
123   {
124     return super.hashCode();
125   }
126
127   /**
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
131
132      @return byte array containg encoded form
133
134      @throws CertificateEncodingException if an error occurs
135   */
136   public abstract byte[] getEncoded() throws CertificateEncodingException;
137
138   /**
139      Verifies that this Certificate was properly signed with the
140      PublicKey that corresponds to its private key. 
141
142      @param key PublicKey to verify with
143
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
149   */
150   public abstract void verify(PublicKey key)
151     throws CertificateException,
152     NoSuchAlgorithmException,
153     InvalidKeyException,
154     NoSuchProviderException,
155     SignatureException;
156
157   /**
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. 
161
162      @param key PublicKey to verify with
163      @param sigProvider Provider to use for signature algorithm
164
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
170   */
171   public abstract void verify(PublicKey key,
172                               String sigProvider)
173     throws CertificateException,
174     NoSuchAlgorithmException,
175     InvalidKeyException,
176     NoSuchProviderException,
177     SignatureException;
178
179   /**
180      Returns a string representing the Certificate.
181
182      @return a string representing the Certificate.
183   */
184   public abstract String toString();
185
186
187   /**
188      Returns the public key stored in the Certificate.
189
190      @return The public key
191   */
192   public abstract PublicKey getPublicKey();
193
194
195   /* INNER CLASS */
196   /**
197      Certificate.CertificateRep is an inner class used to provide an alternate
198      storage mechanism for serialized Certificates.
199   */
200   protected static class CertificateRep implements java.io.Serializable
201   {
202     private String type;
203     private byte[] data;
204
205     /**
206        Create an alternate Certificate class to store a serialized Certificate
207
208        @param type the name of certificate type
209        @param data the certificate data
210     */
211     protected CertificateRep(String type,
212                              byte[] data)
213     {
214       this.type = type;
215       this.data = data;
216     }
217
218     /**
219        Return the stored Certificate
220
221        @return the stored certificate
222
223        @throws ObjectStreamException if certificate cannot be resolved
224     */
225     protected Object readResolve()
226       throws ObjectStreamException
227     {
228       try {
229         return new ObjectInputStream( new ByteArrayInputStream( data ) ).readObject();
230       } catch ( Exception e ) {
231         e.printStackTrace();
232         throw new RuntimeException ( e.toString() );
233       }
234     }
235   }
236
237 }