Blame SOURCES/gnutls-3.6.14-no-renegotiation.patch

3ec527
From 29ee67c205855e848a0a26e6d0e4f65b6b943e0a Mon Sep 17 00:00:00 2001
3ec527
From: Daiki Ueno <ueno@gnu.org>
3ec527
Date: Sat, 22 Aug 2020 17:19:39 +0200
3ec527
Subject: [PATCH] handshake: reject no_renegotiation alert if handshake is
3ec527
 incomplete
3ec527
3ec527
If the initial handshake is incomplete and the server sends a
3ec527
no_renegotiation alert, the client should treat it as a fatal error
3ec527
even if its level is warning.  Otherwise the same handshake
3ec527
state (e.g., DHE parameters) are reused in the next gnutls_handshake
3ec527
call, if it is called in the loop idiom:
3ec527
3ec527
  do {
3ec527
          ret = gnutls_handshake(session);
3ec527
  } while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
3ec527
3ec527
Signed-off-by: Daiki Ueno <ueno@gnu.org>
3ec527
---
3ec527
 ...a04b3d3f7dcc0ab4571cf0df3b67ab7e1005e9e7a8 | Bin 0 -> 671 bytes
3ec527
 ...1da801fb3c6d1f7f846f227721e221adea08aa319c | Bin 0 -> 729 bytes
3ec527
 lib/gnutls_int.h                              |   1 +
3ec527
 lib/handshake.c                               |  48 +++++++++++++-----
3ec527
 4 files changed, 36 insertions(+), 13 deletions(-)
3ec527
 create mode 100644 fuzz/gnutls_client_fuzzer.in/00ea40761ce11e769f1817a04b3d3f7dcc0ab4571cf0df3b67ab7e1005e9e7a8
3ec527
 create mode 100644 fuzz/gnutls_psk_client_fuzzer.in/b16434290b77e13d7a983d1da801fb3c6d1f7f846f227721e221adea08aa319c
3ec527
3ec527
diff --git a/lib/gnutls_int.h b/lib/gnutls_int.h
3ec527
index bb6c19713..31cec5c0c 100644
3ec527
--- a/lib/gnutls_int.h
3ec527
+++ b/lib/gnutls_int.h
3ec527
@@ -1370,6 +1370,7 @@ typedef struct {
3ec527
 #define HSK_RECORD_SIZE_LIMIT_RECEIVED (1<<26) /* server: record_size_limit extension was seen but not accepted yet */
3ec527
 #define HSK_OCSP_REQUESTED (1<<27) /* server: client requested OCSP stapling */
3ec527
 #define HSK_CLIENT_OCSP_REQUESTED (1<<28) /* client: server requested OCSP stapling */
3ec527
+#define HSK_SERVER_HELLO_RECEIVED (1<<29) /* client: Server Hello message has been received */
3ec527
 
3ec527
 	/* The hsk_flags are for use within the ongoing handshake;
3ec527
 	 * they are reset to zero prior to handshake start by gnutls_handshake. */
3ec527
diff --git a/lib/handshake.c b/lib/handshake.c
3ec527
index b40f84b3d..ce2d160e2 100644
3ec527
--- a/lib/handshake.c
3ec527
+++ b/lib/handshake.c
3ec527
@@ -2061,6 +2061,8 @@ read_server_hello(gnutls_session_t session,
3ec527
 	if (ret < 0)
3ec527
 		return gnutls_assert_val(ret);
3ec527
 
3ec527
+	session->internals.hsk_flags |= HSK_SERVER_HELLO_RECEIVED;
3ec527
+
3ec527
 	return 0;
3ec527
 }
3ec527
 
3ec527
@@ -2585,16 +2587,42 @@ int gnutls_rehandshake(gnutls_session_t session)
3ec527
 	return 0;
3ec527
 }
3ec527
 
3ec527
+/* This function checks whether the error code should be treated fatal
3ec527
+ * or not, and also does the necessary state transition.  In
3ec527
+ * particular, in the case of a rehandshake abort it resets the
3ec527
+ * handshake's internal state.
3ec527
+ */
3ec527
 inline static int
3ec527
 _gnutls_abort_handshake(gnutls_session_t session, int ret)
3ec527
 {
3ec527
-	if (((ret == GNUTLS_E_WARNING_ALERT_RECEIVED) &&
3ec527
-	     (gnutls_alert_get(session) == GNUTLS_A_NO_RENEGOTIATION))
3ec527
-	    || ret == GNUTLS_E_GOT_APPLICATION_DATA)
3ec527
-		return 0;
3ec527
+	switch (ret) {
3ec527
+	case GNUTLS_E_WARNING_ALERT_RECEIVED:
3ec527
+		if (gnutls_alert_get(session) == GNUTLS_A_NO_RENEGOTIATION) {
3ec527
+			/* The server always toleretes a "no_renegotiation" alert. */
3ec527
+			if (session->security_parameters.entity == GNUTLS_SERVER) {
3ec527
+				STATE = STATE0;
3ec527
+				return ret;
3ec527
+			}
3ec527
+
3ec527
+			/* The client should tolerete a "no_renegotiation" alert only if:
3ec527
+			 * - the initial handshake has completed, or
3ec527
+			 * - a Server Hello is not yet received
3ec527
+			 */
3ec527
+			if (session->internals.initial_negotiation_completed ||
3ec527
+			    !(session->internals.hsk_flags & HSK_SERVER_HELLO_RECEIVED)) {
3ec527
+				STATE = STATE0;
3ec527
+				return ret;
3ec527
+			}
3ec527
 
3ec527
-	/* this doesn't matter */
3ec527
-	return GNUTLS_E_INTERNAL_ERROR;
3ec527
+			return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET);
3ec527
+		}
3ec527
+		return ret;
3ec527
+	case GNUTLS_E_GOT_APPLICATION_DATA:
3ec527
+		STATE = STATE0;
3ec527
+		return ret;
3ec527
+	default:
3ec527
+		return ret;
3ec527
+	}
3ec527
 }
3ec527
 
3ec527
 
3ec527
@@ -2756,13 +2784,7 @@ int gnutls_handshake(gnutls_session_t session)
3ec527
 	}
3ec527
 
3ec527
 	if (ret < 0) {
3ec527
-		/* In the case of a rehandshake abort
3ec527
-		 * we should reset the handshake's internal state.
3ec527
-		 */
3ec527
-		if (_gnutls_abort_handshake(session, ret) == 0)
3ec527
-			STATE = STATE0;
3ec527
-
3ec527
-		return ret;
3ec527
+		return _gnutls_abort_handshake(session, ret);
3ec527
 	}
3ec527
 
3ec527
 	/* clear handshake buffer */
3ec527
-- 
3ec527
2.26.2
3ec527