-
Notifications
You must be signed in to change notification settings - Fork 200
/
X509CertImpl.java
660 lines (599 loc) · 22.3 KB
/
X509CertImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Alexander Y. Kleymenov
* @version $Revision$
*/
package org.apache.harmony.security.provider.cert;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Principal;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.security.auth.x500.X500Principal;
import org.apache.harmony.security.internal.nls.Messages;
import org.apache.harmony.security.utils.AlgNameMapper;
import org.apache.harmony.security.x509.Certificate;
import org.apache.harmony.security.x509.Extension;
import org.apache.harmony.security.x509.Extensions;
import org.apache.harmony.security.x509.TBSCertificate;
// BEGIN android-added
import java.security.interfaces.RSAPublicKey;
import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
// END android-added
/**
* This class is an implementation of X509Certificate. It wraps
* the instance of org.apache.harmony.security.x509.Certificate
* built on the base of provided ASN.1 DER encoded form of
* Certificate structure (as specified in RFC 3280
* http://www.ietf.org/rfc/rfc3280.txt).
* @see org.apache.harmony.security.x509.Certificate
* @see java.security.cert.X509Certificate
*/
public class X509CertImpl extends X509Certificate {
/**
* @serial
*/
private static final long serialVersionUID = 2972248729446736154L;
// the core object to be wrapped in X509Certificate
private final Certificate certificate;
// to speed up access to the info, the following fields
// cache values retrieved from the certificate object
private final TBSCertificate tbsCert;
private final Extensions extensions;
private long notBefore = -1;
private long notAfter;
private BigInteger serialNumber;
private X500Principal issuer;
private X500Principal subject;
private byte[] tbsCertificate;
private byte[] signature;
private String sigAlgName;
private String sigAlgOID;
private byte[] sigAlgParams;
// indicates whether the signature algorithm parameters are null
private boolean nullSigAlgParams;
private PublicKey publicKey;
// encoding of the certificate
// BEGIN android-changed
private volatile byte[] encoding;
// END android-changed
//
// ---------------------- Constructors -------------------------------
//
/**
* Constructs the instance on the base of ASN.1 encoded
* form of X.509 certificate provided via stream parameter.
* @param in input stream containing ASN.1 encoded form of certificate.
* @throws CertificateException if some decoding problems occur.
*/
public X509CertImpl(InputStream in) throws CertificateException {
try {
// decode the Certificate object
this.certificate = (Certificate) Certificate.ASN1.decode(in);
// cache the values of TBSCertificate and Extensions
this.tbsCert = certificate.getTbsCertificate();
this.extensions = tbsCert.getExtensions();
} catch (IOException e) {
throw new CertificateException(e);
}
}
/**
* Constructs the instance on the base of existing Certificate object to
* be wrapped.
*/
public X509CertImpl(Certificate certificate) {
this.certificate = certificate;
// cache the values of TBSCertificate and Extensions
this.tbsCert = certificate.getTbsCertificate();
this.extensions = tbsCert.getExtensions();
}
/**
* Constructs the instance on the base of ASN.1 encoded
* form of X.509 certificate provided via array of bytes.
* @param encoding byte array containing ASN.1 encoded form of certificate.
* @throws IOException if some decoding problems occur.
*/
public X509CertImpl(byte[] encoding) throws IOException {
this((Certificate) Certificate.ASN1.decode(encoding));
}
//
// ----------------- Public methods implementations ------------------
//
/**
* @see java.security.cert.X509Certificate#checkValidity()
* method documentation for more information.
*/
public void checkValidity() throws CertificateExpiredException,
CertificateNotYetValidException {
if (notBefore == -1) {
// retrieve and cache the value of validity period
notBefore = tbsCert.getValidity().getNotBefore().getTime();
notAfter = tbsCert.getValidity().getNotAfter().getTime();
}
long time = System.currentTimeMillis();
if (time < notBefore) {
throw new CertificateNotYetValidException();
}
if (time > notAfter) {
throw new CertificateExpiredException();
}
}
/**
* @see java.security.cert.X509Certificate#checkValidity(Date)
* method documentation for more information.
*/
public void checkValidity(Date date)
throws CertificateExpiredException,
CertificateNotYetValidException {
if (notBefore == -1) {
// retrieve and cache the value of validity period
notBefore = tbsCert.getValidity().getNotBefore().getTime();
notAfter = tbsCert.getValidity().getNotAfter().getTime();
}
long time = date.getTime();
if (time < notBefore) {
// BEGIN android-changed
throw new CertificateNotYetValidException("current time: " + date
+ ", validation time: " + new Date(notBefore));
// END android-changed
}
if (time > notAfter) {
// BEGIN android-changed
throw new CertificateExpiredException("current time: " + date
+ ", expiration time: " + new Date(notAfter));
// END android-changed
}
}
/**
* @see java.security.cert.X509Certificate#getVersion()
* method documentation for more information.
*/
public int getVersion() {
return tbsCert.getVersion() + 1;
}
/**
* @see java.security.cert.X509Certificate#getSerialNumber()
* method documentation for more information.
*/
public BigInteger getSerialNumber() {
if (serialNumber == null) {
serialNumber = tbsCert.getSerialNumber();
}
return serialNumber;
}
/**
* @see java.security.cert.X509Certificate#getIssuerDN()
* method documentation for more information.
*/
public Principal getIssuerDN() {
if (issuer == null) {
// retrieve the issuer's principal
issuer = tbsCert.getIssuer().getX500Principal();
}
return issuer;
}
/**
* @see java.security.cert.X509Certificate#getIssuerX500Principal()
* method documentation for more information.
*/
public X500Principal getIssuerX500Principal() {
if (issuer == null) {
// retrieve the issuer's principal
issuer = tbsCert.getIssuer().getX500Principal();
}
return issuer;
}
/**
* @see java.security.cert.X509Certificate#getSubjectDN()
* method documentation for more information.
*/
public Principal getSubjectDN() {
if (subject == null) {
// retrieve the subject's principal
subject = tbsCert.getSubject().getX500Principal();
}
return subject;
}
/**
* @see java.security.cert.X509Certificate#getSubjectX500Principal()
* method documentation for more information.
*/
public X500Principal getSubjectX500Principal() {
if (subject == null) {
// retrieve the subject's principal
subject = tbsCert.getSubject().getX500Principal();
}
return subject;
}
/**
* @see java.security.cert.X509Certificate#getNotBefore()
* method documentation for more information.
*/
public Date getNotBefore() {
if (notBefore == -1) {
// the value was not retrieved from the certificate, do it:
notBefore = tbsCert.getValidity().getNotBefore().getTime();
notAfter = tbsCert.getValidity().getNotAfter().getTime();
}
return new Date(notBefore);
}
/**
* @see java.security.cert.X509Certificate#getNotAfter()
* method documentation for more information.
*/
public Date getNotAfter() {
if (notBefore == -1) {
// the value was not retrieved from the certificate, do it:
notBefore = tbsCert.getValidity().getNotBefore().getTime();
notAfter = tbsCert.getValidity().getNotAfter().getTime();
}
return new Date(notAfter);
}
/**
* @see java.security.cert.X509Certificate#getTBSCertificate()
* method documentation for more information.
*/
public byte[] getTBSCertificate()
throws CertificateEncodingException {
if (tbsCertificate == null) {
// retrieve the encoded form of the TBSCertificate structure
tbsCertificate = tbsCert.getEncoded();
}
byte[] result = new byte[tbsCertificate.length];
System.arraycopy(tbsCertificate, 0, result, 0, tbsCertificate.length);
return result;
}
/**
* @see java.security.cert.X509Certificate#getSignature()
* method documentation for more information.
*/
public byte[] getSignature() {
if (signature == null) {
// retrieve the value of the signature
signature = certificate.getSignatureValue();
}
byte[] result = new byte[signature.length];
System.arraycopy(signature, 0, result, 0, signature.length);
return result;
}
/**
* @see java.security.cert.X509Certificate#getSigAlgName()
* method documentation for more information.
*/
public String getSigAlgName() {
if (sigAlgOID == null) {
// if info was not retrieved (and cached), do it:
sigAlgOID = tbsCert.getSignature().getAlgorithm();
// retrieve the name of the signing algorithm
sigAlgName = AlgNameMapper.map2AlgName(sigAlgOID);
if (sigAlgName == null) {
// if could not be found, use OID as a name
sigAlgName = sigAlgOID;
}
}
return sigAlgName;
}
/**
* @see java.security.cert.X509Certificate#getSigAlgOID()
* method documentation for more information.
*/
public String getSigAlgOID() {
if (sigAlgOID == null) {
// if info was not retrieved (and cached), do it:
sigAlgOID = tbsCert.getSignature().getAlgorithm();
// retrieve the name of the signing algorithm
sigAlgName = AlgNameMapper.map2AlgName(sigAlgOID);
if (sigAlgName == null) {
// if could not be found, use OID as a name
sigAlgName = sigAlgOID;
}
}
return sigAlgOID;
}
/**
* @see java.security.cert.X509Certificate#getSigAlgParams()
* method documentation for more information.
*/
public byte[] getSigAlgParams() {
if (nullSigAlgParams) {
return null;
}
if (sigAlgParams == null) {
sigAlgParams = tbsCert.getSignature().getParameters();
if (sigAlgParams == null) {
nullSigAlgParams = true;
return null;
}
}
return sigAlgParams;
}
/**
* @see java.security.cert.X509Certificate#getIssuerUniqueID()
* method documentation for more information.
*/
public boolean[] getIssuerUniqueID() {
return tbsCert.getIssuerUniqueID();
}
/**
* @see java.security.cert.X509Certificate#getSubjectUniqueID()
* method documentation for more information.
*/
public boolean[] getSubjectUniqueID() {
return tbsCert.getSubjectUniqueID();
}
/**
* @see java.security.cert.X509Certificate#getKeyUsage()
* method documentation for more information.
*/
public boolean[] getKeyUsage() {
if (extensions == null) {
return null;
}
return extensions.valueOfKeyUsage();
}
/**
* @see java.security.cert.X509Certificate#getExtendedKeyUsage()
* method documentation for more information.
*/
public List/*<String>*/ getExtendedKeyUsage()
throws CertificateParsingException {
if (extensions == null) {
return null;
}
try {
return extensions.valueOfExtendedKeyUsage();
} catch (IOException e) {
throw new CertificateParsingException(e);
}
}
/**
* @see java.security.cert.X509Certificate#getBasicConstraints()
* method documentation for more information.
*/
public int getBasicConstraints() {
if (extensions == null) {
return Integer.MAX_VALUE;
}
return extensions.valueOfBasicConstrains();
}
/**
* @see java.security.cert.X509Certificate#getSubjectAlternativeNames()
* method documentation for more information.
*/
public Collection/*<List<?>>*/ getSubjectAlternativeNames()
throws CertificateParsingException {
if (extensions == null) {
return null;
}
try {
// Retrieve the extension value from the cached extensions object
// This extension is not checked for correctness during
// certificate generation, so now it can throw exception
return extensions.valueOfSubjectAlternativeName();
} catch (IOException e) {
throw new CertificateParsingException(e);
}
}
/**
* @see java.security.cert.X509Certificate#getIssuerAlternativeNames()
* method documentation for more information.
*/
public Collection/*FIXME <List<?>>*/ getIssuerAlternativeNames()
throws CertificateParsingException {
if (extensions == null) {
return null;
}
try {
// Retrieve the extension value from the cached extensions object
// This extension is not checked for correctness during
// certificate generation, so now it can throw exception
return extensions.valueOfIssuerAlternativeName();
} catch (IOException e) {
throw new CertificateParsingException(e);
}
}
//
// ----- java.security.cert.Certificate methods implementations ------
//
/**
* @see java.security.cert.Certificate#getEncoded()
* method documentation for more information.
*/
public byte[] getEncoded() throws CertificateEncodingException {
if (encoding == null) {
encoding = certificate.getEncoded();
}
byte[] result = new byte[encoding.length];
System.arraycopy(encoding, 0, result, 0, encoding.length);
return result;
}
/**
* @see java.security.cert.Certificate#getPublicKey()
* method documentation for more information.
*/
public PublicKey getPublicKey() {
if (publicKey == null) {
// retrieve the public key from SubjectPublicKeyInfo
// substructure of X.509 certificate
publicKey = tbsCert.getSubjectPublicKeyInfo().getPublicKey();
}
return publicKey;
}
/**
* @see java.security.cert.Certificate#toString()
* method documentation for more information.
*/
public String toString() {
return certificate.toString();
}
/**
* Verifies the signature of the certificate.
* @see java.security.cert.Certificate#verify(PublicKey)
* method documentation for more information.
*/
public void verify(PublicKey key)
throws CertificateException, NoSuchAlgorithmException,
InvalidKeyException, NoSuchProviderException,
SignatureException {
// BEGIN android-added
if (getSigAlgName().endsWith("withRSA")) {
fastVerify(key);
return;
}
// END android-added
Signature signature = Signature.getInstance(getSigAlgName());
signature.initVerify(key);
// retrieve the encoding of the TBSCertificate structure
if (tbsCertificate == null) {
tbsCertificate = tbsCert.getEncoded();
}
// compute and verify the signature
signature.update(tbsCertificate, 0, tbsCertificate.length);
if (!signature.verify(certificate.getSignatureValue())) {
throw new SignatureException(Messages.getString("security.15C")); //$NON-NLS-1$
}
}
/**
* Verifies the signature of the certificate.
* @see java.security.cert.Certificate#verify(PublicKey,String)
* method documentation for more information.
*/
public void verify(PublicKey key, String sigProvider)
throws CertificateException, NoSuchAlgorithmException,
InvalidKeyException, NoSuchProviderException,
SignatureException {
// BEGIN android-added
if (getSigAlgName().endsWith("withRSA")) {
fastVerify(key);
return;
}
// END android-added
Signature signature =
Signature.getInstance(getSigAlgName(), sigProvider);
signature.initVerify(key);
// retrieve the encoding of the TBSCertificate structure
if (tbsCertificate == null) {
tbsCertificate = tbsCert.getEncoded();
}
// compute and verify the signature
signature.update(tbsCertificate, 0, tbsCertificate.length);
if (!signature.verify(certificate.getSignatureValue())) {
throw new SignatureException(Messages.getString("security.15C")); //$NON-NLS-1$
}
}
// BEGIN android-added
/**
* Implements a faster RSA verification method that delegates to OpenSSL
* native code. In all other aspects it behaves just like the ordinary
* {@link verify} method.
*
* @param key The RSA public key to use
*
* @throws SignatureException If the verification fails.
* @throws InvalidKeyException
*/
private void fastVerify(PublicKey key) throws SignatureException,
InvalidKeyException, NoSuchAlgorithmException {
if (!(key instanceof RSAPublicKey)) {
throw new InvalidKeyException(Messages.getString("security.15C1"));
}
RSAPublicKey rsaKey = (RSAPublicKey) key;
String algorithm = getSigAlgName();
// We don't support MD2 anymore. This needs to also check for aliases
// and OIDs.
if ("MD2withRSA".equalsIgnoreCase(algorithm) ||
"MD2withRSAEncryption".equalsIgnoreCase(algorithm) ||
"1.2.840.113549.1.1.2".equalsIgnoreCase(algorithm) ||
"MD2/RSA".equalsIgnoreCase(algorithm)) {
throw new NoSuchAlgorithmException(algorithm);
}
int i = algorithm.indexOf("with");
algorithm = algorithm.substring(i + 4) + "-" + algorithm.substring(0, i);
if (tbsCertificate == null) {
tbsCertificate = tbsCert.getEncoded();
}
byte[] sig = certificate.getSignatureValue();
if (!OpenSSLSocketImpl.verifySignature(tbsCertificate, sig, algorithm, rsaKey)) {
throw new SignatureException(Messages.getString("security.15C")); //$NON-NLS-1$
}
}
// END android-added
//
// ----- java.security.cert.X509Extension methods implementations ----
//
/**
* @see java.security.cert.X509Extension#getNonCriticalExtensionOIDs()
* method documentation for more information.
*/
public Set getNonCriticalExtensionOIDs() {
if (extensions == null) {
return null;
}
// retrieve the info from the cached extensions object
return extensions.getNonCriticalExtensions();
}
/**
* @see java.security.cert.X509Extension#getCriticalExtensionOIDs()
* method documentation for more information.
*/
public Set getCriticalExtensionOIDs() {
if (extensions == null) {
return null;
}
// retrieve the info from the cached extensions object
return extensions.getCriticalExtensions();
}
/**
* @see java.security.cert.X509Extension#getExtensionValue(String)
* method documentation for more information.
*/
public byte[] getExtensionValue(String oid) {
if (extensions == null) {
return null;
}
// retrieve the info from the cached extensions object
Extension ext = extensions.getExtensionByOID(oid);
return (ext == null) ? null : ext.getRawExtnValue();
}
/**
* @see java.security.cert.X509Extension#hasUnsupportedCriticalExtension()
* method documentation for more information.
*/
public boolean hasUnsupportedCriticalExtension() {
if (extensions == null) {
return false;
}
// retrieve the info from the cached extensions object
return extensions.hasUnsupportedCritical();
}
}