KeyStore, HttpClient and HTTPS: Can someone explain this code to me?

I am trying to understand what is going on in this code .

KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());        
FileInputStream instream = new FileInputStream(new File("my.keystore")); 
try {
    trustStore.load(instream, "nopassword".toCharArray());
} finally {
    instream.close();
}

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);

      

My questions:

trustStore.load(instream, "nopassword".toCharArray());

does what exactly? Reading the documentation load()

will load the KeyStore data from the input stream (which is just the empty file we just created) using an arbitrary "nopassword". Why not just load it with null

an InputStream as the parameter and an empty string as the password field?

And what happens when this empty KeyStore is passed to the SSLSocketFactory constructor? What is the result of such an operation?

Or is this just an example where in a real application you would need to put a link to an existing keystore file / password?

+2


a source to share


2 answers


Or is this just an example where in a real application you would need to put a link to an existing keystore file / password?

It really is. No file "my.keystore"

redistributable in HttpClient 4.0.1 binaries or source distributions. To do this, you will create an actual key store. You can use keytool or Portecle .



This example shows how to use a different trust store than the one the JVM uses by default ($ JAVA_HOME / jre / lib / security / cacerts) for this instance DefaultHttpClient

. This is useful when SSL site uses a certificate signed by its own private certificate authority . An SSL connection will only be established when the server's signing certificate is recognized. The Wikipedia entry for TLS is a decent introduction if you are not familiar with the concept.

+1


a source


This example shows how to upload your own trust store. For this example to work, you need to have the file "my.keystore" in your current directory and the password for the keystore to be "nopassword".



Note that it new File("my.keystore")

does not necessarily create a new file. It just creates a File object pointing to the path.

+2


a source







All Articles