Skip to content

Commit 4598010

Browse files
committed
Fix wolfSSL#3058: GetSigLen NULL-deref on key->params
wc_LmsKey_GetSigLen dereferenced key->params->sig_len without first guarding against key->params == NULL, so an Init -> GetSigLen call without a SetParameters in between (a plausible verify-only flow) would crash. wc_XmssKey_GetSigLen has a state check that incidentally catches the same condition, but lacks the explicit guard its sibling GetPubLen / GetPrivLen carry. Add (key->params == NULL) to the BAD_FUNC_ARG precondition block in both functions, matching wc_LmsKey_GetPubLen / wc_LmsKey_GetPrivLen and wc_XmssKey_GetPubLen / wc_XmssKey_GetPrivLen. Documented return contracts already list BAD_FUNC_ARG. Negative tests in test_rfc9802_x509_verify now Init a key and call GetSigLen on it without SetParameters, expecting BAD_FUNC_ARG, for both LMS and XMSS. https://claude.ai/code/session_01SnSQMb145Hkyyf7hQQQ8cq
1 parent 94a546d commit 4598010

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

tests/api.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35754,6 +35754,16 @@ static int rfc9802_lms_import_negative(void)
3575435754
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
3575535755
wc_LmsKey_Free(&key);
3575635756

35757+
/* GetSigLen on a key with no params set must not NULL-deref the
35758+
* params pointer; it must return BAD_FUNC_ARG instead. */
35759+
{
35760+
word32 sigLen = 0;
35761+
ExpectIntEQ(wc_LmsKey_Init(&key, NULL, INVALID_DEVID), 0);
35762+
ExpectIntEQ(wc_LmsKey_GetSigLen(&key, &sigLen),
35763+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
35764+
wc_LmsKey_Free(&key);
35765+
}
35766+
3575735767
return EXPECT_RESULT();
3575835768
}
3575935769
#endif
@@ -35789,6 +35799,16 @@ static int rfc9802_xmss_import_negative(void)
3578935799
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
3579035800
wc_XmssKey_Free(&key);
3579135801

35802+
/* GetSigLen on a key with no params set must not NULL-deref the
35803+
* params pointer; it must return BAD_FUNC_ARG instead. */
35804+
{
35805+
word32 sigLen = 0;
35806+
ExpectIntEQ(wc_XmssKey_Init(&key, NULL, INVALID_DEVID), 0);
35807+
ExpectIntEQ(wc_XmssKey_GetSigLen(&key, &sigLen),
35808+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
35809+
wc_XmssKey_Free(&key);
35810+
}
35811+
3579235812
return EXPECT_RESULT();
3579335813
}
3579435814
#endif

wolfcrypt/src/wc_lms.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ int wc_LmsKey_GetSigLen(const LmsKey* key, word32* len)
15311531
int ret = 0;
15321532

15331533
/* Validate parameters. */
1534-
if ((key == NULL) || (len == NULL)) {
1534+
if ((key == NULL) || (len == NULL) || (key->params == NULL)) {
15351535
ret = BAD_FUNC_ARG;
15361536
}
15371537

wolfcrypt/src/wc_xmss.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ int wc_XmssKey_GetSigLen(const XmssKey* key, word32* len)
16521652
int ret = 0;
16531653

16541654
/* Validate parameters. */
1655-
if ((key == NULL) || (len == NULL)) {
1655+
if ((key == NULL) || (len == NULL) || (key->params == NULL)) {
16561656
ret = BAD_FUNC_ARG;
16571657
}
16581658
/* Validate state. */

0 commit comments

Comments
 (0)