Skip to content

Only change ssl_renegotiation setting if the connection is on SSL. - #453

Closed
franciscojunior wants to merge 2 commits into
npgsql:masterfrom
franciscojunior:change-ssl-renegotiation-only-on-ssl
Closed

franciscojunior wants to merge 2 commits into
npgsql:masterfrom
franciscojunior:change-ssl-renegotiation-only-on-ssl

Conversation

@franciscojunior

Copy link
Copy Markdown
Member

Before that Npgsql was sending the ssl renegotiation statement regardless if
the connection was secured or not.

@Emill

Emill commented Jan 9, 2015

Copy link
Copy Markdown
Contributor

Maybe we should also poke the mono people to fix this bug about ssl renegotiation?

@franciscojunior

Copy link
Copy Markdown
Member Author

Yes. I think so.
In the past we tried to figure out where the problem was with no luck. You can check the bug report here: https://bugzilla.novell.com/show_bug.cgi?id=321325

Albe Laurenz told me in the past about this configuration setting added by the postgresql guys to handle problems with broken ssl libraries and client which couldn't handle ssl renegotiations and so we added this configuration to Npgsql. I talked about that here: http://fxjr.blogspot.com.br/2010/03/ssl-renegotiation-patch.html

Another improvement to this PR which we can make is to only send this setting if we are in a secure connection AND running under mono. SSLStream on windows (ms.net) doesn't have this problem. When I was working with the sslstream, I made this test to confirm it.

@Emill

Emill commented Jan 9, 2015

Copy link
Copy Markdown
Contributor

Nothing has happened since 2011 :/

@roji

roji commented Jan 9, 2015

Copy link
Copy Markdown
Member

Looks good to me, but one thought... I think we mentioned dropping mono.security entirely for v3.0. I'm not sure if there are postgresql SSL settings that aren't supported by the Microsoft .NET implementation, if not we can just kill it?

@franciscojunior

Copy link
Copy Markdown
Member Author

Yes, we did. I think we can remove it with no problem.

The issue is that when running under mono on osx and gnu/linux,
mono.security is used to implement the sslstream class. I also confirmed
that by using sslstream on osx and gnu/Linux :-)

@franciscojunior

Copy link
Copy Markdown
Member Author

