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 1gwgAr-0000Ed-HW for pgsql-general@arkaria.postgresql.org; Thu, 21 Feb 2019 04:41:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gwgAp-0002SP-9G for pgsql-general@arkaria.postgresql.org; Thu, 21 Feb 2019 04:41:27 +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 1gwgAo-0002HV-V4 for pgsql-general@lists.postgresql.org; Thu, 21 Feb 2019 04:41:27 +0000 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gwgAh-0003Tw-N8 for pgsql-general@lists.postgresql.org; Thu, 21 Feb 2019 04:41:23 +0000 Received: by mail-pg1-x544.google.com with SMTP id y1so13126985pgk.11 for ; Wed, 20 Feb 2019 20:41:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=/ovl+dHOX5CTVsrwiXGraAvbS01aEvHP9TwA8A+GxDQ=; b=WxItW9nL36TD5atHIHHgvLKDBEq1yfubYz6QkSDKbQH/wQoWVAj47XdDJCdGwutfCf cqaVBfTRm9joEY0jYkoSOrox661CF0eyCW6cWJiLJvK/DYqKbiwlNqt49pd5PY+I0PY5 sitW7bAYrFW99l9GJQee2wRjf9NVsNRcf4XIerCYLSDXbbJd9mvtYr/bzLMfseCU7N+6 EXYwhLUiwMXlPn70X86MrPOuM0ZHnfxR31H0/WNE6J9bca0dcnVCimtaOppjEMMHmA+i OgLqveZ71IuBb7RdJFpd/MWK5HI2L2zh/SwVarhrXYO6VVhdmRjwg282yQawOYpsJysS MmOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=/ovl+dHOX5CTVsrwiXGraAvbS01aEvHP9TwA8A+GxDQ=; b=bScV6c8kuwyLWcucAdUTlcWRzuBYKofkaVRk+MXJ1fcXDMX6BTK6tt+JcxuVuaR/A3 YUmVk64Qi2IGxccssBSvrSFxZpAcdIAj8vOK8zBYT0pNU0kgP2TqK1slnPsf/J2/5yax QRxqcDGqyvL1WFg3nZM5JeFNr7/JhhtD39pqBvi2jvgLSn7zn1TZBtVyppTT+gL7ANHQ idxZwr3MAJhVLsuOoNp07bZRglcG2Jii/WwAi2VqNghl0wp2SIvi3JhBc8VKhrerFmKG 0sUiKKYExzuS9TRcaNLgbAD5RqKPl4nwFLKNhJT5bhkwaxZ951H6DbknrYNB0hSwk1Io 4Kog== X-Gm-Message-State: AHQUAuZeubDRLZ6qLQIWFhd8lVvRrsFmUlH+5ynU4D92cV8eKcNuTuRd RUP5d8pFy4mN5LMjQyb1PSY= X-Google-Smtp-Source: AHgI3IbqQ/CzGbKsRXmwQ1Q6q70vcCqlvA994HQ8dVVCGzJubtgY3+VEdVq14NRltfwgs5X/zirVnw== X-Received: by 2002:a63:6949:: with SMTP id e70mr3414006pgc.89.1550724077012; Wed, 20 Feb 2019 20:41:17 -0800 (PST) Received: from 192-168-1-102.tpgi.com.au ([61.69.131.212]) by smtp.gmail.com with ESMTPSA id s79sm27217073pgs.50.2019.02.20.20.41.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 20 Feb 2019 20:41:16 -0800 (PST) Message-ID: Subject: Re: Connection string for Java to connect to PostgreSQL, using client certificates From: rob stone To: s400t@yahoo.co.jp, "pgsql-general@lists.postgresql.org" Date: Thu, 21 Feb 2019 15:41:12 +1100 In-Reply-To: <1321198867.134127.1550722250138.JavaMail.yahoo@mail.yahoo.co.jp> References: <1321198867.134127.1550722250138.JavaMail.yahoo.ref@mail.yahoo.co.jp> <1321198867.134127.1550722250138.JavaMail.yahoo@mail.yahoo.co.jp> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.5-1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, 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. > > 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. > > user1=# > //------------------------------- > > However, I have been unable to connect using a Java connection > string. > > 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. > > 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=FWK3lR6bSn8). > > For my own memo, I am reproducing the steps to create certificates > and keys below, copied directly from that youtube: > > After creating those files, I copied the server side files to > /etc/postgresql/9.6/main/) (I am using Debian, and "data" 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. > > Now for the Java test: > I copied the "client side" files to /home/user1/cert/ (created "cert" > folder) > > The files are: > postgresql.crt (1) > postgresql.key (2) > root.crt (3) > > > (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 192.168.56.102:/var/lib/CA/, > and copied as "root.crt" > > My connection string is: > > Connection c = null; > Statement st = null; > > > try { > Class.forName("org.postgresql.Driver"); > > //credit: https://github.com/pgjdbc/pgjdbc/issues/1364 > String url = "jdbc:postgresql://192.168.56.102:5432/testdb"; > > Properties props = new Properties(); > props.setProperty("user","user1"); > props.setProperty("password",""); > props.setProperty("sslmode","verify-ca"); > > props.setProperty("sslrootcert","/home/user1/cert/root.crt"); > > props.setProperty("sslcert","/home/user1/cert/postgresql.crt"); > > props.setProperty("sslkey","/home/user1/cert/postgresql.key"); > props.setProperty("loggerLevel","TRACE"); > > c = DriverManager.getConnection(url,props); > Statement st = c.createStatement(); > > c.setAutoCommit(false); > System.out.println("Opened database successfully"); > > ResultSet rs = stmt.executeQuery( "SELECT * FROM " + > someTableName ); > while ( rs.next() ) { > ...... > ...... > } > .... > .... > > > When I run the code (in Eclipse, in client machine/Debian), I get > this error: > > 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) > > 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:../crypto/pem/pem_lib.c:686:Expecting: TRUSTED CERTIFICATE > user1@192.168.56.101:~/cert$ > > I guess it expects PEM format. > > I am stuck. Please help. > Please read chapter 4 documentation:- https://jdbc.postgresql.org/documentation/head/ssl-client.html I think you are missing some steps. HTH, Robert