F-5739: honor caller-supplied size in WolfSSLContext memrestoreCertCache

pull/406/head
Chris Conlon 2026-08-21 15:05:13 -06:00
parent ebf551a180
commit 912fe9c92f
3 changed files with 21 additions and 7 deletions

View File

@ -1022,16 +1022,19 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLContext_memrestoreCertCache
word32 buffSz = 0;
WOLFSSL_CTX* ctx = (WOLFSSL_CTX*)(uintptr_t)ctxPtr;
(void)jcl;
(void)sz;
if (jenv == NULL || ctx == NULL || mem == NULL) {
return (jint)BAD_FUNC_ARG;
}
buff = (byte*)(*jenv)->GetByteArrayElements(jenv, mem, NULL);
buffSz = (*jenv)->GetArrayLength(jenv, mem);
if (sz <= 0 || sz > (*jenv)->GetArrayLength(jenv, mem)) {
return (jint)BAD_FUNC_ARG;
}
buffSz = (word32)sz;
if (buff != NULL && buffSz > 0) {
buff = (byte*)(*jenv)->GetByteArrayElements(jenv, mem, NULL);
if (buff != NULL) {
ret = wolfSSL_CTX_memrestore_cert_cache(ctx, buff, buffSz);
}

View File

@ -811,11 +811,14 @@ public class WolfSSLContext {
*
* @param mem memory buffer containing the stored certificate cache
* to restore
* @param sz size of the input memory buffer, <b>mem</b>
* @param sz number of bytes from <b>mem</b> to restore, must be
* greater than zero and no larger than the <b>mem</b>
* array length
* @return <b><code>SSL_SUCCESS</code></b> upon success,
* <b><code>SSL_FAILURE</code></b> upon general failure,
* <b><code>BAD_FUNC_ARG</code></b> if null or negative
* parameters are passed in,
* <b><code>BAD_FUNC_ARG</code></b> if <b>mem</b> is
* null, or <b>sz</b> is not positive or larger than
* the <b>mem</b> array length,
* <b><code>BUFFER_E</code></b> if the certificate cache
* memory buffer is too small,
* <b><code>CACHE_MATCH_ERROR</code></b> if the cert cache

View File

@ -297,6 +297,14 @@ public class WolfSSLContextTest {
try {
ret = ctx2.memrestoreCertCache(mem, used[0]);
assertEquals(WolfSSL.SSL_SUCCESS, ret);
/* Ensure we reject bad sizes */
assertEquals(WolfSSL.BAD_FUNC_ARG,
ctx2.memrestoreCertCache(mem, mem.length + 1));
assertEquals(WolfSSL.BAD_FUNC_ARG,
ctx2.memrestoreCertCache(mem, 0));
assertEquals(WolfSSL.BAD_FUNC_ARG,
ctx2.memrestoreCertCache(mem, -1));
} finally {
ctx2.free();
}