From: Bruno Harbulot <bruno@distributedmatter.net>
To: pgsql-jdbc@postgresql.org
Subject: Re: JDBC SSL hostname verification
Date: Sat, 06 Aug 2011 16:30:27 +0100
Message-ID: <j1jmmg$f06$1@dough.gmane.org> (raw)
In-Reply-To: <4E3D2D40.3000405@ringerc.id.au>
References: <j1iaoo$uu$1@dough.gmane.org>
<4E3D2D40.3000405@ringerc.id.au>
On 06/08/2011 13:02, Craig Ringer wrote:
> On 6/08/2011 11:00 AM, Bruno Harbulot wrote:
>
> JSSE doesn't verify the hostname automatically. Quoting the JSSE
> reference guide for Java 6:
>
> "When using raw SSLSockets/SSLEngines you should always check the peer's
> credentials before sending any data. The SSLSocket and SSLEngine classes
> do not automatically verify that the hostname in a URL matches the
> hostname in the peer's credentials. An application could be exploited
> with URL spoofing if the hostname is not verified."
>
> I was under the impression that PgJDBC verified the hostname its self
> unless verification was disabled, but it seems not. Hmm. I guess you can
> use a custom SSLSocketFactory to do the verification, but it really
> should be something done by the stock JDBC driver. Patch?
Yes, you're absolutely right, there is no verification by default. In
fact, until the relatively recent RFC 6125 [1], the way host names
should be verified was specified differently in each protocol (e.g. RFC
2818 for HTTPS, see RFC 6125 appendix B).
This patch (attached), modifies org.postgresql.ssl.MakeSSL and changes this:
> Socket newConnection = factory.createSocket(stream.getSocket(), stream.getHost(), stream.getPort(), true);
> stream.changeSocket(newConnection);
Into this:
> SSLSocket newConnection = (SSLSocket)factory.createSocket(stream.getSocket(), stream.getHost(), stream.getPort(), true);
> // Gets the session and do the verification...
> stream.changeSocket(newConnection);
In principle, this verification could indeed be done within the custom
SSLSocketFactory.createSocket(...) method. However, this might need to
be an SSLSocketFactory intended to be used only with this JDBC driver as
it would break the general API of SSLSocketFactory.
This is because the factory would return an SSLSocket for which the TLS
handshake has already taken place (since it needs to get the peer
certificate in order to verify them). From the SSLSocketFactory API
point of view, the factory creates SSLSockets, but doesn't initiate the
handshake. The handshake is only initiated (perhaps implicitly)
afterwards, in one of the 3 conditions documented in SSLSocket [2].
A number of settings can still be made before the handshake, and it's
normally not up to the SSLSocketFactory to make them.
I've tried to follow the rules in RFC 6125 to perform the verification
(although I haven't looked at what it says about certificates for IP
addresses, only host names).
Firstly, it looks for Subject Alternative Names and then for CN entries
in the Subject Distinguished Names (Subject DN). (It is preferrable to
use subjectAltNames rather than relying on CNs in the Subject DN, see
RFC 6125 section 1.5.)
If it finds a subjectAltName entry of type DNS that matches the
requested host name, it stops and accepts the certificate.
Otherwise, it looks within the Subject DN for and RDN with only one AVA
such as CN=the.host.name (section 2.3.1 of RFC 6125).
(I must admit I think the CN verification could be done more elegantly;
in addition, I haven't looked in details into the subtleties of
character encoding.)
I understand this may not be obvious if one is not used to certificates.
The Subject DN in the certificate is a sequence of relative
distinguished names (RDNs). Each RDN is an unordered set of Attribute
Value Assertions (AVAs), for example "CN=the.host.name".
Using the RFC 2253 string representation of a DN, RDNs are separated
with ',' and AVAs are separated with '+'. (OpenSSL tends to use a
different string representation, separated with "/" and sometimes in a
different order.) They can also be escaped with '\' (which means it's
not necessarily easy to implement with a StringTokenizer for example).
There are examples in RFC 2253, section 5 [3].
I guess one of the things that is missing is a property to turn this off.
From a security perspective, my opinion tends to be to have this sort
of feature turned on by default, but I guess it may break some existing
configurations so some users may want to turn it off. (I would argue
that the motivation for using SSL/TLS is to secure the connection, so
making users aware that it's not secure because SSL/TLS wasn't fully
configured isn't necessarily a bad thing, but I also understand that
raising awareness by potentially breaking existing working
configurations can be a harsh way to do so.)
Anyway, the patch is attached, it's probably not perfect, but I'd be
happy to discuss it further if this feature is of interest to the
community. I hope this helps.
Best wishes,
Bruno.
[1] http://tools.ietf.org/html/rfc6125
[2]
http://download.oracle.com/javase/6/docs/api/javax/net/ssl/SSLSocket.html
[3] http://tools.ietf.org/html/rfc2253#section-5Attachments:sslhostname.patch (text/plain, 4.4K) download
? sslhostname.patch
Index: org/postgresql/ssl/MakeSSL.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/ssl/MakeSSL.java,v
retrieving revision 1.7
diff -r1.7 MakeSSL.java
11a12,14
> import java.util.Collection;
> import java.util.Iterator;
> import java.util.List;
15a19,23
> import java.security.cert.CertificateParsingException;
> import java.security.cert.X509Certificate;
>
> import javax.net.ssl.SSLPeerUnverifiedException;
> import javax.net.ssl.SSLSocket;
16a25
> import javax.security.auth.x500.X500Principal;
63c72,141
< Socket newConnection = factory.createSocket(stream.getSocket(), stream.getHost(), stream.getPort(), true);
---
> SSLSocket newConnection = (SSLSocket)factory.createSocket(stream.getSocket(), stream.getHost(), stream.getPort(), true);
> X509Certificate[] peerCerts = (X509Certificate[])newConnection.getSession().getPeerCertificates();
> if (peerCerts == null || peerCerts.length == 0) {
> throw new SSLPeerUnverifiedException("Could not find peer certificates!");
> }
> X509Certificate serverCert = peerCerts[0];
> try {
> boolean foundHostName = false;
> // Look for host name in the certificate subjectAltName (DNS entries)
> Collection subjectAltNames = serverCert.getSubjectAlternativeNames();
> Iterator subjectAltNameIt = subjectAltNames.iterator();
> while (subjectAltNameIt.hasNext()) {
> List subjectAltNameEntry = (List)subjectAltNameIt.next();
> Integer subjectAltNameEntryType = (Integer)subjectAltNameEntry.get(0);
> if (!subjectAltNameEntryType.equals(Integer.valueOf(2))) {
> continue;
> }
> String subjectAltNameEntryValue = (String)subjectAltNameEntry.get(1);
> if (stream.getHost().equalsIgnoreCase(subjectAltNameEntryValue)) {
> foundHostName = true;
> break;
> }
> }
> if (!foundHostName) {
> // If not found in subjectAltNames, look for CN in Subject DN
> String subjectDN = serverCert.getSubjectX500Principal().getName(X500Principal.RFC2253);
> // RDNs are separated with ','. AVAs within each RDN are separated with '+'
> String avaName = null;
> String avaValue = null;
> int avaStart = 0;
> for (int i = 0; i < subjectDN.length(); i++) {
> char currentChar = subjectDN.charAt(i);
> if (currentChar == '\\') {
> continue;
> }
> if (avaName == null) {
> if (currentChar == '=') {
> avaName = subjectDN.substring(avaStart, i);
> avaStart = i + 1;
> }
> } else {
> if (currentChar == '+') {
> // This is an RDN with multiple AVAs, discarded (RFC 6125 2.3.1)
> avaName = "DISCARDED";
> }
> if (currentChar == ',') {
> avaValue = subjectDN.substring(avaStart, i);
> avaStart = i + 1;
> if ("CN".equals(avaName) && stream.getHost().equalsIgnoreCase(avaValue)) {
> foundHostName = true;
> avaName = null;
> break;
> }
> avaName = null;
> }
> }
> if (avaName != null) {
> avaValue = subjectDN.substring(avaStart);
> if ("CN".equals(avaName) && stream.getHost().equalsIgnoreCase(avaValue)) {
> foundHostName = true;
> }
> }
> }
> }
> if (!foundHostName) {
> throw new SSLPeerUnverifiedException("Could not find requested host name ("+stream.getHost()+") in the certificate.");
> }
> } catch (CertificateParsingException e) {
> throw new SSLPeerUnverifiedException(e.getMessage());
> }
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-jdbc@postgresql.org
Cc: bruno@distributedmatter.net
Subject: Re: JDBC SSL hostname verification
In-Reply-To: <j1jmmg$f06$1@dough.gmane.org>
References: <j1iaoo$uu$1@dough.gmane.org> <4E3D2D40.3000405@ringerc.id.au> <j1jmmg$f06$1@dough.gmane.org>
* 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