Blame SOURCES/gnutls-3.6.8-multiple-key-updates.patch

8dd812
From 6023c69c616d866e19ab1c0bb87931e5143c79d3 Mon Sep 17 00:00:00 2001
8dd812
From: Daiki Ueno <dueno@redhat.com>
8dd812
Date: Wed, 5 Jun 2019 16:48:39 +0200
8dd812
Subject: [PATCH] tls13/key_update: ignore multiple key updates instead of
8dd812
 error
8dd812
8dd812
This fixes the multiple KeyUpdate messages handling in commit
8dd812
65e2aa80d114d4bef095d129c2eda475e473244a, where illegal_parameter is
8dd812
sent even if the limit doesn't exceed.
8dd812
8dd812
Signed-off-by: Daiki Ueno <dueno@redhat.com>
8dd812
---
8dd812
 .gitignore                        |   1 +
8dd812
 lib/tls13/key_update.c            |   2 -
8dd812
 tests/Makefile.am                 |   2 +
8dd812
 tests/tls13/key_update_multiple.c | 232 ++++++++++++++++++++++++++++++
8dd812
 4 files changed, 235 insertions(+), 2 deletions(-)
8dd812
 create mode 100644 tests/tls13/key_update_multiple.c
8dd812
8dd812
diff --git a/lib/tls13/key_update.c b/lib/tls13/key_update.c
8dd812
index d542a214b..c6f6e0aa1 100644
8dd812
--- a/lib/tls13/key_update.c
8dd812
+++ b/lib/tls13/key_update.c
8dd812
@@ -117,8 +117,6 @@ int _gnutls13_recv_key_update(gnutls_session_t session, gnutls_buffer_st *buf)
8dd812
 			session->internals.rsend_state = RECORD_SEND_KEY_UPDATE_1;
8dd812
 		else if (session->internals.rsend_state == RECORD_SEND_CORKED)
8dd812
 			session->internals.rsend_state = RECORD_SEND_CORKED_TO_KU;
8dd812
-		else
8dd812
-			return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
8dd812
 
8dd812
 		break;
8dd812
 	default:
8dd812
diff --git a/tests/Makefile.am b/tests/Makefile.am
8dd812
index ca0481879..4ffa69825 100644
8dd812
--- a/tests/Makefile.am
8dd812
+++ b/tests/Makefile.am
8dd812
@@ -119,6 +119,8 @@ ctests += tls13/psk-ext
8dd812
 
8dd812
 ctests += tls13/key_update
8dd812
 
8dd812
+ctests += tls13/key_update_multiple
8dd812
+
8dd812
 ctests += tls13/key_limits
8dd812
 
8dd812
 ctests += tls13/multi-ocsp