Yes :-(

After the existence of the postgresql setting , I decided to simply use it
and check from time to time if a fix exists for this problem.
Em 09/01/2015 18:51, "Emill" [email protected] escreveu:

Nothing has happened since 2011 :/

2015-01-09 21:24 GMT+01:00 Francisco Figueiredo Jr. <
[email protected]>:

Yes. I think so.
In the past we tried to figure out where the problem was with no luck.
You
can check the bug report here:
https://bugzilla.novell.com/show_bug.cgi?id=321325

Albe Laurenz told me in the past about this configuration setting added
by
the postgresql guys to handle problems with broken ssl libraries and
client
which couldn't handle ssl renegotiations and so we added this
configuration
to Npgsql. I talked about that here:
http://fxjr.blogspot.com.br/2010/03/ssl-renegotiation-patch.html

Another improvement to this PR which we can make is to only send this
setting if we are in a secure connection AND running under mono.
SSLStream
on windows (ms.net) doesn't have this problem. When I was working with
the sslstream, I made this test to confirm it.

—
Reply to this email directly or view it on GitHub
#453 (comment).

—
Reply to this email directly or view it on GitHub
#453 (comment).

@roji

roji commented Jan 11, 2015

Copy link
Copy Markdown
Member

Ahh, I understand.

So it seem that what we need is to drop the dependency on Mono.Security, but include a runtime check whether we're on mono, and if so, send the ssl_renegotiation=0 setting... This way when running on Windows we do ssl_renegotiation.

Does that sound like a good way to go?

@Emill

Emill commented Jan 11, 2015

Copy link
Copy Markdown
Contributor

If running mono, we should probably use Mono's own SslClientStream and not
System.Net.Security.SslStream.

Check out
https://github.com/mono/mono/blob/effa4c07ba850bedbe1ff54b2a5df281c058ebcb/mcs/class/System/System.Net.Security/SslStream.cs#L569,
they are for some very strange reason using sync-over-async, although the
underlying SslStreamBase has support for sync reads/writes...

2015-01-11 17:34 GMT+01:00 Shay Rojansky [email protected]:

Ahh, I understand.

So it seem that what we need is to drop the dependency on Mono.Security,
but include a runtime check whether we're on mono, and if so, send the
ssl_renegotiation=0 setting... This way when running on Windows we do
ssl_renegotiation.

Does that sound like a good way to go?

—
Reply to this email directly or view it on GitHub
#453 (comment).

@franciscojunior

Copy link
Copy Markdown
Member Author

Ahh, I understand.

So it seem that what we need is to drop the dependency on Mono.Security, but include a runtime check whether we're on mono, and if so, send the ssl_renegotiation=0 setting... This way when running on Windows we do ssl_renegotiation.

Does that sound like a good way to go?

Yes. That's the idea. +1

@franciscojunior

Copy link
Copy Markdown
Member Author

hey are for some very strange reason using sync-over-async, although the underlying SslStreamBase has support for sync reads/writes...

Hmmmm, my bet is that they changed everything to use the Begin/End idiom and used this to implement the sync operations. I think this was the way to go when the Begin/End idiom appeared.

@franciscojunior
franciscojunior force-pushed the change-ssl-renegotiation-only-on-ssl branch from b892f8a to 0e71556 Compare January 11, 2015 19:30
@Emill

Emill commented Jan 11, 2015

Copy link
Copy Markdown
Contributor

Probably, but it turned out to be a bad idea. HttpWebRequest, used
sync-over-async when retrieving a web site, but due to the deadlock problem
we talked about before, they had to re-implement a separate sync version
from the async version. (See
http://blogs.msdn.com/b/pfxteam/archive/2012/04/13/10293638.aspx).

2015-01-11 20:20 GMT+01:00 Francisco Figueiredo Jr. <
[email protected]>:

hey are for some very strange reason using sync-over-async, although the
underlying SslStreamBase has support for sync reads/writes...

Hmmmm, my bet is that they changed everything to use the Begin/End idiom
and used this to implement the sync operations. I think this was the way to
go when the Begin/End idiom appeared.

—
Reply to this email directly or view it on GitHub
#453 (comment).

@franciscojunior

Copy link
Copy Markdown
Member Author

Probably, but it turned out to be a bad idea. HttpWebRequest, used sync-over-async when retrieving a web site, but due to the deadlock problem we talked about before, they had to re-implement a separate sync version from the async version. (See http://blogs.msdn.com/b/pfxteam/archive/2012/04/13/10293638.aspx).

Yes. I remember this discussion about threadpool and deadlocks.

Well, I think removing the dependency on Mono.Security would be a good thing as it would be one less dependency we would need to carry.

On the other side, your observation is totally valid and this mean a possible deadlock problem for people using ssl connections under mono. We could document about that and they would be aware of it. We could give a heads up to Mono guys talking to them about that.
As ms.net is being opensource little by little, maybe sometime sslstream implementation will be available and mono can use it.

Another approach would be to use reflection to get SslClientStream instance and use it instead. We would document that and explain users how to use it if they find problems using sslstream.

What do you think?

@Emill

Emill commented Jan 11, 2015

Copy link
Copy Markdown
Contributor

Another approach would be to use reflection to get SslClientStream instance and use it instead.

Seems like a good idea. Just check if current environment is mono, and if it is, use SslClientStream.

@franciscojunior

Copy link
Copy Markdown
Member Author

Seems like a good idea. Just check if current environment is mono, and if it is, use SslClientStream.

Whooops, that's true. :)

I was still thinking about the situation where we would need to use it when running under ms.net :)

@franciscojunior

Copy link
Copy Markdown
Member Author

Whooops, that's true. :)

I was still thinking about the situation where we would need to use it when running under ms.net :)

I'll work on this and will push commits when done.

@Emill

Emill commented Jan 11, 2015

Copy link
Copy Markdown
Contributor

When do we need to use it when running under ms.net?

2015-01-11 21:34 GMT+01:00 Francisco Figueiredo Jr. <
[email protected]>:

Whooops, that's true. :)

I was still thinking about the situation where we would need to use it
when running under ms.net :)

I'll work on this and will push commits when done.

—
Reply to this email directly or view it on GitHub
#453 (comment).

@franciscojunior

Copy link
Copy Markdown
Member Author

