Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 6E19C2E3410 for ; Thu, 29 Nov 2007 06:00:31 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 48371-08 for ; Thu, 29 Nov 2007 06:00:22 -0400 (AST) Received: from mail.cisx.com (mail.cisx.com [213.133.202.146]) by postgresql.org (Postfix) with ESMTP id 185352E3585 for ; Thu, 29 Nov 2007 06:00:24 -0400 (AST) Received: from LISTINGS12 ([192.168.100.32]) by mail.cisx.com (8.13.1/8.13.1) with SMTP id lATA0Hpt006632 for ; Thu, 29 Nov 2007 10:00:23 GMT Message-ID: <001b01c8326e$a53ba500$2064a8c0@LISTINGS12> From: "Donald Fraser" To: "[JDBC]" References: Subject: Re: ssl connection and webstart Date: Thu, 29 Nov 2007 10:00:11 -0000 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0018_01C8326E.A1950B30" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2869 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 X-Virus-Scanned: ClamAV 0.91.2/4948/Wed Nov 28 20:42:33 2007 on mail.cisx.com X-Virus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.002 tagged_above=0 required=5 tests=AWL=0.001, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200711/91 X-Sequence-Number: 17846 This is a multi-part message in MIME format. ------=_NextPart_000_0018_01C8326E.A1950B30 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit >How can I add the cert via webstart so a "user" can do this? Are you using any standard certificates from the default certificate key store? If the answer is no, then one idea is to create your own certificate key store in your software and add your certificate with java code. You do this as part of creating your SSLSocketFactory, see attached example. And then when your application starts up, the first thing you do before making any connections to postgresql database is: java.security.Security.setProperty("ssl.SocketFactory.provider", "MyOrganisation.MyClientApp.ssl.MySSLSocketFactory"); Hope that helps. Regards Donald Fraser ------=_NextPart_000_0018_01C8326E.A1950B30 Content-Type: application/octet-stream; name="MySSLSocketFactory.java" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="MySSLSocketFactory.java" /**=0A= * MySSLSocketFactory.java=0A= *=0A= * @author Created by Donald Fraser=0A= */=0A= =0A= package MyOrganisation.MyClientApp.ssl;=0A= =0A= =0A= import java.io.InputStream;=0A= import java.io.StringReader;=0A= import java.io.IOException;=0A= import java.net.InetAddress;=0A= import java.net.Socket;=0A= import java.security.cert.CertificateFactory;=0A= import java.security.cert.X509Certificate;=0A= import java.security.cert.CertificateException;=0A= import java.security.cert.CertificateFactory;=0A= import java.security.cert.Certificate;=0A= import java.security.KeyStore;=0A= import javax.net.ssl.KeyManagerFactory;=0A= import javax.net.ssl.SSLSocketFactory;=0A= import javax.net.ssl.SSLContext;=0A= import javax.net.ssl.TrustManagerFactory;=0A= import javax.net.ssl.SSLSocket;=0A= import java.util.ArrayList;=0A= =0A= =0A= /**=0A= * Provides a wrapper to a real SSLSocketFactory delegating all calls=0A= * to the contained instance. The contained instance is created in=0A= * the Constructor and will validate against any server certificate that=0A= * was signed by the Your Companies CA.=0A= */=0A= public final class MySSLSocketFactory extends SSLSocketFactory=0A= {=0A= /** The global ssl socket factory. We only create or attempt to create=0A= * it once **/=0A= private static SSLSocketFactory g_factory;=0A= /** The global error that occured when creating the ssl socket factory = **/=0A= private static IOException g_IOException;=0A= =0A= private static String [] g_saEnabledCiphers;=0A= =0A= /**=0A= * This is an exaustive list of acceptable ciphers.=0A= * Note: CISX server certificates are only signed using RSA, as apposed = to=0A= * DSS.=0A= **/=0A= private static final String [] ACCEPTABLE_CIPHERS =3D {=0A= "SSL_RSA_WITH_RC4_128_SHA",=0A= "TLS_DHE_RSA_WITH_AES_128_CBC_SHA",=0A= "TLS_RSA_WITH_AES_128_CBC_SHA",=0A= "TLS_DHE_RSA_WITH_AES_256_CBC_SHA",=0A= };=0A= =0A= =0A= /**=0A= * Constructor for creating the ssl socket factory.=0A= * We only attempt to create the factory once, if successful it is = stored=0A= * in the global member variableg_factory.=0A= **/=0A= public MySSLSocketFactory()=0A= {=0A= createInstance();=0A= }=0A= =0A= /** Method to create the global ssl factory object=0A= * We only attempt to create the factory once, if successful it is = stored=0A= * in the global member variableg_factory.=0A= **/=0A= public static void createInstance()=0A= {=0A= if (g_factory !=3D null || g_IOException !=3D null)=0A= return;=0A= =0A= CertificateFactory cf =3D null;=0A= try=0A= {=0A= cf =3D CertificateFactory.getInstance("X.509");=0A= }=0A= catch (CertificateException e)=0A= {=0A= g_IOException =3D new IOException("Failed to create X.509 certificate = factory - " + e.getMessage());=0A= g_IOException.initCause(e);=0A= return;=0A= }=0A= =0A= /* This is the public CISX root CA certificate that was used to sign=0A= the server certificates.*/=0A= final StringBuffer sCertificate =3D new StringBuffer(1390);=0A= // Copy your generated certificate - LINE for LINE into the string = buffer=0A= sCertificate.append("-----BEGIN CERTIFICATE-----\n");=0A= sCertificate.append("Your certificate code goes here\n");=0A= sCertificate.append("-----END CERTIFICATE-----\n");=0A= =0A= javax.net.ssl.SSLContext ctx =3D null;=0A= try=0A= {=0A= final Certificate cert =3D cf.generateCertificate(new = StringInputStream(sCertificate.toString()));=0A= =0A= KeyStore ks =3D KeyStore.getInstance("jks");=0A= char [] pass =3D new char[6];=0A= ks.load(null, pass); // initialise keystore with null stream=0A= ks.setCertificateEntry("CA CISX", cert);=0A= =0A= KeyManagerFactory mgr =3D KeyManagerFactory.getInstance("SunX509");=0A= mgr.init(ks, pass);=0A= =0A= TrustManagerFactory tmgr =3D = TrustManagerFactory.getInstance("SunX509");=0A= tmgr.init(ks);=0A= =0A= ctx =3D SSLContext.getInstance("TLS");=0A= ctx.init(mgr.getKeyManagers(), tmgr.getTrustManagers(), null);=0A= // use the following if you don't want to validate the server!=0A= /*=0A= ctx.init( null,=0A= new javax.net.ssl.TrustManager[]=0A= {=0A= =0A= new javax.net.ssl.X509TrustManager()=0A= {=0A= public void checkClientTrusted(X509Certificate[] p1, String p2) = throws CertificateException=0A= {=0A= }=0A= =0A= public void checkServerTrusted(X509Certificate[] p1, String p2) = throws CertificateException=0A= {=0A= }=0A= =0A= public X509Certificate[] getAcceptedIssuers()=0A= {=0A= return new X509Certificate[0];=0A= }=0A= }=0A= },=0A= null);=0A= */=0A= }=0A= catch (Exception e)=0A= {=0A= g_IOException =3D new IOException("Failed to create SSL Context - " + = e.getMessage());=0A= g_IOException.initCause(e);=0A= return;=0A= }=0A= =0A= SSLSocketFactory factory =3D ctx.getSocketFactory();=0A= =0A= // now get the list of available acceptable ciphers to enable=0A= String [] strCiphers =3D factory.getSupportedCipherSuites();=0A= ArrayList alAvailable =3D new ArrayList();=0A= =0A= for (int a =3D 0; a < ACCEPTABLE_CIPHERS.length; a++)=0A= for (int i=3DstrCiphers.length; --i >=3D 0;)=0A= {=0A= // Determine if pattern exists in input=0A= if (ACCEPTABLE_CIPHERS[a].compareTo(strCiphers[i]) =3D=3D 0)=0A= alAvailable.add(strCiphers[i]);=0A= }=0A= if (alAvailable.size() > 0)=0A= g_saEnabledCiphers =3D alAvailable.toArray(new = String[alAvailable.size()]);=0A= else=0A= g_saEnabledCiphers =3D new String[] {"SSL_NULL_WITH_NULL_NULL"};=0A= =0A= g_factory =3D factory;=0A= =0A= /*=0A= //list enabled acceptable ciphers=0A= for (int i=3D0; i < g_saEnabledCiphers.length; i++)=0A= System.out.println(g_saEnabledCiphers[i]);=0A= System.out.println();=0A= */=0A= }=0A= =0A= public Socket createSocket(InetAddress host, int port) throws = IOException=0A= {=0A= if (g_IOException !=3D null)=0A= throw g_IOException;=0A= SSLSocket s =3D (SSLSocket)g_factory.createSocket(host, port);=0A= s.setEnabledCipherSuites(g_saEnabledCiphers);=0A= return s;=0A= }=0A= =0A= public Socket createSocket(String host, int port) throws IOException=0A= {=0A= if (g_IOException !=3D null)=0A= throw g_IOException;=0A= SSLSocket s =3D (SSLSocket)g_factory.createSocket(host, port);=0A= s.setEnabledCipherSuites(g_saEnabledCiphers);=0A= return s;=0A= }=0A= =0A= public Socket createSocket(String host, int port, InetAddress = localHost, int localPort) throws IOException=0A= {=0A= if (g_IOException !=3D null)=0A= throw g_IOException;=0A= SSLSocket s =3D (SSLSocket)g_factory.createSocket(host, port, = localHost, localPort);=0A= s.setEnabledCipherSuites(g_saEnabledCiphers);=0A= return s;=0A= }=0A= =0A= public Socket createSocket(InetAddress address, int port, = InetAddress localAddress, int localPort) throws IOException=0A= {=0A= if (g_IOException !=3D null)=0A= throw g_IOException;=0A= SSLSocket s =3D (SSLSocket)g_factory.createSocket(address, port, = localAddress, localPort);=0A= s.setEnabledCipherSuites(g_saEnabledCiphers);=0A= return s;=0A= }=0A= =0A= public Socket createSocket(Socket socket, String host, int port, = boolean autoClose) throws IOException=0A= {=0A= if (g_IOException !=3D null)=0A= throw g_IOException;=0A= SSLSocket s =3D (SSLSocket)g_factory.createSocket(socket, host, port, = autoClose);=0A= s.setEnabledCipherSuites(g_saEnabledCiphers);=0A= return s;=0A= }=0A= =0A= public String[] getDefaultCipherSuites()=0A= {=0A= if (g_factory =3D=3D null)=0A= return new String[] {};=0A= return g_factory.getDefaultCipherSuites();=0A= }=0A= =0A= public String[] getSupportedCipherSuites()=0A= {=0A= if (g_factory =3D=3D null)=0A= return new String[] {};=0A= return g_factory.getSupportedCipherSuites();=0A= }=0A= =0A= /** Class to represent a StringStream by wrapping StringReader into=0A= * the InputStream class.=0A= **/=0A= private final static class StringInputStream extends InputStream=0A= {=0A= private StringReader m_StringReader;=0A= =0A= public StringInputStream(String s)=0A= {=0A= super();=0A= m_StringReader =3D new StringReader(s);=0A= }=0A= =0A= public int read() throws IOException=0A= {=0A= return m_StringReader.read();=0A= }=0A= }=0A= }=0A= =0A= =0A= ------=_NextPart_000_0018_01C8326E.A1950B30--