Skip to content

Commit c7d5eba

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 65d0880 commit c7d5eba

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
@@ -11911,14 +11911,18 @@ static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
1191111911
{
1191211912
if (msgType == client_hello) {
1191311913
/* Length of identities + Length of binders. */
11914-
word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
11914+
word32 len = OPAQUE16_LEN + OPAQUE16_LEN;
1191511915
while (list != NULL) {
1191611916
/* Each entry has: identity, ticket age and binder. */
1191711917
len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
11918-
OPAQUE8_LEN + (word16)list->binderLen;
11918+
OPAQUE8_LEN + (word32)list->binderLen;
11919+
if (len > WOLFSSL_MAX_16BIT) {
11920+
WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
11921+
return LENGTH_ERROR;
11922+
}
1191911923
list = list->next;
1192011924
}
11921-
*pSz += len;
11925+
*pSz += (word16)len;
1192211926
return 0;
1192311927
}
1192411928

@@ -11941,7 +11945,7 @@ static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
1194111945
int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
1194211946
word16* pSz)
1194311947
{
11944-
word16 len;
11948+
word32 len;
1194511949

1194611950
if (msgType != client_hello) {
1194711951
WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
@@ -11951,11 +11955,15 @@ int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
1195111955
/* Length of all binders. */
1195211956
len = OPAQUE16_LEN;
1195311957
while (list != NULL) {
11954-
len += OPAQUE8_LEN + (word16)list->binderLen;
11958+
len += OPAQUE8_LEN + (word32)list->binderLen;
11959+
if (len > WOLFSSL_MAX_16BIT) {
11960+
WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
11961+
return LENGTH_ERROR;
11962+
}
1195511963
list = list->next;
1195611964
}
1195711965

11958-
*pSz = len;
11966+
*pSz = (word16)len;
1195911967
return 0;
1196011968
}
1196111969

0 commit comments

Comments
 (0)