Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brUtS-0006kP-PI for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Oct 2016 18:56:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1brUtR-0001HP-RQ for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Oct 2016 18:56:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1brUsm-0000XG-VJ for pgsql-bugs@postgresql.org; Tue, 04 Oct 2016 18:56:05 +0000 Received: from mail-lf0-x231.google.com ([2a00:1450:4010:c07::231]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1brUsf-00045I-04 for pgsql-bugs@postgresql.org; Tue, 04 Oct 2016 18:56:04 +0000 Received: by mail-lf0-x231.google.com with SMTP id b75so60242716lfg.3 for ; Tue, 04 Oct 2016 11:55:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:subject:to:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=xDnd2H6IoPUk2wEKyElKK5vadyyfIiuoj8x6R2UGj+Q=; b=k7b67dBrBC12ahC3lzULDTZt5KJXq7ZnBxUi9vsPGwGjAkmykH1MLI9av39e/pKX65 seCDTerj/y9IY0iCZKNtFheo3nYc2kx6Gxyo1gQLEn3qgmgAQJU98RMuZUrOaDK0IV07 M1jflSXjg4WdQDndzYhar+fBNCi75jG/Pr70/mi49xV/3GC6KOliCMcyOIveCCswaXwB n5YyAEum3fsBnHkKRZ0hryLt5Dl7/tpBHv8Leht3NMZT2f519JpEWEIie8KP6c1+5xKn J90Ko8tgWdla+bKb7/Y0ITWTJmWKQxHRNGSQd+PZzGwLRK0tI8E9Y7UNfPu3B8lf/WZL aylw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:subject:to:references:from:message-id :date:user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=xDnd2H6IoPUk2wEKyElKK5vadyyfIiuoj8x6R2UGj+Q=; b=kidN/AA9rS2uGfj/uk1aj8KGCPXJGh35uC/+pswjHA1Hw05m/tOY6p2wMOH7LtvGNd fKc0j8dF0UHQMo1vdR6a6Wy5sV0R+ozTu5Zq7V9sQh/pmT37V2eJp4qVMVYqL6002krf Ha7xn/Hr5JJclpCgmTGoP1n0zogTkSwOq1tq6vqrZES/rolc77daY4tlaUi+WEWXhYuz +dNFF6CYvSp9St04dYOzhKNAEAnMv6ug2ydGkO/Bt3Tg0W6cSL1rGgWzI59oBk4XVRM8 yxYz/+mzPZKEOH8RBSj5uSJ88Q9RBhUSEenVEajG7jNz2a42dzZXPncFVSfqRpBbz+2J Y/gA== X-Gm-Message-State: AA6/9RlURLnmNJki91WVeIjVVq1tlwlPtSCHFrmLg9hHaAA1ICV+Z9vhRWjCx8d8rUiv8g== X-Received: by 10.25.219.11 with SMTP id s11mr2027703lfg.129.1475607354537; Tue, 04 Oct 2016 11:55:54 -0700 (PDT) Received: from [192.168.1.111] (dsl-hkibrasgw1-58c38f-82.dhcp.inet.fi. [88.195.143.82]) by smtp.gmail.com with ESMTPSA id e38sm181550lji.38.2016.10.04.11.55.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Oct 2016 11:55:53 -0700 (PDT) Subject: Re: BUG #14329: libpq doesn't send complete client certificate chain on first SSL connection To: "Zuk, Kacper" , "pgsql-bugs@postgresql.org" References: <20160920101051.1355.79453@wrigleys.postgresql.org> From: Heikki Linnakangas Message-ID: Date: Tue, 4 Oct 2016 21:55:52 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On 10/04/2016 02:08 PM, Zuk, Kacper wrote: > On 9/21/16, 10:06 AM, "Heikki Linnakangas" wrote: >> Setting up a test environment with the required certificates and CAs is >> a bit tedious. Would you be interested in adding a test case for this in >> the SSL test suite, in src/test/ssl, and posting a patch for that? I can >> take a stab at fixing this, but having a test case ready would give me a >> head start. > > I’ve attached the patch that adds a test case. Please let me know if I should send it to pgsql-hackers instead. Remember to run make sslfiles, as I didn’t include generated certificate in patch. Thanks! Now that I look closer into this, I can see more trouble: 1. Since the SSL context is shared between all connections and all threads, there's a race condition if two threads try to open a connection at the same time. The SSL_CTX_use_certificate_chain_file() call, and SSL_CTX_load_verify_locations() later in the function, are protected by the global mutex, but we only hold the mutex for the duration of those calls. It doesn't protect from the fact that while one thread is opening a connection with one set of options, another thread opens a connection with different options. They might get mixed up. That's a problem if the connections use different sslcert or sslrootcert settings. 2. Even with a single thread, using different sslrootcert options in different PQconnectdb() calls fails outright, with error: SSL error: block type is not 01 I got that with a test program that simply calls: PQconnectdb(); PQfinish(); PQconnectdb(); PQfinish(); when the two connection strings point to different servers, with different certificates signed by different root CA, and the connection strings have different (correct) sslrootcert options for the servers. Either PQconnectdb() call works separately, but put them in the same program, you get that error. I don't understand why that second error happens. I don't think we're doing anything illegal by reusing the same SSL_CTX object for two different connections. I'm starting to feel that using the same SSL_CTX object for multiple connections is just too fragile. Perhaps we could share one SSL_CTX object for all the connections with no sslcert and no sslrootcert, but I'm not sure if even that is worth it. In quick testing, calling SSL_CTX_new() for each connection adds about 3% of overhead to establishing a new connection, with the default OpenSSL settings (seems to use ECDHE-RSA-AES256-GCM-SHA384 cipher here). I also tested memory usage with a program that opens 10000 connections, and it used about 15% more memory, when SSL_CTX_new() is called for each connection. I think that's acceptable. Barring objections, I'm going to write a patch to use a separate SSL context for every connection. - Heikki -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs