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

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