Skip to content

Commit 78a5270

Browse files
committed
JCE: use DATE_ERR_OKAY flag when loading expired trust anchors with custom validation date
1 parent 6b316a4 commit 78a5270

5 files changed

Lines changed: 135 additions & 11 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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ static int nativeVerifyCallback(int preverify, WOLFSSL_X509_STORE_CTX* store)
281281
return (int)result;
282282
}
283283

284+
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_WolfSSLCertManager_getWOLFSSL_1LOAD_1FLAG_1DATE_1ERR_1OKAY
285+
(JNIEnv* env, jclass jcl)
286+
{
287+
(void)env;
288+
(void)jcl;
289+
290+
return WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
291+
}
292+
284293
JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_WolfSSLCertManager_CertManagerNew
285294
(JNIEnv* env, jclass jcl)
286295
{
@@ -363,6 +372,31 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_WolfSSLCertManager_CertManager
363372
return (jint)ret;
364373
}
365374

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

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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4461,11 +4461,8 @@ public boolean match(Certificate cert) {
44614461
return false;
44624462
}
44634463
public Object clone() {
4464-
try {
4465-
return super.clone();
4466-
} catch (CloneNotSupportedException e) {
4467-
throw new RuntimeException(e);
4468-
}
4464+
/* Stateless selector, safe to return this */
4465+
return this;
44694466
}
44704467
};
44714468

@@ -4596,8 +4593,7 @@ public void testFallbackBuilderDefersNotBeforeCheckToValidator()
45964593
/**
45974594
* Test end-to-end builder + validator with a valid custom date. Builds a
45984595
* 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).
4596+
* validates the result.
46014597
*/
46024598
@Test
46034599
public void testBuildThenValidateWithValidCustomDate()

0 commit comments

Comments
 (0)