cd0318
From 7b700dbcd5907944a7dd2f74cd26ad8586cd4bac Mon Sep 17 00:00:00 2001
cd0318
From: Daiki Ueno <ueno@gnu.org>
cd0318
Date: Tue, 28 Jun 2022 09:37:22 +0900
cd0318
Subject: [PATCH 1/3] tests: enable KTLS config while running gnutls_ktls test
cd0318
cd0318
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cd0318
---
cd0318
 tests/Makefile.am   |  9 +++++----
cd0318
 tests/gnutls_ktls.c |  4 ++--
cd0318
 tests/ktls.sh       | 46 +++++++++++++++++++++++++++++++++++++++++++++
cd0318
 3 files changed, 53 insertions(+), 6 deletions(-)
cd0318
 create mode 100755 tests/ktls.sh
cd0318
cd0318
diff --git a/tests/Makefile.am b/tests/Makefile.am
cd0318
index 4deeb6462b..cba67e8db8 100644
cd0318
--- a/tests/Makefile.am
cd0318
+++ b/tests/Makefile.am
cd0318
@@ -441,10 +441,6 @@ ctests += x509self x509dn anonself pskself pskself2 dhepskself	\
cd0318
 	resume-with-record-size-limit
cd0318
 endif
cd0318
 
cd0318
-if ENABLE_KTLS
cd0318
-ctests += gnutls_ktls
cd0318
-endif
cd0318
-
cd0318
 ctests += record-sendfile
cd0318
 
cd0318
 gc_CPPFLAGS = $(AM_CPPFLAGS) \
cd0318
@@ -500,6 +496,11 @@ if ENABLE_TPM2
cd0318
 dist_check_SCRIPTS += tpm2.sh
cd0318
 endif
cd0318
 
cd0318
+if ENABLE_KTLS
cd0318
+indirect_tests += gnutls_ktls
cd0318
+dist_check_SCRIPTS += ktls.sh
cd0318
+endif
cd0318
+
cd0318
 if !WINDOWS
cd0318
 
cd0318
 #
cd0318
diff --git a/tests/gnutls_ktls.c b/tests/gnutls_ktls.c
cd0318
index 3966e2b10a..8f9c5fa36e 100644
cd0318
--- a/tests/gnutls_ktls.c
cd0318
+++ b/tests/gnutls_ktls.c
cd0318
@@ -84,7 +84,7 @@ static void client(int fd, const char *prio)
cd0318
 
cd0318
 	ret = gnutls_transport_is_ktls_enabled(session);
cd0318
 	if (!(ret & GNUTLS_KTLS_RECV)){
cd0318
-		fail("client: KTLS was not properly inicialized\n");
cd0318
+		fail("client: KTLS was not properly initialized\n");
cd0318
 		goto end;
cd0318
 	}
cd0318
 
cd0318
@@ -208,7 +208,7 @@ static void server(int fd, const char *prio)
cd0318
 
cd0318
 	ret = gnutls_transport_is_ktls_enabled(session);
cd0318
 	if (!(ret & GNUTLS_KTLS_SEND)){
cd0318
-		fail("server: KTLS was not properly inicialized\n");
cd0318
+		fail("server: KTLS was not properly initialized\n");
cd0318
 		goto end;
cd0318
 	}
