Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 1F15463373B for ; Mon, 23 Feb 2009 10:54:26 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 20960-02 for ; Mon, 23 Feb 2009 10:54:21 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp.nmset.info (cop60-2-88-163-134-117.fbx.proxad.net [88.163.134.117]) by mail.postgresql.org (Postfix) with ESMTP id 58465632214 for ; Mon, 23 Feb 2009 10:54:20 -0400 (AST) Received: by smtp.nmset.info (Postfix, from userid 65534) id 93BD098390; Mon, 23 Feb 2009 15:54:16 +0100 (CET) Received: from vos.nmset.info (cop60-2-88-163-134-117.fbx.proxad.net [88.163.134.117]) by smtp.nmset.info (Postfix) with ESMTP id 6564B9838D; Mon, 23 Feb 2009 15:54:13 +0100 (CET) Resent-From: "Saleem EDAH-TALLY" Resent-To: "Saleem Edah-Tally" Resent-Date: Mon, 23 Feb 2009 15:50:42 +0100 Resent-Message-ID: <200902231550.42914.nmset@netcourrier.com> From: "Saleem EDAH-TALLY" To: pgsql-jdbc@postgresql.org Subject: Re: SSL - Providing client certificates Date: Mon, 23 Feb 2009 15:53:02 +0100 User-Agent: KMail/1.11.0 (Linux/2.6.27.7; KDE/4.2.0; x86_64; ; ) References: <200902201030.33352.nmset@netcourrier.com> <87iqn1v1hn.fsf@meuh.mnc.lan> In-Reply-To: <87iqn1v1hn.fsf@meuh.mnc.lan> MIME-Version: 1.0 Content-Disposition: inline Message-Id: <200902231519.29993.nmset@netcourrier.com> Content-Type: multipart/alternative; boundary="Boundary-00=_PhroJDNQIjNXH0g" Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.218 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001, RCVD_IN_SORBS_WEB=1.117, RDNS_DYNAMIC=0.1 X-Spam-Level: * X-Archive-Number: 200902/42 X-Sequence-Number: 19317 --Boundary-00=_PhroJDNQIjNXH0g Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Regarding Kris Jurka's question : > > What I don't understand is how it selects the certificate to send. If > > you have multiple keys in your keystore, how do you indicate which one > > to use? http://java.sun.com/javase/6/docs/api/javax/net/ssl/SSLContext.html#init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], java.security.SecureRandom) states <> Only the first instance of a particular key and/or trust manager implementation type in the array is used... Hence it's not necessary to overload a keystore or a truststore. And who knows how the entries in the keystore are sorted an presented to SSLContext.init. My guess they are sorted in ascending order of the aliases, but it's just a guess. As an add-on to this thread, the following class allows to build a SSLSocketFactory that is not JVM wide but specific to a connection. It can be passed as sslfactory, and a label passed as sslfactoryarg. It may be adapted and included in the jdbc-driver if found useful. ********************************************************************** public class SSLBoth extends javax.net.ssl.SSLSocketFactory{ private String ksFile = null; private String ksType = null; private String ksPwd = null; private String tsFile = null; private String tsType = null; private String tsPwd = null; private javax.net.ssl.SSLSocketFactory ssf = null; public SSLBoth(String label) { setParams(label); javax.net.ssl.KeyManagerFactory kmf = makeKMF(); javax.net.ssl.TrustManagerFactory tmf = makeTMF(); javax.net.ssl.SSLContext sslc = makeSSLContext(kmf.getKeyManagers(), tmf.getTrustManagers()); ssf = sslc.getSocketFactory(); } private void setParams(String label) { ksFile = System.getProperty("KEYSTORE_FILE_" + label); ksType = System.getProperty("KEYSTORE_TYPE_" + label); ksPwd = System.getProperty("KEYSTORE_PWD_" + label); tsFile = System.getProperty("TRUSTSTORE_FILE_" + label); tsType = System.getProperty("TRUSTSTORE_TYPE_" + label); tsPwd = System.getProperty("TRUSTSTORE_PWD_" + label); } private javax.net.ssl.KeyManagerFactory makeKMF() { java.security.KeyStore ks = loadKeyStore(ksFile, ksType, ksPwd); if (ks != null) { try { javax.net.ssl.KeyManagerFactory kmf = javax.net.ssl.KeyManagerFactory.getInstance(javax.net.ssl.KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, ksPwd.toCharArray()); return kmf; } catch (java.security.KeyStoreException ex) { ex.printStackTrace(); } catch (java.security.UnrecoverableKeyException ex) { ex.printStackTrace(); } catch (java.security.NoSuchAlgorithmException ex) { ex.printStackTrace(); } } return null; } private javax.net.ssl.TrustManagerFactory makeTMF() { java.security.KeyStore ts = loadKeyStore(tsFile, tsType, tsPwd); if (ts != null) { try { javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance(javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ts); return tmf; } catch (java.security.KeyStoreException ex) { ex.printStackTrace(); } catch (java.security.NoSuchAlgorithmException ex) { ex.printStackTrace(); } } return null; } private javax.net.ssl.SSLContext makeSSLContext(javax.net.ssl.KeyManager[] km, javax.net.ssl.TrustManager[] tm) { try { javax.net.ssl.SSLContext sslc = javax.net.ssl.SSLContext.getInstance("TLS"); sslc.init(km, tm, new java.security.SecureRandom()); return sslc; } catch (java.security.KeyManagementException ex) { ex.printStackTrace(); } catch (java.security.NoSuchAlgorithmException ex) { ex.printStackTrace(); } return null; } public static java.security.KeyStore loadKeyStore(String ksPath, String type, String pwd) { try { java.security.KeyStore ks = java.security.KeyStore.getInstance(type); java.io.FileInputStream fis = new java.io.FileInputStream(ksPath); ks.load(fis, pwd.toCharArray()); fis.close(); return ks; } catch (java.security.KeyStoreException ex) { ex.printStackTrace(); } catch (java.io.FileNotFoundException ex) { ex.printStackTrace(); } catch (java.io.IOException ex) { ex.printStackTrace(); } catch (java.security.NoSuchAlgorithmException ex) { ex.printStackTrace(); } catch (java.security.cert.CertificateException ex) { ex.printStackTrace(); } return null; } @Override public String[] getDefaultCipherSuites() { return ssf.getDefaultCipherSuites(); } @Override public String[] getSupportedCipherSuites() { return ssf.getSupportedCipherSuites(); } @Override public java.net.Socket createSocket(java.net.Socket arg0, String arg1, int arg2, boolean arg3) throws java.io.IOException { return ssf.createSocket(arg0, arg1, arg2, arg3); } @Override public java.net.Socket createSocket(String arg0, int arg1) throws java.io.IOException, java.net.UnknownHostException { return ssf.createSocket(arg0, arg1); } @Override public java.net.Socket createSocket(String arg0, int arg1, java.net.InetAddress arg2, int arg3) throws java.io.IOException, java.net.UnknownHostException { return ssf.createSocket(arg0, arg1, arg2, arg3); } @Override public java.net.Socket createSocket(java.net.InetAddress arg0, int arg1) throws java.io.IOException { return ssf.createSocket(arg0, arg1); } @Override public java.net.Socket createSocket(java.net.InetAddress arg0, int arg1, java.net.InetAddress arg2, int arg3) throws java.io.IOException { return ssf.createSocket(arg0, arg1, arg2, arg3); } } ********************************************************************** --Boundary-00=_PhroJDNQIjNXH0g Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable p, li { white-space: pre-wrap; }Regarding Kris Jurka's question :



