author | xuelei |
Wed, 13 Mar 2019 07:14:50 -0700 | |
changeset 54099 | fd332722014c |
parent 47216 | 71c04702a3d5 |
child 57950 | 4612a3cfb927 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54099
fd332722014c
8160247: Mark deprecated javax.security.cert APIs with forRemoval=true
xuelei
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 com.sun.security.cert.internal.x509; |
|
27 |
||
28 |
import java.io.ByteArrayInputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.Serializable; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.io.ObjectInputStream; |
|
33 |
import java.io.OutputStream; |
|
34 |
import java.io.ObjectOutputStream; |
|
35 |
import java.math.BigInteger; |
|
36 |
import java.security.Signature; |
|
37 |
import javax.security.cert.*; |
|
38 |
import java.security.*; |
|
39 |
import java.util.Date; |
|
40 |
import java.util.BitSet; |
|
41 |
import java.util.Enumeration; |
|
42 |
import java.util.Vector; |
|
43 |
||
44 |
/** |
|
45 |
* The X509V1CertImpl class is used as a conversion wrapper around |
|
46 |
* sun.security.x509.X509Cert certificates when running under JDK1.1.x. |
|
47 |
* |
|
29377
d3f457ce9c48
8073430: Deprecate security APIs that have been superseded
juh
parents:
25859
diff
changeset
|
48 |
* @deprecated This is the implementation class for the deprecated |
d3f457ce9c48
8073430: Deprecate security APIs that have been superseded
juh
parents:
25859
diff
changeset
|
49 |
* {@code javax.security.cert.X509Certificate} class. The classes in the |
d3f457ce9c48
8073430: Deprecate security APIs that have been superseded
juh
parents:
25859
diff
changeset
|
50 |
* {@code java.security.cert} package should be used instead. |
d3f457ce9c48
8073430: Deprecate security APIs that have been superseded
juh
parents:
25859
diff
changeset
|
51 |
* |
2 | 52 |
* @author Jeff Nisewanger |
53 |
*/ |
|
54099
fd332722014c
8160247: Mark deprecated javax.security.cert APIs with forRemoval=true
xuelei
parents:
47216
diff
changeset
|
54 |
@SuppressWarnings("removal") |
fd332722014c
8160247: Mark deprecated javax.security.cert APIs with forRemoval=true
xuelei
parents:
47216
diff
changeset
|
55 |
@Deprecated(since="9", forRemoval=true) |
2 | 56 |
public class X509V1CertImpl extends X509Certificate implements Serializable { |
57 |
static final long serialVersionUID = -2048442350420423405L; |
|
58 |
private java.security.cert.X509Certificate wrappedCert; |
|
59 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
29377
diff
changeset
|
60 |
private static synchronized java.security.cert.CertificateFactory |
2 | 61 |
getFactory() |
62 |
throws java.security.cert.CertificateException |
|
63 |
{ |
|
64 |
return java.security.cert.CertificateFactory.getInstance("X.509"); |
|
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* Default constructor. |
|
69 |
*/ |
|
70 |
public X509V1CertImpl() { } |
|
71 |
||
72 |
/** |
|
73 |
* Unmarshals a certificate from its encoded form, parsing the |
|
74 |
* encoded bytes. This form of constructor is used by agents which |
|
75 |
* need to examine and use certificate contents. That is, this is |
|
76 |
* one of the more commonly used constructors. Note that the buffer |
|
77 |
* must include only a certificate, and no "garbage" may be left at |
|
78 |
* the end. If you need to ignore data at the end of a certificate, |
|
79 |
* use another constructor. |
|
80 |
* |
|
81 |
* @param certData the encoded bytes, with no trailing padding. |
|
82 |
* @exception CertificateException on parsing errors. |
|
83 |
*/ |
|
84 |
public X509V1CertImpl(byte[] certData) |
|
85 |
throws CertificateException { |
|
86 |
try { |
|
87 |
ByteArrayInputStream bs; |
|
88 |
||
89 |
bs = new ByteArrayInputStream(certData); |
|
90 |
wrappedCert = (java.security.cert.X509Certificate) |
|
91 |
getFactory().generateCertificate(bs); |
|
92 |
} catch (java.security.cert.CertificateException e) { |
|
93 |
throw new CertificateException(e.getMessage()); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* unmarshals an X.509 certificate from an input stream. |
|
99 |
* |
|
100 |
* @param in an input stream holding at least one certificate |
|
101 |
* @exception CertificateException on parsing errors. |
|
102 |
*/ |
|
103 |
public X509V1CertImpl(InputStream in) |
|
104 |
throws CertificateException { |
|
105 |
try { |
|
106 |
wrappedCert = (java.security.cert.X509Certificate) |
|
107 |
getFactory().generateCertificate(in); |
|
108 |
} catch (java.security.cert.CertificateException e) { |
|
109 |
throw new CertificateException(e.getMessage()); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Returns the encoded form of this certificate. It is |
|
115 |
* assumed that each certificate type would have only a single |
|
116 |
* form of encoding; for example, X.509 certificates would |
|
117 |
* be encoded as ASN.1 DER. |
|
118 |
*/ |
|
119 |
public byte[] getEncoded() throws CertificateEncodingException { |
|
120 |
try { |
|
121 |
return wrappedCert.getEncoded(); |
|
122 |
} catch (java.security.cert.CertificateEncodingException e) { |
|
123 |
throw new CertificateEncodingException(e.getMessage()); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Throws an exception if the certificate was not signed using the |
|
129 |
* verification key provided. Successfully verifying a certificate |
|
130 |
* does <em>not</em> indicate that one should trust the entity which |
|
131 |
* it represents. |
|
132 |
* |
|
133 |
* @param key the public key used for verification. |
|
134 |
*/ |
|
135 |
public void verify(PublicKey key) |
|
136 |
throws CertificateException, NoSuchAlgorithmException, |
|
137 |
InvalidKeyException, NoSuchProviderException, |
|
138 |
SignatureException |
|
139 |
{ |
|
140 |
try { |
|
141 |
wrappedCert.verify(key); |
|
142 |
} catch (java.security.cert.CertificateException e) { |
|
143 |
throw new CertificateException(e.getMessage()); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Throws an exception if the certificate was not signed using the |
|
149 |
* verification key provided. Successfully verifying a certificate |
|
150 |
* does <em>not</em> indicate that one should trust the entity which |
|
151 |
* it represents. |
|
152 |
* |
|
153 |
* @param key the public key used for verification. |
|
154 |
* @param sigProvider the name of the provider. |
|
155 |
*/ |
|
156 |
public void verify(PublicKey key, String sigProvider) |
|
157 |
throws CertificateException, NoSuchAlgorithmException, |
|
158 |
InvalidKeyException, NoSuchProviderException, |
|
159 |
SignatureException |
|
160 |
{ |
|
161 |
try { |
|
162 |
wrappedCert.verify(key, sigProvider); |
|
163 |
} catch (java.security.cert.CertificateException e) { |
|
164 |
throw new CertificateException(e.getMessage()); |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Checks that the certificate is currently valid, i.e. the current |
|
170 |
* time is within the specified validity period. |
|
171 |
*/ |
|
172 |
public void checkValidity() throws |
|
173 |
CertificateExpiredException, CertificateNotYetValidException { |
|
174 |
checkValidity(new Date()); |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Checks that the specified date is within the certificate's |
|
179 |
* validity period, or basically if the certificate would be |
|
180 |
* valid at the specified date/time. |
|
181 |
* |
|
182 |
* @param date the Date to check against to see if this certificate |
|
183 |
* is valid at that date/time. |
|
184 |
*/ |
|
185 |
public void checkValidity(Date date) throws |
|
186 |
CertificateExpiredException, CertificateNotYetValidException { |
|
187 |
try { |
|
188 |
wrappedCert.checkValidity(date); |
|
189 |
} catch (java.security.cert.CertificateNotYetValidException e) { |
|
190 |
throw new CertificateNotYetValidException(e.getMessage()); |
|
191 |
} catch (java.security.cert.CertificateExpiredException e) { |
|
192 |
throw new CertificateExpiredException(e.getMessage()); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
||
197 |
/** |
|
198 |
* Returns a printable representation of the certificate. This does not |
|
199 |
* contain all the information available to distinguish this from any |
|
200 |
* other certificate. The certificate must be fully constructed |
|
201 |
* before this function may be called. |
|
202 |
*/ |
|
203 |
public String toString() { |
|
204 |
return wrappedCert.toString(); |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Gets the publickey from this certificate. |
|
209 |
* |
|
210 |
* @return the publickey. |
|
211 |
*/ |
|
212 |
public PublicKey getPublicKey() { |
|
213 |
PublicKey key = wrappedCert.getPublicKey(); |
|
214 |
return key; |
|
215 |
} |
|
216 |
||
217 |
/* |
|
218 |
* Gets the version number from the certificate. |
|
219 |
* |
|
220 |
* @return the version number. |
|
221 |
*/ |
|
222 |
public int getVersion() { |
|
223 |
return wrappedCert.getVersion() - 1; |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Gets the serial number from the certificate. |
|
228 |
* |
|
229 |
* @return the serial number. |
|
230 |
*/ |
|
231 |
public BigInteger getSerialNumber() { |
|
232 |
return wrappedCert.getSerialNumber(); |
|
233 |
} |
|
234 |
||
235 |
/** |
|
236 |
* Gets the subject distinguished name from the certificate. |
|
237 |
* |
|
238 |
* @return the subject name. |
|
239 |
* @exception CertificateException if a parsing error occurs. |
|
240 |
*/ |
|
241 |
public Principal getSubjectDN() { |
|
242 |
return wrappedCert.getSubjectDN(); |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* Gets the issuer distinguished name from the certificate. |
|
247 |
* |
|
248 |
* @return the issuer name. |
|
249 |
* @exception CertificateException if a parsing error occurs. |
|
250 |
*/ |
|
251 |
public Principal getIssuerDN() { |
|
252 |
return wrappedCert.getIssuerDN(); |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* Gets the notBefore date from the validity period of the certificate. |
|
257 |
* |
|
258 |
* @return the start date of the validity period. |
|
259 |
* @exception CertificateException if a parsing error occurs. |
|
260 |
*/ |
|
261 |
public Date getNotBefore() { |
|
262 |
return wrappedCert.getNotBefore(); |
|
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Gets the notAfter date from the validity period of the certificate. |
|
267 |
* |
|
268 |
* @return the end date of the validity period. |
|
269 |
* @exception CertificateException if a parsing error occurs. |
|
270 |
*/ |
|
271 |
public Date getNotAfter() { |
|
272 |
return wrappedCert.getNotAfter(); |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Gets the signature algorithm name for the certificate |
|
277 |
* signature algorithm. |
|
278 |
* For example, the string "SHA1/DSA". |
|
279 |
* |
|
280 |
* @return the signature algorithm name. |
|
281 |
* @exception CertificateException if a parsing error occurs. |
|
282 |
*/ |
|
283 |
public String getSigAlgName() { |
|
284 |
return wrappedCert.getSigAlgName(); |
|
285 |
} |
|
286 |
||
287 |
/** |
|
288 |
* Gets the signature algorithm OID string from the certificate. |
|
289 |
* For example, the string "1.2.840.10040.4.3" |
|
290 |
* |
|
291 |
* @return the signature algorithm oid string. |
|
292 |
* @exception CertificateException if a parsing error occurs. |
|
293 |
*/ |
|
294 |
public String getSigAlgOID() { |
|
295 |
return wrappedCert.getSigAlgOID(); |
|
296 |
} |
|
297 |
||
298 |
/** |
|
299 |
* Gets the DER encoded signature algorithm parameters from this |
|
300 |
* certificate's signature algorithm. |
|
301 |
* |
|
302 |
* @return the DER encoded signature algorithm parameters, or |
|
303 |
* null if no parameters are present. |
|
304 |
* @exception CertificateException if a parsing error occurs. |
|
305 |
*/ |
|
306 |
public byte[] getSigAlgParams() { |
|
307 |
return wrappedCert.getSigAlgParams(); |
|
308 |
} |
|
309 |
||
310 |
private synchronized void writeObject(ObjectOutputStream stream) |
|
311 |
throws IOException { |
|
312 |
try { |
|
313 |
stream.write(getEncoded()); |
|
314 |
} catch (CertificateEncodingException e) { |
|
315 |
throw new IOException("getEncoded failed: " + e.getMessage()); |
|
316 |
} |
|
317 |
} |
|
318 |
||
319 |
private synchronized void readObject(ObjectInputStream stream) |
|
320 |
throws IOException { |
|
321 |
try { |
|
322 |
wrappedCert = (java.security.cert.X509Certificate) |
|
323 |
getFactory().generateCertificate(stream); |
|
324 |
} catch (java.security.cert.CertificateException e) { |
|
325 |
throw new IOException("generateCertificate failed: " + e.getMessage()); |
|
326 |
} |
|
327 |
} |
|
328 |
||
329 |
public java.security.cert.X509Certificate getX509Certificate() { |
|
330 |
return wrappedCert; |
|
331 |
} |
|
332 |
} |