@@ -734,6 +734,13 @@ int Dtls13RtxAddAck(WOLFSSL* ssl, w64wrapper epoch, w64wrapper seq)
734734 Dtls13RecordNumber * * prevNext = & ssl -> dtls13Rtx .seenRecords ;
735735 Dtls13RecordNumber * cur = ssl -> dtls13Rtx .seenRecords ;
736736
737+ if (ssl -> dtls13Rtx .seenRecordsCount >= DTLS13_ACK_MAX_RECORDS ) {
738+ #ifdef WOLFSSL_RW_THREADED
739+ wc_UnLockMutex (& ssl -> dtls13Rtx .mutex );
740+ #endif
741+ return 0 ; /* list full, silently drop */
742+ }
743+
737744 for (; cur != NULL ; prevNext = & cur -> next , cur = cur -> next ) {
738745 if (w64Equal (cur -> epoch , epoch ) && w64Equal (cur -> seq , seq )) {
739746 /* already in list. no duplicates. */
@@ -759,6 +766,7 @@ int Dtls13RtxAddAck(WOLFSSL* ssl, w64wrapper epoch, w64wrapper seq)
759766
760767 * prevNext = rn ;
761768 rn -> next = cur ;
769+ ssl -> dtls13Rtx .seenRecordsCount ++ ;
762770 #ifdef WOLFSSL_RW_THREADED
763771 wc_UnLockMutex (& ssl -> dtls13Rtx .mutex );
764772 #endif
@@ -788,6 +796,7 @@ static void Dtls13RtxFlushAcks(WOLFSSL* ssl)
788796 }
789797
790798 ssl -> dtls13Rtx .seenRecords = NULL ;
799+ ssl -> dtls13Rtx .seenRecordsCount = 0 ;
791800 #ifdef WOLFSSL_RW_THREADED
792801 wc_UnLockMutex (& ssl -> dtls13Rtx .mutex );
793802 #endif
@@ -850,6 +859,8 @@ static void Dtls13RtxRemoveCurAck(WOLFSSL* ssl)
850859 w64Equal (rn -> seq , ssl -> keys .curSeq )) {
851860 * prevNext = rn -> next ;
852861 XFREE (rn , ssl -> heap , DYNAMIC_TYPE_DTLS_MSG );
862+ if (ssl -> dtls13Rtx .seenRecordsCount > 0 )
863+ ssl -> dtls13Rtx .seenRecordsCount -- ;
853864#ifdef WOLFSSL_RW_THREADED
854865 wc_UnLockMutex (& ssl -> dtls13Rtx .mutex );
855866#endif
@@ -2563,39 +2574,26 @@ int Dtls13SetRecordNumberKeys(WOLFSSL* ssl, enum encrypt_side side)
25632574 return NOT_COMPILED_IN ;
25642575}
25652576
2566- /* 64 bits epoch + 64 bits sequence */
2567- #define DTLS13_RN_SIZE 16
2568-
2569- static int Dtls13GetAckListLength (Dtls13RecordNumber * list , word16 * length )
2570- {
2571- int numberElements ;
2572-
2573- numberElements = 0 ;
2574-
2575- /* TODO: check that we don't exceed the maximum length */
2576-
2577- while (list != NULL ) {
2578- list = list -> next ;
2579- numberElements ++ ;
2580- }
2581-
2582- * length = (word16 )(DTLS13_RN_SIZE * numberElements );
2583- return 0 ;
2584- }
25852577
25862578int Dtls13WriteAckMessage (WOLFSSL * ssl ,
2587- Dtls13RecordNumber * recordNumberList , word32 * length )
2579+ Dtls13RecordNumber * recordNumberList , word16 recordsCount , word32 * length )
25882580{
25892581 word16 msgSz , headerLength ;
25902582 byte * output , * ackMessage ;
25912583 word32 sendSz ;
2584+ word32 written ;
25922585 int ret ;
25932586
25942587 sendSz = 0 ;
2588+ written = 0 ;
25952589
25962590 if (ssl -> dtls13EncryptEpoch == NULL )
25972591 return BAD_STATE_E ;
25982592
2593+ if (recordsCount > DTLS13_ACK_MAX_RECORDS )
2594+ return BUFFER_E ;
2595+ msgSz = (word16 )(DTLS13_RN_SIZE * recordsCount );
2596+
25992597 if (w64IsZero (ssl -> dtls13EncryptEpoch -> epochNumber )) {
26002598 /* unprotected ACK */
26012599 headerLength = DTLS_RECORD_HEADER_SZ ;
@@ -2605,10 +2603,6 @@ int Dtls13WriteAckMessage(WOLFSSL* ssl,
26052603 sendSz += MAX_MSG_EXTRA ;
26062604 }
26072605
2608- ret = Dtls13GetAckListLength (recordNumberList , & msgSz );
2609- if (ret != 0 )
2610- return ret ;
2611-
26122606 sendSz += headerLength ;
26132607
26142608 /* ACK list 2 bytes length field */
@@ -2631,15 +2625,21 @@ int Dtls13WriteAckMessage(WOLFSSL* ssl,
26312625 WOLFSSL_MSG ("write ack records" );
26322626
26332627 while (recordNumberList != NULL ) {
2628+ if (written + DTLS13_RN_SIZE > msgSz )
2629+ return BUFFER_E ;
26342630 WOLFSSL_MSG_EX ("epoch %d seq %d" , recordNumberList -> epoch ,
26352631 recordNumberList -> seq );
26362632 c64toa (& recordNumberList -> epoch , ackMessage );
26372633 ackMessage += OPAQUE64_LEN ;
26382634 c64toa (& recordNumberList -> seq , ackMessage );
26392635 ackMessage += OPAQUE64_LEN ;
26402636 recordNumberList = recordNumberList -> next ;
2637+ written += DTLS13_RN_SIZE ;
26412638 }
26422639
2640+ if (written != msgSz )
2641+ return BUFFER_E ;
2642+
26432643 * length = msgSz + OPAQUE16_LEN ;
26442644
26452645 return 0 ;
@@ -2750,6 +2750,7 @@ int Dtls13DoScheduledWork(WOLFSSL* ssl)
27502750 tail = & (* tail )-> next ;
27512751 * tail = ssl -> dtls13Rtx .seenRecords ;
27522752 ssl -> dtls13Rtx .seenRecords = NULL ;
2753+ ssl -> dtls13Rtx .seenRecordsCount = 0 ;
27532754 ssl -> dupWrite -> sendAcks = 1 ;
27542755 wc_UnLockMutex (& ssl -> dupWrite -> dupMutex );
27552756 }
@@ -2963,12 +2964,13 @@ int SendDtls13Ack(WOLFSSL* ssl)
29632964 if (ret < 0 )
29642965 return ret ;
29652966#endif
2966- ret = Dtls13WriteAckMessage (ssl , ssl -> dtls13Rtx .seenRecords , & length );
2967+ ret = Dtls13WriteAckMessage (ssl , ssl -> dtls13Rtx .seenRecords ,
2968+ ssl -> dtls13Rtx .seenRecordsCount , & length );
29672969#ifdef WOLFSSL_RW_THREADED
29682970 wc_UnLockMutex (& ssl -> dtls13Rtx .mutex );
29692971#endif
2970- if (ret != 0 )
2971- return ret ;
2972+ if (ret != 0 )
2973+ return ret ;
29722974
29732975 output = GetOutputBuffer (ssl );
29742976
0 commit comments