Search code examples
javaweb-servicessslapache-axis

Create a trusted ssl handshake with server using Java


I need help creating a trusted connection with a web service using Axis2 and org.jsslutils.sslcontext.X509SSLContextFactory. I have a generated client cert from the web service's admin console (client.p12). I also have grabbed the server's public cert by going to the endpoint uri and exporting it to a file using the browser (pubserver.cer). I converted the client.p12 to a jks using keytool (mywsks.jks). I also imported pubserver.cer into the keystore. I'm very new to ssl. Do I need to import these certs into .../jre/lib/security/cacert or cacerts or trusted.libraries or can I just reference mywsks.jks? How do I set up my code for the server to trust me using X509SSLContextFactory? It appears to need a keyStore and a trustStore which I'm deriving from this example:

X509SSLContextFactory sslContextFactory = new X509SSLContextFactory( 
    keyStore, keyStorePassword, trustStore); 

I'm currently using this to create the keyStore and trustStore:

KeyStore keyStore = KeyStore.getInstance("JKS");     
String keyStoreFile = "mywsks.jks"; 
InputStream keyInput = new FileInputStream(keyStoreFile);
String keyStorePassword = "thepassword"; 
keyStore.load(keyInput, keyStorePassword.toCharArray());
keyInput.close();

String trustStoreFile = "/path/to/cacert"; 
KeyStore trustStore = KeyStore.getInstance("JKS");
keyInput = new FileInputStream(trustStoreFile);
String trustStorePassword = "thepassword";
trustStore.load(keyInput, trustStorePassword.toCharArray());
keyInput.close();

I'm getting the following error:

org.apache.axis2.AxisFault: sun.security.validator.ValidatorException: No trusted certificate found

Solution

    1. Add your client certificate that is used for authenticate into web service into a keyStore(client.p12).
    2. Add the server's public key(pubserver.cer) into your trustStore. It can be /jre/lib/security/cacert

    Could you post what issue you are facing ? Because according to me, it is a confusing and a big topic to discuss..

    This example will help you implementing this..

    http://code.google.com/p/jsslutils/wiki/ApacheHttpClientUsage

    And finally you could make an URL connection..