@@ -4077,7 +4077,7 @@ static int TLSX_CSR2_Parse(WOLFSSL* ssl, const byte* input, word16 length,
40774077 return BUFFER_ERROR;
40784078
40794079 ato16(input + offset, &size);
4080- if (length - offset < size)
4080+ if (length - offset - OPAQUE16_LEN < size)
40814081 return BUFFER_ERROR;
40824082
40834083 offset += OPAQUE16_LEN + size;
@@ -6622,11 +6622,27 @@ static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
66226622 /* total length, not include itself */
66236623 ato16(input, &profile_len);
66246624 offset += OPAQUE16_LEN;
6625+ /* Check profile length is not bigger than remaining length. */
6626+ if (profile_len > length - offset) {
6627+ return BUFFER_ERROR;
6628+ }
6629+ /* Protection profiles are 2 bytes long - ensure not an odd no. bytes. */
6630+ if ((profile_len & 1) == 1) {
6631+ return BUFFER_ERROR;
6632+ }
6633+ /* Ignoring srtp_mki field - SRTP Make Key Identifier.
6634+ * Defined to be 0..255 bytes long.
6635+ */
6636+ if ((length - profile_len - offset) > 255) {
6637+ return BUFFER_ERROR;
6638+ }
66256639
66266640 if (!isRequest) {
66276641#ifndef NO_WOLFSSL_CLIENT
6628- if (length < offset + OPAQUE16_LEN)
6642+ /* Only one SRTP Protection Profile can be chosen. */
6643+ if (profile_len != OPAQUE16_LEN) {
66296644 return BUFFER_ERROR;
6645+ }
66306646
66316647 ato16(input + offset, &profile_value);
66326648
@@ -6641,14 +6657,8 @@ static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
66416657 else {
66426658 /* parse remainder one profile at a time, looking for match in CTX */
66436659 ret = 0;
6644- for (i=offset; i<length; i+=OPAQUE16_LEN) {
6645- if (length < (i + OPAQUE16_LEN)) {
6646- WOLFSSL_MSG("Unexpected length when parsing SRTP profile");
6647- ret = BUFFER_ERROR;
6648- break;
6649- }
6650-
6651- ato16(input+i, &profile_value);
6660+ for (i = 0; i < profile_len; i += OPAQUE16_LEN) {
6661+ ato16(input + offset + i, &profile_value);
66526662 /* find first match */
66536663 if (profile_value < 16 &&
66546664 ssl->dtlsSrtpProfiles & (1 << profile_value)) {
@@ -6680,7 +6690,6 @@ static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
66806690 ssl->dtlsSrtpId = 0;
66816691 TLSX_UseSRTP_Free(srtp, ssl->heap);
66826692 }
6683- (void)profile_len;
66846693
66856694 return ret;
66866695}
@@ -7468,7 +7477,7 @@ static int TLSX_CA_Names_Parse(WOLFSSL *ssl, const byte* input,
74687477 return BUFFER_ERROR;
74697478
74707479 while (length) {
7471- word32 idx = 0;
7480+ word16 idx = 0;
74727481 WOLFSSL_X509_NAME* name = NULL;
74737482 int ret = 0;
74747483 int didInit = FALSE;
@@ -7491,7 +7500,7 @@ static int TLSX_CA_Names_Parse(WOLFSSL *ssl, const byte* input,
74917500 ato16(input, &extLen);
74927501 idx += OPAQUE16_LEN;
74937502
7494- if (idx + extLen > length)
7503+ if (extLen > length - idx )
74957504 ret = BUFFER_ERROR;
74967505 }
74977506
@@ -7521,7 +7530,7 @@ static int TLSX_CA_Names_Parse(WOLFSSL *ssl, const byte* input,
75217530 return ret;
75227531
75237532 input += idx;
7524- length -= (word16) idx;
7533+ length -= idx;
75257534 }
75267535 return 0;
75277536}
@@ -7652,12 +7661,11 @@ static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, const byte* input,
76527661 if (length != OPAQUE16_LEN + len)
76537662 return BUFFER_ERROR;
76547663
7664+ /* Truncate hashSigAlgo list if too long. */
7665+ suites->hashSigAlgoSz = len;
76557666 /* Sig Algo list size must be even. */
76567667 if (suites->hashSigAlgoSz % 2 != 0)
76577668 return BUFFER_ERROR;
7658-
7659- /* truncate hashSigAlgo list if too long */
7660- suites->hashSigAlgoSz = len;
76617669 if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
76627670 WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
76637671 suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
@@ -13545,7 +13553,11 @@ static int TLSX_ECH_Parse(WOLFSSL* ssl, const byte* readBuf, word16 size,
1354513553 WOLFSSL_EchConfig* echConfig;
1354613554 byte* aadCopy;
1354713555 byte* readBuf_p = (byte*)readBuf;
13556+ word32 offset = 0;
13557+ word16 len;
13558+
1354813559 WOLFSSL_MSG("TLSX_ECH_Parse");
13560+
1354913561 if (size == 0)
1355013562 return BAD_FUNC_ARG;
1355113563 if (ssl->options.disableECH) {
@@ -13580,43 +13592,58 @@ static int TLSX_ECH_Parse(WOLFSSL* ssl, const byte* readBuf, word16 size,
1358013592 /* read the ech parameters before the payload */
1358113593 ech->type = *readBuf_p;
1358213594 readBuf_p++;
13595+ offset += 1;
1358313596 if (ech->type == ECH_TYPE_INNER) {
1358413597 ech->state = ECH_PARSED_INTERNAL;
1358513598 return 0;
1358613599 }
13587- /* technically the payload would only be 1 byte at this length */
13588- if (size < 11 + ech->encLen)
13589- return BAD_FUNC_ARG;
13600+ /* Must have kdfId, aeadId, configId, enc len and payload len. */
13601+ if (size < offset + 2 + 2 + 1 + 2 + 2) {
13602+ return BUFFER_ERROR;
13603+ }
1359013604 /* read kdfId */
1359113605 ato16(readBuf_p, &ech->cipherSuite.kdfId);
1359213606 readBuf_p += 2;
13607+ offset += 2;
1359313608 /* read aeadId */
1359413609 ato16(readBuf_p, &ech->cipherSuite.aeadId);
1359513610 readBuf_p += 2;
13611+ offset += 2;
1359613612 /* read configId */
1359713613 ech->configId = *readBuf_p;
1359813614 readBuf_p++;
13615+ offset++;
13616+ /* read encLen */
13617+ ato16(readBuf_p, &len);
13618+ readBuf_p += 2;
13619+ offset += 2;
13620+ /* Check encLen isn't more than remaining bytes minus payload length. */
13621+ if (len > size - offset - 2) {
13622+ return BAD_FUNC_ARG;
13623+ }
13624+ if (len > HPKE_Npk_MAX) {
13625+ return BAD_FUNC_ARG;
13626+ }
1359913627 /* only get enc if we don't already have the hpke context */
1360013628 if (ech->hpkeContext == NULL) {
13601- /* read encLen */
13602- ato16(readBuf_p, &ech->encLen);
13603- readBuf_p += 2;
13604- if (ech->encLen > HPKE_Npk_MAX)
13605- return BAD_FUNC_ARG;
1360613629 /* read enc */
13607- XMEMCPY(ech->enc, readBuf_p, ech->encLen);
13608- readBuf_p += ech->encLen;
13609- }
13610- else {
13611- readBuf_p += 2;
13630+ XMEMCPY(ech->enc, readBuf_p, len);
13631+ ech->encLen = len;
1361213632 }
13633+ readBuf_p += len;
13634+ offset += len;
1361313635 /* read hello inner len */
1361413636 ato16(readBuf_p, &ech->innerClientHelloLen);
13637+ readBuf_p += 2;
13638+ offset += 2;
13639+ /* Check payload is no biffer than remaining bytes. */
13640+ if (ech->innerClientHelloLen > size - offset) {
13641+ return BAD_FUNC_ARG;
13642+ }
1361513643 if (ech->innerClientHelloLen < WC_AES_BLOCK_SIZE) {
1361613644 return BUFFER_ERROR;
1361713645 }
1361813646 ech->innerClientHelloLen -= WC_AES_BLOCK_SIZE;
13619- readBuf_p += 2;
1362013647 ech->outerClientPayload = readBuf_p;
1362113648 /* make a copy of the aad */
1362213649 aadCopy = (byte*)XMALLOC(ech->aadLen, ssl->heap,
0 commit comments