Skip to content

Commit 5d1b5e7

Browse files
committed
harden TLS extension processing
1 parent 31278ee commit 5d1b5e7

1 file changed

Lines changed: 45 additions & 6 deletions

File tree

src/tls.c

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7592,28 +7592,36 @@ static word16 TLSX_CA_Names_GetSize(void* data)
75927592
{
75937593
WOLFSSL* ssl = (WOLFSSL*)data;
75947594
WOLF_STACK_OF(WOLFSSL_X509_NAME)* names;
7595-
word16 size = 0;
7595+
word32 size = OPAQUE16_LEN;
75967596

7597-
/* Length of names */
7598-
size += OPAQUE16_LEN;
75997597
for (names = SSL_PRIORITY_CA_NAMES(ssl); names != NULL; names = names->next) {
76007598
byte seq[MAX_SEQ_SZ];
76017599
WOLFSSL_X509_NAME* name = names->data.name;
76027600

76037601
if (name != NULL) {
76047602
/* 16-bit length | SEQ | Len | DER of name */
7605-
size += (word16)(OPAQUE16_LEN + SetSequence(name->rawLen, seq) +
7606-
name->rawLen);
7603+
word32 entrySz = (word32)OPAQUE16_LEN +
7604+
(word32)SetSequence(name->rawLen, seq) +
7605+
(word32)name->rawLen;
7606+
/* Truncate at the 16-bit wire limit rather than overflowing
7607+
* a word16 and producing a heap over-write. Write() must
7608+
* mirror this truncation exactly. */
7609+
if (size + entrySz > WOLFSSL_MAX_16BIT) {
7610+
WOLFSSL_MSG("CA Names truncated at 16-bit wire limit");
7611+
break;
7612+
}
7613+
size += entrySz;
76077614
}
76087615
}
7609-
return size;
7616+
return (word16)size;
76107617
}
76117618

76127619
static word16 TLSX_CA_Names_Write(void* data, byte* output)
76137620
{
76147621
WOLFSSL* ssl = (WOLFSSL*)data;
76157622
WOLF_STACK_OF(WOLFSSL_X509_NAME)* names;
76167623
byte* len;
7624+
word32 written = OPAQUE16_LEN;
76177625

76187626
/* Reserve space for the length value */
76197627
len = output;
@@ -7623,6 +7631,14 @@ static word16 TLSX_CA_Names_Write(void* data, byte* output)
76237631
WOLFSSL_X509_NAME* name = names->data.name;
76247632

76257633
if (name != NULL) {
7634+
word32 entrySz = (word32)OPAQUE16_LEN +
7635+
(word32)SetSequence(name->rawLen, seq) +
7636+
(word32)name->rawLen;
7637+
/* Must match GetSize() truncation exactly. */
7638+
if (written + entrySz > WOLFSSL_MAX_16BIT)
7639+
break;
7640+
written += entrySz;
7641+
76267642
c16toa((word16)name->rawLen +
76277643
(word16)SetSequence(name->rawLen, seq), output);
76287644
output += OPAQUE16_LEN;
@@ -17770,6 +17786,20 @@ int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
1777017786
#ifdef WOLFSSL_SRTP
1777117787
case TLSX_USE_SRTP:
1777217788
WOLFSSL_MSG("Use SRTP extension received");
17789+
17790+
#if defined(WOLFSSL_TLS13)
17791+
if (IsAtLeastTLSv1_3(ssl->version)) {
17792+
if (msgType != client_hello &&
17793+
msgType != encrypted_extensions)
17794+
return EXT_NOT_ALLOWED;
17795+
}
17796+
else
17797+
#endif
17798+
{
17799+
if (msgType != client_hello &&
17800+
msgType != server_hello)
17801+
return EXT_NOT_ALLOWED;
17802+
}
1777317803
ret = SRTP_PARSE(ssl, input + offset, size, isRequest);
1777417804
break;
1777517805
#endif
@@ -17864,6 +17894,15 @@ int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
1786417894
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
1786517895
case TLSX_ECH:
1786617896
WOLFSSL_MSG("ECH extension received");
17897+
if (!IsAtLeastTLSv1_3(ssl->version))
17898+
break;
17899+
17900+
if (msgType != client_hello &&
17901+
msgType != encrypted_extensions &&
17902+
msgType != hello_retry_request) {
17903+
return EXT_NOT_ALLOWED;
17904+
}
17905+
1786717906
ret = ECH_PARSE(ssl, input + offset, size, msgType);
1786817907
break;
1786917908
#endif

0 commit comments

Comments
 (0)