Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 37FCAB5DBD3 for ; Wed, 18 May 2011 04:50:25 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 82713-03 for ; Wed, 18 May 2011 07:50:17 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from atcmail.atc.tcs.com (atcmail.atc.tcs.co.in [203.200.212.145]) by mail.postgresql.org (Postfix) with ESMTP id 374F0B5D802 for ; Wed, 18 May 2011 04:50:15 -0300 (ADT) Received: from [172.25.149.79] (marc.atc.tcs.com [172.25.149.79]) by atcmail.atc.tcs.com (8.13.8/8.13.8) with ESMTP id p4I7oBcS004022 for ; Wed, 18 May 2011 13:20:12 +0530 Message-ID: <4DD379C5.1030703@atc.tcs.com> Date: Wed, 18 May 2011 13:18:21 +0530 From: =?windows-1252?Q?Marc-Andr=E9_Laverdi=E8re?= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Re: Support for cert auth in JDBC References: <4DD34FE5.3020609@atc.tcs.com> <4DD35BEA.3020902@postnewspapers.com.au> <4DD361CA.3070907@atc.tcs.com> <4DD36EFC.6070707@postnewspapers.com.au> In-Reply-To: <4DD36EFC.6070707@postnewspapers.com.au> Content-Type: multipart/mixed; boundary="------------090501030703020408070009" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201105/50 X-Sequence-Number: 21880 This is a multi-part message in MIME format. --------------090501030703020408070009 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit It is true that anyone who knows the right Java APIs can put that factory together with little pain. I just think that the average user shouldn't have to bother about it. Also, some use cases do not allow to use the default keystore and trust store for everything. Just for example, this is the setup in my application: * 1 Cert for some server component that speaks TLS * 1 Cert for the pgsql client component * 1 Cert for the JNDI client component (connects to LDAP) So we end up with 3 different keystores (and maybe trust stores too), all of which may have different passwords. It is actually not that much of a big deal to use, but the 'defaults' won't help us here too much I think. Using this kind of configurable setup allows much simpler deployments than having to change stuff in a central keystore (which may have other keys to NOT mess up with), and allows to test in different environments and very easily. Maybe our use case is overkill. Anyways, I'm attaching the code as I have it now. I tested the code from which this one comes from, but this incarnation isn't tested. I'll put more time on it once I hear back from the maintainers. :) I'd love to have some comments on this rough version also (CVS diff didn't do anything with this file, so there is no point to include that). P.S. Undocumented detail: we need to put the user's name in the connection settings anyway, as it won't work without it. Regards, Marc-André Laverdière Software Security Scientist Innovation Labs, Tata Consultancy Services Hyderabad, India On Wednesday 18 May 2011 12:32 PM, Craig Ringer wrote: > On 18/05/2011 2:06 PM, Marc-André Laverdière wrote: >> Hello, >> >> This implementations allows to specify which keystore and which >> truststore to load. This allows certificate authentication in the >> application easily. > > It's already pretty easy. You can: > > - Load your keys in the standard keystore; > - Populate a new JECKSf-format keystore with your cert and key and use > the javax.net.ssl.trustStore etc system properties on the cmdline or via > System.setProperty() to use that store instead of the default store; > - Provide your own javax.net.ssl.SSLSocketFactory that wraps the default > implementation with any additional behavior you need. > > PgJDBC could use some documentation/examples for the latter option, but > it shouldn't need any changes to the PgJDBC code. > >> The Java SSL API is not very well known to the JDBC driver developers >> and we would be interested in any interesting and generally useful >> extensions that you have implemented using this mechanism. Specifically >> it would be nice to be able to provide client certificates to be >> validated by the server. >> >> >> What I'm talking about is this factory referred to in the last paragraph. > > OK, so you've just written an easy-to-use canned SSLSocketFactory that > loads client certificates from an application-configurable > keystore/truststore? > > I did this in my app to allow it to use a PKCS#12 format X.509 > certificate directly and it was a pretty low-pain procedure. It'd be > nice to have more general purpose examples in the PgJDBC sources, > though. Maybe I should dig out the examples I posted to this mailing > list a while ago and submit them, too? > > I'm not one of the PgJDBC developers, but I'd be happy to have a look at > what you've done and see if I can be of any help, provide useful > feedback, etc. > > It'd probably be best if you published your work as a patch ("diff") > against PgJDBC CVS HEAD, posting the patch to this mailing list so > people can look at it and try it. Make sure the email you send the patch > in clearly explains the reason for any change to the existing PgJDBC > code, and if you do change the core PgJDBC code make sure it passes any > tests and that it compiles under the oldest supported JDK. > --------------090501030703020408070009 Content-Type: text/x-java; name="CertAuthFactory.java" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="CertAuthFactory.java" /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.io.FileInputStream; import java.io.IOException; import java.net.Socket; import java.net.InetAddress; import java.net.UnknownHostException; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.SecureRandom; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; /** * Provide an implementation of SSLSocketFactory that allows client authentication * @author Marc-André Laverdière (marc-andre@atc.tcs.com / marcandre.laverdiere@tcs.com) */ public class CertAuthFactory extends SSLSocketFactory { public final static String CONFIG_KEYSTORE_PATH = "org.postgresql.jdbc.keystore.path"; public final static String CONFIG_KEYSTORE_PWD = "org.postgresql.jdbc.keystore.password"; public final static String CONFIG_TRUSTSTORE_PATH = "org.postgresql.jdbc.truststore.path"; public final static String CONFIG_TRUSTSTORE_PWD = "org.postgresql.jdbc.truststore.password"; public final static String SSL_PROTOCOL_NAME = "SSL"; public final static String SECURE_RANDOM_NAME = "SHA1PRNG"; protected final SSLSocketFactory _internalFactory; public CertAuthFactory() throws IOException, GeneralSecurityException { SSLContext context = buildSSLContext(); _internalFactory = context.getSocketFactory(); } public CertAuthFactory(String ignored) throws IOException, GeneralSecurityException { SSLContext context = buildSSLContext(); _internalFactory = context.getSocketFactory(); } /** * Builds an SSLContext with the specified trust store and key store * */ protected static SSLContext buildSSLContext() throws IOException, GeneralSecurityException{ FileInputStream fInKeyStore = null; FileInputStream fInTrustStore = null; try{ //Load configuration String trustPath = System.getProperty(CONFIG_TRUSTSTORE_PATH); String trustPwd = System.getProperty(CONFIG_TRUSTSTORE_PWD); String keyPath = System.getProperty(CONFIG_KEYSTORE_PATH); String keyPwd = System.getProperty(CONFIG_KEYSTORE_PWD); KeyManagerFactory managerFactory = null; TrustManagerFactory trustFactory = null; if (keyPath != null && !"".equals(keyPath)){ //Load the Key Managers KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); fInKeyStore = new FileInputStream(keyPath); ks.load(fInKeyStore, keyPwd.toCharArray()); managerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); managerFactory.init(ks, keyPwd.toCharArray()); } if (trustPath != null && !"".equals(trustPath)){ // Load the trust store KeyStore trustKs = KeyStore.getInstance(KeyStore.getDefaultType()); fInTrustStore = new FileInputStream(keyPath); trustKs.load(fInTrustStore, trustPwd.toCharArray()); trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(trustKs); } //Create + Initialize TLS context SSLContext context = SSLContext.getInstance(SSL_PROTOCOL_NAME); //can be TLS too context.init(managerFactory.getKeyManagers(), trustFactory.getTrustManagers(), SecureRandom.getInstance(SECURE_RANDOM_NAME)); return context; } finally{ try{ if (fInKeyStore != null) fInKeyStore.close(); if (fInTrustStore != null) fInTrustStore.close(); } catch (IOException e){ //ignore it } } } /** * Enables the client mode and the client authentication * */ protected SSLSocket enableClientAuth(SSLSocket sock){ sock.setNeedClientAuth(true); sock.setUseClientMode(true); return sock; } @Override public Socket createSocket() throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(); return enableClientAuth(sock); } @Override public Socket createSocket(InetAddress address, int port, InetAddress localhost, int localPort) throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(address, port, localhost, localPort); return enableClientAuth(sock); } @Override public Socket createSocket(InetAddress host, int port) throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(host, port); return enableClientAuth(sock); } @Override public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(s, host, port, autoClose); return enableClientAuth(sock); } @Override public Socket createSocket(String host, int port) throws IOException, UnknownHostException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(host, port); return enableClientAuth(sock); } @Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(host, port, localAddress, localPort); return enableClientAuth(sock); } @Override public String[] getDefaultCipherSuites() { return _internalFactory.getDefaultCipherSuites(); } @Override public String[] getSupportedCipherSuites() { return _internalFactory.getSupportedCipherSuites(); } } --------------090501030703020408070009--