Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 05D05632514 for ; Thu, 27 May 2010 00:44:33 -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 23171-01 for ; Thu, 27 May 2010 03:44:24 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [202.4.227.11]) by mail.postgresql.org (Postfix) with ESMTP id 22F47632266 for ; Thu, 27 May 2010 00:44:24 -0300 (ADT) Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by www.postnewspapers.com.au (Postfix) with ESMTP id 0D8DE9F64 for ; Thu, 27 May 2010 11:44:22 +0800 (WST) Received: from localhost (localhost [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 0D077AE6B5 for ; Thu, 27 May 2010 11:43:11 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (mail.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id l7cUavJf+6IB for ; Thu, 27 May 2010 11:43:05 +0800 (WST) Received: from [10.1.1.8] (124-168-255-216.dyn.iinet.net.au [124.168.255.216]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 32A5EAE232 for ; Thu, 27 May 2010 11:43:05 +0800 (WST) Message-ID: <4BFDEA8F.6040707@postnewspapers.com.au> Date: Thu, 27 May 2010 11:44:15 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100423 Lightning/1.0b1 Thunderbird/3.0.4 MIME-Version: 1.0 To: PG-JDBC Mailing List Subject: Re: New code: Easy PKCS#12 client certificate support for pgjdbc References: <4BEA9BC4.4040607@postnewspapers.com.au> In-Reply-To: <4BEA9BC4.4040607@postnewspapers.com.au> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.79 tagged_above=-10 required=5 tests=BAYES_50=0.8, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201005/72 X-Sequence-Number: 20556 Just a follow-up on this: The code I posted works, but is the wrong approach to take. I've removed it and replaced it with some suitable documentation. Given how much trouble I had figuring out the right way to do this, I bet I'm not alone, so I thought I'd write it up. In summary: Applications should use the core SunPKIX trust and certificate managers and the default SSLSocketFactory if at all possible, because they're flexible enough to handle pluggable authentication methods like hardware tokens with no app code changes. If you override the SSLSocketFactory and provide your own trust and key managers you lose a lot of the power of the system. If, like mine, your app needs to offer a user a way to configure additional keys and trusted certs, just create your own JKS or JECKS KeyStores for the trust- and key- stores during startup (if they don't already exist), and set the javax.net.ssl. properties to point to them. The default Sun providers will then load those app-specific stores instead of the global cert- and key- stores. If the user attempts a connection to a service that requires a client certificate, or a service whose certificate isn't trusted, you can catch the resulting exception, examine it to determine the root cause, and prompt the user to supply a client certificate or to install a trusted root cert as appropriate. Apache Commons Lang is useful here for inspection of deep exception chains. If you'd prefer to load a user PKCS#12 file directly for the keystore, just specify it in your app's javax.net.ssl. system properties via System.setProperty(...) before any SSL code is invoked, and the default X509KeyStore will be happy to use it. Make sure to set javax.net.ssl.keyStoreType to "pkcs12". If you really do need to replace the X509TrustManager for some reason, use the certificate verification built-in to the JVM rather than re-implementing it. Create a Set from your list of trusted X509Certificate instances from your trust store, and use it with CertPathValidator to validate a CertPath created from the X509Certificate[] provided to the TrustManager. This does proper PKIX validation a few lines of code. Even better is to load the default "SunPKIX" X509TrustManager and X509KeyManager and put them before your own in the appropriate array when creating the SSL context in your SSLSocketFactory. The SSL context will try them in order. That way the user can still use all the pluggable features, but you can (eg) fall back to your X509TrustManager if the cert isn't trusted so you can display a "add to trust store?" prompt. About the only downside with this that I've found is that there doesn't appear to be any way to force the SunPKIX code to re-load a KeyStore (the trust store or the key store) after SSL has been inited, so if the user adds a trusted cert or key they seem to need to re-start the app. You can get around it by using non-public com.sun api, but that's never a good idea. If you're having problems in this area, you should read the JSSE reference guide, as it'll help you understand how it all works: http://java.sun.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html -- Craig Ringer