javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate

I am trying to connect to a server process using SSL socket. However, during handshake it fails with the exception.Any ideas why would this happen? I know that the server process works, because we have a TCL client that connects to it without any problem (tls::socket -cipher EDH-RSA-DES-CBC3-SHA).

The java code and the exception is below.

publicclass SecureChannel{

privatestatic SecureChannel instance=null;

private SSLContext ctx=null;

privatestaticfinal String keystore ="keystore";

privatestaticfinal String[] cipherSuites ={

"SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA"

};

publicstatic SecureChannel getInstance()throws Exception{

if(instance==null)

instance=new SecureChannel();

return instance;

}

private SecureChannel()throws Exception{

InputStream is=new FileInputStream(keystore);

TrustManagerFactory tmf=TrustManagerFactory.getInstance(

TrustManagerFactory.getDefaultAlgorithm());

KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());

ks.load(is,null);

is.close();

tmf.init(ks);

ctx = SSLContext.getInstance("TLS");//"SSLv3");

ctx.init(null, tmf.getTrustManagers(),null);

}

public SSLSocket getSocket(String ip,int port)throws Exception{

SSLSocketFactory sf = ctx.getSocketFactory();

String ciphers[]=sf.getSupportedCipherSuites();

for(int i=0; i<ciphers.length; i++){

System.out.println(ciphers[i]);

}

SSLSocket sslSock = (SSLSocket)sf.createSocket(ip, port);

sslSock.setEnabledCipherSuites(cipherSuites);

return sslSock;

}

}

javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair

at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:166)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1476)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1443)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1426)

at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:86)

at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:411)

at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:453)

at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:183)

at java.io.InputStreamReader.read(InputStreamReader.java:167)

at java.io.BufferedReader.fill(BufferedReader.java:136)

at java.io.BufferedReader.read(BufferedReader.java:157)

at clitest.CliParse.makeList(CliParse.java:30)

at clitest.CliParse.parseInput(CliParse.java:12)

at clitest.CliTest.connect(CliTest.java:45)

at clitest.CliTest.main(CliTest.java:160)

Caused by: java.lang.RuntimeException: Could not generate DH keypair

at com.sun.net.ssl.internal.ssl.DHKeyExchange.generateKeyPair(DHKeyExchange.java:137)

at com.sun.net.ssl.internal.ssl.ClientHandshaker.getDHephemeral(ClientHandshaker.java:370)

at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverKeyExchange(ClientHandshaker.java:385)

at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:125)

at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:495)

at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:433)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:815)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1025)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:675)

at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)

... 10 more

Caused by: java.security.InvalidAlgorithmParameterException: Exponent value must be less than (modulus value -1)

at com.sun.crypto.provider.DHKeyPairGenerator.initialize(DashoA12275)

at java.security.KeyPairGenerator$Delegate.initialize(KeyPairGenerator.java:609)

at java.security.KeyPairGenerator.initialize(KeyPairGenerator.java:351)

at com.sun.net.ssl.internal.ssl.DHKeyExchange.generateKeyPair(DHKeyExchange.java:123)

... 19 more

>

[5734 byte] By [michaelmigala] at [2007-10-3 9:58:47]
# 1
Why are you cutting down the cipherSuites to just one? Not recommended: let TLS/SSL figure it out for itself depending on what the other end supports.
ejpa at 2007-7-15 5:17:11 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 2
Even if I don't cut down ciphers to just one, I get the same error.
michaelmigala at 2007-7-15 5:17:11 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 3
OK. The client has received an invalid Diffie-Hellman keyexchange message from the server, containing an invalid modulus or base.What implementation of TLS/SSL is in use at the server?
ejpa at 2007-7-15 5:17:11 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 4
the server is using TLS 1.5.0 with binaries built against OpenSSL 0.9.7c.
michaelmigala at 2007-7-15 5:17:11 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 5
Well you're going to need to investigate what happened at the server end, and if it's openSSL it's no longer a Java question.
ejpa at 2007-7-15 5:17:11 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 6
Is there any way I can turn off the authentication then and always trust the certificate from the server, regardless of what DH keypair is returned?
michaelmigala at 2007-7-15 5:17:11 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 7
I am interested in encrypted communication, but not necessary in authentication of certificate. So, I created my own X509TrustManager and I made it to not validate certificate. However, I still get the same exception.
michaelmigala at 2007-7-15 5:17:11 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 8
This is definetely a Java problem. I decided to try and downgrade to Java 1.4.2 (I was using 1.5), and everything works fine now. However, I need to use Java 1.5 in my real application, so is this a bug in 1.5?
michaelmigala at 2007-7-15 5:17:11 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 9

OK, After trying many ways to fix this, the rela fix came in the form of the latest JDK 5.0 Update 9. As soon as I upgraded to that, my SSL connection works just fine (don't even need to have TrustManager that skips certs authentication). I guess, there was something in the JDK 5.0 Update 5 that didn't work well, but it looks like it got fixed. Thank you all for help.

michaelmigala at 2007-7-15 5:17:11 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...