Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UHv0V-00043T-EY for pgsql-jdbc@arkaria.postgresql.org; Tue, 19 Mar 2013 11:47:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UHv0U-0006Ys-0v for pgsql-jdbc@arkaria.postgresql.org; Tue, 19 Mar 2013 11:47:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UHv0S-0006Ym-Vy for pgsql-jdbc@postgresql.org; Tue, 19 Mar 2013 11:47:05 +0000 Received: from outmail149058.authsmtp.co.uk ([62.13.149.58]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UHv0O-00036c-Vu for pgsql-jdbc@postgresql.org; Tue, 19 Mar 2013 11:47:04 +0000 Received: from mail-c233.authsmtp.com (mail-c233.authsmtp.com [62.13.128.233]) by punt14.authsmtp.com (8.14.2/8.14.2/) with ESMTP id r2JBkxVk044160; Tue, 19 Mar 2013 11:46:59 GMT Received: from ayaki.localdomain (124-169-167-240.dyn.iinet.net.au [124.169.167.240]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/) with ESMTP id r2JBkoZg091492 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Tue, 19 Mar 2013 11:46:55 GMT Message-ID: <51485028.80008@2ndquadrant.com> Date: Tue, 19 Mar 2013 19:46:48 +0800 From: Craig Ringer Organization: 2nd Quadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130219 Thunderbird/17.0.3 MIME-Version: 1.0 To: =?ISO-8859-1?Q?=22Gabriel_E=2E_S=E1nchez_Mart=EDnez=22?= CC: List Subject: Re: Client Certificate Authentication References: <514358B4.7030109@gmail.com> In-Reply-To: <514358B4.7030109@gmail.com> X-Enigmail-Version: 1.5.1 Content-Type: multipart/alternative; boundary="------------050109040305090407020303" X-Server-Quench: b3cc9de8-908a-11e2-a49c-0025907707a1 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZeVg0b BQteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksadBtFaQxbYFhD HGQLWldEUVx7XWV/ bg0fbAdZfEtIVhtr Vk9XQFZSCwdtAxke B1BcUGMALFosL355 ZEJrXD5aWUJ+cAgv QR0HHDlTbXppaGhO TUFZIgFJIQdDLQJF alJ6XXMKZmUAN3su JQJhdwMTER90HWx+ RgAAK05aaksRAj8y RhsLAThnNFAZRyg1 Mx0tI0IdG0B5 X-Authentic-SMTP: 61633235383639.1021:706 X-AuthFastPath: 0 (Was 255) X-AuthSMTP-Origin: 124.169.167.240/2525 X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org This is a multi-part message in MIME format. --------------050109040305090407020303 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by punt14.authsmtp.com id r2JBkxVk044160 On 03/16/2013 01:21 AM, "Gabriel E. S=E1nchez Mart=EDnez" wrote: > I have a PostgreSQL database on a server. For security reasons, all > client access from outside the server is restricted to certificate > mode, i.e. the client sends SSL certificates rather than a password.=20 > Connecting with pgAdmin works well. Is there a way of creating a Java > application that connects to this database with certificates? Any > how-to guides, examples, or pointers on how to achieve this? In most cases you can set the JSEE keystore and truststore properties to get Java to use an existing keystore and truststore with your private key and the server's public certificate installed. In particular, see: http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSER= efGuide.html#CustomizingStores The keystore must be in JECKS format. You can create it with keytool. Import the user's client certificate into it; you'll most likely have a pkcs#12 format cert (.p12 file) but you might instead have separate PEM format key and certs. The truststore is optional and may be in JKS format if provided. If you don't provide a truststore the system wide SSL root certificate trust store for Java is used. That's fine unless your server is using a self-signed cert for the server, in which case you'll want to either add your private CA cert to the system truststore or specify your own truststore as a jvm system property. If you cannot use the system properties for whatever reason then you need to provide your own SSLSocketFactory that uses the JSSE APIs to load a keystore and truststore. --=20 Craig Ringer http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------050109040305090407020303 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 03/16/2013 01:21 AM, "Gabriel E. Sánchez Martínez" wrote:
I have a PostgreSQL database on a server.  For security reasons, all client access from outside the server is restricted to certificate mode, i.e. the client sends SSL certificates rather than a password.  Connecting with pgAdmin works well.  Is there a way of creating a Java application that connects to this database with certificates?  Any how-to guides, examples, or pointers on how to achieve this?
In most cases you can set the JSEE keystore and truststore properties to get Java to use an existing keystore and truststore with your private key and the server's public certificate installed. In particular, see:

http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#CustomizingStores

The keystore must be in JECKS format. You can create it with keytool. Import the user's client certificate into it; you'll most likely have a pkcs#12 format cert (.p12 file) but you might instead have separate PEM format key and certs.

The truststore is optional and may be in JKS format if provided. If you don't provide a truststore the system wide SSL root certificate trust store for Java is used. That's fine unless your server is using a self-signed cert for the server, in which case you'll want to either add your private CA cert to the system truststore or specify your own truststore as a jvm system property.

If you cannot use the system properties for whatever reason then you need to provide your own SSLSocketFactory that uses the JSSE APIs to load a keystore and truststore.
-- 
 Craig Ringer                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services
--------------050109040305090407020303--