From patchwork Thu Aug 4 14:20:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 41229 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 45E1227BBE9; Thu, 4 Aug 2022 15:21:16 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id A589F27BBEA for ; Thu, 4 Aug 2022 15:21:15 +0100 (BST) Received: from localhost ([::1]:47512 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oJbis-0001rk-PO for patchwork@mira.cbaines.net; Thu, 04 Aug 2022 10:21:14 -0400 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 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 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 Subject: [bug#56867] [PATCH] download: Do not wrap TLS port on GnuTLS >= 3.7.7. 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 To: 56867@debbugs.gnu.org Cc: guile-devel@gnu.org Received: via spool by 56867-submit@debbugs.gnu.org id=B56867.16596228365365 (code B ref 56867); Thu, 04 Aug 2022 14:21:01 +0000 Received: (at 56867) by debbugs.gnu.org; 4 Aug 2022 14:20:36 +0000 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 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 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==; 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 From: Ludovic =?utf-8?q?Court=C3=A8s?= References: <20220801090749.11655-1-ludo@gnu.org> Date: Thu, 04 Aug 2022 16:20:12 +0200 In-Reply-To: <20220801090749.11655-1-ludo@gnu.org> ("Ludovic =?utf-8?q?Court?= =?utf-8?q?=C3=A8s?= "'s message of "Mon, 1 Aug 2022 11:07:49 +0200") Message-ID: <87pmhgks4j.fsf@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 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+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches Ludovic Courtès 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 >= 3.7.7. I realized that’s not enough to make it possible to use non-blocking ports though. First, I noticed that GnuTLS doesn’t implement ‘write_wait_fd’, only ‘read_wait_fd’ (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 = "3.7.7" scheme@(guile-user)> ((@@ (ice-9 suspendable-ports) wait-for-readable) p) $2 = 1 --8<---------------cut here---------------end--------------->8--- Second, ‘open-socket-for-uri’ creates a blocking socket and uses that as the backing file descriptor of the TLS session. We’d need a way to pass flags for the ‘socket’ call made by ‘open-socket-for-uri’ so we can pass O_NONBLOCK, maybe as show below: … 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’. 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)