Blame SOURCES/wget-1.19.5-Enable-post-handshake-auth-under-gnutls-on-TLS1.3.patch

11f80c
From c11cc83d9ee9230f090c2400a57bbd562905d782 Mon Sep 17 00:00:00 2001
11f80c
From: Nikos Mavrogiannopoulos <nmav@redhat.com>
11f80c
Date: Mon, 8 Oct 2018 10:42:22 +0200
11f80c
Subject: [PATCH] Enable post-handshake auth under gnutls on TLS1.3
11f80c
11f80c
---
11f80c
 src/gnutls.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++
11f80c
 1 file changed, 96 insertions(+)
11f80c
11f80c
diff --git a/src/gnutls.c b/src/gnutls.c
11f80c
index 206d0b09..a2c9d1c1 100644
11f80c
--- a/src/gnutls.c
11f80c
+++ b/src/gnutls.c
11f80c
@@ -60,6 +60,11 @@ as that of the covered work.  */
11f80c
 static int
11f80c
 _do_handshake (gnutls_session_t session, int fd, double timeout);
11f80c
 
11f80c
+#if GNUTLS_VERSION_NUMBER >= 0x030604
11f80c
+static int
11f80c
+_do_reauth (gnutls_session_t session, int fd, double timeout);
11f80c
+#endif
11f80c
+
11f80c
 static int
11f80c
 key_type_to_gnutls_type (enum keyfile_type type)
11f80c
 {
11f80c
@@ -287,6 +292,14 @@ wgnutls_read_timeout (int fd, char *buf, int bufsize, void *arg, double timeout)
11f80c
               if ((ret = _do_handshake (ctx->session, fd, timeout)) == 0)
11f80c
                 ret = GNUTLS_E_AGAIN; /* restart reading */
11f80c
             }
11f80c
+#if GNUTLS_VERSION_NUMBER >= 0x030604
11f80c
+          if (!timed_out && ret == GNUTLS_E_REAUTH_REQUEST)
11f80c
+            {
11f80c
+              DEBUGP (("GnuTLS: *** re-authentication while reading\n"));
11f80c
+              if ((ret = _do_reauth (ctx->session, fd, timeout)) == 0)
11f80c
+                ret = GNUTLS_E_AGAIN; /* restart reading */
11f80c
+            }
11f80c
+#endif
11f80c
         }
11f80c
     }
11f80c
   while (ret == GNUTLS_E_INTERRUPTED || (ret == GNUTLS_E_AGAIN && !timed_out));
11f80c
@@ -519,6 +532,84 @@ _do_handshake (gnutls_session_t session, int fd, double timeout)
11f80c
   return err;
11f80c
 }
11f80c
 
11f80c
+#if GNUTLS_VERSION_NUMBER >= 0x030604
11f80c
+static int
11f80c
+_do_reauth (gnutls_session_t session, int fd, double timeout)
11f80c
+{
11f80c
+#ifdef F_GETFL
11f80c
+  int flags = 0;
11f80c
+#endif
11f80c
+  int err;
11f80c
+
11f80c
+  if (timeout)
11f80c
+    {
11f80c
+#ifdef F_GETFL
11f80c
+      flags = fcntl (fd, F_GETFL, 0);
11f80c
+      if (flags < 0)
11f80c
+        return flags;
11f80c
+      if (fcntl (fd, F_SETFL, flags | O_NONBLOCK))
11f80c
+        return -1;
11f80c
+#else
11f80c
+      /* XXX: Assume it was blocking before.  */
11f80c
+      const int one = 1;
11f80c
+      if (ioctl (fd, FIONBIO, &one) < 0)
11f80c
+        return -1;
11f80c
+#endif
11f80c
+    }
11f80c
+
11f80c
+  /* We don't stop the handshake process for non-fatal errors */
11f80c
+  do
11f80c
+    {
11f80c
+      err = gnutls_reauth (session, 0);
11f80c
+
11f80c
+      if (timeout && err == GNUTLS_E_AGAIN)
11f80c
+        {
11f80c
+          if (gnutls_record_get_direction (session))
11f80c
+            {
11f80c
+              /* wait for writeability */
11f80c
+              err = select_fd (fd, timeout, WAIT_FOR_WRITE);
11f80c
+            }
11f80c
+          else
11f80c
+            {
11f80c
+              /* wait for readability */
11f80c
+              err = select_fd (fd, timeout, WAIT_FOR_READ);
11f80c
+            }
11f80c
+
11f80c
+          if (err <= 0)
11f80c
+            {
11f80c
+              if (err == 0)
11f80c
+                {
11f80c
+                  errno = ETIMEDOUT;
11f80c
+                  err = -1;
11f80c
+                }
11f80c
+              break;
11f80c
+            }
11f80c
+
11f80c
+           err = GNUTLS_E_AGAIN;
11f80c
+        }
11f80c
+      else if (err < 0)
11f80c
+        {
11f80c
+          logprintf (LOG_NOTQUIET, "GnuTLS: %s\n", gnutls_strerror (err));
11f80c
+        }
11f80c
+    }
11f80c
+  while (err && gnutls_error_is_fatal (err) == 0);
11f80c
+
11f80c
+  if (timeout)
11f80c
+    {
11f80c
+#ifdef F_GETFL
11f80c
+      if (fcntl (fd, F_SETFL, flags) < 0)
11f80c
+        return -1;
11f80c
+#else
11f80c
+      const int zero = 0;
11f80c
+      if (ioctl (fd, FIONBIO, &zero) < 0)
11f80c
+        return -1;
11f80c
+#endif
11f80c
+    }
11f80c
+
11f80c
+  return err;
11f80c
+}
11f80c
+#endif
11f80c
+
11f80c
 static const char *
11f80c
 _sni_hostname(const char *hostname)
11f80c
 {
11f80c
@@ -655,7 +746,12 @@ ssl_connect_wget (int fd, const char *hostname, int *continue_session)
11f80c
   gnutls_session_t session;
11f80c
   int err;
11f80c
 
11f80c
+#if GNUTLS_VERSION_NUMBER >= 0x030604
11f80c
+  // enable support of TLS1.3 post-handshake authentication
11f80c
+  gnutls_init (&session, GNUTLS_CLIENT | GNUTLS_POST_HANDSHAKE_AUTH);
11f80c
+#else
11f80c
   gnutls_init (&session, GNUTLS_CLIENT);
11f80c
+#endif
11f80c
 
11f80c
   /* We set the server name but only if it's not an IP address. */
11f80c
   if (! is_valid_ip_address (hostname))
11f80c
-- 
11f80c
2.17.2
11f80c