@@ -1637,6 +1637,102 @@ static int Dtls13AcceptFragmented(WOLFSSL *ssl, enum HandShakeType type)
16371637#endif
16381638 return 0 ;
16391639}
1640+
1641+ int Dtls13CheckEpoch (WOLFSSL * ssl , enum HandShakeType type )
1642+ {
1643+ w64wrapper plainEpoch = w64From32 (0x0 , 0x0 );
1644+ w64wrapper hsEpoch = w64From32 (0x0 , DTLS13_EPOCH_HANDSHAKE );
1645+ w64wrapper t0Epoch = w64From32 (0x0 , DTLS13_EPOCH_TRAFFIC0 );
1646+
1647+ if (IsAtLeastTLSv1_3 (ssl -> version )) {
1648+ switch (type ) {
1649+ case client_hello :
1650+ case server_hello :
1651+ case hello_verify_request :
1652+ case hello_retry_request :
1653+ case hello_request :
1654+ if (!w64Equal (ssl -> keys .curEpoch64 , plainEpoch )) {
1655+ WOLFSSL_MSG ("Msg should be epoch 0" );
1656+ WOLFSSL_ERROR_VERBOSE (SANITY_MSG_E );
1657+ return SANITY_MSG_E ;
1658+ }
1659+ break ;
1660+ case encrypted_extensions :
1661+ case server_key_exchange :
1662+ case server_hello_done :
1663+ case client_key_exchange :
1664+ if (!w64Equal (ssl -> keys .curEpoch64 , hsEpoch )) {
1665+ if (ssl -> options .side == WOLFSSL_CLIENT_END &&
1666+ ssl -> options .serverState < SERVER_HELLO_COMPLETE ) {
1667+ /* before processing SH we don't know which version
1668+ * will be negotiated. */
1669+ if (!w64Equal (ssl -> keys .curEpoch64 , plainEpoch )) {
1670+ WOLFSSL_MSG ("Msg should be epoch 2 or 0" );
1671+ WOLFSSL_ERROR_VERBOSE (SANITY_MSG_E );
1672+ return SANITY_MSG_E ;
1673+ }
1674+ }
1675+ else {
1676+ WOLFSSL_MSG ("Msg should be epoch 2" );
1677+ WOLFSSL_ERROR_VERBOSE (SANITY_MSG_E );
1678+ return SANITY_MSG_E ;
1679+ }
1680+ }
1681+ break ;
1682+ case certificate_request :
1683+ case certificate :
1684+ case certificate_verify :
1685+ case finished :
1686+ if (!ssl -> options .handShakeDone ) {
1687+ if (!w64Equal (ssl -> keys .curEpoch64 , hsEpoch )) {
1688+ if (ssl -> options .side == WOLFSSL_CLIENT_END &&
1689+ ssl -> options .serverState < SERVER_HELLO_COMPLETE ) {
1690+ /* before processing SH we don't know which version
1691+ * will be negotiated. */
1692+ if (!w64Equal (ssl -> keys .curEpoch64 , plainEpoch )) {
1693+ WOLFSSL_MSG ("Msg should be epoch 2 or 0" );
1694+ WOLFSSL_ERROR_VERBOSE (SANITY_MSG_E );
1695+ return SANITY_MSG_E ;
1696+ }
1697+ }
1698+ else {
1699+ WOLFSSL_MSG ("Msg should be epoch 2" );
1700+ WOLFSSL_ERROR_VERBOSE (SANITY_MSG_E );
1701+ return SANITY_MSG_E ;
1702+ }
1703+ }
1704+ }
1705+ else {
1706+ /* Allow epoch 2 in case of rtx */
1707+ if (!w64GTE (ssl -> keys .curEpoch64 , hsEpoch )) {
1708+ WOLFSSL_MSG ("Msg should be epoch 2+" );
1709+ WOLFSSL_ERROR_VERBOSE (SANITY_MSG_E );
1710+ return SANITY_MSG_E ;
1711+ }
1712+ }
1713+ break ;
1714+ case certificate_status :
1715+ case change_cipher_hs :
1716+ case key_update :
1717+ case session_ticket :
1718+ if (!w64GTE (ssl -> keys .curEpoch64 , t0Epoch )) {
1719+ WOLFSSL_MSG ("Msg should be epoch 3+" );
1720+ WOLFSSL_ERROR_VERBOSE (SANITY_MSG_E );
1721+ return SANITY_MSG_E ;
1722+ }
1723+ break ;
1724+ case end_of_early_data :
1725+ case message_hash :
1726+ case no_shake :
1727+ default :
1728+ WOLFSSL_MSG ("Unknown message type" );
1729+ WOLFSSL_ERROR_VERBOSE (SANITY_MSG_E );
1730+ return SANITY_MSG_E ;
1731+ }
1732+ }
1733+ return 0 ;
1734+ }
1735+
16401736/**
16411737 * Dtls13HandshakeRecv() - process an handshake message. Deal with
16421738 fragmentation if needed
@@ -1672,6 +1768,12 @@ static int _Dtls13HandshakeRecv(WOLFSSL* ssl, byte* input, word32 size,
16721768 return ret ;
16731769 }
16741770
1771+ ret = Dtls13CheckEpoch (ssl , (enum HandShakeType )handshakeType );
1772+ if (ret != 0 ) {
1773+ WOLFSSL_ERROR (ret );
1774+ return ret ;
1775+ }
1776+
16751777 if (ssl -> options .side == WOLFSSL_SERVER_END &&
16761778 ssl -> options .acceptState < TLS13_ACCEPT_FIRST_REPLY_DONE ) {
16771779 if (handshakeType != client_hello ) {
0 commit comments