Blame SOURCES/gnutls-3.3.8-md5-downgrade.patch

4c24d7
From 6612a7181af32903b03020090fe361360142258b Mon Sep 17 00:00:00 2001
4c24d7
From: Nikos Mavrogiannopoulos <nmav@redhat.com>
4c24d7
Date: Wed, 9 Dec 2015 10:07:18 +0100
4c24d7
Subject: [PATCH] Prevent a downgrade attack to RSA-MD5 signatures
4c24d7
4c24d7
This is by considering the list of the signature algorithms set by
4c24d7
priorities.
4c24d7
---
4c24d7
 lib/ext/signature.c  |  18 +--
4c24d7
 tests/Makefile.am    |   2 +-
4c24d7
 tests/sign-md5-rep.c | 344 +++++++++++++++++++++++++++++++++++++++++++++++++++
4c24d7
 3 files changed, 346 insertions(+), 18 deletions(-)
4c24d7
 create mode 100644 tests/sign-md5-rep.c
4c24d7
4c24d7
diff --git a/lib/ext/signature.c b/lib/ext/signature.c
4c24d7
index fb971f5..6f3066e 100644
4c24d7
--- a/lib/ext/signature.c
4c24d7
+++ b/lib/ext/signature.c
4c24d7
@@ -313,28 +313,12 @@ _gnutls_session_sign_algo_enabled(gnutls_session_t session,
4c24d7
 				  gnutls_sign_algorithm_t sig)
