pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Zuk, Kacper <kzuk@akamai.com>
To: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14329: libpq doesn't send complete client certificate chain on first SSL connection
Date: Tue, 4 Oct 2016 21:55:52 +0300
Message-ID: <e9315ff0-1252-c4f5-5669-acbcfcabd059@iki.fi> (raw)
In-Reply-To: <F44EA122-A55B-41C1-B85C-826CFEE5D28B@akamai.com>
References: <20160920101051.1355.79453@wrigleys.postgresql.org>
	<c2315357-00b1-5141-c06c-8fca91736027@iki.fi>
	<F44EA122-A55B-41C1-B85C-826CFEE5D28B@akamai.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On 10/04/2016 02:08 PM, Zuk, Kacper wrote:
> On 9/21/16, 10:06 AM, "Heikki Linnakangas" <hlinnaka@gmail.com on behalf of hlinnaka@iki.fi> 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(<connection string 1>);
PQfinish();
PQconnectdb(<connection string 2>);
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



view thread (13+ messages)  latest in thread

Message-ID: <e9315ff0-1252-c4f5-5669-acbcfcabd059@iki.fi>
Permalink:  ../e9315ff0-1252-c4f5-5669-acbcfcabd059@iki.fi/
Also on:    postgresql.org/message-id/e9315ff0-1252-c4f5-5669-acbcfcabd059@iki.fi

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: hlinnaka@iki.fi, kzuk@akamai.com
  Subject: Re: BUG #14329: libpq doesn't send complete client certificate chain on first SSL connection
  In-Reply-To: <e9315ff0-1252-c4f5-5669-acbcfcabd059@iki.fi>
  References: <20160920101051.1355.79453@wrigleys.postgresql.org> <c2315357-00b1-5141-c06c-8fca91736027@iki.fi> <F44EA122-A55B-41C1-B85C-826CFEE5D28B@akamai.com> <e9315ff0-1252-c4f5-5669-acbcfcabd059@iki.fi>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox