From 3a2c83763200b83d92eaab7c0c980abd0ba97461 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Fri, 14 Aug 2026 10:32:49 -0600 Subject: [PATCH] F-9119: validate min key size range before narrowing to word16/short --- native/com_wolfssl_WolfSSLContext.c | 24 ++++++++++++++++--- .../com/wolfssl/test/WolfSSLContextTest.java | 21 ++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/native/com_wolfssl_WolfSSLContext.c b/native/com_wolfssl_WolfSSLContext.c index b992ad1..4797821 100644 --- a/native/com_wolfssl_WolfSSLContext.c +++ b/native/com_wolfssl_WolfSSLContext.c @@ -7088,11 +7088,17 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_setMinDhKeySz WOLFSSL_CTX* ctx = (WOLFSSL_CTX*)(uintptr_t)ctxPtr; (void)obj; - /* wolfSSL_CTX_SetMinDhKey_Sz() sanitizes ctx and keySzBits */ + /* wolfSSL_CTX_SetMinDhKey_Sz() sanitizes ctx */ if (jenv == NULL) { return (jint)BAD_FUNC_ARG; } + /* Validate range before narrowing to word16, otherwise out of range + * value can wrap to a smaller key size */ + if ((keySzBits < 0) || (keySzBits > (jint)WOLFSSL_MAX_16BIT)) { + return (jint)BAD_FUNC_ARG; + } + return (jint)wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)keySzBits); #else (void)jenv; @@ -7110,11 +7116,17 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_setMinRsaKeySz WOLFSSL_CTX* ctx = (WOLFSSL_CTX*)(uintptr_t)ctxPtr; (void)obj; - /* wolfSSL_CTX_SetMinRsaKey_Sz() sanitizes ctx and keySzBits */ + /* wolfSSL_CTX_SetMinRsaKey_Sz() sanitizes ctx */ if (jenv == NULL) { return (jint)BAD_FUNC_ARG; } + /* Validate range before narrowing to short, otherwise out of range value + * can wrap to a smaller key size */ + if ((keySzBits < 0) || (keySzBits > INT16_MAX)) { + return (jint)BAD_FUNC_ARG; + } + return (jint)wolfSSL_CTX_SetMinRsaKey_Sz(ctx, (short)keySzBits); #else (void)jenv; @@ -7132,11 +7144,17 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_setMinEccKeySz WOLFSSL_CTX* ctx = (WOLFSSL_CTX*)(uintptr_t)ctxPtr; (void)obj; - /* wolfSSL_CTX_SetMinEccKey_Sz() sanitizes ctx and keySzBits */ + /* wolfSSL_CTX_SetMinEccKey_Sz() sanitizes ctx */ if (jenv == NULL) { return (jint)BAD_FUNC_ARG; } + /* Validate range before narrowing to short, otherwise out of range value + * can wrap to a smaller key size */ + if ((keySzBits < 0) || (keySzBits > INT16_MAX)) { + return (jint)BAD_FUNC_ARG; + } + return (jint)wolfSSL_CTX_SetMinEccKey_Sz(ctx, (short)keySzBits); #else (void)jenv; diff --git a/src/test/com/wolfssl/test/WolfSSLContextTest.java b/src/test/com/wolfssl/test/WolfSSLContextTest.java index 71e492b..534a4b2 100644 --- a/src/test/com/wolfssl/test/WolfSSLContextTest.java +++ b/src/test/com/wolfssl/test/WolfSSLContextTest.java @@ -762,6 +762,13 @@ public class WolfSSLContextTest { fail("setMinRSAKeySize should fail with negative key size"); } + /* value that wraps across the 16-bit boundary should fail, + * 66560 wraps to 1024. */ + ret = ctx.setMinRSAKeySize(66560); + if (ret != WolfSSL.BAD_FUNC_ARG) { + fail("setMinRSAKeySize should fail for out-of-range value"); + } + /* key length not % 8 should fail */ ret = ctx.setMinRSAKeySize(1023); if (ret != WolfSSL.BAD_FUNC_ARG) { @@ -814,6 +821,13 @@ public class WolfSSLContextTest { fail("setMinECCKeySize should fail with negative key size"); } + /* value that wraps across the 16-bit boundary should fail, + * 66048 narrows to 512. */ + ret = ctx.setMinECCKeySize(66048); + if (ret != WolfSSL.BAD_FUNC_ARG) { + fail("setMinECCKeySize should fail for out-of-range value"); + } + /* valid key length should succeed */ ret = ctx.setMinECCKeySize(128); if (ret != WolfSSL.SSL_SUCCESS) { @@ -868,6 +882,13 @@ public class WolfSSLContextTest { fail("setMinDHKeySize should fail with key size too large"); } + /* value that wraps across the 16-bit boundary should fail, + * 66560 narrows to 1024. */ + ret = ctx.setMinDHKeySize(66560); + if (ret != WolfSSL.BAD_FUNC_ARG) { + fail("setMinDHKeySize should fail for out-of-range value"); + } + /* key length not % 8 should fail */ ret = ctx.setMinDHKeySize(1023); if (ret != WolfSSL.BAD_FUNC_ARG) {