4c24d7
 {
4c24d7
 	unsigned i;
4c24d7
-	int ret;
4c24d7
 	const version_entry_st *ver = get_version(session);
4c24d7
-	sig_ext_st *priv;
4c24d7
-	extension_priv_data_t epriv;
4c24d7
 
4c24d7
 	if (unlikely(ver == NULL))
4c24d7
 		return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
4c24d7
 
4c24d7
-	ret =
4c24d7
-	    _gnutls_ext_get_session_data(session,
4c24d7
-					 GNUTLS_EXTENSION_SIGNATURE_ALGORITHMS,
4c24d7
-					 &epriv);
4c24d7
-	if (ret < 0) {
4c24d7
-		gnutls_assert();
4c24d7
-		return 0;
4c24d7
-	}
4c24d7
-	priv = epriv.ptr;
4c24d7
-
4c24d7
-	if (!_gnutls_version_has_selectable_sighash(ver)
4c24d7
-	    || priv->sign_algorithms_size == 0)
4c24d7
-		/* none set, allow all */
4c24d7
-	{
4c24d7
+	if (!_gnutls_version_has_selectable_sighash(ver)) {
4c24d7
 		return 0;
4c24d7
 	}
4c24d7
 
4c24d7
diff --git a/tests/Makefile.am b/tests/Makefile.am
4c24d7
index 95d6541..d1c4839 100644
4c24d7
--- a/tests/Makefile.am
4c24d7
+++ b/tests/Makefile.am
4c24d7
@@ -84,7 +84,7 @@ ctests = mini-record-2 simple gc set_pkcs12_cred certder certuniqueid	\
4c24d7
 	 mini-cert-status mini-rsa-psk global-init sec-params \
4c24d7
 	 fips-test mini-global-load name-constraints x509-extensions \
4c24d7
 	 long-session-id mini-x509-callbacks-intr \
4c24d7
-	 crlverify init_fds
4c24d7
+	 crlverify init_fds sign-md5-rep
4c24d7
 
4c24d7
 if ENABLE_OCSP
4c24d7
 ctests += ocsp
4c24d7
diff --git a/tests/sign-md5-rep.c b/tests/sign-md5-rep.c
4c24d7
new file mode 100644
4c24d7
index 0000000..72869fa
4c24d7
--- /dev/null
4c24d7
+++ b/tests/sign-md5-rep.c
4c24d7
@@ -0,0 +1,344 @@
4c24d7
+/*
4c24d7
+ * Copyright (C) 2015 Nikos Mavrogiannopoulos
4c24d7
+ *
4c24d7
+ * Author: Nikos Mavrogiannopoulos
4c24d7
+ *
4c24d7
+ * This file is part of GnuTLS.
4c24d7
+ *
4c24d7
+ * GnuTLS is free software; you can redistribute it and/or modify it
4c24d7
+ * under the terms of the GNU General Public License as published by
4c24d7
+ * the Free Software Foundation; either version 3 of the License, or
4c24d7
+ * (at your option) any later version.
4c24d7
+ *
4c24d7
+ * GnuTLS is distributed in the hope that it will be useful, but
4c24d7
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
4c24d7
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c24d7
+ * General Public License for more details.
4c24d7
+ *
4c24d7
+ * You should have received a copy of the GNU General Public License
4c24d7
+ * along with GnuTLS; if not, write to the Free Software Foundation,
4c24d7
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
4c24d7
+ */
4c24d7
+
4c24d7
+#ifdef HAVE_CONFIG_H
4c24d7
+#include <config.h>
4c24d7
+#endif
4c24d7
+
4c24d7
+#include <stdio.h>
4c24d7
+#include <stdlib.h>
4c24d7
+
4c24d7
+#if defined(_WIN32)
4c24d7
+
4c24d7
+int main()
4c24d7
+{
4c24d7
+	exit(77);
4c24d7
+}
4c24d7
+
4c24d7
+#else
4c24d7
+
4c24d7
+#include <string.h>
4c24d7
+#include <sys/types.h>
4c24d7
+#include <netinet/in.h>
4c24d7
+#include <sys/socket.h>
4c24d7
+#include <sys/wait.h>
4c24d7
+#include <arpa/inet.h>
4c24d7
+#include <unistd.h>
4c24d7
+#include <gnutls/gnutls.h>
4c24d7
+#include <gnutls/dtls.h>
4c24d7
+#include <signal.h>
4c24d7
+
4c24d7
+#include "utils.h"
4c24d7
+
4c24d7
+static void terminate(void);
4c24d7
+
4c24d7
+/* This program tests whether EtM is negotiated as expected.
4c24d7
+ */
4c24d7
+
4c24d7
+static void server_log_func(int level, const char *str)
4c24d7
+{
4c24d7
+	fprintf(stderr, "server|<%d>| %s", level, str);
4c24d7
+}
4c24d7
+
4c24d7
+static void client_log_func(int level, const char *str)
4c24d7
+{
4c24d7
+	fprintf(stderr, "client|<%d>| %s", level, str);
4c24d7
+}
4c24d7
+
4c24d7
+static unsigned char server_cert_pem[] =
4c24d7
+    "-----BEGIN CERTIFICATE-----\n"
4c24d7
+    "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
4c24d7
+    "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
4c24d7
+    "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
4c24d7
+    "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
4c24d7
+    "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
4c24d7
+    "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
4c24d7
+    "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
4c24d7
+    "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
4c24d7
+    "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
4c24d7
+    "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
4c24d7
+    "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
4c24d7
+    "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
4c24d7
+    "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
4c24d7
+
4c24d7
+const gnutls_datum_t server_cert = { server_cert_pem,
4c24d7
+	sizeof(server_cert_pem)
4c24d7
+};
4c24d7
+
4c24d7
+static unsigned char server_key_pem[] =
4c24d7
+    "-----BEGIN RSA PRIVATE KEY-----\n"
4c24d7
+    "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
4c24d7
+    "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
4c24d7
+    "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
4c24d7
+    "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
4c24d7
+    "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
4c24d7
+    "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
4c24d7
+    "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
4c24d7
+    "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
4c24d7
+    "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
4c24d7
+    "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
4c24d7
+    "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
4c24d7
+    "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
4c24d7
+    "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
4c24d7
+    "-----END RSA PRIVATE KEY-----\n";
4c24d7
+
4c24d7
+const gnutls_datum_t server_key = { server_key_pem,
4c24d7
+	sizeof(server_key_pem)
4c24d7
+};
4c24d7
+
4c24d7
+
4c24d7
+static int handshake_callback(gnutls_session_t session, unsigned int htype,
4c24d7
+	unsigned post, unsigned int incoming)
4c24d7
+{
4c24d7
+	gnutls_priority_set_direct(session, "NORMAL:-KX-ALL:+ECDHE-RSA", NULL);
4c24d7
+	return 0;
4c24d7
+}
4c24d7
+	
4c24d7
+
4c24d7
+/* A very basic TLS client, with anonymous authentication.
4c24d7
+ */
4c24d7
+
4c24d7
+#define MAX_BUF 1024
4c24d7
+
4c24d7
+static void client(int fd)
4c24d7
+{
4c24d7
+	int ret;
4c24d7
+	char buffer[MAX_BUF + 1];
4c24d7
+	gnutls_certificate_credentials_t x509_cred;
4c24d7
+	gnutls_session_t session;
4c24d7
+	/* Need to enable anonymous KX specifically. */
4c24d7
+
4c24d7
+	global_init();
4c24d7
+
4c24d7
+	if (debug) {
4c24d7
+		gnutls_global_set_log_function(client_log_func);
4c24d7
+		gnutls_global_set_log_level(7);
4c24d7
+	}
4c24d7
+
4c24d7
+	gnutls_certificate_allocate_credentials(&x509_cred);
4c24d7
+
4c24d7
+	/* Initialize TLS session
4c24d7
+	 */
4c24d7
+	gnutls_init(&session, GNUTLS_CLIENT);
4c24d7
+
4c24d7
+	/* Use default priorities */
4c24d7
+	gnutls_priority_set_direct(session, "NORMAL:-KX-ALL:+ECDHE-RSA:-SIGN-ALL:+SIGN-RSA-MD5", NULL);
4c24d7
+
4c24d7
+	/* put the anonymous credentials to the current session
4c24d7
+	 */
4c24d7
+	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
4c24d7
+
4c24d7
+	gnutls_transport_set_int(session, fd);
4c24d7
+	gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE,
4c24d7
+					   GNUTLS_HOOK_PRE,
4c24d7
+					   handshake_callback);
4c24d7
+
4c24d7
+	/* Perform the TLS handshake
4c24d7
+	 */
4c24d7
+	do {
4c24d7
+		ret = gnutls_handshake(session);
4c24d7
+	}
4c24d7
+	while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
4c24d7
+
4c24d7
+	if (ret == GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM) {
4c24d7
+		/* success */
4c24d7
+		goto end;
4c24d7
+	}
4c24d7
+
4c24d7
+	if (ret < 0) {
4c24d7
+		terminate();
4c24d7
+		fail("client: Handshake failed: %s\n", gnutls_strerror(ret));
4c24d7
+		exit(1);
4c24d7
+	} else {
4c24d7
+		if (debug)
4c24d7
+			success("client: Handshake was completed\n");
4c24d7
+	}
4c24d7
+
4c24d7
+	if (gnutls_sign_algorithm_get(session) == GNUTLS_SIGN_RSA_MD5) {
4c24d7
+		terminate();
4c24d7
+		fail("client: MD5 was negotiated\n");
4c24d7
+		exit(1);
4c24d7
+	}
4c24d7
+	success("client: %s was negotiated\n", gnutls_sign_get_name(gnutls_sign_algorithm_get(session)));
4c24d7
+
4c24d7
+	if (debug)
4c24d7
+		success("client: TLS version is: %s\n",
4c24d7
+			gnutls_protocol_get_name
4c24d7
+			(gnutls_protocol_get_version(session)));
4c24d7
+
4c24d7
+	do {
4c24d7
+		do {
4c24d7
+			ret = gnutls_record_recv(session, buffer, MAX_BUF);
4c24d7
+		} while (ret == GNUTLS_E_AGAIN
4c24d7
+			 || ret == GNUTLS_E_INTERRUPTED);
4c24d7
+	} while (ret > 0);
4c24d7
+
4c24d7
+	if (ret == 0) {
4c24d7
+		if (debug)
4c24d7
+			success
4c24d7
+			    ("client: Peer has closed the TLS connection\n");
4c24d7
+		goto end;
4c24d7
+	} else if (ret < 0) {
4c24d7
+		terminate();
4c24d7
+		fail("client: Error: %s\n", gnutls_strerror(ret));
4c24d7
+		exit(1);
4c24d7
+	}
4c24d7
+
4c24d7
+	gnutls_bye(session, GNUTLS_SHUT_WR);
4c24d7
+
4c24d7
+      end:
4c24d7
+
4c24d7
+	close(fd);
4c24d7
+
4c24d7
+	gnutls_deinit(session);
4c24d7
+
4c24d7
+	gnutls_certificate_free_credentials(x509_cred);
4c24d7
+
4c24d7
+	gnutls_global_deinit();
4c24d7
+}
4c24d7
+
4c24d7
+
4c24d7
+/* These are global */
4c24d7
+pid_t child;
4c24d7
+
4c24d7
+static void terminate(void)
4c24d7
+{
4c24d7
+	kill(child, SIGTERM);
4c24d7
+	exit(1);
4c24d7
+}
4c24d7
+
4c24d7
+static void server(int fd)
4c24d7
+{
4c24d7
+	int ret;
4c24d7
+	char buffer[MAX_BUF + 1];
4c24d7
+	gnutls_session_t session;
4c24d7
+	gnutls_certificate_credentials_t x509_cred;
4c24d7
+
4c24d7
+	/* this must be called once in the program
4c24d7
+	 */
4c24d7
+	global_init();
4c24d7
+	memset(buffer, 0, sizeof(buffer));
4c24d7
+
4c24d7
+	if (debug) {
4c24d7
+		gnutls_global_set_log_function(server_log_func);
4c24d7
+		gnutls_global_set_log_level(4711);
4c24d7
+	}
4c24d7
+
4c24d7
+	gnutls_certificate_allocate_credentials(&x509_cred);
4c24d7
+	gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
4c24d7
+					    &server_key,
4c24d7
+					    GNUTLS_X509_FMT_PEM);
4c24d7
+
4c24d7
+	gnutls_init(&session, GNUTLS_SERVER);
4c24d7
+
4c24d7
+	/* avoid calling all the priority functions, since the defaults
4c24d7
+	 * are adequate.
4c24d7
+	 */
4c24d7
+	gnutls_priority_set_direct(session, "NORMAL:-KX-ALL:+ECDHE-RSA:-SIGN-ALL:+SIGN-RSA-MD5", NULL);
4c24d7
+
4c24d7
+	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
4c24d7
+
4c24d7
+	gnutls_transport_set_int(session, fd);
4c24d7
+
4c24d7
+	do {
4c24d7
+		ret = gnutls_handshake(session);
4c24d7
+	} while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
4c24d7
+	if (ret < 0) {
4c24d7
+		/* failure is expected here */
4c24d7
+		goto end;
4c24d7
+	}
4c24d7
+
4c24d7
+	if (debug) {
4c24d7
+		success("server: Handshake was completed\n");
4c24d7
+		success("server: %s was negotiated\n", gnutls_sign_get_name(gnutls_sign_algorithm_get(session)));
4c24d7
+	}
4c24d7
+
4c24d7
+	if (debug)
4c24d7
+		success("server: TLS version is: %s\n",
4c24d7
+			gnutls_protocol_get_name
4c24d7
+			(gnutls_protocol_get_version(session)));
4c24d7
+
4c24d7
+	/* do not wait for the peer to close the connection.
4c24d7
+	 */
4c24d7
+	gnutls_bye(session, GNUTLS_SHUT_WR);
4c24d7
+
4c24d7
+ end:
4c24d7
+	close(fd);
4c24d7
+	gnutls_deinit(session);
4c24d7
+
4c24d7
+	gnutls_certificate_free_credentials(x509_cred);
4c24d7
+
4c24d7
+	gnutls_global_deinit();
4c24d7
+
4c24d7
+	if (debug)
4c24d7
+		success("server: finished\n");
4c24d7
+}
4c24d7
+
4c24d7
+static void ch_handler(int sig)
4c24d7
+{
4c24d7
+	int status;
4c24d7
+	wait(&status);
4c24d7
+	if (WEXITSTATUS(status) != 0 ||
4c24d7
+	    (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)) {
4c24d7
+		if (WIFSIGNALED(status))
4c24d7
+			fail("Child died with sigsegv\n");
4c24d7
+		else
4c24d7
+			fail("Child died with status %d\n",
4c24d7
+			     WEXITSTATUS(status));
4c24d7
+		terminate();
4c24d7
+	}
4c24d7
+	return;
4c24d7
+}
4c24d7
+
4c24d7
+void doit(void)
4c24d7
+{
4c24d7
+	int fd[2];
4c24d7
+	int ret;
4c24d7
+
4c24d7
+	signal(SIGCHLD, ch_handler);
4c24d7
+
4c24d7
+	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
4c24d7
+	if (ret < 0) {
4c24d7
+		perror("socketpair");
4c24d7
+		exit(1);
4c24d7
+	}
4c24d7
+
4c24d7
+	child = fork();
4c24d7
+	if (child < 0) {
4c24d7
+		perror("fork");
4c24d7
+		fail("fork");
4c24d7
+		exit(1);
4c24d7
+	}
4c24d7
+
4c24d7
+	if (child) {
4c24d7
+		/* parent */
4c24d7
+		close(fd[1]);
4c24d7
+		client(fd[0]);
4c24d7
+		kill(child, SIGTERM);
4c24d7
+	} else {
4c24d7
+		close(fd[0]);
4c24d7
+		server(fd[1]);
4c24d7
+		exit(0);
4c24d7
+	}
4c24d7
+}
4c24d7
+
4c24d7
+#endif				/* _WIN32 */
4c24d7
-- 
4c24d7
2.5.0
4c24d7