cd0318
 	do {
cd0318
diff --git a/tests/ktls.sh b/tests/ktls.sh
cd0318
new file mode 100755
cd0318
index 0000000000..ba52bd5775
cd0318
--- /dev/null
cd0318
+++ b/tests/ktls.sh
cd0318
@@ -0,0 +1,46 @@
cd0318
+#!/bin/sh
cd0318
+
cd0318
+# Copyright (C) 2022 Red Hat, Inc.
cd0318
+#
cd0318
+# Author: Daiki Ueno
cd0318
+#
cd0318
+# This file is part of GnuTLS.
cd0318
+#
cd0318
+# GnuTLS is free software; you can redistribute it and/or modify it
cd0318
+# under the terms of the GNU General Public License as published by the
cd0318
+# Free Software Foundation; either version 3 of the License, or (at
cd0318
+# your option) any later version.
cd0318
+#
cd0318
+# GnuTLS is distributed in the hope that it will be useful, but
cd0318
+# WITHOUT ANY WARRANTY; without even the implied warranty of
cd0318
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cd0318
+# General Public License for more details.
cd0318
+#
cd0318
+# You should have received a copy of the GNU General Public License
cd0318
+# along with GnuTLS; if not, write to the Free Software Foundation,
cd0318
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cd0318
+
cd0318
+: ${builddir=.}
cd0318
+
cd0318
+. "$srcdir/scripts/common.sh"
cd0318
+
cd0318
+if ! grep '^tls ' /proc/modules 2>1 >& /dev/null; then
cd0318
+    exit 77
cd0318
+fi
cd0318
+
cd0318
+testdir=`create_testdir ktls`
cd0318
+
cd0318
+cfg="$testdir/config"
cd0318
+
cd0318
+cat <<EOF > "$cfg"
cd0318
+[global]
cd0318
+ktls = true
cd0318
+EOF
cd0318
+
cd0318
+GNUTLS_SYSTEM_PRIORITY_FAIL_ON_INVALID=1 \
cd0318
+GNUTLS_SYSTEM_PRIORITY_FILE="$cfg" \
cd0318
+"$builddir/gnutls_ktls" "$@"
cd0318
+rc=$?
cd0318
+
cd0318
+rm -rf "$testdir"
cd0318
+exit $rc
cd0318
-- 
cd0318
2.36.1
cd0318
cd0318
cd0318
From 4a492462535a7f3a831685d3cf420b50ef219511 Mon Sep 17 00:00:00 2001
cd0318
From: Daiki Ueno <ueno@gnu.org>
cd0318
Date: Tue, 28 Jun 2022 10:23:33 +0900
cd0318
Subject: [PATCH 2/3] handshake: do not reset KTLS enablement in
cd0318
 gnutls_handshake
cd0318
cd0318
As gnutls_handshake can be repeatedly called upon non-blocking setup,
cd0318
we shouldn't try to call setsockopt for KTLS upon every call.
cd0318
cd0318
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cd0318
---
cd0318
 lib/handshake.c | 12 ++++++------
cd0318
 1 file changed, 6 insertions(+), 6 deletions(-)
cd0318
cd0318
diff --git a/lib/handshake.c b/lib/handshake.c
cd0318
index 4dd457bf22..3886306eb4 100644
cd0318
--- a/lib/handshake.c
cd0318
+++ b/lib/handshake.c
cd0318
@@ -2813,12 +2813,6 @@ int gnutls_handshake(gnutls_session_t session)
cd0318
 	const version_entry_st *vers = get_version(session);
cd0318
 	int ret;
cd0318
 
cd0318
-	session->internals.ktls_enabled = 0;
cd0318
-#ifdef ENABLE_KTLS
cd0318
-	if (_gnutls_config_is_ktls_enabled() == true)
cd0318
-		_gnutls_ktls_enable(session);
cd0318
-#endif
cd0318
-
cd0318
 	if (unlikely(session->internals.initial_negotiation_completed)) {
cd0318
 		if (vers->tls13_sem) {
cd0318
 			if (session->security_parameters.entity == GNUTLS_CLIENT) {
cd0318
@@ -2864,6 +2858,12 @@ int gnutls_handshake(gnutls_session_t session)
cd0318
 			end->tv_nsec =
cd0318
 				(start->tv_nsec + tmo_ms * 1000000LL) % 1000000000LL;
cd0318
 		}
cd0318
+
cd0318
+#ifdef ENABLE_KTLS
cd0318
+		if (_gnutls_config_is_ktls_enabled()) {
cd0318
+			_gnutls_ktls_enable(session);
cd0318
+		}
cd0318
+#endif
cd0318
 	}
cd0318
 
cd0318
 	if (session->internals.recv_state == RECV_STATE_FALSE_START) {
cd0318
-- 
cd0318
2.36.1
cd0318
cd0318
cd0318
From ce13208e13b5dec73993c583d4c64ab7714e4a7a Mon Sep 17 00:00:00 2001
cd0318
From: Daiki Ueno <ueno@gnu.org>
cd0318
Date: Tue, 28 Jun 2022 10:53:55 +0900
cd0318
Subject: [PATCH 3/3] ktls: _gnutls_ktls_enable: fix GNUTLS_KTLS_SEND
cd0318
 calculation
cd0318
cd0318
Previously, if the first setsockopt for GNUTLS_KTLS_RECV fails and the
cd0318
same socket is used for both sending and receiving, GNUTLS_KTLS_SEND
cd0318
was unconditionally set.  This fixes the conditions and also adds more
cd0318
logging.
cd0318
cd0318
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cd0318
---
cd0318
 lib/system/ktls.c | 21 ++++++++++++++++-----
cd0318
 1 file changed, 16 insertions(+), 5 deletions(-)
cd0318
cd0318
diff --git a/lib/system/ktls.c b/lib/system/ktls.c
cd0318
index b9f7a73fb5..ddf27fac76 100644
cd0318
--- a/lib/system/ktls.c
cd0318
+++ b/lib/system/ktls.c
cd0318
@@ -47,7 +47,7 @@
cd0318
 gnutls_transport_ktls_enable_flags_t
cd0318
 gnutls_transport_is_ktls_enabled(gnutls_session_t session){
cd0318
 	if (unlikely(!session->internals.initial_negotiation_completed)){
cd0318
-		_gnutls_debug_log("Initial negotiation is not yet complete");
cd0318
+		_gnutls_debug_log("Initial negotiation is not yet complete\n");
cd0318
 		return 0;
cd0318
 	}
cd0318
 
cd0318
@@ -57,16 +57,27 @@ gnutls_transport_is_ktls_enabled(gnutls_session_t session){
cd0318
 void _gnutls_ktls_enable(gnutls_session_t session)
cd0318
 {
cd0318
 	int sockin, sockout;
cd0318
+
cd0318
 	gnutls_transport_get_int2(session, &sockin, &sockout);
cd0318
 
cd0318
-	if (setsockopt(sockin, SOL_TCP, TCP_ULP, "tls", sizeof ("tls")) == 0)
cd0318
+	if (setsockopt(sockin, SOL_TCP, TCP_ULP, "tls", sizeof ("tls")) == 0) {
cd0318
 		session->internals.ktls_enabled |= GNUTLS_KTLS_RECV;
cd0318
+		if (sockin == sockout) {
cd0318
+			session->internals.ktls_enabled |= GNUTLS_KTLS_SEND;
cd0318
+		}
cd0318
+	} else {
cd0318
+		_gnutls_record_log("Unable to set TCP_ULP for read socket: %d\n",
cd0318
+				   errno);
cd0318
+	}
cd0318
 
cd0318
 	if (sockin != sockout) {
cd0318
-		if (setsockopt(sockout, SOL_TCP, TCP_ULP, "tls", sizeof ("tls")) == 0)
cd0318
+		if (setsockopt(sockout, SOL_TCP, TCP_ULP, "tls", sizeof ("tls")) == 0) {
cd0318
 			session->internals.ktls_enabled |= GNUTLS_KTLS_SEND;
cd0318
-	} else
cd0318
-		session->internals.ktls_enabled |= GNUTLS_KTLS_SEND;
cd0318
+		} else {
cd0318
+			_gnutls_record_log("Unable to set TCP_ULP for write socket: %d\n",
cd0318
+					   errno);
cd0318
+		}
cd0318
+	}
cd0318
 }
cd0318
 
cd0318
 int _gnutls_ktls_set_keys(gnutls_session_t session)
cd0318
-- 
cd0318
2.36.1
cd0318
cd0318
From 2d3cba6bb21acb40141180298f3924c73c7de8f8 Mon Sep 17 00:00:00 2001
cd0318
From: Daiki Ueno <ueno@gnu.org>
cd0318
Date: Tue, 26 Jul 2022 11:38:41 +0900
cd0318
Subject: [PATCH 1/2] handshake: do not enable KTLS if custom pull/push
cd0318
 functions are set
cd0318
cd0318
If gnutls_transport_set_pull_function or
cd0318
gnutls_transport_set_push_function is used, we can't assume the
cd0318
underlying transport handle is an FD.
cd0318
cd0318
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cd0318
---
cd0318
 lib/handshake.c | 9 ++++++++-
cd0318
 1 file changed, 8 insertions(+), 1 deletion(-)
cd0318
cd0318
diff --git a/lib/handshake.c b/lib/handshake.c
cd0318
index 3886306eb4..cf025a84f6 100644
cd0318
--- a/lib/handshake.c
cd0318
+++ b/lib/handshake.c
cd0318
@@ -2861,7 +2861,14 @@ int gnutls_handshake(gnutls_session_t session)
cd0318
 
cd0318
 #ifdef ENABLE_KTLS
cd0318
 		if (_gnutls_config_is_ktls_enabled()) {
cd0318
-			_gnutls_ktls_enable(session);
cd0318
+			if (session->internals.pull_func ||
cd0318
+			    session->internals.push_func) {
cd0318
+				_gnutls_audit_log(session,
cd0318
+						  "Not enabling KTLS with "
cd0318
+						  "custom pull/push function\n");
cd0318
+			} else {
cd0318
+				_gnutls_ktls_enable(session);
cd0318
+			}
cd0318
 		}
cd0318
 #endif
cd0318
 	}
cd0318
-- 
cd0318
2.37.1
cd0318
cd0318
cd0318
From f7160e4fb970b4ba6f96e85e21f8395eae735d95 Mon Sep 17 00:00:00 2001
cd0318
From: Daiki Ueno <ueno@gnu.org>
cd0318
Date: Tue, 26 Jul 2022 11:39:57 +0900
cd0318
Subject: [PATCH 2/2] socket: only set pull/push functions when --save-*-trace
cd0318
 is used
cd0318
cd0318
This allows gnutls-cli to use KTLS for the transport, unless either
cd0318
--save-client-trace or --save-server-trace is used.
cd0318
cd0318
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cd0318
---
cd0318
 src/socket.c | 16 ++++++++--------
cd0318
 1 file changed, 8 insertions(+), 8 deletions(-)
cd0318
cd0318
diff --git a/src/socket.c b/src/socket.c
cd0318
index 39f18dbe18..36ac292700 100644
cd0318
--- a/src/socket.c
cd0318
+++ b/src/socket.c
cd0318
@@ -586,16 +586,16 @@ socket_open2(socket_st * hd, const char *hostname, const char *service,
cd0318
 				gnutls_session_set_data(hd->session, hd->rdata.data, hd->rdata.size);
cd0318
 			}
cd0318
 
cd0318
-			if (server_trace)
cd0318
+			if (client_trace || server_trace) {
cd0318
 				hd->server_trace = server_trace;
cd0318
-
cd0318
-			if (client_trace)
cd0318
 				hd->client_trace = client_trace;
cd0318
-
cd0318
-			gnutls_transport_set_push_function(hd->session, wrap_push);
cd0318
-			gnutls_transport_set_pull_function(hd->session, wrap_pull);
cd0318
-			gnutls_transport_set_pull_timeout_function(hd->session, wrap_pull_timeout_func);
cd0318
-			gnutls_transport_set_ptr(hd->session, hd);
cd0318
+				gnutls_transport_set_push_function(hd->session, wrap_push);
cd0318
+				gnutls_transport_set_pull_function(hd->session, wrap_pull);
cd0318
+				gnutls_transport_set_pull_timeout_function(hd->session, wrap_pull_timeout_func);
cd0318
+				gnutls_transport_set_ptr(hd->session, hd);
cd0318
+			} else {
cd0318
+				gnutls_transport_set_int(hd->session, hd->fd);
cd0318
+			}
cd0318
 		}
cd0318
 
cd0318
 		if (!(flags & SOCKET_FLAG_RAW) && !(flags & SOCKET_FLAG_SKIP_INIT)) {
cd0318
-- 
cd0318
2.37.1
cd0318
cd0318
From a5b671fc9105cb5dbe6e6a1c0f39fa787d862076 Mon Sep 17 00:00:00 2001
cd0318
From: Frantisek Krenzelok <krenzelok.frantisek@gmail.com>
cd0318
Date: Fri, 29 Jul 2022 10:38:42 +0200
cd0318
Subject: [PATCH] KTLS: hotfix
cd0318
cd0318
session->internals.pull_func is set to system_read during gnutls_init()
cd0318
so check for user set pull/push function added in commit mentioned
cd0318
bellow will never pass.
cd0318
cd0318
source: 2d3cba6bb21acb40141180298f3924c73c7de8f8
cd0318
cd0318
Signed-off-by: Frantisek Krenzelok <krenzelok.frantisek@gmail.com>
cd0318
---
cd0318
 lib/handshake.c | 3 ++-
cd0318
 1 file changed, 2 insertions(+), 1 deletion(-)
cd0318
cd0318
diff --git a/lib/handshake.c b/lib/handshake.c
cd0318
index cf025a84f6..21edc5ece9 100644
cd0318
--- a/lib/handshake.c
cd0318
+++ b/lib/handshake.c
cd0318
@@ -2861,7 +2861,8 @@ int gnutls_handshake(gnutls_session_t session)
cd0318
 
cd0318
 #ifdef ENABLE_KTLS
cd0318
 		if (_gnutls_config_is_ktls_enabled()) {
cd0318
-			if (session->internals.pull_func ||
cd0318
+			if ((session->internals.pull_func &&
cd0318
+				session->internals.pull_func != system_read) ||
cd0318
 			    session->internals.push_func) {
cd0318
 				_gnutls_audit_log(session,
cd0318
 						  "Not enabling KTLS with "
cd0318
-- 
cd0318
2.37.1
cd0318