@@ -1573,6 +1573,19 @@ static int Dtls13RtxSendBuffered(WOLFSSL* ssl)
15731573 return 0 ;
15741574}
15751575
1576+ static int Dtls13AcceptFragmented (WOLFSSL * ssl , enum HandShakeType type )
1577+ {
1578+ if (IsEncryptionOn (ssl , 0 ))
1579+ return 1 ;
1580+ if (ssl -> options .side == WOLFSSL_CLIENT_END && type == server_hello )
1581+ return 1 ;
1582+ #ifdef WOLFSSL_DTLS_CH_FRAG
1583+ if (ssl -> options .side == WOLFSSL_SERVER_END && type == client_hello &&
1584+ ssl -> options .dtls13ChFrag && ssl -> options .dtlsStateful )
1585+ return 1 ;
1586+ #endif
1587+ return 0 ;
1588+ }
15761589/**
15771590 * Dtls13HandshakeRecv() - process an handshake message. Deal with
15781591 fragmentation if needed
@@ -1646,13 +1659,35 @@ static int _Dtls13HandshakeRecv(WOLFSSL* ssl, byte* input, word32 size,
16461659 isFirst = fragOff == 0 ;
16471660 isComplete = isFirst && fragLength == messageLength ;
16481661
1649- if (!isComplete && !IsEncryptionOn (ssl , 0 )) {
1662+ if (!isComplete && !Dtls13AcceptFragmented (ssl , handshakeType )) {
1663+ #ifdef WOLFSSL_DTLS_CH_FRAG
1664+ byte tls13 = 0 ;
1665+ /* check if the first CH fragment contains a valid cookie */
1666+ if (ssl -> options .dtls13ChFrag && !ssl -> options .dtlsStateful &&
1667+ isFirst && handshakeType == client_hello &&
1668+ DoClientHelloStateless (ssl , input + idx , fragLength , 1 , & tls13 )
1669+ == 0 && tls13 ) {
1670+ /* We can save this message and continue as stateful. */
1671+ if (ssl -> chGoodCb != NULL ) {
1672+ int cbret = ssl -> chGoodCb (ssl , ssl -> chGoodCtx );
1673+ if (cbret < 0 ) {
1674+ ssl -> error = cbret ;
1675+ WOLFSSL_MSG ("ClientHello Good Cb don't continue error" );
1676+ return WOLFSSL_FATAL_ERROR ;
1677+ }
1678+ }
1679+ WOLFSSL_MSG ("ClientHello fragment verified" );
1680+ }
1681+ else
1682+ #endif
1683+ {
16501684#ifdef WOLFSSL_DEBUG_TLS
1651- WOLFSSL_MSG ("DTLS1.3 not accepting fragmented plaintext message" );
1685+ WOLFSSL_MSG ("DTLS1.3 not accepting fragmented plaintext message" );
16521686#endif /* WOLFSSL_DEBUG_TLS */
1653- /* ignore the message */
1654- * processedSize = idx + fragLength + ssl -> keys .padSz ;
1655- return 0 ;
1687+ /* ignore the message */
1688+ * processedSize = idx + fragLength + ssl -> keys .padSz ;
1689+ return 0 ;
1690+ }
16561691 }
16571692
16581693 usingAsyncCrypto = ssl -> devId != INVALID_DEVID ;
@@ -2369,7 +2404,11 @@ static int Dtls13WriteAckMessage(WOLFSSL* ssl,
23692404 c16toa (msgSz , ackMessage );
23702405 ackMessage += OPAQUE16_LEN ;
23712406
2407+ WOLFSSL_MSG ("write ack records" );
2408+
23722409 while (recordNumberList != NULL ) {
2410+ WOLFSSL_MSG_EX ("epoch %d seq %d" , recordNumberList -> epoch ,
2411+ recordNumberList -> seq );
23732412 c64toa (& recordNumberList -> epoch , ackMessage );
23742413 ackMessage += OPAQUE64_LEN ;
23752414 c64toa (& recordNumberList -> seq , ackMessage );
@@ -2561,10 +2600,13 @@ int DoDtls13Ack(WOLFSSL* ssl, const byte* input, word32 inputSize,
25612600 if (length % (DTLS13_RN_SIZE ) != 0 )
25622601 return PARSE_ERROR ;
25632602
2603+ WOLFSSL_MSG ("read ack records" );
2604+
25642605 ackMessage = input + OPAQUE16_LEN ;
25652606 for (i = 0 ; i < length ; i += DTLS13_RN_SIZE ) {
25662607 ato64 (ackMessage + i , & epoch );
25672608 ato64 (ackMessage + i + OPAQUE64_LEN , & seq );
2609+ WOLFSSL_MSG_EX ("epoch %d seq %d" , epoch , seq );
25682610 Dtls13RtxRemoveRecord (ssl , epoch , seq );
25692611 }
25702612
@@ -2635,28 +2677,20 @@ int SendDtls13Ack(WOLFSSL* ssl)
26352677 if (ret != 0 )
26362678 return ret ;
26372679
2638- if (w64IsZero (ssl -> dtls13EncryptEpoch -> epochNumber )) {
2639-
2640- ret = Dtls13WriteAckMessage (ssl , ssl -> dtls13Rtx .seenRecords , & length );
2641- if (ret != 0 )
2642- return ret ;
2680+ ret = Dtls13WriteAckMessage (ssl , ssl -> dtls13Rtx .seenRecords , & length );
2681+ if (ret != 0 )
2682+ return ret ;
26432683
2644- output = GetOutputBuffer (ssl );
2684+ output = GetOutputBuffer (ssl );
26452685
2686+ if (w64IsZero (ssl -> dtls13EncryptEpoch -> epochNumber )) {
26462687 ret = Dtls13RlAddPlaintextHeader (ssl , output , ack , (word16 )length );
26472688 if (ret != 0 )
26482689 return ret ;
26492690
26502691 ssl -> buffers .outputBuffer .length += length + DTLS_RECORD_HEADER_SZ ;
26512692 }
26522693 else {
2653-
2654- ret = Dtls13WriteAckMessage (ssl , ssl -> dtls13Rtx .seenRecords , & length );
2655- if (ret != 0 )
2656- return ret ;
2657-
2658- output = GetOutputBuffer (ssl );
2659-
26602694 outputSize = ssl -> buffers .outputBuffer .bufferSize -
26612695 ssl -> buffers .outputBuffer .idx -
26622696 ssl -> buffers .outputBuffer .length ;
@@ -2797,4 +2831,16 @@ int Dtls13CheckAEADFailLimit(WOLFSSL* ssl)
27972831}
27982832#endif
27992833
2834+ #ifdef WOLFSSL_DTLS_CH_FRAG
2835+ int wolfSSL_dtls13_allow_ch_frag (WOLFSSL * ssl , int enabled )
2836+ {
2837+ if (ssl -> options .side == WOLFSSL_CLIENT_END ) {
2838+ return WOLFSSL_FAILURE ;
2839+ }
2840+ ssl -> options .dtls13ChFrag = !!enabled ;
2841+ return WOLFSSL_SUCCESS ;
2842+ }
2843+ #endif
2844+
2845+
28002846#endif /* WOLFSSL_DTLS13 */
0 commit comments