pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ulrich Meis <kenobi@halifax.rwth-aachen.de>
To: pgsql-jdbc@postgresql.org
Subject: A solution to the SSL customizing problem
Date: Mon, 11 Oct 2004 17:52:33 +0200
Message-ID: <200410111752.33375.kenobi@halifax.rwth-aachen.de> (raw)

I have read previous posts and the proposal that the user should supply a 
SSLSocketFactory. Besides the obvious question - how - I see an additional 
problem in that approach:

It's anything but user friendly.
All you want is a secure connection to postgres and you end up digging deeply 
into the JSSE spec.
Even if all you want is turn validation off, you will have to have some 
knowledge of the spec.
The really hard bit comes when you want to retrieve the certificate from the 
server. You are using SSL because you are concerned about security, so you 
probably want checking. Now you will have to give your own TrustManager 
implementation that saves the certificate received for checking. Furthermore, 
you will have to save that certificate somehow and you'll end up handling 
keystores and stuff. What an effort just for this feature!

**********
I propose a different solution.
There are three modes I can think of when opening a connection:

1. Disabling validation.

Not interesting for a user with security concerns - you loose half of SSL's 
functionality.

2. Enabling validation (standard)
The problem is that you need to get your hands on the server's certificate.
If you distribute an applet for a single server, like I do, you can ship the 
server's certificate with your applet. One thing you certainly don't want is 
to tell people how to insert it with a command line tool (keytool) and so a 
nice solution is to provide it in a keystore and point jdbc to it. 
Furthermore you are likely not to have write permissions on the standard 
keystore (if you want to update it in your applet) because it is in a subdir 
of java_home.

3. Trust and save (disable validation and save received key)
Same as with openssl and known_hosts, you accept the host the first time you 
connect, save its certificate and use the standard mode from then on. 
Implementation of this feature is easier within the driver because you have 
access to the SSL connection and can retrieve the certificate(s). As a user 
you can't just pull it off the postgres port because postgres doesn't start 
off in SSL mode. You will have to implement a TrustManager.

**************
What I did for the driver is the following: Anologously to 

javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword

I introduced

org.postgresql.trustStore and org.postgresql.trustStorePassword

Now you can specify your own trustStore for pgjdbc connections.
If it doesn't exist, it will be created for you.

Additionally I introduced the trust-and-save mode.

Currently I look for a property org.postgresql.ssl_trustandsave but this would 
better be handled in a URL parameter like "ssl_trustandsave".

When the driver is in trust-and-save mode, it will trust any server you 
connect to and save its certificate in the trustStore.

In an application, you would first run in normal mode. If the connection 
fails, you ask the user if he wants to trust the unknown key and if he 
agrees, you connect again with ssl_trustandsave mode activated and the key is 
saved. In subsequent connections, you turn ssl_trustandsave off.

**********
Implementation:

I introduced a class org.postgresql.util.PGSSLSocketFactory and changed one 
line in org.postgresql.Driver and method makeSSL from

javax.net.ssl.SSLSocketFactory factory = (javax.net.ssl.SSLSocketFactory) 
javax.net.ssl.SSLSocketFactory.getDefault();

to

org.postgresql.util.PGSSLSocketFactory factory = 
org.postgresql.util.PGSSLSocketFactory.getInstance();
.

What do you think?

Kind Regards,

Uli

Attachments:

