Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gwwlh-0001pq-Jf for pgsql-general@arkaria.postgresql.org; Thu, 21 Feb 2019 22:24:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gwwlg-0004AD-5l for pgsql-general@arkaria.postgresql.org; Thu, 21 Feb 2019 22:24:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gwwle-00047b-W5 for pgsql-general@lists.postgresql.org; Thu, 21 Feb 2019 22:24:35 +0000 Received: from ns605-vm3.bullet.mail.ssk.yahoo.co.jp ([182.22.91.116]) by makus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1gwwlW-0000Vd-Dn for pgsql-general@lists.postgresql.org; Thu, 21 Feb 2019 22:24:30 +0000 Received: from [182.22.66.104] by ns605.bullet.mail.ssk.yahoo.co.jp with NNFMP; 21 Feb 2019 22:24:21 -0000 Received: from [182.22.91.207] by t602.bullet.mail.ssk.yahoo.co.jp with NNFMP; 21 Feb 2019 22:24:21 -0000 Received: from [127.0.0.1] by omp610.mail.ssk.yahoo.co.jp with NNFMP; 21 Feb 2019 22:24:21 -0000 X-Yahoo-Newman-Property: ymail-5 X-Yahoo-Newman-Id: 668636.30033.bm@omp610.mail.ssk.yahoo.co.jp X-YMail-OSG: vnSLB1cVM1koxYus9afoRtmFJPsd4J582rQHzsrDz92jTxZkLn1E5FkAkB1NBhu YgSXA.d6qlBmb13qReTbPxwMqy5skvGxHsREafiJhDA8qrr.e53yk3ROsc68CrEutHV6Na2KA7xW YkhimF1ObFnObQmcTObYvSQJRyNFENSSFAIOXih_zCV3XBORF2B1LxqHDt3XyJ912AtGWKzt59lj GLdw3Gdnj9WxPNK_s6XX7TXPDFXa_HYhY7z2h8HUtSICd9VIy42BGu4LHtNOZ7COuS0dpwxiyKpG SJLluV4RmS40L.bCQeKcQ5nRihZts0R0z0SxgaUPzzT.W6hTIrQETmEPTeUANYTGUsBZ46TW9zry .U3t4szEyL6wazDjmflM7RkM44sVo.gTcx6eizvKMEO.P7bY8t.huVVc.2co3UxZAmhGB4WNhrte Xqn6c4cqAWBdLXIgZ0LdVp8ZWBabdG0w1dP3MucR7xQkNKEkh81L.vwMJK3yP1O7GpGPQD3AgEMx oEmxEHEj_dVS0wPIDI1O42MkYm.vZufAe5fOyCehpgLWioUqYP_ta88Xg9nrITANLEexqF8xLCXM HzqDgsfRnzkzqwUylyDgG0P5bRKJwAK8oo0wD4fVXC7H.ginNue_IEinbnmc7qx4BNO9pjpwpyB3 WjdoiGQVuIzWERIP5tupWI8qxeP.OUIhNuBdJS83nAImzXxkFyJ6cXYxcjF7ESHmgNpbMaWzwV15 6FyO_5z7YEIGk Received: from jws700002.mail.kks.yahoo.co.jp by sendmailws512.mail.kks.yahoo.co.jp; Fri, 22 Feb 2019 07:24:21 +0000; 1550787861.051 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1550787861; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:To:Message-ID:In-Reply-To:References:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=N23jXqM6nINIHqP13zXm0XfDvtYL0IslIJArS0FMOpo=; b=F2VY2kbmlegeDlg3+8/Sr6rMeSzZ/KFr3qYPDZ9WCuMYg/Ohu/7hEKStuCS/ezSo KvCdhQWoSSUqczLaA4l5zZ1Oid8Qy7S3xT+BnNNiJFanAfJrebfqrRVQ9gs3Kn03K9T nqPpd7PjMQFSna/+ihzwXNsv6Dm3vb0vxYCX/rLg= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:Message-ID:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding; b=sxZkxhDGZ/iFqLK3r1MC1sdWtGKo8xOIPkbHZ7/qszpUYCZWBojtm60KCUZtZktW kX1pNstkIZOwJipEudG4RvnZ2IFNolHbe/PodPsTz2aW3smC+fW49QTtgPjYD1ksr9Y 01nhvZ+/6O9/VygZ0o+mkLDvZH/JnwDGiDwC397Q=; Date: Fri, 22 Feb 2019 07:24:19 +0900 (JST) From: Reply-To: To: rob stone , "pgsql-general@lists.postgresql.org" Message-ID: <2009032291.258721.1550787859332.JavaMail.yahoo@mail.yahoo.co.jp> In-Reply-To: References: <1321198867.134127.1550722250138.JavaMail.yahoo.ref@mail.yahoo.co.jp> <1321198867.134127.1550722250138.JavaMail.yahoo@mail.yahoo.co.jp> Subject: Re: Connection string for Java to connect to PostgreSQL, using client certificates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Length: 8060 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello again Rob, Thank you for pointing that. Now what I did: 1. Copied the=C2=A0server.crt created on the postgresqlSERVER's /var/lib/CA= /server directory to client side. 2. Ran this script:| openssl x509 -in server.crt -out server.crt.der -outform der 3. keytool -keystore $JAVA_HOME/jre/lib/security/cacerts -alias postgresql = -import -file server.crt.der --- some message--- Trust this certificate? [no]:=C2=A0 yes Certificate was added to keystore 4. In my connection string, I added these lines, with hints from that site = you mentioned. props.setProperty("trustStore", "/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/= security/cacerts"); props.setProperty("trustStorePassword", "changeit"); (I entered that passwo= rd when I ran script #3) ... and I still got org.postgresql.util.PSQLException: Could not read SSL key file /home/user1/= cert/postgresql.key. for bonus I had one extra error.. Caused by: java.io.IOException: extra data given to DerValue constructor After spending better part of the morning, and going through different erro= rs, I came to this site: https://postgresrocks.enterprisedb.com/t5/EDB-Guides/How-to-setup-SSL-authe= ntication/ba-p/1647 This one (#5.5) helped: 5.5 convert the client key in DER format: openssl pkcs8 -topk8 -outform DER -in postgresql.key -out postgresql.key.pk= 8 -nocrypt Yes, instead of props.setProperty("sslkey","/home/user1/cert/postgresql.key"); I used=C2=A0=C2=A0 props.setProperty("sslkey","/home/user1/cert/postgresql.key.pk8");=C2=A0 and it worked! My final connection string: =C2=A0 =C2=A0 String url =3D "jdbc:postgresql://192.168.56.102:5432/testdb"= ; =C2=A0 =C2=A0 Properties props =3D new Properties(); =C2=A0 =C2=A0 props.setProperty("user","user1"); =C2=A0 =C2=A0 props.setProperty("ssl","true"); =C2=A0 =C2=A0 props.setProperty("sslmode","verify-ca"); =C2=A0 =C2=A0 props.setProperty("sslrootcert","/home/user1/cert/root.crt"); =C2=A0 =C2=A0 props.setProperty("sslkey","/home/user1/cert/postgresql.key.p= k8"); =C2=A0 =C2=A0 props.setProperty("sslcert","/home/user1/cert/postgresql.crt"= ); =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 c =3D DriverManager.getConnection(url,props); I did find many sites mentioning the need for making the server certificate= available to Java, but I don't know why mine didn't work. For now, immediate problem has been solved. Cheers! ----- Original Message ----- > From: rob stone > To: s400t@yahoo.co.jp; "pgsql-general@lists.postgresql.org" > Cc:=20 > Date: 2019/2/20, Wed 21:41 > Subject: Re: Connection string for Java to connect to PostgreSQL, using c= lient certificates >=20 > Hello, >=20 > On Thu, 2019-02-21 at 13:10 +0900, s400t@yahoo.co.jp wrote: >> I am having hard time to connect to PostgreSQL server using client >> certificate from within a Java program. >> Any insight would be helpful. >>=20 >> I can connect to the server using psql command line from a client >> machine(192.168.56.101) (psql -h 192.168.56.102 -U user1 -d testdb) >> [192.168.56.102 is "postgreSERVER" machine) >> //-------------------- >> successful outcome looks like this: >> psql (9.6.10) >> SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM- >> SHA384, bits: 256, compression: off) >> Type "help" for help. >>=20 >> user1=3D#=20 >> //------------------------------- >>=20 >> However, I have been unable to connect using a Java connection >> string. >>=20 >> This is more like ssl/certificate issue, and only tangentially >> related to the postgreSQL, but since I want to make it work using >> Java (running a test program from Eclipse), I am trying my luck here. >>=20 >> I started by creating a CA, server side key and certificate, and >> client side key and certificate. This I learnt by watching a Youtube >> video (https://www.youtube.com/watch?v=3DFWK3lR6bSn8 ). >>=20 >> For my own memo, I am reproducing the steps to create certificates >> and keys below, copied directly from that youtube: >>=20 >> After creating those files, I copied the server side files to >> /etc/postgresql/9.6/main/) (I am using Debian, and "data"=20 > directory >> seems to be "/etc/postgresql/9.6/main/"). >> and the client side files to /home/user1/.postgresql folder. (had to >> created ".postgresql" folder) >> The files were chmodded to 600. >> And when I used psql from a client machine (Debian), I can connect >> happily as I mentioned above. >>=20 >> Now for the Java test: >> I copied the "client side" files to /home/user1/cert/ (created=20 > "cert" >> folder)=20 >>=20 >> The files are:=20 >> postgresql.crt (1) >> postgresql.key (2) >> root.crt (3) >>=20 >>=20 >> (1)originally created as "client.crt" >> in 192.168.56.102:/var/lib/CA/client/, and copied as postgresql.crt >> to the client side >> (2)originally created as "client.key" >> in 192.168.56.102:/var/lib/CA/client/, and copied as postgresql.key >> (3)originally created as "rootCA.crt" in=20 > 192.168.56.102:/var/lib/CA/, >> and copied as "root.crt"=C2=A0=20 >>=20 >> My connection string is: >>=20 >> Connection c =3D null; >> Statement st =3D null; >>=20 >>=20 >> try { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Class.forName("org.postgresql.Driver"= ); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=20 >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 //credit: https://github.com/pgjdbc/p= gjdbc/issues/1364=20 >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 String url =3D=20 > "jdbc:postgresql://192.168.56.102:5432/testdb"; >>=20 >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Properties props =3D new Properties()= ; >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 props.setProperty("user","user1"); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 props.setProperty("password",""); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 props.setProperty("sslmode","verify-c= a"); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0=20 >> =C2=A0=20 > props.setProperty("sslrootcert","/home/user1/cert/root.crt"); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0=20 >> =C2=A0=20 > props.setProperty("sslcert","/home/user1/cert/postgresql.crt"); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0=20 >> =C2=A0=20 > props.setProperty("sslkey","/home/user1/cert/postgresql.key"); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 props.setProperty("loggerLevel","TRAC= E"); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0=20 >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 c =3D DriverManager.getConnection(url= ,props); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Statement st =3D c.createStatement(); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=20 >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 c.setAutoCommit(false); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 System.out.println("Opened database s= uccessfully"); >>=20 >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ResultSet rs =3D stmt.executeQuery( "= SELECT * FROM " + >> someTableName ); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 while ( rs.next() ) { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ...... >> ...... >> } >> .... >> .... >>=20 >>=20 >> When I run the code (in Eclipse, in client machine/Debian), I get >> this error: >> =C2=A0=20 >> org.postgresql.util.PSQLException: Could not read SSL key file >> /home/user1/cert/postgresql.key. >> at >> org.postgresql.ssl.jdbc4.LazyKeyManager.getPrivateKey(LazyKeyManager. >> java:250) >> at >> sun.security.ssl.AbstractKeyManagerWrapper.getPrivateKey(SSLContextIm >> pl.java:1250) >>=20 >> I googled, and someone suggested I convert the key file to a=20 > "der" >> format. >>=20 >> I tried this: >> user1@192.168.56.101:~/cert$ openssl x509 -outform der -in >> postgresql.key -out postgresql.der >>=20 >>=20 >> but then it says, >> unable to load certificate >> 140663292355968:error:0906D06C:PEM routines:PEM_read_bio:no start >> line:../crypto/pem/pem_lib.c:686:Expecting: TRUSTED CERTIFICATE >> user1@192.168.56.101:~/cert$=20 >>=20 >> I guess it expects PEM format.=20 >>=20 >> I am stuck. Please help. >>=20 >=20 >=20 > Please read chapter 4 documentation:- >=20 > https://jdbc.postgresql.org/documentation/head/ssl-client.html=20 >=20 > I think you are missing some steps. >=20 > HTH, >=20 > Robert >=20