Skip to content

Commit 8f1586e

Browse files
committed
JCE: use DATE_ERR_OKAY flag when loading expired trust anchors with custom validation date
1 parent bf7dcc9 commit 8f1586e

5 files changed

Lines changed: 134 additions & 6 deletions

File tree

jni/include/com_wolfssl_wolfcrypt_WolfSSLCertManager.h

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jni/jni_wolfssl_cert_manager.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ static int nativeVerifyCallback(int preverify, WOLFSSL_X509_STORE_CTX* store)
281281
return (int)result;
282282
}
283283

284+
JNIEXPORT jint JNICALL
285+
Java_com_wolfssl_wolfcrypt_WolfSSLCertManager_getWOLFSSL_1LOAD_1FLAG_1DATE_1ERR_1OKAY
286+
(JNIEnv* env, jclass jcl)
287+
{
288+
(void)env;
289+
(void)jcl;
290+
291+
return WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
292+
}
293+
284294
JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_WolfSSLCertManager_CertManagerNew
285295
(JNIEnv* env, jclass jcl)
286296
{
@@ -363,6 +373,31 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_WolfSSLCertManager_CertManager
363373
return (jint)ret;
364374
}
365375

376+
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_WolfSSLCertManager_CertManagerLoadCABufferEx
377+
(JNIEnv* env, jclass jcl, jlong cmPtr, jbyteArray in, jlong sz, jint format, jint flags)
378+
{
379+
int ret = 0;
380+
word32 buffSz = 0;
381+
byte* buff = NULL;
382+
WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)(uintptr_t)cmPtr;
383+
(void)jcl;
384+
(void)sz;
385+
386+
if (env == NULL || in == NULL) {
387+
return BAD_FUNC_ARG;
388+
}
389+
390+
buff = (byte*)(*env)->GetByteArrayElements(env, in, NULL);
391+
buffSz = (*env)->GetArrayLength(env, in);
392+
393+
ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, buff, buffSz, format, 0,
394+
(word32)flags);
395+
396+
(*env)->ReleaseByteArrayElements(env, in, (jbyte*)buff, JNI_ABORT);
397+
398+
return (jint)ret;
399+
}
400+
366401
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_WolfSSLCertManager_CertManagerUnloadCAs
367402
(JNIEnv* env, jclass jcl, jlong cmPtr)
368403
{

src/main/java/com/wolfssl/provider/jce/WolfCryptPKIXCertPathValidator.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,15 @@ private void callCertPathCheckers(X509Certificate cert,
569569
*
570570
* @param params PKIXParameters from which to get TrustAnchor Set
571571
* @param cm WolfSSLCertManager to load TrustAnchors into as trusted roots
572+
* @param validationDate custom validation date, or null to use current
573+
* time. When non-null,
574+
* WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY is used to allow
575+
* loading expired/not-yet-valid CAs
572576
*
573577
* @throws CertPathValidatorException on failure to load trust anchors
574578
*/
575-
private void loadTrustAnchorsIntoCertManager(
576-
PKIXParameters params, WolfSSLCertManager cm)
579+
private void loadTrustAnchorsIntoCertManager(PKIXParameters params,
580+
WolfSSLCertManager cm, Date validationDate)
577581
throws CertPathValidatorException {
578582

579583
Set<TrustAnchor> trustAnchors = null;
@@ -601,7 +605,12 @@ private void loadTrustAnchorsIntoCertManager(
601605
X509Certificate anchorCert = anchor.getTrustedCert();
602606
if (anchorCert != null) {
603607
try {
604-
cm.CertManagerLoadCA(anchorCert);
608+
if (validationDate != null) {
609+
cm.CertManagerLoadCA(anchorCert,
610+
WolfSSLCertManager.WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY);
611+
} else {
612+
cm.CertManagerLoadCA(anchorCert);
613+
}
605614

606615
log("loaded TrustAnchor: " +
607616
anchorCert.getSubjectX500Principal().getName());
@@ -1118,7 +1127,8 @@ public CertPathValidatorResult engineValidate(
11181127
/* Load trust anchors into CertManager from PKIXParameters.
11191128
* This must happen before initializing cert path checkers since
11201129
* OCSP validation requires trust anchors to verify responses. */
1121-
loadTrustAnchorsIntoCertManager(pkixParams, cm);
1130+
loadTrustAnchorsIntoCertManager(pkixParams, cm,
1131+
pkixParams.getDate());
11221132

11231133
/* Initialize all PKIXCertPathCheckers before calling check().
11241134
* Store the returned list so we use the same checker instances

src/main/java/com/wolfssl/wolfcrypt/WolfSSLCertManager.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,22 @@ public class WolfSSLCertManager {
5555
/* lock around native WOLFSSL_CERT_MANAGER pointer use */
5656
private final Object cmLock = new Object();
5757

58+
/** Flag to allow loading certs with date errors */
59+
public static final int WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY =
60+
getWOLFSSL_LOAD_FLAG_DATE_ERR_OKAY();
61+
5862
/* Verification callback, null if not set */
5963
private WolfSSLCertManagerVerifyCallback verifyCallback = null;
6064

65+
private static native int getWOLFSSL_LOAD_FLAG_DATE_ERR_OKAY();
66+
6167
static native long CertManagerNew();
6268
static native void CertManagerFree(long cm);
6369
static native int CertManagerLoadCA(long cm, String f, String d);
6470
static native int CertManagerLoadCABuffer(
6571
long cm, byte[] in, long sz, int format);
72+
static native int CertManagerLoadCABufferEx(
73+
long cm, byte[] in, long sz, int format, int flags);
6674
static native int CertManagerUnloadCAs(long cm);
6775
static native int CertManagerVerifyBuffer(
6876
long cm, byte[] in, long sz, int format);
@@ -188,6 +196,66 @@ public synchronized void CertManagerLoadCA(X509Certificate cert)
188196
}
189197
}
190198

199+
/**
200+
* Load CA into CertManager from byte array with extended flags.
201+
*
202+
* @param in byte array holding X.509 certificate to load
203+
* @param sz size of input byte array, bytes
204+
* @param format format of input certificate, either
205+
* WolfCrypt.SSL_FILETYPE_PEM (PEM formatted) or
206+
* WolfCrypt.SSL_FILETYPE_ASN1 (ASN.1/DER).
207+
* @param flags load flags, e.g.
208+
* WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY
209+
*
210+
* @throws IllegalStateException WolfSSLCertManager has been freed
211+
* @throws WolfCryptException on native wolfSSL error
212+
*/
213+
public synchronized void CertManagerLoadCABufferEx(byte[] in, long sz,
214+
int format, int flags)
215+
throws IllegalStateException, WolfCryptException {
216+
217+
int ret = 0;
218+
219+
confirmObjectIsActive();
220+
221+
synchronized (cmLock) {
222+
ret = CertManagerLoadCABufferEx(this.cmPtr, in, sz, format, flags);
223+
if (ret != WolfCrypt.WOLFSSL_SUCCESS) {
224+
throw new WolfCryptException(ret);
225+
}
226+
}
227+
}
228+
229+
/**
230+
* Load CA into CertManager from X509Certificate object with extended flags.
231+
*
232+
* @param cert X509Certificate containing CA cert
233+
* @param flags load flags, e.g.
234+
* WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY
235+
*
236+
* @throws IllegalStateException WolfSSLCertManager has been freed
237+
* @throws WolfCryptException on native wolfSSL error
238+
*/
239+
public synchronized void CertManagerLoadCA(X509Certificate cert, int flags)
240+
throws IllegalStateException, WolfCryptException {
241+
242+
confirmObjectIsActive();
243+
244+
if (cert == null) {
245+
throw new WolfCryptException("Input X509Certificate is null");
246+
}
247+
248+
synchronized (cmLock) {
249+
try {
250+
CertManagerLoadCABufferEx(cert.getEncoded(),
251+
cert.getEncoded().length, WolfCrypt.SSL_FILETYPE_ASN1,
252+
flags);
253+
} catch (CertificateEncodingException e) {
254+
throw new WolfCryptException(e);
255+
}
256+
}
257+
}
258+
191259
/**
192260
* Loads KeyStore certificates into WolfSSLCertManager object.
193261
*

src/test/java/com/wolfssl/provider/jce/test/WolfCryptPKIXCertPathBuilderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4596,8 +4596,7 @@ public void testFallbackBuilderDefersNotBeforeCheckToValidator()
45964596
/**
45974597
* Test end-to-end builder + validator with a valid custom date. Builds a
45984598
* path with expired certs using a date within their validity, then
4599-
* validates the result. Should succeed on all wolfSSL versions (native or
4600-
* Java fallback).
4599+
* validates the result.
46014600
*/
46024601
@Test
46034602
public void testBuildThenValidateWithValidCustomDate()

0 commit comments

Comments
 (0)