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 | 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.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 |
|
---|
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;
|
---|
48 |
|
---|
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).
|
---|
55 |
|
---|
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.
|
---|
61 |
|
---|
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.
|
---|
65 |
|
---|
66 | @since JDK 1.2
|
---|
67 |
|
---|
68 | @author Mark Benvenuto
|
---|
69 | */
|
---|
70 | public abstract class Certificate
|
---|
71 | {
|
---|
72 | static final long serialVersionUID = -6751606818319535583L;
|
---|
73 |
|
---|
74 | private String type;
|
---|
75 | /**
|
---|
76 | Constructs a new certificate of the specified type. An example
|
---|
77 | is "X.509".
|
---|
78 |
|
---|
79 | @param type a valid standard name for a certificate.
|
---|
80 | */
|
---|
81 | protected Certificate(String type)
|
---|
82 | {
|
---|
83 | this.type = type;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | Returns the Certificate type.
|
---|
88 |
|
---|
89 | @return a string representing the Certificate type
|
---|
90 | */
|
---|
91 | public final String getType()
|
---|
92 | {
|
---|
93 | return type;
|
---|
94 | }
|
---|
95 |
|
---|
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.
|
---|
100 |
|
---|
101 | @param other An Object to test for equality
|
---|
102 |
|
---|
103 | @return true if equal, false otherwise
|
---|
104 | */
|
---|
105 | public boolean equals(Object other)
|
---|
106 | {
|
---|
107 | if( other instanceof Certificate ) {
|
---|
108 | try {
|
---|
109 | Certificate x = (Certificate) other;
|
---|
110 | if( getEncoded().length != x.getEncoded().length )
|
---|
111 | return false;
|
---|
112 |
|
---|
113 | byte b1[] = getEncoded();
|
---|
114 | byte b2[] = x.getEncoded();
|
---|
115 |
|
---|
116 | for( int i = 0; i < b1.length; i++ )
|
---|
117 | if( b1[i] != b2[i] )
|
---|
118 | return false;
|
---|
119 |
|
---|
120 | } catch( CertificateEncodingException cee ) {
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | Returns a hash code for this Certificate in its encoded
|
---|
130 | form.
|
---|
131 |
|
---|
132 | @return A hash code of this class
|
---|
133 | */
|
---|
134 | public int hashCode()
|
---|
135 | {
|
---|
136 | return super.hashCode();
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
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
|
---|
143 |
|
---|
144 | @return byte array containg encoded form
|
---|
145 |
|
---|
146 | @throws CertificateEncodingException if an error occurs
|
---|
147 | */
|
---|
148 | public abstract byte[] getEncoded() throws CertificateEncodingException;
|
---|
149 |
|
---|
150 | /**
|
---|
151 | Verifies that this Certificate was properly signed with the
|
---|
152 | PublicKey that corresponds to its private key.
|
---|
153 |
|
---|
154 | @param key PublicKey to verify with
|
---|
155 |
|
---|
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
|
---|
161 | */
|
---|
162 | public abstract void verify(PublicKey key)
|
---|
163 | throws CertificateException,
|
---|
164 | NoSuchAlgorithmException,
|
---|
165 | InvalidKeyException,
|
---|
166 | NoSuchProviderException,
|
---|
167 | SignatureException;
|
---|
168 |
|
---|
169 | /**
|
---|
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.
|
---|
173 |
|
---|
174 | @param key PublicKey to verify with
|
---|
175 | @param sigProvider Provider to use for signature algorithm
|
---|
176 |
|
---|
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
|
---|
182 | */
|
---|
183 | public abstract void verify(PublicKey key,
|
---|
184 | String sigProvider)
|
---|
185 | throws CertificateException,
|
---|
186 | NoSuchAlgorithmException,
|
---|
187 | InvalidKeyException,
|
---|
188 | NoSuchProviderException,
|
---|
189 | SignatureException;
|
---|
190 |
|
---|
191 | /**
|
---|
192 | Returns a string representing the Certificate.
|
---|
193 |
|
---|
194 | @return a string representing the Certificate.
|
---|
195 | */
|
---|
196 | public abstract String toString();
|
---|
197 |
|
---|
198 |
|
---|
199 | /**
|
---|
200 | Returns the public key stored in the Certificate.
|
---|
201 |
|
---|
202 | @return The public key
|
---|
203 | */
|
---|
204 | public abstract PublicKey getPublicKey();
|
---|
205 |
|
---|
206 |
|
---|
207 | /* INNER CLASS */
|
---|
208 | /**
|
---|
209 | Certificate.CertificateRep is an inner class used to provide an alternate
|
---|
210 | storage mechanism for serialized Certificates.
|
---|
211 | */
|
---|
212 | protected static class CertificateRep implements java.io.Serializable
|
---|
213 | {
|
---|
214 | private String type;
|
---|
215 | private byte[] data;
|
---|
216 |
|
---|
217 | /**
|
---|
218 | Create an alternate Certificate class to store a serialized Certificate
|
---|
219 |
|
---|
220 | @param type the name of certificate type
|
---|
221 | @param data the certificate data
|
---|
222 | */
|
---|
223 | protected CertificateRep(String type,
|
---|
224 | byte[] data)
|
---|
225 | {
|
---|
226 | this.type = type;
|
---|
227 | this.data = data;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | Return the stored Certificate
|
---|
232 |
|
---|
233 | @return the stored certificate
|
---|
234 |
|
---|
235 | @throws ObjectStreamException if certificate cannot be resolved
|
---|
236 | */
|
---|
237 | protected Object readResolve()
|
---|
238 | throws ObjectStreamException
|
---|
239 | {
|
---|
240 | try {
|
---|
241 | return new ObjectInputStream( new ByteArrayInputStream( data ) ).readObject();
|
---|
242 | } catch ( Exception e ) {
|
---|
243 | e.printStackTrace();
|
---|
244 | throw new RuntimeException ( e.toString() );
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | }
|
---|