forked from wolfSSL/wolfcrypt-jni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWolfCryptPKIXRevocationChecker.java
More file actions
712 lines (621 loc) · 23.4 KB
/
WolfCryptPKIXRevocationChecker.java
File metadata and controls
712 lines (621 loc) · 23.4 KB
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* WolfCryptPKIXRevocationChecker.java
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
package com.wolfssl.provider.jce;
import java.net.URI;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertPathValidatorException;
import java.security.cert.CertPathValidatorException.BasicReason;
import java.security.cert.Extension;
import java.security.cert.PKIXRevocationChecker;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.wolfssl.wolfcrypt.WolfCrypt;
import com.wolfssl.wolfcrypt.WolfSSLCertManager;
import com.wolfssl.wolfcrypt.WolfCryptException;
import java.security.cert.TrustAnchor;
import javax.security.auth.x500.X500Principal;
/**
* wolfJCE implementation of PKIXRevocationChecker.
*
* This implementation supports:
* - OCSP checking via native wolfSSL OCSP implementation
* - CRL checking (when PREFER_CRLS option is set)
* - Standard PKIXRevocationChecker.Option values
* - OCSP responder URL override
* - Pre-loaded OCSP responses (OCSP stapling)
* - Soft-fail exception collection
*
* Note: This checker must be used with WolfCryptPKIXCertPathValidator.
*/
public class WolfCryptPKIXRevocationChecker extends PKIXRevocationChecker {
/* Configuration */
private URI ocspResponder;
private X509Certificate ocspResponderCert;
private List<Extension> ocspExtensions;
private Map<X509Certificate, byte[]> ocspResponses;
private Set<Option> options;
private List<CertPathValidatorException> softFailExceptions;
/* Reference to WolfSSLCertManager for OCSP operations */
private WolfSSLCertManager certManager;
/* State Tracking */
private boolean initialized;
/* Full certificate chain for finding issuers */
private List<X509Certificate> certChain;
/* Trust anchors for determining if issuer is a trust anchor */
private Set<TrustAnchor> trustAnchors;
/* Last applied I/O timeout value from wolfjce.ioTimeout property.
* Used to skip redundant JNI calls when multiple checkers or
* repeated init() calls read the same property value. wolfIO_SetTimeout()
* sets a global value, so all checkers in the JVM share the same timeout.
* Integer.MIN_VALUE indicates no timeout has been applied yet. */
private static volatile int lastAppliedIOTimeout =
Integer.MIN_VALUE;
/**
* Create new WolfCryptPKIXRevocationChecker.
*/
public WolfCryptPKIXRevocationChecker() {
this.ocspResponder = null;
this.ocspResponderCert = null;
this.ocspExtensions = null;
this.ocspResponses = new HashMap<X509Certificate, byte[]>();
this.options = EnumSet.noneOf(Option.class);
this.certManager = null;
this.initialized = false;
this.certChain = null;
this.trustAnchors = null;
this.softFailExceptions =
new ArrayList<CertPathValidatorException>();
}
/**
* Set CertManager for OCSP operations.
*
* @param cm WolfSSLCertManager to use for OCSP checking
*/
public void setCertManager(WolfSSLCertManager cm) {
this.certManager = cm;
}
/**
* Set certificate chain for finding issuers during OCSP validation.
*
* The chain should be ordered from end-entity (index 0) toward root.
* This allows the revocation checker to find the issuer certificate
* for any certificate being checked.
*
* @param chain List of certificates in the chain
*/
public void setCertChain(List<X509Certificate> chain) {
this.certChain = chain;
}
/**
* Set trust anchors for OCSP validation.
*
* Trust anchors are used to determine if a certificate's issuer is a
* trust anchor. If the issuer is a trust anchor with an actual
* certificate, that certificate is loaded so OCSP response verification
* can succeed.
*
* @param anchors Set of trust anchors
*/
public void setTrustAnchors(Set<TrustAnchor> anchors) {
this.trustAnchors = anchors;
}
/**
* Initialize the checker for certificate path validation.
*
* @param forward true if checking in forward direction,
* false for reverse
* @throws CertPathValidatorException if initialization fails
*/
@Override
public void init(boolean forward) throws CertPathValidatorException {
this.initialized = true;
this.softFailExceptions.clear();
/* Set wolfSSL I/O timeout for HTTP-based operations (OCSP lookups,
* CRL fetching) if 'wolfjce.ioTimeout' System property is set. */
setIOTimeoutFromProperty();
/* Verify we have OCSP support if needed */
if (!options.contains(Option.PREFER_CRLS)) {
if (!WolfCrypt.OcspEnabled()) {
String msg = "OCSP not compiled into native wolfSSL";
if (options.contains(Option.SOFT_FAIL)) {
softFailExceptions.add(
new CertPathValidatorException(msg,
null, null, -1,
BasicReason.UNDETERMINED_REVOCATION_STATUS));
return;
} else {
throw new CertPathValidatorException(msg);
}
}
}
/* Configure CertManager if present */
if (certManager != null) {
try {
/* Enable OCSP if not preferring CRLs */
if (!options.contains(Option.PREFER_CRLS)) {
int ocspOptions = 0;
/* Check all certs unless ONLY_END_ENTITY specified */
if (!options.contains(Option.ONLY_END_ENTITY)) {
ocspOptions |= WolfCrypt.WOLFSSL_OCSP_CHECKALL;
}
certManager.CertManagerEnableOCSP(ocspOptions);
/* Set override URL if specified */
if (ocspResponder != null) {
certManager.CertManagerSetOCSPOverrideURL(
ocspResponder.toString());
}
}
} catch (WolfCryptException e) {
if (options.contains(Option.SOFT_FAIL)) {
softFailExceptions.add(
new CertPathValidatorException(
"Failed to initialize OCSP: " + e.getMessage(),
e, null, -1,
BasicReason.UNDETERMINED_REVOCATION_STATUS));
} else {
throw new CertPathValidatorException(
"Failed to initialize OCSP", e);
}
}
}
}
/**
* Check if forward checking is supported.
*
* @return false - wolfSSL validates in reverse order
*/
@Override
public boolean isForwardCheckingSupported() {
return false;
}
/**
* Get set of supported extensions.
*
* @return empty set - no critical extensions are processed
*/
@Override
public Set<String> getSupportedExtensions() {
return Collections.<String>emptySet();
}
/**
* Check the revocation status of a certificate.
*
* @param cert Certificate to check
* @param unresolvedCritExts Collection of unresolved critical extensions
* @throws CertPathValidatorException if certificate is revoked or
* check fails
*/
@Override
public void check(Certificate cert, Collection<String> unresolvedCritExts)
throws CertPathValidatorException {
boolean preferCrls, noFallback;
if (!initialized) {
throw new CertPathValidatorException(
"RevocationChecker not initialized");
}
if (!(cert instanceof X509Certificate)) {
throw new CertPathValidatorException(
"Certificate is not an X509Certificate");
}
X509Certificate x509Cert = (X509Certificate)cert;
/* Check for pre-loaded OCSP response first (OCSP stapling) */
if (ocspResponses.containsKey(x509Cert)) {
checkPreloadedOcspResponse(x509Cert);
return;
}
preferCrls = options.contains(Option.PREFER_CRLS);
noFallback = options.contains(Option.NO_FALLBACK);
if (preferCrls) {
/* PREFER_CRLS set: Skip OCSP here, rely on CRL checking done by
* WolfCryptPKIXCertPathValidator. CRLs are loaded into the
* CertManager via checkRevocationEnabledAndLoadCRLs() and
* checked automatically during CertManagerVerify() which
* happens after this check() method returns.
*
* If NO_FALLBACK is not set, also try OCSP as secondary check. */
if (!noFallback) {
try {
checkOcsp(x509Cert);
} catch (CertPathValidatorException e) {
/* OCSP failed, but CRL is primary - let CRL checking
* in CertManagerVerify() handle revocation status */
handleException(e);
}
}
/* CRL checking happens in CertManagerVerify() after this */
}
else {
/* OCSP is primary revocation check method */
try {
checkOcsp(x509Cert);
return; /* OCSP succeeded */
} catch (CertPathValidatorException e) {
/* OCSP failed. If NO_FALLBACK, fail now. Otherwise, CRL
* checking in CertManagerVerify() serves as implicit
* fallback (if CRL is enabled in PKIXParameters). */
handleException(e);
}
}
}
/**
* Check certificate via OCSP.
*
* @param cert Certificate to check
* @throws CertPathValidatorException if check fails or cert is revoked
*/
private void checkOcsp(X509Certificate cert)
throws CertPathValidatorException {
byte[] certDer;
if (certManager == null) {
throw new CertPathValidatorException(
"CertManager not available for OCSP checking");
}
/* Load issuer cert so OCSP response signature can be verified */
loadIssuerForOcspVerification(cert);
try {
certDer = cert.getEncoded();
certManager.CertManagerCheckOCSP(certDer, certDer.length);
} catch (CertificateEncodingException e) {
throw new CertPathValidatorException(
"Failed to encode certificate", e);
} catch (WolfCryptException e) {
throw new CertPathValidatorException(
"OCSP check failed: " + e.getMessage(), e,
null, -1, BasicReason.UNDETERMINED_REVOCATION_STATUS);
}
}
/**
* Find and load the issuer certificate for OCSP response verification.
*
* OCSP responses are typically signed by the issuer of the certificate
* being checked. This method finds the issuer in the certificate chain
* or trust anchors and loads it into the CertManager so the OCSP
* response signature can be verified.
*
* For the last certificate in the chain, the issuer is typically a
* trust anchor. If the trust anchor has an actual certificate, it is
* loaded for OCSP verification.
*
* @param cert Certificate whose issuer should be loaded
*/
private void loadIssuerForOcspVerification(X509Certificate cert) {
if (certManager == null) {
return;
}
/* Find cert's position in the chain */
int certIndex = -1;
if (certChain != null) {
for (int i = 0; i < certChain.size(); i++) {
if (certChain.get(i).equals(cert)) {
certIndex = i;
break;
}
}
}
/* Issuer is the next cert in the chain (if it exists) */
if (certChain != null && certIndex >= 0 &&
certIndex + 1 < certChain.size()) {
X509Certificate issuer = certChain.get(certIndex + 1);
/* Only load if this is a CA certificate */
if (issuer.getBasicConstraints() >= 0) {
try {
certManager.CertManagerLoadCA(issuer);
} catch (WolfCryptException e) {
/* Ignore - may already be loaded or not needed */
}
}
}
else if (certIndex >= 0 && trustAnchors != null) {
/* Last cert in chain - issuer may be a trust anchor.
* Look for a trust anchor with a certificate that matches
* this cert's issuer. */
X500Principal issuerPrincipal = cert.getIssuerX500Principal();
for (TrustAnchor anchor : trustAnchors) {
X509Certificate anchorCert = anchor.getTrustedCert();
if (anchorCert != null) {
/* Trust anchor has a certificate - check if it's
* the issuer of our cert */
if (anchorCert.getSubjectX500Principal().equals(
issuerPrincipal)) {
try {
certManager.CertManagerLoadCA(anchorCert);
} catch (WolfCryptException e) {
/* Ignore - may already be loaded */
}
break;
}
}
}
}
}
/**
* Check pre-loaded OCSP response.
*
* Note: PKIXParameters.setDate() does not affect OCSP response validation.
* OCSP responses are always validated against current system time by
* wolfSSL. Date override only applies to certificate validity checking.
*
* @param cert Certificate to check
* @throws CertPathValidatorException if response is invalid or
* cert is revoked
*/
private void checkPreloadedOcspResponse(X509Certificate cert)
throws CertPathValidatorException {
byte[] response;
byte[] certDer;
response = ocspResponses.get(cert);
if (response == null || response.length == 0) {
throw new CertPathValidatorException(
"Empty OCSP response for certificate: " +
cert.getSubjectX500Principal());
}
if (certManager == null) {
throw new CertPathValidatorException(
"CertManager not available for OCSP response checking");
}
/* Load issuer cert so OCSP response signature can be verified */
loadIssuerForOcspVerification(cert);
try {
certDer = cert.getEncoded();
certManager.CertManagerCheckOCSPResponse(
response, response.length, certDer, certDer.length);
} catch (CertificateEncodingException e) {
throw new CertPathValidatorException(
"Failed to encode certificate", e);
} catch (WolfCryptException e) {
throw new CertPathValidatorException(
"OCSP response check failed: " + e.getMessage(), e,
null, -1, BasicReason.UNDETERMINED_REVOCATION_STATUS);
}
}
/**
* Handle exception based on SOFT_FAIL option.
*
* @param e Exception to handle
* @throws CertPathValidatorException if not in soft-fail mode
*/
private void handleException(CertPathValidatorException e)
throws CertPathValidatorException {
if (options.contains(Option.SOFT_FAIL)) {
softFailExceptions.add(e);
} else {
throw e;
}
}
/**
* Set OCSP responder URI override.
*
* @param uri OCSP responder URI
*/
@Override
public void setOcspResponder(URI uri) {
this.ocspResponder = uri;
}
/**
* Get OCSP responder URI override.
*
* @return OCSP responder URI or null if not set
*/
@Override
public URI getOcspResponder() {
return this.ocspResponder;
}
/**
* Set OCSP responder certificate.
*
* @param cert OCSP responder certificate
*/
@Override
public void setOcspResponderCert(X509Certificate cert) {
this.ocspResponderCert = cert;
}
/**
* Get OCSP responder certificate.
*
* @return OCSP responder certificate or null if not set
*/
@Override
public X509Certificate getOcspResponderCert() {
return this.ocspResponderCert;
}
/**
* Set OCSP extensions.
*
* @param extensions List of OCSP extensions
*/
@Override
public void setOcspExtensions(List<Extension> extensions) {
if (extensions == null) {
this.ocspExtensions = null;
} else {
this.ocspExtensions =
new ArrayList<Extension>(extensions);
}
}
/**
* Get OCSP extensions.
*
* @return List of OCSP extensions, empty list if not set
*/
@Override
public List<Extension> getOcspExtensions() {
if (this.ocspExtensions == null) {
return Collections.emptyList();
}
return Collections.unmodifiableList(this.ocspExtensions);
}
/**
* Set pre-loaded OCSP responses (for OCSP stapling).
*
* @param responses Map of certificates to OCSP response bytes
*/
@Override
public void setOcspResponses(Map<X509Certificate, byte[]> responses) {
if (responses == null) {
this.ocspResponses = new HashMap<X509Certificate, byte[]>();
} else {
this.ocspResponses =
new HashMap<X509Certificate, byte[]>(responses);
}
}
/**
* Get pre-loaded OCSP responses.
*
* Returns the internal mutable map, not an unmodifiable copy.
* JDK sun.security.validator.PKIXValidator.addResponses() expects
* to be able to add OCSP responses to this map when using the internal
* Validator API.
*
* @return Map of certificates to OCSP response bytes
*/
@Override
public Map<X509Certificate, byte[]> getOcspResponses() {
return this.ocspResponses;
}
/**
* Set revocation checker options.
*
* @param options Set of Option values
*/
@Override
public void setOptions(Set<Option> options) {
if (options == null) {
this.options = EnumSet.noneOf(Option.class);
} else {
this.options = EnumSet.copyOf(options);
}
}
/**
* Get revocation checker options.
*
* @return Set of Option values
*/
@Override
public Set<Option> getOptions() {
return Collections.unmodifiableSet(this.options);
}
/**
* Get list of exceptions encountered in soft-fail mode.
*
* @return List of CertPathValidatorException from soft-fail checks
*/
@Override
public List<CertPathValidatorException> getSoftFailExceptions() {
return Collections.unmodifiableList(this.softFailExceptions);
}
/**
* Read and apply wolfjce.ioTimeout system property.
*
* Sets the native wolfSSL I/O timeout via wolfIO_SetTimeout()
* if the property is set and valid. If the property is set but
* contains an invalid value, throws CertPathValidatorException
* to fail revocation checker initialization.
*
* Note: The native timeout is a global (process-wide) setting
* shared by all threads and validations in the JVM. To reduce
* redundant JNI calls, the parsed value is compared against
* the last applied value and the native call is skipped if
* unchanged.
*
* @throws CertPathValidatorException if property value is
* invalid (not a number, negative, exceeds max, or
* HAVE_IO_TIMEOUT not compiled in)
*/
private void setIOTimeoutFromProperty() throws CertPathValidatorException {
int timeoutSec;
String ioTimeout;
try {
ioTimeout = System.getProperty("wolfjce.ioTimeout");
} catch (SecurityException e) {
/* SecurityManager blocked property access, treat as
* property not set and continue without timeout */
return;
}
if (ioTimeout == null) {
return;
}
final String trimmed = ioTimeout.trim();
if (trimmed.isEmpty()) {
return;
}
try {
timeoutSec = Integer.parseInt(trimmed);
/* Skip JNI call if value unchanged from last apply */
if (timeoutSec != lastAppliedIOTimeout) {
WolfCrypt.setIOTimeout(timeoutSec);
lastAppliedIOTimeout = timeoutSec;
WolfCryptDebug.log(
WolfCryptPKIXRevocationChecker.class,
WolfCryptDebug.INFO,
() -> "wolfjce.ioTimeout set to " +
trimmed + " seconds");
}
} catch (NumberFormatException e) {
throw new CertPathValidatorException(
"Invalid wolfjce.ioTimeout value: " + trimmed +
", must be integer seconds: " + e.getMessage(), e);
} catch (IllegalArgumentException e) {
throw new CertPathValidatorException(
"Invalid wolfjce.ioTimeout value: " + trimmed +
": " + e.getMessage(), e);
} catch (WolfCryptException e) {
throw new CertPathValidatorException(
"wolfjce.ioTimeout set but native wolfSSL not " +
"compiled with HAVE_IO_TIMEOUT: " + e.getMessage(), e);
}
}
/**
* Clone this revocation checker.
*
* Clones configuration state (OCSP responder, options, responses, etc.)
* but not validation state (certManager, certChain, trustAnchors) which
* is set fresh by the CertPathValidator before each validation.
*
* @return Cloned WolfCryptPKIXRevocationChecker
*/
@Override
public WolfCryptPKIXRevocationChecker clone() {
WolfCryptPKIXRevocationChecker cloned =
new WolfCryptPKIXRevocationChecker();
/* Clone configuration state */
cloned.ocspResponder = this.ocspResponder;
cloned.ocspResponderCert = this.ocspResponderCert;
if (this.ocspExtensions != null) {
cloned.ocspExtensions = new ArrayList<Extension>(
this.ocspExtensions);
}
cloned.ocspResponses = new HashMap<X509Certificate, byte[]>(
this.ocspResponses);
cloned.options = EnumSet.copyOf(this.options);
/* Note: certManager, certChain, and trustAnchors are not cloned.
* These are set by WolfCryptPKIXCertPathValidator before each
* validation call, so cloning them is unnecessary. */
return cloned;
}
}