Nevermind. :-)
I was the one confusing things .
The idea would be like you said plus using the reflection idea. Checking if
running under mono and if so, get sslclientstream with reflection and using
it.
Em 11/01/2015 18:35, "Emill" [email protected] escreveu:

When do we need to use it when running under ms.net?

2015-01-11 21:34 GMT+01:00 Francisco Figueiredo Jr. <
[email protected]>:

Whooops, that's true. :)

I was still thinking about the situation where we would need to use it
when running under ms.net :)

I'll work on this and will push commits when done.

—
Reply to this email directly or view it on GitHub
#453 (comment).

—
Reply to this email directly or view it on GitHub
#453 (comment).

@Emill

Emill commented Jan 12, 2015

Copy link
Copy Markdown
Contributor

Just a question, is it possible to set this renegotiation setting as a
startup parameter instead?

2015-01-11 21:44 GMT+01:00 Francisco Figueiredo Jr. <
[email protected]>:

Nevermind. :-)
I was the one confusing things .
The idea would be like you said plus using the reflection idea. Checking
if
running under mono and if so, get sslclientstream with reflection and
using
it.
Em 11/01/2015 18:35, "Emill" [email protected] escreveu:

When do we need to use it when running under ms.net?

2015-01-11 21:34 GMT+01:00 Francisco Figueiredo Jr. <
[email protected]>:

Whooops, that's true. :)

I was still thinking about the situation where we would need to use it
when running under ms.net :)

I'll work on this and will push commits when done.

—
Reply to this email directly or view it on GitHub
#453 (comment).

—
Reply to this email directly or view it on GitHub
#453 (comment).

—
Reply to this email directly or view it on GitHub
#453 (comment).

@franciscojunior

Copy link
Copy Markdown
Member Author

Just a question, is it possible to set this renegotiation setting as a startup parameter instead?

That was the initial idea. We even have some parameters which are passed like that. Unfortunately, at the time we were using ssl_renegotiation, it isn't supported as startup parameter.

@Emill

Emill commented Jan 18, 2015

Copy link
Copy Markdown
Contributor

I found another thing disappointing with Mono's ssl stream: http://forums.xamarin.com/discussion/9098/weak-ciphers-with-ssl-tls

It does not provide any cipher suite with Perfect Forward Secrecy. That means if an attacker has recorded a communication and some time later happen to get a copy of the server's private key, he can decrypt the recorded communication.

They also do not support TLS 1.1 or TLS 1.2; only TLS 1.0 which is 16 years old and have some security flaws...

Maybe we should just drop Mono's ssl stream and find/use a better one?

However, support for TLS 1.2 might be on its way: https://trello.com/c/PvUaV89u/16-tls-stack

One problem with Microsoft's SslStream though is that it does not provide a "DataAvailable" property, which means we can't get (a)synchronous notifications to work reliably.

@roji

roji commented Jan 19, 2015

Copy link
Copy Markdown
Member

I'm sorry I have too much going on to participate seriously on this important issue...

One thought though: it would be good to get a clear list of exactly what the PostgreSQL backend supports - which cyphers etc. This way we can compare against what mono's SSL implementation provides and see exactly if/what is missing...

@Emill

Emill commented Jan 19, 2015

Copy link
Copy Markdown
Contributor

It uses OpenSSL, so it supports almost everything. The problem is however that the client should support secure cipher suites, so even if it only supported the best and nothing else, that would be enough (maybe a "backup" that is also secure in case the first one gets cracked, then if the server is the first to be upgraded, there is no harm).

@roji

roji commented Jan 21, 2015

Copy link
Copy Markdown
Member

One more thought about this...

In a way, the fact that mono's SslStream implementation doesn't have the most secure ciphers it could isn't really our problem with Npgsql. I mean that users who are unhappy with the cipher being used to communicate with Npgsql should really address (or contribute to) mono itself, which implements SSL - we're just using a standard .NET class/service here.

Of course, I understand this can be viewed another way - that we want to provide the best service for Npgsql users and if mono doesn't do so we should find a workaround - but I'm not sure in this case it's worth the trouble of an additional dependency, etc. After all, SSL is implemented, even if it isn't the best it could be...

All this is regardless of the DataAvailable problem, which we indeed have to study/work around for async notifications if we keep SslStream... Mentioning this on #270 to remember.

