author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58242 | 94bb65cb37d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1997, 2019, 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 |
||
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
47216
diff
changeset
|
65 |
@java.io.Serial |
2 | 66 |
private static final long serialVersionUID = -3585440601605666277L; |
67 |
||
68 |
// the certificate type |
|
69 |
private final String type; |
|
70 |
||
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
71 |
/** Cache the hash code for the certiticate */ |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
72 |
private int hash = -1; // Default to -1 |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
73 |
|
2 | 74 |
/** |
75 |
* Creates a certificate of the specified type. |
|
76 |
* |
|
77 |
* @param type the standard name of the certificate type. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
78 |
* See the CertificateFactory section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
29492
diff
changeset
|
79 |
* "{@docRoot}/../specs/security/standard-names.html#certificatefactory-types"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
29492
diff
changeset
|
80 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 81 |
* for information about standard certificate types. |
82 |
*/ |
|
83 |
protected Certificate(String type) { |
|
84 |
this.type = type; |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Returns the type of this certificate. |
|
89 |
* |
|
90 |
* @return the type of this certificate. |
|
91 |
*/ |
|
92 |
public final String getType() { |
|
93 |
return this.type; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* 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
|
98 |
* 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
|
99 |
* {@code instanceof} {@code Certificate}, then |
2 | 100 |
* its encoded form is retrieved and compared with the |
101 |
* encoded form of this certificate. |
|
102 |
* |
|
103 |
* @param other the object to test for equality with this certificate. |
|
104 |
* @return true iff the encoded forms of the two certificates |
|
105 |
* match, false otherwise. |
|
106 |
*/ |
|
107 |
public boolean equals(Object other) { |
|
108 |
if (this == other) { |
|
109 |
return true; |
|
110 |
} |
|
111 |
if (!(other instanceof Certificate)) { |
|
112 |
return false; |
|
113 |
} |
|
114 |
try { |
|
115 |
byte[] thisCert = X509CertImpl.getEncodedInternal(this); |
|
116 |
byte[] otherCert = X509CertImpl.getEncodedInternal((Certificate)other); |
|
117 |
||
118 |
return Arrays.equals(thisCert, otherCert); |
|
119 |
} catch (CertificateException e) { |
|
120 |
return false; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Returns a hashcode value for this certificate from its |
|
126 |
* encoded form. |
|
127 |
* |
|
128 |
* @return the hashcode value. |
|
129 |
*/ |
|
130 |
public int hashCode() { |
|
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
131 |
int h = hash; |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
132 |
if (h == -1) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
133 |
try { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
134 |
h = Arrays.hashCode(X509CertImpl.getEncodedInternal(this)); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
135 |
} catch (CertificateException e) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
136 |
h = 0; |
2 | 137 |
} |
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
138 |
hash = h; |
2 | 139 |
} |
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
18551
diff
changeset
|
140 |
return h; |
2 | 141 |
} |
142 |
||
143 |
/** |
|
144 |
* Returns the encoded form of this certificate. It is |
|
145 |
* assumed that each certificate type would have only a single |
|
146 |
* form of encoding; for example, X.509 certificates would |
|
147 |
* be encoded as ASN.1 DER. |
|
148 |
* |
|
149 |
* @return the encoded form of this certificate |
|
150 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
151 |
* @throws CertificateEncodingException if an encoding error occurs. |
2 | 152 |
*/ |
153 |
public abstract byte[] getEncoded() |
|
154 |
throws CertificateEncodingException; |
|
155 |
||
156 |
/** |
|
157 |
* Verifies that this certificate was signed using the |
|
158 |
* private key that corresponds to the specified public key. |
|
159 |
* |
|
160 |
* @param key the PublicKey used to carry out the verification. |
|
161 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
162 |
* @throws NoSuchAlgorithmException on unsupported signature |
2 | 163 |
* algorithms. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
164 |
* @throws InvalidKeyException on incorrect key. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
165 |
* @throws NoSuchProviderException if there's no default provider. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
166 |
* @throws SignatureException on signature errors. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
167 |
* @throws CertificateException on encoding errors. |
2 | 168 |
*/ |
169 |
public abstract void verify(PublicKey key) |
|
170 |
throws CertificateException, NoSuchAlgorithmException, |
|
171 |
InvalidKeyException, NoSuchProviderException, |
|
172 |
SignatureException; |
|
173 |
||
174 |
/** |
|
175 |
* Verifies that this certificate was signed using the |
|
176 |
* private key that corresponds to the specified public key. |
|
177 |
* This method uses the signature verification engine |
|
178 |
* supplied by the specified provider. |
|
179 |
* |
|
180 |
* @param key the PublicKey used to carry out the verification. |
|
181 |
* @param sigProvider the name of the signature provider. |
|
182 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
183 |
* @throws NoSuchAlgorithmException on unsupported signature |
2 | 184 |
* algorithms. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
185 |
* @throws InvalidKeyException on incorrect key. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
186 |
* @throws NoSuchProviderException on incorrect provider. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
187 |
* @throws SignatureException on signature errors. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
188 |
* @throws CertificateException on encoding errors. |
2 | 189 |
*/ |
190 |
public abstract void verify(PublicKey key, String sigProvider) |
|
191 |
throws CertificateException, NoSuchAlgorithmException, |
|
192 |
InvalidKeyException, NoSuchProviderException, |
|
193 |
SignatureException; |
|
194 |
||
195 |
/** |
|
13406
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
196 |
* 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
|
197 |
* 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
|
198 |
* This method uses the signature verification engine |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
199 |
* 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
|
200 |
* 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
|
201 |
* |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
202 |
* <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
|
203 |
* 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
|
204 |
* 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
|
205 |
* 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
|
206 |
* |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
207 |
* @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
|
208 |
* @param sigProvider the signature provider. |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
209 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
210 |
* @throws NoSuchAlgorithmException on unsupported signature |
13406
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
211 |
* algorithms. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
212 |
* @throws InvalidKeyException on incorrect key. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
213 |
* @throws SignatureException on signature errors. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
214 |
* @throws CertificateException on encoding errors. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57950
diff
changeset
|
215 |
* @throws UnsupportedOperationException if the method is not supported |
13406
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
216 |
* @since 1.8 |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
217 |
*/ |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
218 |
public void verify(PublicKey key, Provider sigProvider) |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
219 |
throws CertificateException, NoSuchAlgorithmException, |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
220 |
InvalidKeyException, SignatureException { |
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
221 |
throw new UnsupportedOperationException(); |
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 |
|
33d7bf574b42
7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents:
9035
diff
changeset
|
224 |
/** |
2 | 225 |
* Returns a string representation of this certificate. |
226 |
* |
|
227 |
* @return a string representation of this certificate. |
|
228 |
*/ |
|
229 |
public abstract String toString(); |
|
230 |
||
231 |
/** |
|
232 |
* Gets the public key from this certificate. |
|
233 |
* |
|
234 |
* @return the public key. |
|
235 |
*/ |
|
236 |
public abstract PublicKey getPublicKey(); |
|
237 |
||
238 |
/** |
|
239 |
* Alternate Certificate class for serialization. |
|
240 |
* @since 1.3 |
|
241 |
*/ |
|
242 |
protected static class CertificateRep implements java.io.Serializable { |
|
243 |
||
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
47216
diff
changeset
|
244 |
@java.io.Serial |
2 | 245 |
private static final long serialVersionUID = -8563758940495660020L; |
246 |
||
247 |
private String type; |
|
248 |
private byte[] data; |
|
249 |
||
250 |
/** |
|
251 |
* Construct the alternate Certificate class with the Certificate |
|
252 |
* type and Certificate encoding bytes. |
|
253 |
* |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
254 |
* @param type the standard name of the Certificate type. |
2 | 255 |
* |
256 |
* @param data the Certificate data. |
|
257 |
*/ |
|
258 |
protected CertificateRep(String type, byte[] data) { |
|
259 |
this.type = type; |
|
260 |
this.data = data; |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Resolve the Certificate Object. |
|
265 |
* |
|
266 |
* @return the resolved Certificate Object |
|
267 |
* |
|
268 |
* @throws java.io.ObjectStreamException if the Certificate |
|
269 |
* could not be resolved |
|
270 |
*/ |
|
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
47216
diff
changeset
|
271 |
@java.io.Serial |
2 | 272 |
protected Object readResolve() throws java.io.ObjectStreamException { |
273 |
try { |
|
274 |
CertificateFactory cf = CertificateFactory.getInstance(type); |
|
275 |
return cf.generateCertificate |
|
276 |
(new java.io.ByteArrayInputStream(data)); |
|
277 |
} catch (CertificateException e) { |
|
278 |
throw new java.io.NotSerializableException |
|
279 |
("java.security.cert.Certificate: " + |
|
280 |
type + |
|
281 |
": " + |
|
282 |
e.getMessage()); |
|
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
||
287 |
/** |
|
288 |
* Replace the Certificate to be serialized. |
|
289 |
* |
|
290 |
* @return the alternate Certificate object to be serialized |
|
291 |
* |
|
292 |
* @throws java.io.ObjectStreamException if a new object representing |
|
293 |
* this Certificate could not be created |
|
294 |
* @since 1.3 |
|
295 |
*/ |
|
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
47216
diff
changeset
|
296 |
@java.io.Serial |
2 | 297 |
protected Object writeReplace() throws java.io.ObjectStreamException { |
298 |
try { |
|
299 |
return new CertificateRep(type, getEncoded()); |
|
300 |
} catch (CertificateException e) { |
|
301 |
throw new java.io.NotSerializableException |
|
302 |
("java.security.cert.Certificate: " + |
|
303 |
type + |
|
304 |
": " + |
|
305 |
e.getMessage()); |
|
306 |
} |
|
307 |
} |
|
308 |
} |