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 1gwfhb-0007DC-9k for pgsql-general@arkaria.postgresql.org; Thu, 21 Feb 2019 04:11:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gwfhY-0005cQ-K6 for pgsql-general@arkaria.postgresql.org; Thu, 21 Feb 2019 04:11:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gwfhX-0005T1-J5 for pgsql-general@lists.postgresql.org; Thu, 21 Feb 2019 04:11:12 +0000 Received: from ns603-vm7.bullet.mail.ssk.yahoo.co.jp ([182.22.91.88]) by magus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1gwfhJ-0002qS-TQ for pgsql-general@lists.postgresql.org; Thu, 21 Feb 2019 04:11:07 +0000 Received: from [182.22.66.104] by ns603.bullet.mail.ssk.yahoo.co.jp with NNFMP; 21 Feb 2019 04:10:51 -0000 Received: from [182.22.91.128] by t602.bullet.mail.ssk.yahoo.co.jp with NNFMP; 21 Feb 2019 04:10:51 -0000 Received: from [127.0.0.1] by omp601.mail.ssk.yahoo.co.jp with NNFMP; 21 Feb 2019 04:10:51 -0000 X-Yahoo-Newman-Property: ymail-5 X-Yahoo-Newman-Id: 359870.67193.bm@omp601.mail.ssk.yahoo.co.jp X-YMail-OSG: wdUh.h8VM1kYct_71YpqBYJ5RSlq0tfbv2DrT8E4EDITYNL3Fh5uXMyRDKnsj8R 1Yvn_VTcd6QPnrQuBYv2IK5xx86AEWipXGDAxVkqVaad9rlJObz66RayQ4WaRs02kphvA1n1mM4Z KoTTIH2rbrx9hW5M2c57teNaaIrbheZEtqDwFOszD5AEn7BO2y1vSDLAWlMOYsCAN9atWIdeKz0N zbx405XdN0Xqfk13LJzmEDh.uG2mDf6qf2y2ePRPlGoZaQhBI3XqiGnyz.eQDvAAjWfiKFbXAYjI AnMmx_0ywWQJgHHv_seBAwAGYdvis6Ea0je1PrAbqMjl3.Xwbjh_gCRkDI1aPVmlKEy56HixAY95 ZlwhCsGPBDcuVMmaGuIqHpBKL6_oy2KDLgY81hPwGHF9Lra4DR8IoqzQJEfHfa4c0eSWlxdjG.e7 EcBM5pQ3O1d4_suw19C0DIz48bW0tBMoKLb_am9.m94Uuhhhr9KmJIkfonVXx0S4k0v6NMFvUb_p YR0.0STYqxtdlKLAaEyjsw4q_BGjQScNhii0eJgZ8_nqkOhK.hEA_ZyP8HExfW_DCJGf9hj6nyWO bcLhEQyNEKJCFJgHAY0IYQ_.zZkWlKui.ankzF8mwDtEGCbfGYONqxi342Sug6XCSz7DMq5bIc5Y Dbdd_59yq6CP2LlyrYgA6DS.GPqZtMbIBzYjI Received: from jws700004.mail.kks.yahoo.co.jp by sendmailws512.mail.kks.yahoo.co.jp; Thu, 21 Feb 2019 13:10:50 +0000; 1550722250.766 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1550722251; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:To:Message-ID:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:References; bh=DvXhgn8TmmyuW/2Lo8aG95rwEVE0lYBsJgsd8FKZ/tg=; b=hE4K3/XYLKprU2K/CFaNNda4zX52uzphtA8j70BlKKM55cuGkW7o5GRo//p1YVHi lOajBFtV7HFTaPrkN55X1sQ1t/5HcTqWRpzGi9Czta4Rr06ywmBnUsXk2ELRL/aw58/ i0Vydp/2JahQIwWecV/lJsMvFIBa9gkm/2blDsdc= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:References; b=KZt5Lii18spFSiYBtDvptuNUIii+06PsXibslXZXm2zvUqpvOTsWn0UtGr4epO6a DuA2s4j9gL36/PeR9BIX9+C+6uzoEEbuH4nvL67ROWy92kju5b8xHkHFVpi56D9Baeg 4A7KrDeHt2ocFyo/Zs88Gle5EtXG3ZCB/gxYPudc=; Date: Thu, 21 Feb 2019 13:10:50 +0900 (JST) From: Reply-To: To: "pgsql-general@lists.postgresql.org" Message-ID: <1321198867.134127.1550722250138.JavaMail.yahoo@mail.yahoo.co.jp> Subject: 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 References: <1321198867.134127.1550722250138.JavaMail.yahoo.ref@mail.yahoo.co.jp> Content-Length: 6817 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I am having hard time to connect to PostgreSQL server using client certific= ate from within a Java program. Any insight would be helpful. I can connect to the server using psql command line from a client machine(1= 92.168.56.101) (psql -h 192.168.56.102 -U user1 -d testdb) [192.168.56.102 = is "postgreSERVER"=C2=A0machine) //-------------------- successful outcome looks like this: psql (9.6.10) SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bit= s: 256, compression: off) Type "help" for help. user1=3D#=C2=A0 //------------------------------- However, I have been unable to connect using a Java connection string. This is more like ssl/certificate issue, and only tangentially related to t= he postgreSQL, but since I want to make it work using Java (running a test = program from Eclipse), I am trying my luck here. I started by creating a CA, server side key and certificate, and client sid= e key and certificate. This I learnt by watching a Youtube video (https://w= ww.youtube.com/watch?v=3DFWK3lR6bSn8). For my own memo, I am reproducing the steps to create certificates and keys= below, copied directly from that youtube: After creating those files,=C2=A0I copied the server side files=C2=A0to /et= c/postgresql/9.6/main/) (I am using Debian, and "data" directory seems to b= e "/etc/postgresql/9.6/main/"). and the client side files to /home/user1/.postgresql folder. (had to create= d ".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. Now for the Java test: I copied the "client side" files to /home/user1/cert/ (created "cert" folde= r)=C2=A0 The files are:=C2=A0 postgresql.crt (1) postgresql.key (2) root.crt (3) (1)originally created as "client.crt" in=C2=A0192.168.56.102:/var/lib/CA/cl= ient/, and copied as postgresql.crt to the client side (2)originally created as "client.key" in=C2=A0192.168.56.102:/var/lib/CA/cl= ient/, and copied as postgresql.key (3)originally created as "rootCA.crt" in=C2=A0192.168.56.102:/var/lib/CA/, = and copied as "root.crt"=C2=A0=C2=A0 My connection string is: Connection c =3D null; Statement st =3D null; try { =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Class.forName("org.postgresql.Driver"); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0//credit: https://github.com/pgjdbc/pgjdb= c/issues/1364 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0String url =3D "jdbc:postgresql://192.168= .56.102:5432/testdb"; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Properties props =3D new Properties(); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0props.setProperty("user","user1"); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0props.setProperty("password",""); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0props.setProperty("sslmode","verify-ca"); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0props.setProperty("sslrootcert","/home/us= er1/cert/root.crt"); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0props.setProperty("sslcert","/home/user1/= cert/postgresql.crt"); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0props.setProperty("sslkey","/home/user1/c= ert/postgresql.key"); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0props.setProperty("loggerLevel","TRACE"); =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0c =3D DriverManager.getConnection(url,pro= ps); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Statement st =3D c.createStatement(); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0c.setAutoCommit(false); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0System.out.println("Opened database succe= ssfully"); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ResultSet rs =3D stmt.executeQuery( "SELE= CT * FROM " + someTableName ); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0while ( rs.next() ) { =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0...... ...... } .... .... When I run the code (in Eclipse, in client machine/Debian), I get this erro= r: =C2=A0 org.postgresql.util.PSQLException: Could not read SSL key file /home/user1/= cert/postgresql.key. at org.postgresql.ssl.jdbc4.LazyKeyManager.getPrivateKey(LazyKeyManager.jav= a:250) at sun.security.ssl.AbstractKeyManagerWrapper.getPrivateKey(SSLContextImpl.= java:1250) I googled, and someone suggested I convert the key file to a "der" format. I tried this: user1@192.168.56.101:~/cert$ openssl x509 -outform der -in postgresql.key -= out postgresql.der but then it says, unable to load certificate 140663292355968:error:0906D06C:PEM routines:PEM_read_bio:no start line:../c= rypto/pem/pem_lib.c:686:Expecting: TRUSTED CERTIFICATE user1@192.168.56.101:~/cert$=C2=A0 I guess it expects PEM format.=C2=A0 I am stuck. Please help. Thanks for reading a long post. //Notes from the Youtube: (1) become a root and setup CA mkdir /var/lib/CA cd CA openssl genrsa -out rootCA.key 2048 (generate CA private key) openssl req -x509 -new -key rootCA.key -days 10000 -out rootCA.crt (create = root cert signed by the CA private key) (2) Create server key and certificates mkdir server cd server openssl genrsa -out server.key 2048 openssl req -new -key server.key -out server.csr=C2=A0 openssl x509 -req -in server.csr -CA ../rootCA.crt -CAkey ../rootCA.key -CA= createserial -out server.crt -days 5000 (3) Client identities cd .. mkdir client cd client openssl genrsa -out client.key 2048=C2=A0 (private key) openssl req -new -key client.key -out client.csr=C2=A0 (certificate signing= request-- CN MUST be db user name)=C2=A0 #Create a certificate for database client openssl x509 -req -in client.csr -CA ../rootCA.crt -CAkey ../rootCA.key -CA= createserial -out client.crt -days 5000 (4)Copy CA root certificate, server key and certificate into postgresql clu= ster directory ( .. to /etc/postgresql/9.6... NOT /var/lib.. go to to /etc/postgresql/9.6/main cp /var/lib/CA/rootCA.crt . cp /var/lib/CA/server/server.crt . cp /var/lib/CA/server/server.key . chmod 600 server.key (5) edit postgresql.conf, edit pg_hba.conf postgresql.conf: listen_addresses =3D "*" ssl =3D true remove comment out from ssl_ciphers =3D 'HIGH:MEDIUM..'=C2=A0 give proper path to ssl_key_file, ssl_cert_file and ssl_ca_file pg_hba.conf: comment out: host all all=C2=A0 (some IP) md5 (or trust?) add: hostssl testdb all 0.0.0.0/0 cert clientcert=3D1 (6)create .postgresql in client machine's user home directory mkdir ~/.postgresql scp root@postgreSERVER:/var/lib/CA/rootCA.crt ~/.postgresql/root.crt scp root@postgreSERVER:/var/lib/CA/client/client.crt ~/.postgresql/postgres= ql.crt scp root@postgreSERVER:/var/lib/CA/client.key ~/.postgresql/postgresql.key chmod 600 ~/.postgresql/postgresql.key (7) user1@192.168.56.101:~$ psql -h 192.168.56.102 -U user1 testdb psql (9.6.10) SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bit= s: 256, compression: off) Type "help" for help. user1=3D#=C2=A0 //---------------------------