Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java SSL broken in OpenJDK on Ubuntu?

I am on a fresh install of Ubuntu having just installed OpenJDK:

OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode) on Ubuntu 64 bit 10.10

Not sure if this is relevant, but I'm running it from within VMWare Fusion.

The following line:

javax.net.SSLContext.getDefault(); // same as getInstance("Default")

throws the following exception:

java.net.SocketException: java.security.NoSuchAlgorithmException: Default SSLContext not available

My colleagues and I have tried this on several machines, all fresh installs of Ubuntu, and keep getting this. I was advised to try getInstance("TLSv1"), but this threw the same error. Seems like something really fundamental to not be working so I figure we must be doing something wrong.

like image 842
Francisco Ryan Tolmasky I Avatar asked Jan 09 '11 20:01

Francisco Ryan Tolmasky I


2 Answers

guido's answer pointed me in the right direction. It's just a matter of doing:

sudo apt-get install libbcprov-java
like image 120
Paul Baumgart Avatar answered Sep 29 '22 17:09

Paul Baumgart


openjdk shipped with ubuntu may be missing a JCE provider; download the bouncycastle crypto api from http://www.bouncycastle.org/ (its an open source project implementing JCE) and put it in your project classpath.

Then in your class refer to the following sample code:

static {
    Security.addProvider( new BouncyCastleProvider() );
}

public SSLSocket getSSLSocket() {

    // Load the Keystore
    KeyStore ks = KeyStore.getInstance(keystoreType);
    ks.load(new FileInputStream(this.keyStorePath),this.keyStorePass.toCharArray());

    // Get a KeyManager and initialize it 
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");
    kmf.init(ks, this.keyStorePass.toCharArray());

    // Get a TrustManagerFactory and init with KeyStore
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509");
    tmf.init(ks);

    // Get the SSLContext to help create SSLSocketFactory
    SSLContext sslc = SSLContext.getInstance("TLS");
    sslc.init(kmf.getKeyManagers(), null, null);

    // Get SSLSocketFactory and get a SSLSocket
    SSLSocketFactory sslsf = sslc.getSocketFactory();
    SSLSocket socket = (SSLSocket) sslsf.createSocket(host, port);
    return socket;
}
like image 21
guido Avatar answered Sep 29 '22 17:09

guido