Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 67187B5D8C5 for ; Sat, 6 Aug 2011 12:30:49 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 32491-06 for ; Sat, 6 Aug 2011 15:30:43 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 6A270B5D800 for ; Sat, 6 Aug 2011 12:30:42 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Qpipk-0002Ii-RZ for pgsql-jdbc@postgresql.org; Sat, 06 Aug 2011 17:30:40 +0200 Received: from cpc10-with5-2-0-cust74.1-4.cable.virginmedia.com ([86.26.85.75]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 06 Aug 2011 17:30:40 +0200 Received: from bruno by cpc10-with5-2-0-cust74.1-4.cable.virginmedia.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 06 Aug 2011 17:30:40 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-jdbc@postgresql.org From: Bruno Harbulot Subject: Re: JDBC SSL hostname verification Date: Sat, 06 Aug 2011 16:30:27 +0100 Lines: 207 Message-ID: References: <4E3D2D40.3000405@ringerc.id.au> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------070308010600030606040808" X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: cpc10-with5-2-0-cust74.1-4.cable.virginmedia.com User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <4E3D2D40.3000405@ringerc.id.au> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.831 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.931 X-Spam-Level: X-Archive-Number: 201108/24 X-Sequence-Number: 22056 This is a multi-part message in MIME format. --------------070308010600030606040808 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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-5 --------------070308010600030606040808 Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0"; name="sslhostname.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="sslhostname.patch" ? 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()); > } --------------070308010600030606040808--