@@ -6204,6 +6204,8 @@ static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz,
62046204 if ((ret = SetKeysSide (ssl , DECRYPT_SIDE_ONLY )) != 0 )
62056205 return ret ;
62066206
6207+ ssl -> keys .encryptionOn = 1 ;
6208+
62076209#ifdef WOLFSSL_DTLS13
62086210 if (ssl -> options .dtls ) {
62096211 ret = Dtls13NewEpoch (ssl ,
@@ -6916,7 +6918,11 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
69166918 }
69176919 }
69186920 else {
6919- ERROR_OUT (HRR_COOKIE_ERROR , exit_dch );
6921+ #if defined(WOLFSSL_DTLS13 ) && defined(WOLFSSL_DTLS13_NO_HRR_ON_RESUME )
6922+ /* Don't error out as we may be resuming. We confirm this later. */
6923+ if (!ssl -> options .dtls )
6924+ #endif
6925+ ERROR_OUT (HRR_COOKIE_ERROR , exit_dch );
69206926 }
69216927 }
69226928#endif
@@ -6982,14 +6988,16 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
69826988 goto exit_dch ;
69836989 }
69846990 }
6985- else
69866991#endif
69876992#ifdef HAVE_SUPPORTED_CURVES
69886993 if (args -> usingPSK == 2 ) {
69896994 /* Pick key share and Generate a new key if not present. */
69906995 int doHelloRetry = 0 ;
69916996 ret = TLSX_KeyShare_Establish (ssl , & doHelloRetry );
69926997 if (doHelloRetry ) {
6998+ /* Make sure we don't send HRR twice */
6999+ if (ssl -> options .serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE )
7000+ ERROR_OUT (INVALID_PARAMETER , exit_dch );
69937001 ssl -> options .serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE ;
69947002 if (ret != WC_PENDING_E )
69957003 ret = 0 ; /* for hello_retry return 0 */
@@ -7082,32 +7090,58 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
70827090 ret = INPUT_CASE_ERROR ;
70837091 } /* switch (ssl->options.asyncState) */
70847092
7085- #if defined(WOLFSSL_SEND_HRR_COOKIE )
7086- if (ret == 0 && ssl -> options .sendCookie && ssl -> options .cookieGood &&
7087- (ssl -> options .serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE
7093+ #ifdef WOLFSSL_SEND_HRR_COOKIE
7094+ if (ret == 0 && ssl -> options .sendCookie ) {
7095+ if (ssl -> options .cookieGood &&
7096+ ssl -> options .acceptState == TLS13_ACCEPT_FIRST_REPLY_DONE ) {
7097+ /* Processing second ClientHello. Clear HRR state. */
7098+ ssl -> options .serverState = NULL_STATE ;
7099+ }
7100+
7101+ if (ssl -> options .cookieGood &&
7102+ ssl -> options .serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE ) {
7103+ /* If we already verified the peer with a cookie then we can't
7104+ * do another HRR for cipher negotiation. Send alert and restart
7105+ * the entire handshake. */
7106+ ERROR_OUT (INVALID_PARAMETER , exit_dch );
7107+ }
70887108#ifdef WOLFSSL_DTLS13
7089- /* DTLS cookie exchange should be done in stateless code in
7090- * DoClientHelloStateless. If we verified the cookie then
7091- * always advance the state. */
7092- || ssl -> options .dtls
7109+ if (ssl -> options .dtls &&
7110+ ssl -> options .serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE ) {
7111+ /* Cookie and key share negotiation should be handled in
7112+ * DoClientHelloStateless. If we enter here then something went
7113+ * wrong in our logic. */
7114+ ERROR_OUT (BAD_HELLO , exit_dch );
7115+ }
70937116#endif
7094- ))
7095- ssl -> options .serverState = SERVER_HELLO_COMPLETE ;
7117+ /* Send a cookie */
7118+ if (!ssl -> options .cookieGood &&
7119+ ssl -> options .serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE ) {
7120+ #ifdef WOLFSSL_DTLS13
7121+ if (ssl -> options .dtls ) {
7122+ #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
7123+ /* We can skip cookie on resumption */
7124+ if (!ssl -> options .dtls || !ssl -> options .dtls13NoHrrOnResume ||
7125+ !args -> usingPSK )
7126+ #endif
7127+ ERROR_OUT (BAD_HELLO , exit_dch );
7128+ }
7129+ else
70967130#endif
7131+ {
7132+ /* Need to remove the keyshare ext if we found a common group
7133+ * and are not doing curve negotiation. */
7134+ TLSX_Remove (& ssl -> extensions , TLSX_KEY_SHARE , ssl -> heap );
7135+ ssl -> options .serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE ;
7136+ }
70977137
7098- #if defined(WOLFSSL_DTLS13 ) && defined(WOLFSSL_SEND_HRR_COOKIE )
7099- if (ret == 0 && ssl -> options .dtls && ssl -> options .sendCookie &&
7100- ssl -> options .serverState <= SERVER_HELLO_RETRY_REQUEST_COMPLETE ) {
7101- /* Cookie and key share negotiation should be handled in
7102- * DoClientHelloStateless. If we enter here then something went wrong
7103- * in our logic. */
7104- ERROR_OUT (BAD_HELLO , exit_dch );
7138+ }
71057139 }
71067140#endif /* WOLFSSL_DTLS13 */
71077141
71087142#ifdef WOLFSSL_DTLS_CID
71097143 /* do not modify CID state if we are sending an HRR */
7110- if (ssl -> options .useDtlsCID &&
7144+ if (ret == 0 && ssl -> options . dtls && ssl -> options .useDtlsCID &&
71117145 ssl -> options .serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE )
71127146 DtlsCIDOnExtensionsParsed (ssl );
71137147#endif /* WOLFSSL_DTLS_CID */
0 commit comments