8dd812
diff --git a/tests/tls13/key_update_multiple.c b/tests/tls13/key_update_multiple.c
8dd812
new file mode 100644
8dd812
index 000000000..8b2c2db4b
8dd812
--- /dev/null
8dd812
+++ b/tests/tls13/key_update_multiple.c
8dd812
@@ -0,0 +1,232 @@
8dd812
+/*
8dd812
+ * Copyright (C) 2017-2019 Red Hat, Inc.
8dd812
+ *
8dd812
+ * Author: Daiki Ueno
8dd812
+ *
8dd812
+ * This file is part of GnuTLS.
8dd812
+ *
8dd812
+ * GnuTLS is free software; you can redistribute it and/or modify it
8dd812
+ * under the terms of the GNU General Public License as published by
8dd812
+ * the Free Software Foundation; either version 3 of the License, or
8dd812
+ * (at your option) any later version.
8dd812
+ *
8dd812
+ * GnuTLS is distributed in the hope that it will be useful, but
8dd812
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
8dd812
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8dd812
+ * General Public License for more details.
8dd812
+ *
8dd812
+ * You should have received a copy of the GNU Lesser General Public License
8dd812
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>
8dd812
+ */
8dd812
+
8dd812
+#ifdef HAVE_CONFIG_H
8dd812
+#include <config.h>
8dd812
+#endif
8dd812
+
8dd812
+#include <stdbool.h>
8dd812
+#include <stdio.h>
8dd812
+#include <stdlib.h>
8dd812
+#include <string.h>
8dd812
+#include <errno.h>
8dd812
+#include <gnutls/gnutls.h>
8dd812
+#include <gnutls/crypto.h>
8dd812
+#include <assert.h>
8dd812
+#include "cert-common.h"
8dd812
+
8dd812
+#include "utils.h"
8dd812
+#include "virt-time.h"
8dd812
+#define RANDOMIZE
8dd812
+#include "eagain-common.h"
8dd812
+
8dd812
+const char *side = "";
8dd812
+
8dd812
+/* This program tests whether multiple key update messages are handled
8dd812
+ * properly with rate-limit. */
8dd812
+
8dd812
+static void tls_log_func(int level, const char *str)
8dd812
+{
8dd812
+	fprintf(stderr, "%s|<%d>| %s", side, level, str);
8dd812
+}
8dd812
+
8dd812
+#define MAX_BUF 1024
8dd812
+#define MSG "Hello TLS, and hi and how are you and more data here... and more... and even more and even more more data..."
8dd812
+
8dd812
+/* These must match the definitions in lib/tls13/key_update.c. */
8dd812
+#define KEY_UPDATES_WINDOW 1000
8dd812
+#define KEY_UPDATES_PER_WINDOW 8
8dd812
+
8dd812
+static unsigned key_update_msg_inc = 0;
8dd812
+static unsigned key_update_msg_out = 0;
8dd812
+
8dd812
+static int hsk_callback(gnutls_session_t session, unsigned int htype,
8dd812
+			unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
8dd812
+{
8dd812
+	assert(post == GNUTLS_HOOK_PRE);
8dd812
+
8dd812
+	assert(msg->size == 1);
8dd812
+
8dd812
+	if (htype == GNUTLS_HANDSHAKE_KEY_UPDATE) {
8dd812
+		if (incoming)
8dd812
+			key_update_msg_inc++;
8dd812
+		else
8dd812
+			key_update_msg_out++;
8dd812
+	}
8dd812
+
8dd812
+	return 0;
8dd812
+}
8dd812
+
8dd812
+static void run(const char *name, bool exceed_limit)
8dd812
+{
8dd812
+	/* Server stuff. */
8dd812
+	gnutls_certificate_credentials_t ccred;
8dd812
+	gnutls_certificate_credentials_t scred;
8dd812
+	gnutls_session_t server;
8dd812
+	int sret, cret;
8dd812
+	/* Client stuff. */
8dd812
+	gnutls_session_t client;
8dd812
+	/* Need to enable anonymous KX specifically. */
8dd812
+	char buffer[MAX_BUF + 1];
8dd812
+	int ret, transferred = 0;
8dd812
+	size_t i;
8dd812
+
8dd812
+	success("%s\n", name);
8dd812
+
8dd812
+	/* General init. */
8dd812
+	global_init();
8dd812
+	gnutls_global_set_log_function(tls_log_func);
8dd812
+	if (debug)
8dd812
+		gnutls_global_set_log_level(9);
8dd812
+
8dd812
+	/* Init server */
8dd812
+	assert(gnutls_certificate_allocate_credentials(&scred) >= 0);
8dd812
+	assert(gnutls_certificate_set_x509_key_mem(scred,
8dd812
+						   &server_ca3_localhost_cert,
8dd812
+						   &server_ca3_key,
8dd812
+						   GNUTLS_X509_FMT_PEM) >= 0);
8dd812
+
8dd812
+	assert(gnutls_init(&server, GNUTLS_SERVER) >= 0);
8dd812
+	ret =
8dd812
+	    gnutls_priority_set_direct(server,
8dd812
+				       "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.3",
8dd812
+				       NULL);
8dd812
+	if (ret < 0)
8dd812
+		exit(1);
8dd812
+
8dd812
+	gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE, scred);
8dd812
+	gnutls_transport_set_push_function(server, server_push);
8dd812
+	gnutls_transport_set_pull_function(server, server_pull);
8dd812
+	gnutls_transport_set_ptr(server, server);
8dd812
+
8dd812
+	/* Init client */
8dd812
+	assert(gnutls_certificate_allocate_credentials(&ccred) >= 0);
8dd812
+	assert(gnutls_certificate_set_x509_trust_mem
8dd812
+	       (ccred, &ca3_cert, GNUTLS_X509_FMT_PEM) >= 0);
8dd812
+
8dd812
+	gnutls_init(&client, GNUTLS_CLIENT);
8dd812
+	ret =
8dd812
+	    gnutls_priority_set_direct(client,
8dd812
+				       "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.3",
8dd812
+				       NULL);
8dd812
+	assert(ret >= 0);
8dd812
+
8dd812
+	ret = gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE, ccred);
8dd812
+	if (ret < 0)
8dd812
+		exit(1);
8dd812
+
8dd812
+	gnutls_transport_set_push_function(client, client_push);
8dd812
+	gnutls_transport_set_pull_function(client, client_pull);
8dd812
+	gnutls_transport_set_ptr(client, client);
8dd812
+
8dd812
+
8dd812
+	HANDSHAKE(client, server);
8dd812
+	if (debug)
8dd812
+		success("Handshake established\n");
8dd812
+
8dd812
+	key_update_msg_inc = 0;
8dd812
+	key_update_msg_out = 0;
8dd812
+
8dd812
+	gnutls_handshake_set_hook_function(client, -1, GNUTLS_HOOK_PRE, hsk_callback);
8dd812
+
8dd812
+	/* schedule multiple key updates */
8dd812
+	for (i = 0; i < KEY_UPDATES_PER_WINDOW; i++) {
8dd812
+		do {
8dd812
+			ret = gnutls_session_key_update(client, 1);
8dd812
+		} while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
8dd812
+		if (ret < 0)
8dd812
+			fail("error in key update: %s\n", gnutls_strerror(ret));
8dd812
+	}
8dd812
+
8dd812
+	/* server receives the client key update and sends data */
8dd812
+	TRANSFER(client, server, MSG, strlen(MSG), buffer, MAX_BUF);
8dd812
+	TRANSFER(server, client, MSG, strlen(MSG), buffer, MAX_BUF);
8dd812
+	EMPTY_BUF(server, client, buffer, MAX_BUF);
8dd812
+
8dd812
+	if (key_update_msg_out != KEY_UPDATES_PER_WINDOW)
8dd812
+		fail("unexpected number of key updates are sent: %d\n",
8dd812
+			key_update_msg_out);
8dd812
+	else {
8dd812
+		if (debug)
8dd812
+			success("successfully sent %d key updates\n",
8dd812
+				KEY_UPDATES_PER_WINDOW);
8dd812
+	}
8dd812
+	if (key_update_msg_inc != 1)
8dd812
+		fail("unexpected number of key updates received: %d\n",
8dd812
+			key_update_msg_inc);
8dd812
+	else {
8dd812
+		if (debug)
8dd812
+			success("successfully received 1 key update\n");
8dd812
+	}
8dd812
+
8dd812
+	if (exceed_limit) {
8dd812
+		/* excessive key update in the same time window should
8dd812
+		 * be rejected by the peer */
8dd812
+		do {
8dd812
+			ret = gnutls_session_key_update(client, 1);
8dd812
+		} while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
8dd812
+
8dd812
+		/* server receives the client key update and sends data */
8dd812
+		ret = record_send_loop(client, MSG, strlen(MSG), 0);
8dd812
+		assert(ret == strlen(MSG));
8dd812
+		ret = gnutls_record_recv(server, buffer, MAX_BUF);
8dd812
+		if (ret != GNUTLS_E_TOO_MANY_HANDSHAKE_PACKETS)
8dd812
+			fail("server didn't reject excessive number of key updates\n");
8dd812
+		else {
8dd812
+			if (debug)
8dd812
+				success("server rejected excessive number of key updates\n");
8dd812
+		}
8dd812
+	} else {
8dd812
+		virt_sec_sleep(KEY_UPDATES_WINDOW / 1000 + 1);
8dd812
+
8dd812
+		/* the time window should be rolled over now */
8dd812
+		do {
8dd812
+			ret = gnutls_session_key_update(client, 1);
8dd812
+		} while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
8dd812
+		if (ret < 0)
8dd812
+			fail("error in key update: %s\n", gnutls_strerror(ret));
8dd812
+
8dd812
+		/* server receives the client key update and sends data */
8dd812
+		TRANSFER(client, server, MSG, strlen(MSG), buffer, MAX_BUF);
8dd812
+		TRANSFER(server, client, MSG, strlen(MSG), buffer, MAX_BUF);
8dd812
+		EMPTY_BUF(server, client, buffer, MAX_BUF);
8dd812
+	}
8dd812
+
8dd812
+	gnutls_bye(client, GNUTLS_SHUT_WR);
8dd812
+	gnutls_bye(server, GNUTLS_SHUT_WR);
8dd812
+
8dd812
+	gnutls_deinit(client);
8dd812
+	gnutls_deinit(server);
8dd812
+
8dd812
+	gnutls_certificate_free_credentials(scred);
8dd812
+	gnutls_certificate_free_credentials(ccred);
8dd812
+
8dd812
+	gnutls_global_deinit();
8dd812
+	reset_buffers();
8dd812
+}
8dd812
+
8dd812
+void doit(void)
8dd812
+{
8dd812
+	virt_time_init();
8dd812
+
8dd812
+	run("not exceeding limit", 0);
8dd812
+	run("exceeding limit", 1);
8dd812
+}
8dd812
-- 
8dd812
2.20.1
8dd812