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.
guido's answer pointed me in the right direction. It's just a matter of doing:
sudo apt-get install libbcprov-java
                        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;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With