From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Newsgroups: gmane.comp.gnu.guix.patches,gmane.lisp.guile.devel Subject: [bug#56867] [PATCH] download: Do not wrap TLS port on GnuTLS >= 3.7.7. Date: Thu, 04 Aug 2022 16:20:12 +0200 Message-ID: <87pmhgks4j.fsf@gnu.org> References: <20220801090749.11655-1-ludo@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39344"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) Cc: guile-devel@gnu.org To: 56867@debbugs.gnu.org Original-X-From: guix-patches-bounces+gcggp-guix-patches=m.gmane-mx.org@gnu.org Thu Aug 04 16:21:12 2022 Return-path: Envelope-to: gcggp-guix-patches@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oJbin-000A2z-Ez for gcggp-guix-patches@m.gmane-mx.org; Thu, 04 Aug 2022 16:21:09 +0200 Original-Received: from localhost ([::1]:47512 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oJbil-0001rk-Lo for gcggp-guix-patches@m.gmane-mx.org; Thu, 04 Aug 2022 10:21:07 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37402) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oJbig-0001q7-Ez for guix-patches@gnu.org; Thu, 04 Aug 2022 10:21:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36213) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oJbig-0005A4-60 for guix-patches@gnu.org; Thu, 04 Aug 2022 10:21:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oJbig-0001PV-1Y for guix-patches@gnu.org; Thu, 04 Aug 2022 10:21:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 04 Aug 2022 14:21:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56867 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch Original-Received: via spool by 56867-submit@debbugs.gnu.org id=B56867.16596228365365 (code B ref 56867); Thu, 04 Aug 2022 14:21:01 +0000 Original-Received: (at 56867) by debbugs.gnu.org; 4 Aug 2022 14:20:36 +0000 Original-Received: from localhost ([127.0.0.1]:54189 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oJbi3-0001OB-Nr for submit@debbugs.gnu.org; Thu, 04 Aug 2022 10:20:36 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:52560) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oJbi1-0001Nq-VQ for 56867@debbugs.gnu.org; Thu, 04 Aug 2022 10:20:22 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:42100) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oJbhw-00052f-Ny; Thu, 04 Aug 2022 10:20:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:In-Reply-To:Date:References:Subject:To: From; bh=AJICttRZOaEGO+iSWH4Ghlg//5/JOxiOBH+4qG2JF6o=; b=EuarLm90E6finYUhMz2a 4ATot69akhOe6HId70PqwH9Pgbe4VmcPoBONGqh+eCVv+NHU7vDfp1HD8aq9yCn5tSpyg8VsAIfJM HdEQa7MCyLpW3SRJHGNTwQmo/WVCKDfKkUbfeZVLH/ji+4UhfCLxTyONU9eoMJHq8tbq35PQ+cCyZ IRe9VM+C11CjYlomY25pWN3Y7kzzZZhn/UWc+80ThaoXR4Vtq+a1lOrHGgm8kGjeK6LV+iRCOLjfA wCB81I0cxgU/QCfCjVU/j6G82Km+UIckfi9IZXbl7dnOlvY0J11eJ2gq/n8/FqVmsOR6CodGNAv2n QdFXPxByyF7iUQ==; Original-Received: from [2001:660:6102:310:f6b5:dff0:8fea:f7c9] (port=42640 helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oJbhw-0005iU-B2; Thu, 04 Aug 2022 10:20:16 -0400 In-Reply-To: <20220801090749.11655-1-ludo@gnu.org> ("Ludovic =?UTF-8?Q?Court=C3=A8s?="'s message of "Mon, 1 Aug 2022 11:07:49 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+gcggp-guix-patches=m.gmane-mx.org@gnu.org Original-Sender: "Guix-patches" Xref: news.gmane.io gmane.comp.gnu.guix.patches:28603 gmane.lisp.guile.devel:21292 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Ludovic Court=C3=A8s skribis: > The custom input/output port wrapping the TLS session record port would > introduce overhead, and it would also prevent its uses in a non-blocking > context--e.g., with Fibers. The port close mechanism added in GnuTLS > 3.7.7 allows us to get rid of that wrapper. > > * guix/build/download.scm (wrap-record-port-for-gnutls<3.7.7): New > procedure, with code formerly in 'tls-wrap'. > (tls-wrap): Check for 'set-session-record-port-close!' and use it when > available; otherwise call 'wrap-record-port-for-gnutls<3.7.7'. I synchronized Guile's copy of this code: 317b06bf8 web: 'tls-wrap' retries handshake upon non-fatal errors. c01ca10b3 web: Do not wrap TLS port on GnuTLS >=3D 3.7.7. I realized that=E2=80=99s not enough to make it possible to use non-blocking ports though. First, I noticed that GnuTLS doesn=E2=80=99t implement =E2=80=98write_wait_= fd=E2=80=99, only =E2=80=98read_wait_fd=E2=80=99 (not sure how problematic that is): --8<---------------cut here---------------start------------->8--- scheme@(guile-user)> ,use(web client) scheme@(guile-user)> (define p (open-socket-for-uri "https://guix.gnu.org")) scheme@(guile-user)> ((@@ (ice-9 suspendable-ports) wait-for-writable) p) ice-9/boot-9.scm:1685:16: In procedure raise-exception: In procedure write_wait_fd: unimplemented Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. scheme@(guile-user) [1]> ,q scheme@(guile-user)> ,use(gnutls) scheme@(guile-user)> (gnutls-version) $1 =3D "3.7.7" scheme@(guile-user)> ((@@ (ice-9 suspendable-ports) wait-for-readable) p) $2 =3D 1 --8<---------------cut here---------------end--------------->8--- Second, =E2=80=98open-socket-for-uri=E2=80=99 creates a blocking socket and= uses that as the backing file descriptor of the TLS session. We=E2=80=99d need a way to pass flags for the =E2=80=98socket=E2=80=99 call= made by =E2=80=98open-socket-for-uri=E2=80=99 so we can pass O_NONBLOCK, maybe as s= how below: --=-=-= Content-Type: text/x-patch Content-Disposition: inline diff --git a/module/web/client.scm b/module/web/client.scm index a08c4203c..9273a45ad 100644 --- a/module/web/client.scm +++ b/module/web/client.scm @@ -320,7 +320,8 @@ host name without trailing dot." (read-response port)) (define* (open-socket-for-uri uri-or-string - #:key (verify-certificate? #t)) + #:key (verify-certificate? #t) + (flags 0)) "Return an open input/output port for a connection to URI-OR-STRING. When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates." (define uri @@ -373,10 +374,18 @@ When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates." (when (and https? (current-https-proxy)) (setup-http-tunnel s uri)) - (if https? - (tls-wrap s (uri-host uri) - #:verify-certificate? verify-certificate?) - s))) + (let ((port (if https? + (tls-wrap s (uri-host uri) + #:verify-certificate? verify-certificate?) + s))) + (unless (zero? flags) + ;; FLAGS might contain O_NONBLOCK. Thus, set it as a last step + ;; because 'handshake' otherwise throws an exception for + ;; GNUTLS_E_AGAIN. + (let ((initial-flags (fcntl s F_GETFL))) + (fcntl s F_SETFL (logior initial-flags flags)))) + + port))) (define (extend-request r k v . additional) (let ((r (set-field r (request-headers) --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable =E2=80=A6 which lets us do that: --8<---------------cut here---------------start------------->8--- scheme@(guile-user)> ,use(web client) scheme@(guile-user)> (define p (open-socket-for-uri "https://guix.gnu.org" = #:flags O_NONBLOCK)) scheme@(guile-user)> (http-get "https://guix.gnu.org" #:port p) --8<---------------cut here---------------end--------------->8--- Thoughts? Ludo=E2=80=99. --=-=-=--