Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brVnL-0003vv-1p for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Oct 2016 19:54:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1brVnK-0006dR-4T for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Oct 2016 19:54:30 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1brVmf-0005ux-Py for pgsql-bugs@postgresql.org; Tue, 04 Oct 2016 19:53:49 +0000 Received: from d.mail.sonic.net ([64.142.111.50]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1brVmd-0005QA-9t for pgsql-bugs@postgresql.org; Tue, 04 Oct 2016 19:53:48 +0000 Received: from engels (162-236-13-58.lightspeed.sntcca.sbcglobal.net [162.236.13.58]) (authenticated bits=0) by d.mail.sonic.net (8.15.1/8.15.1) with ESMTPSA id u94JrRNr014901 (version=TLSv1.2 cipher=DHE-RSA-AES128-SHA256 bits=128 verify=NOT); Tue, 4 Oct 2016 12:53:27 -0700 Date: Tue, 4 Oct 2016 12:53:28 -0700 From: David Gould To: Heikki Linnakangas Cc: "Zuk, Kacper" , "pgsql-bugs@postgresql.org" Subject: Re: BUG #14329: libpq doesn't send complete client certificate chain on first SSL connection Message-ID: <20161004125328.0a2311c2@engels> In-Reply-To: References: <20160920101051.1355.79453@wrigleys.postgresql.org> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.23; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Sonic-CAuth: UmFuZG9tSVbKb7iGP/7sAGdEy/uD3VQ+1Y9eByYiVHnHpBQv23E40ZOidC6CTBRobZWbAc6WPgPCntx0Wj+uRRPfPf1Ka6ey X-Sonic-ID: C;xPxpN2yK5hGBqMEYTueg2w== M;8NGkN2yK5hGBqMEYTueg2w== X-Spam-Flag: No X-Sonic-Spam-Details: 0.0/5.0 by cerberusd X-Pg-Spam-Score: -5.2 (-----) 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 Tue, 4 Oct 2016 21:55:52 +0300 Heikki Linnakangas wrote: > 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(); > 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. What about keeping a table of connection strings and SSL contexts so that all connections using the same connection string could share the SSL context? It seems likely that most applications reuse the same connection string and could avoid the penalty. -dg -- David Gould 510 282 0869 daveg@sonic.net If simplicity worked, the world would be overrun with insects. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs