Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/security/cert/X509Certificate.java
38918 views
1
/*
2
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
27
package javax.security.cert;
28
29
import java.io.InputStream;
30
import java.lang.Class;
31
import java.lang.reflect.Constructor;
32
import java.lang.reflect.InvocationTargetException;
33
import java.security.Security;
34
35
import java.math.BigInteger;
36
import java.security.AccessController;
37
import java.security.Principal;
38
import java.security.PrivilegedAction;
39
import java.security.PublicKey;
40
import java.util.BitSet;
41
import java.util.Date;
42
43
/**
44
* Abstract class for X.509 v1 certificates. This provides a standard
45
* way to access all the version 1 attributes of an X.509 certificate.
46
* Attributes that are specific to X.509 v2 or v3 are not available
47
* through this interface. Future API evolution will provide full access to
48
* complete X.509 v3 attributes.
49
* <p>
50
* The basic X.509 format was defined by
51
* ISO/IEC and ANSI X9 and is described below in ASN.1:
52
* <pre>
53
* Certificate ::= SEQUENCE {
54
* tbsCertificate TBSCertificate,
55
* signatureAlgorithm AlgorithmIdentifier,
56
* signature BIT STRING }
57
* </pre>
58
* <p>
59
* These certificates are widely used to support authentication and
60
* other functionality in Internet security systems. Common applications
61
* include Privacy Enhanced Mail (PEM), Transport Layer Security (SSL),
62
* code signing for trusted software distribution, and Secure Electronic
63
* Transactions (SET).
64
* <p>
65
* These certificates are managed and vouched for by <em>Certificate
66
* Authorities</em> (CAs). CAs are services which create certificates by
67
* placing data in the X.509 standard format and then digitally signing
68
* that data. CAs act as trusted third parties, making introductions
69
* between principals who have no direct knowledge of each other.
70
* CA certificates are either signed by themselves, or by some other
71
* CA such as a "root" CA.
72
* <p>
73
* The ASN.1 definition of {@code tbsCertificate} is:
74
* <pre>
75
* TBSCertificate ::= SEQUENCE {
76
* version [0] EXPLICIT Version DEFAULT v1,
77
* serialNumber CertificateSerialNumber,
78
* signature AlgorithmIdentifier,
79
* issuer Name,
80
* validity Validity,
81
* subject Name,
82
* subjectPublicKeyInfo SubjectPublicKeyInfo,
83
* }
84
* </pre>
85
* <p>
86
* Here is sample code to instantiate an X.509 certificate:
87
* <pre>
88
* InputStream inStream = new FileInputStream("fileName-of-cert");
89
* X509Certificate cert = X509Certificate.getInstance(inStream);
90
* inStream.close();
91
* </pre>
92
* OR
93
* <pre>
94
* byte[] certData = &lt;certificate read from a file, say&gt;
95
* X509Certificate cert = X509Certificate.getInstance(certData);
96
* </pre>
97
* <p>
98
* In either case, the code that instantiates an X.509 certificate
99
* consults the value of the {@code cert.provider.x509v1} security property
100
* to locate the actual implementation or instantiates a default implementation.
101
* <p>
102
* The {@code cert.provider.x509v1} property is set to a default
103
* implementation for X.509 such as:
104
* <pre>
105
* cert.provider.x509v1=com.sun.security.cert.internal.x509.X509V1CertImpl
106
* </pre>
107
* <p>
108
* The value of this {@code cert.provider.x509v1} property has to be
109
* changed to instantiate another implementation. If this security
110
* property is not set, a default implementation will be used.
111
* Currently, due to possible security restrictions on access to
112
* Security properties, this value is looked up and cached at class
113
* initialization time and will fallback on a default implementation if
114
* the Security property is not accessible.
115
*
116
* <p><em>Note: The classes in the package {@code javax.security.cert}
117
* exist for compatibility with earlier versions of the
118
* Java Secure Sockets Extension (JSSE). New applications should instead
119
* use the standard Java SE certificate classes located in
120
* {@code java.security.cert}.</em></p>
121
*
122
* @author Hemma Prafullchandra
123
* @since 1.4
124
* @see Certificate
125
* @see java.security.cert.X509Extension
126
* @see java.security.Security security properties
127
*/
128
public abstract class X509Certificate extends Certificate {
129
130
/*
131
* Constant to lookup in the Security properties file.
132
* In the Security properties file the default implementation
133
* for X.509 v3 is given as:
134
* <pre>
135
* cert.provider.x509v1=com.sun.security.cert.internal.x509.X509V1CertImpl
136
* </pre>
137
*/
138
private static final String X509_PROVIDER = "cert.provider.x509v1";
139
private static String X509Provider;
140
141
static {
142
X509Provider = AccessController.doPrivileged(
143
new PrivilegedAction<String>() {
144
public String run() {
145
return Security.getProperty(X509_PROVIDER);
146
}
147
}
148
);
149
}
150
151
/**
152
* Instantiates an X509Certificate object, and initializes it with
153
* the data read from the input stream {@code inStream}.
154
* The implementation (X509Certificate is an abstract class) is
155
* provided by the class specified as the value of the
156
* {@code cert.provider.x509v1} security property.
157
*
158
* <p>Note: Only one DER-encoded
159
* certificate is expected to be in the input stream.
160
* Also, all X509Certificate
161
* subclasses must provide a constructor of the form:
162
* <pre>{@code
163
* public <subClass>(InputStream inStream) ...
164
* }</pre>
165
*
166
* @param inStream an input stream with the data to be read to
167
* initialize the certificate.
168
* @return an X509Certificate object initialized with the data
169
* from the input stream.
170
* @exception CertificateException if a class initialization
171
* or certificate parsing error occurs.
172
*/
173
public static final X509Certificate getInstance(InputStream inStream)
174
throws CertificateException {
175
return getInst((Object)inStream);
176
}
177
178
/**
179
* Instantiates an X509Certificate object, and initializes it with
180
* the specified byte array.
181
* The implementation (X509Certificate is an abstract class) is
182
* provided by the class specified as the value of the
183
* {@code cert.provider.x509v1} security property.
184
*
185
* <p>Note: All X509Certificate
186
* subclasses must provide a constructor of the form:
187
* <pre>{@code
188
* public <subClass>(InputStream inStream) ...
189
* }</pre>
190
*
191
* @param certData a byte array containing the DER-encoded
192
* certificate.
193
* @return an X509Certificate object initialized with the data
194
* from {@code certData}.
195
* @exception CertificateException if a class initialization
196
* or certificate parsing error occurs.
197
*/
198
public static final X509Certificate getInstance(byte[] certData)
199
throws CertificateException {
200
return getInst((Object)certData);
201
}
202
203
private static final X509Certificate getInst(Object value)
204
throws CertificateException {
205
/*
206
* This turns out not to work for now. To run under JDK1.2 we would
207
* need to call beginPrivileged() but we can't do that and run
208
* under JDK1.1.
209
*/
210
String className = X509Provider;
211
if (className == null || className.length() == 0) {
212
// shouldn't happen, but assume corrupted properties file
213
// provide access to sun implementation
214
className = "com.sun.security.cert.internal.x509.X509V1CertImpl";
215
}
216
try {
217
Class<?>[] params = null;
218
if (value instanceof InputStream) {
219
params = new Class<?>[] { InputStream.class };
220
} else if (value instanceof byte[]) {
221
params = new Class<?>[] { value.getClass() };
222
} else
223
throw new CertificateException("Unsupported argument type");
224
Class<?> certClass = Class.forName(className);
225
226
// get the appropriate constructor and instantiate it
227
Constructor<?> cons = certClass.getConstructor(params);
228
229
// get a new instance
230
Object obj = cons.newInstance(new Object[] {value});
231
return (X509Certificate)obj;
232
233
} catch (ClassNotFoundException e) {
234
throw new CertificateException("Could not find class: " + e);
235
} catch (IllegalAccessException e) {
236
throw new CertificateException("Could not access class: " + e);
237
} catch (InstantiationException e) {
238
throw new CertificateException("Problems instantiating: " + e);
239
} catch (InvocationTargetException e) {
240
throw new CertificateException("InvocationTargetException: "
241
+ e.getTargetException());
242
} catch (NoSuchMethodException e) {
243
throw new CertificateException("Could not find class method: "
244
+ e.getMessage());
245
}
246
}
247
248
/**
249
* Checks that the certificate is currently valid. It is if
250
* the current date and time are within the validity period given in the
251
* certificate.
252
* <p>
253
* The validity period consists of two date/time values:
254
* the first and last dates (and times) on which the certificate
255
* is valid. It is defined in
256
* ASN.1 as:
257
* <pre>
258
* validity Validity
259
*
260
* Validity ::= SEQUENCE {
261
* notBefore CertificateValidityDate,
262
* notAfter CertificateValidityDate }
263
*
264
* CertificateValidityDate ::= CHOICE {
265
* utcTime UTCTime,
266
* generalTime GeneralizedTime }
267
* </pre>
268
*
269
* @exception CertificateExpiredException if the certificate has expired.
270
* @exception CertificateNotYetValidException if the certificate is not
271
* yet valid.
272
*/
273
public abstract void checkValidity()
274
throws CertificateExpiredException, CertificateNotYetValidException;
275
276
/**
277
* Checks that the specified date is within the certificate's
278
* validity period. In other words, this determines whether the
279
* certificate would be valid at the specified date/time.
280
*
281
* @param date the Date to check against to see if this certificate
282
* is valid at that date/time.
283
* @exception CertificateExpiredException if the certificate has expired
284
* with respect to the {@code date} supplied.
285
* @exception CertificateNotYetValidException if the certificate is not
286
* yet valid with respect to the {@code date} supplied.
287
* @see #checkValidity()
288
*/
289
public abstract void checkValidity(Date date)
290
throws CertificateExpiredException, CertificateNotYetValidException;
291
292
/**
293
* Gets the {@code version} (version number) value from the
294
* certificate. The ASN.1 definition for this is:
295
* <pre>
296
* version [0] EXPLICIT Version DEFAULT v1
297
*
298
* Version ::= INTEGER { v1(0), v2(1), v3(2) }
299
* </pre>
300
*
301
* @return the version number from the ASN.1 encoding, i.e. 0, 1 or 2.
302
*/
303
public abstract int getVersion();
304
305
/**
306
* Gets the {@code serialNumber} value from the certificate.
307
* The serial number is an integer assigned by the certification
308
* authority to each certificate. It must be unique for each
309
* certificate issued by a given CA (i.e., the issuer name and
310
* serial number identify a unique certificate).
311
* The ASN.1 definition for this is:
312
* <pre>
313
* serialNumber CertificateSerialNumber
314
*
315
* CertificateSerialNumber ::= INTEGER
316
* </pre>
317
*
318
* @return the serial number.
319
*/
320
public abstract BigInteger getSerialNumber();
321
322
/**
323
* Gets the {@code issuer} (issuer distinguished name) value from
324
* the certificate. The issuer name identifies the entity that signed (and
325
* issued) the certificate.
326
*
327
* <p>The issuer name field contains an
328
* X.500 distinguished name (DN).
329
* The ASN.1 definition for this is:
330
* <pre>
331
* issuer Name
332
*
333
* Name ::= CHOICE { RDNSequence }
334
* RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
335
* RelativeDistinguishedName ::=
336
* SET OF AttributeValueAssertion
337
*
338
* AttributeValueAssertion ::= SEQUENCE {
339
* AttributeType,
340
* AttributeValue }
341
* AttributeType ::= OBJECT IDENTIFIER
342
* AttributeValue ::= ANY
343
* </pre>
344
* The {@code Name} describes a hierarchical name composed of
345
* attributes, such as country name, and corresponding values, such as US.
346
* The type of the {@code AttributeValue} component is determined by
347
* the {@code AttributeType}; in general it will be a
348
* {@code directoryString}. A {@code directoryString} is usually
349
* one of {@code PrintableString},
350
* {@code TeletexString} or {@code UniversalString}.
351
*
352
* @return a Principal whose name is the issuer distinguished name.
353
*/
354
public abstract Principal getIssuerDN();
355
356
/**
357
* Gets the {@code subject} (subject distinguished name) value
358
* from the certificate.
359
* The ASN.1 definition for this is:
360
* <pre>
361
* subject Name
362
* </pre>
363
*
364
* <p>See {@link #getIssuerDN() getIssuerDN} for {@code Name}
365
* and other relevant definitions.
366
*
367
* @return a Principal whose name is the subject name.
368
* @see #getIssuerDN()
369
*/
370
public abstract Principal getSubjectDN();
371
372
/**
373
* Gets the {@code notBefore} date from the validity period of
374
* the certificate.
375
* The relevant ASN.1 definitions are:
376
* <pre>
377
* validity Validity
378
*
379
* Validity ::= SEQUENCE {
380
* notBefore CertificateValidityDate,
381
* notAfter CertificateValidityDate }
382
*
383
* CertificateValidityDate ::= CHOICE {
384
* utcTime UTCTime,
385
* generalTime GeneralizedTime }
386
* </pre>
387
*
388
* @return the start date of the validity period.
389
* @see #checkValidity()
390
*/
391
public abstract Date getNotBefore();
392
393
/**
394
* Gets the {@code notAfter} date from the validity period of
395
* the certificate. See {@link #getNotBefore() getNotBefore}
396
* for relevant ASN.1 definitions.
397
*
398
* @return the end date of the validity period.
399
* @see #checkValidity()
400
*/
401
public abstract Date getNotAfter();
402
403
/**
404
* Gets the signature algorithm name for the certificate
405
* signature algorithm. An example is the string "SHA-1/DSA".
406
* The ASN.1 definition for this is:
407
* <pre>
408
* signatureAlgorithm AlgorithmIdentifier
409
*
410
* AlgorithmIdentifier ::= SEQUENCE {
411
* algorithm OBJECT IDENTIFIER,
412
* parameters ANY DEFINED BY algorithm OPTIONAL }
413
* -- contains a value of the type
414
* -- registered for use with the
415
* -- algorithm object identifier value
416
* </pre>
417
*
418
* <p>The algorithm name is determined from the {@code algorithm}
419
* OID string.
420
*
421
* @return the signature algorithm name.
422
*/
423
public abstract String getSigAlgName();
424
425
/**
426
* Gets the signature algorithm OID string from the certificate.
427
* An OID is represented by a set of positive whole numbers separated
428
* by periods.
429
* For example, the string "1.2.840.10040.4.3" identifies the SHA-1
430
* with DSA signature algorithm, as per the PKIX part I.
431
*
432
* <p>See {@link #getSigAlgName() getSigAlgName} for
433
* relevant ASN.1 definitions.
434
*
435
* @return the signature algorithm OID string.
436
*/
437
public abstract String getSigAlgOID();
438
439
/**
440
* Gets the DER-encoded signature algorithm parameters from this
441
* certificate's signature algorithm. In most cases, the signature
442
* algorithm parameters are null; the parameters are usually
443
* supplied with the certificate's public key.
444
*
445
* <p>See {@link #getSigAlgName() getSigAlgName} for
446
* relevant ASN.1 definitions.
447
*
448
* @return the DER-encoded signature algorithm parameters, or
449
* null if no parameters are present.
450
*/
451
public abstract byte[] getSigAlgParams();
452
}
453
454