Skip to content

Commit dbc5559

Browse files
committed
Addressing review feedback
1 parent f35faad commit dbc5559

3 files changed

Lines changed: 13 additions & 78 deletions

File tree

src/tls.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ static void TLSX_SNI_FreeAll(SNI* list, void* heap)
21352135
}
21362136

21372137
/** Tells the buffered size of the SNI objects in a list. */
2138-
static word16 TLSX_SNI_GetSize(SNI* list)
2138+
WOLFSSL_TEST_VIS word16 TLSX_SNI_GetSize(SNI* list)
21392139
{
21402140
SNI* sni;
21412141
word32 length = OPAQUE16_LEN; /* list length */
@@ -3241,15 +3241,19 @@ word16 TLSX_CSR_GetSize_ex(CertificateStatusRequest* csr, byte isRequest,
32413241
if (csr->ssl != NULL && SSL_CM(csr->ssl) != NULL &&
32423242
SSL_CM(csr->ssl)->ocsp_stapling != NULL &&
32433243
SSL_CM(csr->ssl)->ocsp_stapling->statusCb != NULL) {
3244+
if (WOLFSSL_MAX_16BIT - OPAQUE8_LEN - OPAQUE24_LEN <
3245+
csr->ssl->ocspCsrResp[idx].length) {
3246+
return 0;
3247+
}
32443248
size = OPAQUE8_LEN + OPAQUE24_LEN +
32453249
csr->ssl->ocspCsrResp[idx].length;
3246-
if (size > WOLFSSL_MAX_16BIT)
3247-
return 0;
32483250
return (word16)size;
32493251
}
3250-
size = OPAQUE8_LEN + OPAQUE24_LEN + csr->responses[idx].length;
3251-
if (size > WOLFSSL_MAX_16BIT)
3252+
if (WOLFSSL_MAX_16BIT - OPAQUE8_LEN - OPAQUE24_LEN <
3253+
csr->responses[idx].length) {
32523254
return 0;
3255+
}
3256+
size = OPAQUE8_LEN + OPAQUE24_LEN + csr->responses[idx].length;
32533257
return (word16)size;
32543258
}
32553259
#else

tests/api/test_tls_ext.c

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -546,66 +546,8 @@ int test_certificate_authorities_client_hello(void) {
546546
return EXPECT_RESULT();
547547
}
548548

549-
#if defined(HAVE_SNI) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
550-
/* Walk the TLSX list to find an extension by type. */
551-
static TLSX* test_TLSX_find_ext_sni(TLSX* list, TLSX_Type type)
552-
{
553-
while (list) {
554-
if (list->type == type)
555-
return list;
556-
list = list->next;
557-
}
558-
return NULL;
559-
}
560-
561-
/* Replicate the SNI size calculation to test overflow protection.
562-
* The fixed TLSX_SNI_GetSize uses word32 internally and returns 0 on
563-
* overflow. This helper mirrors that logic so we can verify the behavior. */
564-
static word16 test_SNI_GetSize(SNI* list)
565-
{
566-
SNI* sni;
567-
word32 length = OPAQUE16_LEN;
568-
569-
while ((sni = list)) {
570-
list = sni->next;
571-
length += ENUM_LEN + OPAQUE16_LEN;
572-
switch (sni->type) {
573-
case WOLFSSL_SNI_HOST_NAME:
574-
length += (word16)XSTRLEN((char*)sni->data.host_name);
575-
break;
576-
}
577-
if (length > WOLFSSL_MAX_16BIT)
578-
return 0;
579-
}
580-
return (word16)length;
581-
}
582-
583-
/* Replicate the old (vulnerable) SNI size calculation using word16 to
584-
* demonstrate the overflow that the fix prevents. */
585-
static word16 test_SNI_GetSize_old(SNI* list)
586-
{
587-
SNI* sni;
588-
word16 length = OPAQUE16_LEN;
589-
590-
while ((sni = list)) {
591-
list = sni->next;
592-
length += ENUM_LEN + OPAQUE16_LEN;
593-
switch (sni->type) {
594-
case WOLFSSL_SNI_HOST_NAME:
595-
/* Intentional truncation to word16 to mirror the original
596-
* vulnerable implementation for testing overflow behavior. */
597-
length += (word16)XSTRLEN((char*)sni->data.host_name);
598-
break;
599-
}
600-
}
601-
return length;
602-
}
603-
604549
/* Test that the SNI size calculation returns 0 on overflow instead of
605550
* wrapping around to a small value (integer overflow vulnerability). */
606-
/* closing #if for helper functions */
607-
#endif /* HAVE_SNI && !NO_WOLFSSL_CLIENT && !NO_TLS */
608-
609551
int test_TLSX_SNI_GetSize_overflow(void)
610552
{
611553
EXPECT_DECLS;
@@ -616,8 +558,6 @@ int test_TLSX_SNI_GetSize_overflow(void)
616558
SNI* head = NULL;
617559
SNI* sni = NULL;
618560
int i;
619-
word16 fixed_size;
620-
word16 old_size;
621561
/* Each SNI adds ENUM_LEN(1) + OPAQUE16_LEN(2) + hostname_len to the size.
622562
* With a 1-byte hostname, each entry adds 4 bytes. Starting from
623563
* OPAQUE16_LEN(2) base, we need enough entries to exceed UINT16_MAX. */
@@ -632,7 +572,7 @@ int test_TLSX_SNI_GetSize_overflow(void)
632572

633573
/* Find the SNI extension and manually build a long chain */
634574
if (EXPECT_SUCCESS()) {
635-
sni_ext = test_TLSX_find_ext_sni(ssl->extensions, TLSX_SERVER_NAME);
575+
sni_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
636576
ExpectNotNull(sni_ext);
637577
}
638578

@@ -661,17 +601,7 @@ int test_TLSX_SNI_GetSize_overflow(void)
661601

662602
if (EXPECT_SUCCESS()) {
663603
/* The fixed calculation should return 0 (overflow detected) */
664-
fixed_size = test_SNI_GetSize((SNI*)sni_ext->data);
665-
ExpectIntEQ(fixed_size, 0);
666-
667-
/* The old (vulnerable) calculation would wrap around to a small value.
668-
* The unwrapped total is approximately OPAQUE16_LEN + num_sni * 4,
669-
* which is ~65,542. After wrapping past UINT16_MAX the result should
670-
* be drastically smaller than that expected total. */
671-
old_size = test_SNI_GetSize_old((SNI*)sni_ext->data);
672-
ExpectIntNE(old_size, 0);
673-
ExpectIntLT(old_size,
674-
OPAQUE16_LEN + num_sni * (ENUM_LEN + OPAQUE16_LEN + 1));
604+
ExpectIntEQ(TLSX_SNI_GetSize((SNI*)sni_ext->data), 0);
675605
}
676606

677607
wolfSSL_free(ssl);

wolfssl/internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3169,7 +3169,7 @@ struct TLSX {
31693169
struct TLSX* next; /* List Behavior */
31703170
};
31713171

3172-
WOLFSSL_LOCAL TLSX* TLSX_Find(TLSX* list, TLSX_Type type);
3172+
WOLFSSL_TEST_VIS TLSX* TLSX_Find(TLSX* list, TLSX_Type type);
31733173
WOLFSSL_LOCAL void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap);
31743174
WOLFSSL_LOCAL void TLSX_FreeAll(TLSX* list, void* heap);
31753175
WOLFSSL_LOCAL int TLSX_SupportExtensions(WOLFSSL* ssl);
@@ -3237,6 +3237,7 @@ WOLFSSL_LOCAL int TLSX_UseSNI(TLSX** extensions, byte type, const void* data,
32373237
WOLFSSL_LOCAL byte TLSX_SNI_Status(TLSX* extensions, byte type);
32383238
WOLFSSL_LOCAL word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type,
32393239
void** data, byte ignoreStatus);
3240+
WOLFSSL_TEST_VIS word16 TLSX_SNI_GetSize(SNI* list);
32403241

32413242
#ifndef NO_WOLFSSL_SERVER
32423243
WOLFSSL_LOCAL void TLSX_SNI_SetOptions(TLSX* extensions, byte type,

0 commit comments

Comments
 (0)