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 D99BF32C90D for ; Fri, 15 Oct 2004 04:04:51 +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 47294-08 for ; Fri, 15 Oct 2004 03:04:48 +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 3250832C907 for ; Fri, 15 Oct 2004 04:04:46 +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 <0I5L00H2NV7WVC@ms-dienst.rz.rwth-aachen.de> for pgsql-jdbc@postgresql.org; Fri, 15 Oct 2004 05:04:45 +0200 (MEST) Received: from relay.rwth-aachen.de ([134.130.3.1]) by r220-1 (MailMonitor for SMTP v1.2.2 ) ; Fri, 15 Oct 2004 05:04:43 +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 i9F34hR7011465 for ; Fri, 15 Oct 2004 05:04:43 +0200 (MEST) Received: (qmail 22074 invoked from network); Fri, 15 Oct 2004 03:04:44 +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 ; Fri, 15 Oct 2004 03:04:44 +0000 Date: Fri, 15 Oct 2004 05:04:30 +0200 From: Ulrich Meis Subject: Re: A solution to the SSL customizing problem In-reply-to: <416DCBCB.5010908@opencloud.com> To: Oliver Jowett Cc: pgsql-jdbc@postgresql.org Message-id: <200410150504.30514.kenobi@halifax.rwth-aachen.de> MIME-version: 1.0 Content-type: multipart/mixed; boundary="Boundary_(ID_nEFDLKHm1D61kIq+BXPoAg)" User-Agent: KMail/1.7 References: <200410111752.33375.kenobi@halifax.rwth-aachen.de> <200410140224.56728.kenobi@halifax.rwth-aachen.de> <416DCBCB.5010908@opencloud.com> 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/157 X-Sequence-Number: 11262 --Boundary_(ID_nEFDLKHm1D61kIq+BXPoAg) Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: 7BIT Content-disposition: inline On Thursday 14 October 2004 02:43, Oliver Jowett wrote: >... > Otherwise it looks fine. I wouldn't worry about the DataSource piece for > the moment.. if you have the ability to pass args to the constructed > class via a URL or properties file it is less of an issue. How does that look like? Since I found the same code in v2 and v3, I copied the new method as well. Uli --Boundary_(ID_nEFDLKHm1D61kIq+BXPoAg) Content-type: text/plain; charset=iso-8859-1; name=pgjdbc-custom-ssl.txt Content-transfer-encoding: 7BIT Content-disposition: attachment; filename=pgjdbc-custom-ssl.txt Index: org/postgresql/Driver.java.in =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/Driver.java.in,v retrieving revision 1.48 diff -c -r1.48 Driver.java.in *** org/postgresql/Driver.java.in 10 Oct 2004 15:39:30 -0000 1.48 --- org/postgresql/Driver.java.in 15 Oct 2004 03:03:04 -0000 *************** *** 189,194 **** --- 189,198 ---- "Force use of a particular protocol version when connecting; if set, disables protocol version fallback.", }, { "ssl", Boolean.FALSE, "Control use of SSL; any nonnull value causes SSL to be required." }, + { "sslfactory", Boolean.FALSE, + "Provide a SSLSocketFactory class when using SSL." }, + { "sslfactoryargs", Boolean.FALSE, + "Arguments forwarded to constructor of SSLSocketFactory class;in addition to url." }, { "logLevel", Boolean.FALSE, "Control the driver's log verbosity: 0 is off, 1 is INFO, 2 is DEBUG.", new String[] { "0", "1", "2" } }, *************** *** 413,418 **** --- 417,425 ---- } } + //needed for a custom SSLSocketFactory in ConnectionFactoryImpl + urlProps.put("&urlServer&",l_urlServer); + return urlProps; } *************** *** 524,533 **** } ! public static void makeSSL(org.postgresql.core.PGStream p_stream) throws IOException { @SSL@ if (logDebug) @SSL@ debug("converting regular socket connection to ssl"); ! @SSL@ javax.net.ssl.SSLSocketFactory factory = (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault(); @SSL@ java.net.Socket newConnection = factory.createSocket(p_stream.getSocket(), p_stream.getHost(), p_stream.getPort(), true); @SSL@ p_stream.changeSocket(newConnection); } --- 531,540 ---- } ! public static void makeSSL(org.postgresql.core.PGStream p_stream, Object factory_o) throws IOException { @SSL@ if (logDebug) @SSL@ debug("converting regular socket connection to ssl"); ! @SSL@ javax.net.ssl.SSLSocketFactory factory = (javax.net.ssl.SSLSocketFactory) factory_o; @SSL@ java.net.Socket newConnection = factory.createSocket(p_stream.getSocket(), p_stream.getHost(), p_stream.getPort(), true); @SSL@ p_stream.changeSocket(newConnection); } Index: org/postgresql/core/v2/ConnectionFactoryImpl.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/v2/ConnectionFactoryImpl.java,v retrieving revision 1.3 diff -c -r1.3 ConnectionFactoryImpl.java *** org/postgresql/core/v2/ConnectionFactoryImpl.java 10 Oct 2004 15:39:35 -0000 1.3 --- org/postgresql/core/v2/ConnectionFactoryImpl.java 15 Oct 2004 03:03:04 -0000 *************** *** 68,74 **** // Construct and send an ssl startup packet if requested. if (trySSL) ! newStream = enableSSL(newStream, requireSSL); // Construct and send a startup packet. sendStartupPacket(newStream, user, database); --- 68,74 ---- // Construct and send an ssl startup packet if requested. if (trySSL) ! newStream = enableSSL(newStream, requireSSL, info); // Construct and send a startup packet. sendStartupPacket(newStream, user, database); *************** *** 107,113 **** } } ! private PGStream enableSSL(PGStream pgStream, boolean requireSSL) throws IOException, SQLException { if (Driver.logDebug) Driver.debug(" FE=> SSLRequest"); --- 107,113 ---- } } ! private PGStream enableSSL(PGStream pgStream, boolean requireSSL, Properties info) throws IOException, SQLException { if (Driver.logDebug) Driver.debug(" FE=> SSLRequest"); *************** *** 147,153 **** Driver.debug(" <=BE SSLOk"); // Server supports ssl ! Driver.makeSSL(pgStream); return pgStream; default: --- 147,153 ---- Driver.debug(" <=BE SSLOk"); // Server supports ssl ! Driver.makeSSL(pgStream,getSSLSocketFactory(info)); return pgStream; default: *************** *** 155,160 **** --- 155,186 ---- } } + private javax.net.ssl.SSLSocketFactory getSSLSocketFactory(Properties info) throws SQLException { + // If the url contains no class, return the default factory + String classname = info.getProperty("sslfactory"); + if (classname==null) + return (javax.net.ssl.SSLSocketFactory)javax.net.ssl.SSLSocketFactory.getDefault();; + + // those are passed to the provided class if a fitting constructor is found + Object[] args = { info.getProperty("&urlServer&"), info.getProperty("sslfactoryargs") }; + java.lang.reflect.Constructor ctor; + Class factory_class; + + // get the (String,String) constructor and use () if that fails + try { + factory_class = Class.forName(classname); + try { + ctor = factory_class.getConstructor(new Class[] { String.class, String.class}); + } catch (NoSuchMethodException e) { + ctor = factory_class.getConstructor(null); + args = null; + } + return (javax.net.ssl.SSLSocketFactory)ctor.newInstance(args); + } catch (Exception e) { + throw new PSQLException(GT.tr("The SSLSocketFactory class provided in the connection URL could not be instantiated"), PSQLState.CONNECTION_FAILURE,e); + } + } + private void sendStartupPacket(PGStream pgStream, String user, String database) throws IOException { // 4: total size including self // 2: protocol major Index: org/postgresql/core/v3/ConnectionFactoryImpl.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v retrieving revision 1.3 diff -c -r1.3 ConnectionFactoryImpl.java *** org/postgresql/core/v3/ConnectionFactoryImpl.java 10 Oct 2004 15:39:37 -0000 1.3 --- org/postgresql/core/v3/ConnectionFactoryImpl.java 15 Oct 2004 03:03:05 -0000 *************** *** 78,84 **** // Construct and send an ssl startup packet if requested. if (trySSL) ! newStream = enableSSL(newStream, requireSSL); // Construct and send a startup packet. String[][] params = { --- 78,84 ---- // Construct and send an ssl startup packet if requested. if (trySSL) ! newStream = enableSSL(newStream, requireSSL, info); // Construct and send a startup packet. String[][] params = { *************** *** 126,132 **** } } ! private PGStream enableSSL(PGStream pgStream, boolean requireSSL) throws IOException, SQLException { if (Driver.logDebug) Driver.debug(" FE=> SSLRequest"); --- 126,132 ---- } } ! private PGStream enableSSL(PGStream pgStream, boolean requireSSL, Properties info) throws IOException, SQLException { if (Driver.logDebug) Driver.debug(" FE=> SSLRequest"); *************** *** 166,172 **** Driver.debug(" <=BE SSLOk"); // Server supports ssl ! Driver.makeSSL(pgStream); return pgStream; default: --- 166,172 ---- Driver.debug(" <=BE SSLOk"); // Server supports ssl ! Driver.makeSSL(pgStream,getSSLSocketFactory(info)); return pgStream; default: *************** *** 174,179 **** --- 174,205 ---- } } + private javax.net.ssl.SSLSocketFactory getSSLSocketFactory(Properties info) throws SQLException { + // If the url contains no class, return the default factory + String classname = info.getProperty("sslfactory"); + if (classname==null) + return (javax.net.ssl.SSLSocketFactory)javax.net.ssl.SSLSocketFactory.getDefault();; + + // those are passed to the provided class if a fitting constructor is found + Object[] args = { info.getProperty("&urlServer&"), info.getProperty("sslfactoryargs") }; + java.lang.reflect.Constructor ctor; + Class factory_class; + + // get the (String,String) constructor and use () if that fails + try { + factory_class = Class.forName(classname); + try { + ctor = factory_class.getConstructor(new Class[] { String.class, String.class}); + } catch (NoSuchMethodException e) { + ctor = factory_class.getConstructor(null); + args = null; + } + return (javax.net.ssl.SSLSocketFactory)ctor.newInstance(args); + } catch (Exception e) { + throw new PSQLException(GT.tr("The SSLSocketFactory class provided in the connection URL could not be instantiated"), PSQLState.CONNECTION_FAILURE,e); + } + } + private void sendStartupPacket(PGStream pgStream, String[][] params) throws IOException { if (Driver.logDebug) { String details = ""; --Boundary_(ID_nEFDLKHm1D61kIq+BXPoAg)--