PGSSLSocketFactory.java (text/x-java, 7.5K) download package org.postgresql.util; import org.postgresql.Driver; import javax.net.ssl.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.Socket; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; /** * Creates SSL sockets. * If the property org.postgresql.Driver.trustStore is set, it will * use that truststore for the socket. The password is read from * org.postgresql.Driver.trustStorePassword . * If trustandsave is on, it will trust any host and attempt to save its certs in the truststore * * @author Ulrich Meis */ public class PGSSLSocketFactory { private static PGSSLSocketFactory instance = new PGSSLSocketFactory(); private String storeLocation; private String storePass; private KeyStore ks; private String trustStoreProp = "org.postgresql.trustStore"; private String trustStorePasswordProp = "org.postgresql.trustStorePassword"; /** * loads contents of a trustStore file into ks. * If no custom trustStore is supplied, it implements JSSE's fallback mechanism: * Check if property is present, check java_home/jre/lib/security/jssecacerts, check * java_home/jre/lib/security/cacerts * * @throws KeyStoreException * @throws IOException * @throws NoSuchAlgorithmException * @throws CertificateException */ private void load() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { storeLocation = System.getProperty(trustStoreProp); storePass = System.getProperty(trustStorePasswordProp); if (storeLocation == null) { // Fallback to JSSE properties storeLocation = System.getProperty("javax.net.ssl.trustStore"); storePass = System.getProperty("javax.net.ssl.trustStorePassword"); } if (storeLocation == null) { // Fallback to JSEE standard files storePass = System.getProperty("changeit"); storeLocation = System.getProperty("java.home") + File.separatorChar + "lib" + File.separatorChar + "security" + File.separatorChar + "jssecacerts"; File f = new File(storeLocation); if (!f.exists()) storeLocation = System.getProperty("java.home") + File.separatorChar + "lib" + File.separatorChar + "security" + File.separatorChar + "cacerts"; } if (storePass == null) storePass = "changeit"; Driver.info("Using trustStore " + storeLocation); ks = KeyStore.getInstance(KeyStore.getDefaultType()); File keystore = new File(storeLocation); if (keystore.exists()) { FileInputStream fi = new FileInputStream(storeLocation); ks.load(fi, storePass.toCharArray()); fi.close(); } else ks.load(null, storePass.toCharArray()); } /** * stores given certificate in the trustStore * * @param serverCerts * @throws KeyStoreException * @throws IOException * @throws NoSuchAlgorithmException * @throws CertificateException */ private void store(Certificate[] serverCerts) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { File keystore = new File(storeLocation); if (!keystore.exists()) { if (!keystore.getParentFile().canWrite()) { Driver.debug("Cannot write to directory " + keystore.getParentFile().getAbsolutePath()); return; } } else if (!keystore.canWrite()) { Driver.debug("Cannot write to keystore " + storeLocation); return; } for (int i = 0; i < serverCerts.length; i++) { X509Certificate cert = (X509Certificate) serverCerts[i]; ks.setCertificateEntry("PGJDBC " + cert.getIssuerDN().toString(), cert); } FileOutputStream fo = new FileOutputStream(storeLocation); ks.store(fo, storePass.toCharArray()); fo.close(); } /** * creates a socket. * Will use a custom trustStore if provided. If the property org.postgresql.ssl_trustandsave is defined, * it will accept any certificate(s) and save them to the used keystore. * * @param socket * @param host * @param port * @param autoClose * @return * @throws IOException */ public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException { if ((System.getProperty("org.postgresql.Driver.trustStore") == null) && (System.getProperty("org.postgresql.ssl_trustandsave") == null)) return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(socket, host, port, autoClose); boolean trustandsave = System.getProperty("org.postgresql.ssl_trustandsave") != null; SSLSocketFactory factory; try { // the truststore is only loaded once per class load. If you want to change the properties/trustStore // at runtime, remove the if. if (ks == null) load(); SSLContext sc = SSLContext.getInstance("SSL"); if (trustandsave) { // trust anyone sc.init(null, trustseveryone(), null); Driver.debug("trustandsave is on for upcoming connection"); } else { // Initalize KeyManagerFactory and TrustManagerFactory with keystore KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ks); kmf.init(ks, storePass.toCharArray()); sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); } factory = sc.getSocketFactory(); } catch (Exception e) { e.printStackTrace(); Driver.debug("Couldn't set up custom SSLSocketFactory due to previous exception," + " using SSLSocketFactory.getDefault()"); factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); } SSLSocket sslsocket = (SSLSocket) factory.createSocket(socket, host, port, autoClose); try { if (trustandsave) store(sslsocket.getSession().getPeerCertificates()); } catch (Exception e) { e.printStackTrace(); Driver.debug("Couldn't save certificate due to previous exception"); } return sslsocket; } /** * Get static instance * * @return */ public static PGSSLSocketFactory getInstance() { return instance; } /** * will trust everyone. Only used if in trustandsave mode * * @return array suitable to init SSLContext * @see SSLContext */ public TrustManager[] trustseveryone() { return new TrustManager[]{ new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { } } }; } }

view thread (31+ messages)  latest in thread

Message-ID: <200410111752.33375.kenobi@halifax.rwth-aachen.de>
Permalink:  ../200410111752.33375.kenobi@halifax.rwth-aachen.de/
Also on:    postgresql.org/message-id/200410111752.33375.kenobi@halifax.rwth-aachen.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: kenobi@halifax.rwth-aachen.de
  Subject: Re: A solution to the SSL customizing problem
  In-Reply-To: <200410111752.33375.kenobi@halifax.rwth-aachen.de>
  References: <200410111752.33375.kenobi@halifax.rwth-aachen.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox