Skip to content

Commit 64aa393

Browse files
committed
tls: fix TLSX_PreSharedKey_GetSize word16 overflow (F-2925)
Both TLSX_PreSharedKey_GetSize and TLSX_PreSharedKey_GetSizeBinders accumulate per-identity bytes into a word16. With enough PSK entries (or large binderLen/identityLen values) the accumulator wraps silently and the caller allocates an undersized extension buffer, which TLSX_PreSharedKey_Write then overflows. Switch both accumulators to word32 and return LENGTH_ERROR when the total would exceed the 16-bit wire length field.
1 parent ed686d9 commit 64aa393

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/tls.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11908,14 +11908,18 @@ static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
1190811908
{
1190911909
if (msgType == client_hello) {
1191011910
/* Length of identities + Length of binders. */
11911-
word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
11911+
word32 len = OPAQUE16_LEN + OPAQUE16_LEN;
1191211912
while (list != NULL) {
1191311913
/* Each entry has: identity, ticket age and binder. */
1191411914
len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
11915-
OPAQUE8_LEN + (word16)list->binderLen;
11915+
OPAQUE8_LEN + (word32)list->binderLen;
11916+
if (len > WOLFSSL_MAX_16BIT) {
11917+
WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
11918+
return LENGTH_ERROR;
11919+
}
1191611920
list = list->next;
1191711921
}
11918-
*pSz += len;
11922+
*pSz += (word16)len;
1191911923
return 0;
1192011924
}
1192111925

@@ -11938,7 +11942,7 @@ static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
1193811942
int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
1193911943
word16* pSz)
1194011944
{
11941-
word16 len;
11945+
word32 len;
1194211946

1194311947
if (msgType != client_hello) {
1194411948
WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
@@ -11948,11 +11952,15 @@ int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
1194811952
/* Length of all binders. */
1194911953
len = OPAQUE16_LEN;
1195011954
while (list != NULL) {
11951-
len += OPAQUE8_LEN + (word16)list->binderLen;
11955+
len += OPAQUE8_LEN + (word32)list->binderLen;
11956+
if (len > WOLFSSL_MAX_16BIT) {
11957+
WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
11958+
return LENGTH_ERROR;
11959+
}
1195211960
list = list->next;
1195311961
}
1195411962

11955-
*pSz = len;
11963+
*pSz = (word16)len;
1195611964
return 0;
1195711965
}
1195811966

0 commit comments

Comments
 (0)