From ef96cec0de14200df3f34cc405c85178321f9834 Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Wed, 11 Nov 2026 05:15:56 +0530 Subject: [PATCH] der: test invalid UTF8 leading continuation bytes directly Exercise UTF8 continuation bytes directly instead of depending on a later flexi-parser error. Fixes: libtom/libtomcrypt#561 Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- tests/der_test.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/der_test.c b/tests/der_test.c index f8625a2b9..8eec96b30 100644 --- a/tests/der_test.c +++ b/tests/der_test.c @@ -1055,6 +1055,29 @@ static int s_der_decode_sequence_flexi_always_OK(const void *in, unsigned long i #endif +/* Issue #561: test the UTF8 error itself, not a later flexi parser error. */ +static void s_der_utf8_continuation_lead_test(void) +{ + unsigned char encoded[] = { 0x0c, 0x02, 0x80, 0x80 }; + static const unsigned char issue_507_utf8[] = { 0x0c, 0x02, 0xbf, 0xbf }; + wchar_t decoded[2]; + unsigned long outlen; + unsigned int lead; + + /* Every continuation byte is invalid as the first byte of a character. + * Supply another continuation byte so the pre-#507 decoder accepts the + * pair instead of failing an unrelated truncated-input check. */ + for (lead = 0x80; lead <= 0xbf; ++lead) { + encoded[2] = (unsigned char)lead; + outlen = LTC_ARRAY_SIZE(decoded); + SHOULD_FAIL_WITH(der_decode_utf8_string(encoded, sizeof(encoded), decoded, &outlen), CRYPT_INVALID_PACKET); + } + + /* Exercise the malformed UTF8 payload from #507 directly as well. */ + outlen = LTC_ARRAY_SIZE(decoded); + SHOULD_FAIL_WITH(der_decode_utf8_string(issue_507_utf8, sizeof(issue_507_utf8), decoded, &outlen), CRYPT_INVALID_PACKET); +} + static void s_der_regression_test(void) { static const unsigned char s_broken_sequence[] = { @@ -1373,6 +1396,7 @@ int der_test(void) der_custom_test(); + s_der_utf8_continuation_lead_test(); s_der_regression_test(); der_toolong_test();