author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45434 | jdk/src/java.base/share/classes/java/security/cert/Certificate.java@4582657c7260 |
child 57950 | 4612a3cfb927 |
child 58678 | 9cf78a70fa4f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
29492
diff
changeset
|
2 |
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.security.cert; |
|
27 |
||
28 |
import java.util.Arrays; |
|
29 |
||
13406
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
30 |
import java.security.Provider; |
2 | 31 |
import java.security.PublicKey; |
32 |
import java.security.NoSuchAlgorithmException; |
|
33 |
import java.security.NoSuchProviderException; |
|
34 |
import java.security.InvalidKeyException; |
|
35 |
import java.security.SignatureException; |
|
36 |
||
37 |
import sun.security.x509.X509CertImpl; |
|
38 |
||
39 |
/** |
|
40 |
* <p>Abstract class for managing a variety of identity certificates. |
|
41 |
* An identity certificate is a binding of a principal to a public key which |
|
42 |
* is vouched for by another principal. (A principal represents |
|
43 |
* an entity such as an individual user, a group, or a corporation.) |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
44 |
* <p> |
2 | 45 |
* This class is an abstraction for certificates that have different |
46 |
* formats but important common uses. For example, different types of |
|
47 |
* certificates, such as X.509 and PGP, share general certificate |
|
48 |
* functionality (like encoding and verifying) and |
|
49 |
* some types of information (like a public key). |
|
50 |
* <p> |
|
51 |
* X.509, PGP, and SDSI certificates can all be implemented by |
|
52 |
* subclassing the Certificate class, even though they contain different |
|
53 |
* sets of information, and they store and retrieve the information in |
|
54 |
* different ways. |
|
55 |
* |
|
56 |
* @see X509Certificate |
|
57 |
* @see CertificateFactory |
|
58 |
* |
|
59 |
* @author Hemma Prafullchandra |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
45118
diff
changeset
|
60 |
* @since 1.2 |
2 | 61 |
*/ |
62 |
||
63 |
public abstract class Certificate implements java.io.Serializable { |
|
64 |
||
65 |
private static final long serialVersionUID = -3585440601605666277L; |
|
66 |
||
67 |
// the certificate type |
|
68 |
private final String type; |
|
69 |
||
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
70 |
/** Cache the hash code for the certiticate */ |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
71 |
private int hash = -1; // Default to -1 |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
72 |
|
2 | 73 |
/** |
74 |
* Creates a certificate of the specified type. |
|
75 |
* |
|
76 |
* @param type the standard name of the certificate type. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
77 |
* See the CertificateFactory section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
29492
diff
changeset
|
78 |
* "{@docRoot}/../specs/security/standard-names.html#certificatefactory-types"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
29492
diff
changeset
|
79 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 80 |
* for information about standard certificate types. |
81 |
*/ |
|
82 |
protected Certificate(String type) { |
|
83 |
this.type = type; |
|
84 |
} |
|
85 |
||
86 |
/** |
|
87 |
* Returns the type of this certificate. |
|
88 |
* |
|
89 |
* @return the type of this certificate. |
|
90 |
*/ |
|
91 |
public final String getType() { |
|
92 |
return this.type; |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Compares this certificate for equality with the specified |
|
18551
882a3948c6e6
8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents:
14342
diff
changeset
|
97 |
* object. If the {@code other} object is an |
882a3948c6e6
8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents:
14342
diff
changeset
|
98 |
* {@code instanceof} {@code Certificate}, then |
2 | 99 |
* its encoded form is retrieved and compared with the |
100 |
* encoded form of this certificate. |
|
101 |
* |
|
102 |
* @param other the object to test for equality with this certificate. |
|
103 |
* @return true iff the encoded forms of the two certificates |
|
104 |
* match, false otherwise. |
|
105 |
*/ |
|
106 |
public boolean equals(Object other) { |
|
107 |
if (this == other) { |
|
108 |
return true; |
|
109 |
} |
|
110 |
if (!(other instanceof Certificate)) { |
|
111 |
return false; |
|
112 |
} |
|
113 |
try { |
|
114 |
byte[] thisCert = X509CertImpl.getEncodedInternal(this); |
|
115 |
byte[] otherCert = X509CertImpl.getEncodedInternal((Certificate)other); |
|
116 |
||
117 |
return Arrays.equals(thisCert, otherCert); |
|
118 |
} catch (CertificateException e) { |
|
119 |
return false; |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* Returns a hashcode value for this certificate from its |
|
125 |
* encoded form. |
|
126 |
* |
|
127 |
* @return the hashcode value. |
|
128 |
*/ |
|
129 |
public int hashCode() { |
|
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
130 |
int h = hash; |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
131 |
if (h == -1) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
132 |
try { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
133 |
h = Arrays.hashCode(X509CertImpl.getEncodedInternal(this)); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
134 |
} catch (CertificateException e) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
135 |
h = 0; |
2 | 136 |
} |
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
137 |
hash = h; |
2 | 138 |
} |
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
139 |
return h; |
2 | 140 |
} |
141 |
||
142 |
/** |
|
143 |
* Returns the encoded form of this certificate. It is |
|
144 |
* assumed that each certificate type would have only a single |
|
145 |
* form of encoding; for example, X.509 certificates would |
|
146 |
* be encoded as ASN.1 DER. |
|
147 |
* |
|
148 |
* @return the encoded form of this certificate |
|
149 |
* |
|
150 |
* @exception CertificateEncodingException if an encoding error occurs. |
|
151 |
*/ |
|
152 |
public abstract byte[] getEncoded() |
|
153 |
throws CertificateEncodingException; |
|
154 |
||
155 |
/** |
|
156 |
* Verifies that this certificate was signed using the |
|
157 |
* private key that corresponds to the specified public key. |
|
158 |
* |
|
159 |
* @param key the PublicKey used to carry out the verification. |
|
160 |
* |
|
161 |
* @exception NoSuchAlgorithmException on unsupported signature |
|
162 |
* algorithms. |
|
163 |
* @exception InvalidKeyException on incorrect key. |
|
164 |
* @exception NoSuchProviderException if there's no default provider. |
|
165 |
* @exception SignatureException on signature errors. |
|
166 |
* @exception CertificateException on encoding errors. |
|
167 |
*/ |
|
168 |
public abstract void verify(PublicKey key) |
|
169 |
throws CertificateException, NoSuchAlgorithmException, |
|
170 |
InvalidKeyException, NoSuchProviderException, |
|
171 |
SignatureException; |
|
172 |
||
173 |
/** |
|
174 |
* Verifies that this certificate was signed using the |
|
175 |
* private key that corresponds to the specified public key. |
|
176 |
* This method uses the signature verification engine |
|
177 |
* supplied by the specified provider. |
|
178 |
* |
|
179 |
* @param key the PublicKey used to carry out the verification. |
|
180 |
* @param sigProvider the name of the signature provider. |
|
181 |
* |
|
182 |
* @exception NoSuchAlgorithmException on unsupported signature |
|
183 |
* algorithms. |
|
184 |
* @exception InvalidKeyException on incorrect key. |
|
185 |
* @exception NoSuchProviderException on incorrect provider. |
|
186 |
* @exception SignatureException on signature errors. |
|
187 |
* @exception CertificateException on encoding errors. |
|
188 |
*/ |
|
189 |
public abstract void verify(PublicKey key, String sigProvider) |
|
190 |
throws CertificateException, NoSuchAlgorithmException, |
|
191 |
InvalidKeyException, NoSuchProviderException, |
|
192 |
SignatureException; |
|
193 |
||
194 |
/** |
|
13406
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
195 |
* Verifies that this certificate was signed using the |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
196 |
* private key that corresponds to the specified public key. |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
197 |
* This method uses the signature verification engine |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
198 |
* supplied by the specified provider. Note that the specified |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
199 |
* Provider object does not have to be registered in the provider list. |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
200 |
* |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
201 |
* <p> This method was added to version 1.8 of the Java Platform |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
202 |
* Standard Edition. In order to maintain backwards compatibility with |
18551
882a3948c6e6
8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents:
14342
diff
changeset
|
203 |
* existing service providers, this method cannot be {@code abstract} |
882a3948c6e6
8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents:
14342
diff
changeset
|
204 |
* and by default throws an {@code UnsupportedOperationException}. |
13406
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
205 |
* |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
206 |
* @param key the PublicKey used to carry out the verification. |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
207 |
* @param sigProvider the signature provider. |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
208 |
* |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
209 |
* @exception NoSuchAlgorithmException on unsupported signature |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
210 |
* algorithms. |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
211 |
* @exception InvalidKeyException on incorrect key. |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
212 |
* @exception SignatureException on signature errors. |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
213 |
* @exception CertificateException on encoding errors. |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
214 |
* @exception UnsupportedOperationException if the method is not supported |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
215 |
* @since 1.8 |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
216 |
*/ |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
217 |
public void verify(PublicKey key, Provider sigProvider) |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
218 |
throws CertificateException, NoSuchAlgorithmException, |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
219 |
InvalidKeyException, SignatureException { |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
220 |
throw new UnsupportedOperationException(); |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
221 |
} |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
222 |
|
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
223 |
/** |
2 | 224 |
* Returns a string representation of this certificate. |
225 |
* |
|
226 |
* @return a string representation of this certificate. |
|
227 |
*/ |
|
228 |
public abstract String toString(); |
|
229 |
||
230 |
/** |
|
231 |
* Gets the public key from this certificate. |
|
232 |
* |
|
233 |
* @return the public key. |
|
234 |
*/ |
|
235 |
public abstract PublicKey getPublicKey(); |
|
236 |
||
237 |
/** |
|
238 |
* Alternate Certificate class for serialization. |
|
239 |
* @since 1.3 |
|
240 |
*/ |
|
241 |
protected static class CertificateRep implements java.io.Serializable { |
|
242 |
||
243 |
private static final long serialVersionUID = -8563758940495660020L; |
|
244 |
||
245 |
private String type; |
|
246 |
private byte[] data; |
|
247 |
||
248 |
/** |
|
249 |
* Construct the alternate Certificate class with the Certificate |
|
250 |
* type and Certificate encoding bytes. |
|
251 |
* |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
252 |
* @param type the standard name of the Certificate type. |
2 | 253 |
* |
254 |
* @param data the Certificate data. |
|
255 |
*/ |
|
256 |
protected CertificateRep(String type, byte[] data) { |
|
257 |
this.type = type; |
|
258 |
this.data = data; |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Resolve the Certificate Object. |
|
263 |
* |
|
264 |
* @return the resolved Certificate Object |
|
265 |
* |
|
266 |
* @throws java.io.ObjectStreamException if the Certificate |
|
267 |
* could not be resolved |
|
268 |
*/ |
|
269 |
protected Object readResolve() throws java.io.ObjectStreamException { |
|
270 |
try { |
|
271 |
CertificateFactory cf = CertificateFactory.getInstance(type); |
|
272 |
return cf.generateCertificate |
|
273 |
(new java.io.ByteArrayInputStream(data)); |
|
274 |
} catch (CertificateException e) { |
|
275 |
throw new java.io.NotSerializableException |
|
276 |
("java.security.cert.Certificate: " + |
|
277 |
type + |
|
278 |
": " + |
|
279 |
e.getMessage()); |
|
280 |
} |
|
281 |
} |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Replace the Certificate to be serialized. |
|
286 |
* |
|
287 |
* @return the alternate Certificate object to be serialized |
|
288 |
* |
|
289 |
* @throws java.io.ObjectStreamException if a new object representing |
|
290 |
* this Certificate could not be created |
|
291 |
* @since 1.3 |
|
292 |
*/ |
|
293 |
protected Object writeReplace() throws java.io.ObjectStreamException { |
|
294 |
try { |
|
295 |
return new CertificateRep(type, getEncoded()); |
|
296 |
} catch (CertificateException e) { |
|
297 |
throw new java.io.NotSerializableException |
|
298 |
("java.security.cert.Certificate: " + |
|
299 |
type + |
|
300 |
": " + |
|
301 |
e.getMessage()); |
|
302 |
} |
|
303 |
} |
|
304 |
} |