> > What I don't understand is how it selects the certificate to send= =2E If
> > you have multiple keys in your keystore, how do you = indicate which one
> > to use?



http://java.sun.com/javase/6/docs/api/javax/net/ssl/SSLContext.html#init(ja= vax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], java.security.Secur= eRandom)



states



<>
Only the first instance of a particular key and/or trust mana= ger implementation type in the array is used...
</>



Hence it's not necessary to overload a keystore or a truststore.



And who knows how the entries in the keystore are sorted an presented to SS= LContext.init. My guess they are sorted in ascending order of the aliases, = but it's just a guess.



As an add-on to this thread, the following class allows to build a SSLSocke= tFactory that is not JVM wide but specific to a connection. It can be passe= d as sslfactory, and a label passed as sslfactoryarg.
It may be adapte= d and included in the jdbc-driver if found useful.



**********************************************************************
public class SSLBoth extends javax.net.ssl.SSLSocketFactory{
priv= ate String ksFile =3D null;
private String ksType =3D null;
= private String ksPwd =3D null;
private String tsFile =3D null;<= br /> private String tsType =3D null;
private String tsPwd =3D = null;
private javax.net.ssl.SSLSocketFactory ssf =3D null;
=
public SSLBoth(String label) {
setParams(label); javax.net.ssl.KeyManagerFactory kmf =3D makeKMF();
= javax.net.ssl.TrustManagerFactory tmf =3D makeTMF();
javax.ne= t.ssl.SSLContext sslc =3D makeSSLContext(kmf.getKeyManagers(), tmf.getTrust= Managers());
ssf =3D sslc.getSocketFactory();
}
=
private void setParams(String label) {
ksFile = =3D System.getProperty("KEYSTORE_FILE_" + label);
ksType =3D S= ystem.getProperty("KEYSTORE_TYPE_" + label);
ksPwd =3D System.= getProperty("KEYSTORE_PWD_" + label);
tsFile =3D System.getPro= perty("TRUSTSTORE_FILE_" + label);
tsType =3D System.getProper= ty("TRUSTSTORE_TYPE_" + label);
tsPwd =3D System.getProperty("= TRUSTSTORE_PWD_" + label);
}
private javax.net.ssl.KeyMan= agerFactory makeKMF() {
java.security.KeyStore ks =3D loadKeyS= tore(ksFile, ksType, ksPwd);
if (ks !=3D null) {
= try {
javax.net.ssl.KeyManagerFactory kmf =3D java= x.net.ssl.KeyManagerFactory.getInstance(javax.net.ssl.KeyManagerFactory.get= DefaultAlgorithm());
kmf.init(ks, ksPwd.toCharArray())= ;
return kmf;
} catch (java.security.= KeyStoreException ex) {
ex.printStackTrace();
= } catch (java.security.UnrecoverableKeyException ex) {
= ex.printStackTrace();
} catch (java.security.NoS= uchAlgorithmException ex) {
ex.printStackTrace();
}
}
return null;
}
= private javax.net.ssl.TrustManagerFactory makeTMF() {
java.se= curity.KeyStore ts =3D loadKeyStore(tsFile, tsType, tsPwd);
if= (ts !=3D null) {
try {
javax.net.ssl= =2ETrustManagerFactory tmf =3D javax.net.ssl.TrustManagerFactory.getInstanc= e(javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm());
= tmf.init(ts);
return tmf;
} cat= ch (java.security.KeyStoreException ex) {
ex.printStac= kTrace();
} catch (java.security.NoSuchAlgorithmException = ex) {
ex.printStackTrace();
}
= }
return null;
}
private javax.net.ssl= =2ESSLContext makeSSLContext(javax.net.ssl.KeyManager[] km, javax.net.ssl.T= rustManager[] tm) {
try {
javax.net.ssl.SSLCo= ntext sslc =3D javax.net.ssl.SSLContext.getInstance("TLS");
= sslc.init(km, tm, new java.security.SecureRandom());
ret= urn sslc;
} catch (java.security.KeyManagementException ex) {<= br /> ex.printStackTrace();
} catch (java.security.= NoSuchAlgorithmException ex) {
ex.printStackTrace();
= }
return null;
}
public static java.= security.KeyStore loadKeyStore(String ksPath, String type, String pwd) { try {
java.security.KeyStore ks =3D java.securi= ty.KeyStore.getInstance(type);
java.io.FileInputStream fis= =3D new java.io.FileInputStream(ksPath);
ks.load(fis, pwd= =2EtoCharArray());
fis.close();
return ks= ;
}

catch (java.security.KeySto= reException ex) {
ex.printStackTrace();
}
catch (java.io.FileNotFoundException ex) {
ex.pr= intStackTrace();
}
catch (java.io.IOException ex)= {
ex.printStackTrace();
}
catch= (java.security.NoSuchAlgorithmException ex) {
ex.printSta= ckTrace();
}
catch (java.security.cert.Certificat= eException ex) {
ex.printStackTrace();
}
return null;
}



@Override
public String[] getDefaultCipherSuites() {
= return ssf.getDefaultCipherSuites();
}



@Override
public String[] getSupportedCipherSuites() {
= return ssf.getSupportedCipherSuites();
}



@Override
public java.net.Socket createSocket(java.net.Socket = arg0, String arg1, int arg2, boolean arg3) throws java.io.IOException {
return ssf.createSocket(arg0, arg1, arg2, arg3);
}
=


@Override
public java.net.Socket createSocket(String arg0, int= arg1) throws java.io.IOException, java.net.UnknownHostException {
= return ssf.createSocket(arg0, arg1);
}



@Override
public java.net.Socket createSocket(String arg0, int= arg1, java.net.InetAddress arg2, int arg3) throws java.io.IOException, jav= a.net.UnknownHostException {
return ssf.createSocket(arg0, arg= 1, arg2, arg3);
}



@Override
public java.net.Socket createSocket(java.net.InetAdd= ress arg0, int arg1) throws java.io.IOException {
return ssf.c= reateSocket(arg0, arg1);
}



@Override
public java.net.Socket createSocket(java.net.InetAdd= ress arg0, int arg1, java.net.InetAddress arg2, int arg3) throws java.io.IO= Exception {
return ssf.createSocket(arg0, arg1, arg2, arg3); }



}



**********************************************************************





--Boundary-00=_PhroJDNQIjNXH0g--