@@ -6197,6 +6197,8 @@ static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz,
61976197 if ((ret = SetKeysSide (ssl , DECRYPT_SIDE_ONLY )) != 0 )
61986198 return ret ;
61996199
6200+ ssl -> keys .encryptionOn = 1 ;
6201+
62006202#ifdef WOLFSSL_DTLS13
62016203 if (ssl -> options .dtls ) {
62026204 ret = Dtls13NewEpoch (ssl ,
@@ -6909,7 +6911,11 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
69096911 }
69106912 }
69116913 else {
6912- ERROR_OUT (HRR_COOKIE_ERROR , exit_dch );
6914+ #if defined(WOLFSSL_DTLS13 ) && defined(WOLFSSL_DTLS13_NO_HRR_ON_RESUME )
6915+ /* Don't error out as we may be resuming. We confirm this later. */
6916+ if (!ssl -> options .dtls )
6917+ #endif
6918+ ERROR_OUT (HRR_COOKIE_ERROR , exit_dch );
69136919 }
69146920 }
69156921#endif
@@ -6975,14 +6981,16 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
69756981 goto exit_dch ;
69766982 }
69776983 }
6978- else
69796984#endif
69806985#ifdef HAVE_SUPPORTED_CURVES
69816986 if (args -> usingPSK == 2 ) {
69826987 /* Pick key share and Generate a new key if not present. */
69836988 int doHelloRetry = 0 ;
69846989 ret = TLSX_KeyShare_Establish (ssl , & doHelloRetry );
69856990 if (doHelloRetry ) {
6991+ /* Make sure we don't send HRR twice */
6992+ if (ssl -> options .serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE )
6993+ ERROR_OUT (INVALID_PARAMETER , exit_dch );
69866994 ssl -> options .serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE ;
69876995 if (ret != WC_PENDING_E )
69886996 ret = 0 ; /* for hello_retry return 0 */
@@ -7075,32 +7083,58 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
70757083 ret = INPUT_CASE_ERROR ;
70767084 } /* switch (ssl->options.asyncState) */
70777085
7078- #if defined(WOLFSSL_SEND_HRR_COOKIE )
7079- if (ret == 0 && ssl -> options .sendCookie && ssl -> options .cookieGood &&
7080- (ssl -> options .serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE
7086+ #ifdef WOLFSSL_SEND_HRR_COOKIE
7087+ if (ret == 0 && ssl -> options .sendCookie ) {
7088+ if (ssl -> options .cookieGood &&
7089+ ssl -> options .acceptState == TLS13_ACCEPT_FIRST_REPLY_DONE ) {
7090+ /* Processing second ClientHello. Clear HRR state. */
7091+ ssl -> options .serverState = NULL_STATE ;
7092+ }
7093+
7094+ if (ssl -> options .cookieGood &&
7095+ ssl -> options .serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE ) {
7096+ /* If we already verified the peer with a cookie then we can't
7097+ * do another HRR for cipher negotiation. Send alert and restart
7098+ * the entire handshake. */
7099+ ERROR_OUT (INVALID_PARAMETER , exit_dch );
7100+ }
70817101#ifdef WOLFSSL_DTLS13
7082- /* DTLS cookie exchange should be done in stateless code in
7083- * DoClientHelloStateless. If we verified the cookie then
7084- * always advance the state. */
7085- || ssl -> options .dtls
7102+ if (ssl -> options .dtls &&
7103+ ssl -> options .serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE ) {
7104+ /* Cookie and key share negotiation should be handled in
7105+ * DoClientHelloStateless. If we enter here then something went
7106+ * wrong in our logic. */
7107+ ERROR_OUT (BAD_HELLO , exit_dch );
7108+ }
70867109#endif
7087- ))
7088- ssl -> options .serverState = SERVER_HELLO_COMPLETE ;
7110+ /* Send a cookie */
7111+ if (!ssl -> options .cookieGood &&
7112+ ssl -> options .serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE ) {
7113+ #ifdef WOLFSSL_DTLS13
7114+ if (ssl -> options .dtls ) {
7115+ #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
7116+ /* We can skip cookie on resumption */
7117+ if (!ssl -> options .dtls || !ssl -> options .dtls13NoHrrOnResume ||
7118+ !args -> usingPSK )
7119+ #endif
7120+ ERROR_OUT (BAD_HELLO , exit_dch );
7121+ }
7122+ else
70897123#endif
7124+ {
7125+ /* Need to remove the keyshare ext if we found a common group
7126+ * and are not doing curve negotiation. */
7127+ TLSX_Remove (& ssl -> extensions , TLSX_KEY_SHARE , ssl -> heap );
7128+ ssl -> options .serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE ;
7129+ }
70907130
7091- #if defined(WOLFSSL_DTLS13 ) && defined(WOLFSSL_SEND_HRR_COOKIE )
7092- if (ret == 0 && ssl -> options .dtls && ssl -> options .sendCookie &&
7093- ssl -> options .serverState <= SERVER_HELLO_RETRY_REQUEST_COMPLETE ) {
7094- /* Cookie and key share negotiation should be handled in
7095- * DoClientHelloStateless. If we enter here then something went wrong
7096- * in our logic. */
7097- ERROR_OUT (BAD_HELLO , exit_dch );
7131+ }
70987132 }
70997133#endif /* WOLFSSL_DTLS13 */
71007134
71017135#ifdef WOLFSSL_DTLS_CID
71027136 /* do not modify CID state if we are sending an HRR */
7103- if (ssl -> options .useDtlsCID &&
7137+ if (ret == 0 && ssl -> options . dtls && ssl -> options .useDtlsCID &&
71047138 ssl -> options .serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE )
71057139 DtlsCIDOnExtensionsParsed (ssl );
71067140#endif /* WOLFSSL_DTLS_CID */
0 commit comments