X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A941032AF08 for ; Mon, 11 Oct 2004 16:58:05 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 69996-06 for ; Mon, 11 Oct 2004 15:57:59 +0000 (GMT) Received: from ms-dienst.rz.rwth-aachen.de (ms-1.rz.RWTH-Aachen.DE [134.130.3.130]) by svr1.postgresql.org (Postfix) with ESMTP id 17B8A32ABA9 for ; Mon, 11 Oct 2004 16:57:57 +0100 (BST) Received: from r220-1 (r220-1.rz.RWTH-Aachen.DE [134.130.3.31]) by ms-dienst.rz.rwth-aachen.de (iPlanet Messaging Server 5.2 HotFix 1.12 (built Feb 13 2003)) with ESMTP id <0I5F002L9G3QMU@ms-dienst.rz.rwth-aachen.de> for pgsql-jdbc@postgresql.org; Mon, 11 Oct 2004 17:52:38 +0200 (MEST) Received: from relay.rwth-aachen.de ([134.130.3.1]) by r220-1 (MailMonitor for SMTP v1.2.2 ) ; Mon, 11 Oct 2004 17:52:37 +0200 (MEST) Received: from post.halifax.rwth-aachen.de (post.halifax.RWTH-Aachen.DE [134.130.49.251]) by relay.rwth-aachen.de (8.13.0/8.13.0/1) with ESMTP id i9BFqbWN007123 for ; Mon, 11 Oct 2004 17:52:37 +0200 (MEST) Received: (qmail 14143 invoked from network); Mon, 11 Oct 2004 15:52:42 +0000 Received: from darkenemy.halifax.rwth-aachen.de ([134.130.48.36]) (envelope-sender ) by mail.halifax.rwth-aachen.de (qmail-ldap-1.03) with SMTP for ; Mon, 11 Oct 2004 15:52:42 +0000 Date: Mon, 11 Oct 2004 17:52:33 +0200 From: Ulrich Meis Subject: A solution to the SSL customizing problem To: pgsql-jdbc@postgresql.org Message-id: <200410111752.33375.kenobi@halifax.rwth-aachen.de> MIME-version: 1.0 Content-type: multipart/mixed; boundary="Boundary_(ID_0C86rAW0tM9mXBPSMYM2tA)" User-Agent: KMail/1.7 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/86 X-Sequence-Number: 11191 --Boundary_(ID_0C86rAW0tM9mXBPSMYM2tA) Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7BIT Content-disposition: inline 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 --Boundary_(ID_0C86rAW0tM9mXBPSMYM2tA) Content-type: text/x-java; charset=us-ascii; name=PGSSLSocketFactory.java Content-transfer-encoding: 7BIT Content-disposition: attachment; filename=PGSSLSocketFactory.java 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) { } } }; } } --Boundary_(ID_0C86rAW0tM9mXBPSMYM2tA)--