Before that Npgsql was sending the ssl renegotiation statement regardless if
the connection was secured or not.
@franciscojunior
franciscojunior force-pushed the change-ssl-renegotiation-only-on-ssl branch from 0e71556 to 42faed0 Compare February 19, 2015 19:08
@roji
roji force-pushed the master branch 2 times, most recently from 1236d1f to e8ae2e0 Compare April 12, 2015 07:50
@roji
roji force-pushed the master branch 5 times, most recently from 2fc8944 to 3eb6fa4 Compare April 29, 2015 14:47
@roji

roji commented Aug 9, 2015

Copy link
Copy Markdown
Member

In 3.0 ssl_renegotiation_limit is only sent when SSL is enabled. Note that the whole feature has been deprecated in PostgreSQL (and removed in version 9.5).

@roji roji closed this Aug 9, 2015
@Emill

Emill commented Aug 12, 2015

Copy link
Copy Markdown
Contributor

Will this work even for redshift?

@roji

roji commented Aug 12, 2015

Copy link
Copy Markdown
Member

Yeah, for Redshift you need to specify "Server Compatibility Mode=Redshift" in the connection string, this disables sending of ssl_renegotiation_limit. This wasn't documented so I updated the compatibility page.

@Emill

Emill commented Aug 12, 2015

Copy link
Copy Markdown
Contributor

But doesn't that mean renegotiation still occurs, and the callbacks must remain?

@roji

roji commented Aug 12, 2015

Copy link
Copy Markdown
Member

Ah shit you're probably right :( I missed this...

I'll try to find out what the exact status of Redshift's SSL renegotiation is. If we're very lucky maybe they don't do it or something... Otherwise I'll have to revert f021c11...

@roji

roji commented Aug 13, 2015

Copy link
Copy Markdown
Member

@Emill, take a look at the gitter, I set up a test to checking SSL renegotiation but I can't get it to work

@roji

roji commented Aug 17, 2015

Copy link
Copy Markdown
Member

@Emill, I wanted to play around with SSL renegotiation a little bit (for #453, Redshift)

So I set up a branch with ssl_renegotiation_limit enabled and set to 1MB, along with a test that sends enough data to trigger it

When I tried it locally (before switching to Redshift) it seems to bomb - with both your TlsClientStream and .NET SSLStream. I'm sure I'm doing something wrong, but can you please take a look? The branch is https://github.com/roji/npgsql/tree/ssl-renegotiation.

(the idea is that maybe if we test this against Redshift, we find out that they've ripped out SSL renegotiation anyway)

@Emill

Emill commented Aug 17, 2015

Copy link
Copy Markdown
Contributor

I've fixed a bug in TlsClientStream that occurred if "directBuf" was larger than 16 kb (which is the maximum allowed size of a TLS record). I also added async to TlsClientStream.

I've tested your branch against PostgreSQL 9.4.1 with a Linux backend and it works well with TlsClientStream but times out during renegotiation with SslStream. What behaviour did you get?

I also tested against a Windows build of PostgreSQL 9.3 which doesn't work that well. The test (both TlsClientStream and SslStream) works each other time and fails each other time. When you send large amounts of data the backend requests renegotiation, but since the frontend doesn't reply to that in a short amount of time (or because it keeps sending data without renegotiation), the backend terminates the connection. Of course this is hard to fix and would require another thread or something that reads data at the same time a send happens.

I think the server behaviour depends both on PostgreSQL version and OpenSSL version.

@roji

roji commented Aug 18, 2015

Copy link
Copy Markdown
Member

Ah, OK - I was testing on a Windows build of PostgreSQL 9.4.1. I will try to look at this again on Linux.

My idea was to confirm that renegotiation actually happens on Redshift, by running this test on it. Who knows, maybe it simply doesn't happen and then we can just forget about it forever. If you have some time you can also test against Redshift.

Even if Redshift require us to keep renegotiation, don't forget that we never need to support renegotiation for a Windows server (I assume it's not Windows).

@Emill

Emill commented Aug 18, 2015

Copy link
Copy Markdown
Contributor

You can create an aws account and test redshift for free for two months I think. My free time is up however...

@roji

roji commented Aug 18, 2015

Copy link
Copy Markdown
Member

Yeah, mine too but I'll create a new AWS account. Will let you know about the results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants