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

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