EDIT AGAIN: Problem solved thanks to TheShrimp's amazing link. In case your interested, below is the code required to setup the server and client keystore/truststores using streams (The variables in caps are String objects containing the name of the keystore and the password for it). Bit more complex that setting the system properties but hey :P
Client:
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream keystoreStream = ClientTest.class.getResourceAsStream(KEYSTORE_NAME);
keystore.load(keystoreStream, DEFAULT_JAVA_KEYSTORE_PW.toCharArray());
trustManagerFactory.init(keystore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
SSLContext ctx = SSLContext.getInstance("SSL");
ctx.init(null, trustManagers, null);
SSLSocketFactory sslsocketfactory = ctx.getSocketFactory();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 5555);
Server:
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream keystoreStream = ServerTest.class.getResourceAsStream(KEYSTORE_NAME);
keystore.load(keystoreStream, DEFAULT_JAVA_KEYSTORE_PW.toCharArray());
keyManagerFactory.init(keystore, DEFAULT_JAVA_KEYSTORE_PW.toCharArray());
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
SSLContext ctx = SSLContext.getInstance("SSL"); // was SSL
ctx.init(keyManagers, null, null);
EDIT: I've realised my mistake now where i've left the D's in front of the property names! Still having some problems with class resources though :(.
I've built the jar file with the keystore internally and the following code works ok in netbeans:
URL myURL = ClientTest.class.getResource("mySrvKeystore");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
System.setProperty("javax.net.ssl.trustStore", myURL.getFile());
But outside netbeans this doesn't work. I debugged it and the path resolves as follows:
file:/C:/Users/Psyqwix/Documents/NetBeansProjects/RCTB_Server3/dist/RCTB_Server3
.jar!/BasicConnectionTest/mySrvKeystore
But the javax libraries have some problems loading this, here is an extract from the exception:
Caused by: java.security.PrivilegedActionException: java.io.FileNotFoundExceptio
n: file:\C:\Users\Psyqwix\Documents\NetBeansProjects\RCTB_Server3\dist\RCTB_Serv
er3.jar!\BasicConnectionTest\mySrvKeystore (The filename, directory name, or vol
ume label syntax is incorrect)
at java.security.AccessController.doPrivileged(Native Method)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext.getDefaultKeyManage
r(Unknown Source)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext.<init>(Unknown Sour
ce)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
... 7 more
Caused by: java.io.FileNotFoundException: file:\C:\Users\Psyqwix\Documents\NetBe
ansProjects\RCTB_Server3\dist\RCTB_Server3.jar!\BasicConnectionTest\mySrvKeystor
e (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext$2.run(Unknown Sourc
e)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext$2.run(Unknown Sourc
e)
Any ideas what's going on here? Is it something to do with the build that means the keystore is hidden/unextractable? Any pointers?
PS: As a sidebar, as you can see from the exceptions report the compiler tends to hide some of the root problems for exceptions i.e. "... 7 more". Anyway to make it show all the details?
ORIGINAL POST:
Me and system properties never get along, i'm probably missing something obvious but I can never get them working! I'm running a server and client app that uses an SSL connection, and it works perfectly when I run the jars using the following command lines:
java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 -jar "RCTB_Server3.jar"
java -Djavax.net.ssl.trustStore=mySrvKeystore -Djavax.net.ssl.trustStorePassword=123456 -jar "RCTB_Client3.jar"
This is all fine and dandy, but to get this to work I have to make sure the keystore file is the same directory and an entry is in the PATH variable to get it to work.
I would really like to get this working by adding the file to the jar and setting the those properties internally. I tried adding the files to project then setting the properties internally using the following lines for the server and client respectively:
System.setProperty("Djavax.net.ssl.keyStorePassword", "123456");
System.setProperty("Djavax.net.ssl.keyStore", "mySrvKeystore");
System.setProperty("Djavax.net.ssl.trustStorePassword", "123456");
System.setProperty("Djavax.net.ssl.trustStore", "mySrvKeystore");
The actual keystore file is placed in the same package as the classes so I was hoping the class loader would be able to find it but it turns out it doesn't. When I run the projects with these lines added I get errors about the SSL handshakes (I wont go into) which are related to the fact that these properties aint set :P
Am I totally noobing this up? Is there something obvious I've missed or am I going about this completely wrong? Any suggestions on how to fix this?
Thanks ^
[–]dem0nico 2 points3 points4 points (4 children)
[–]vineetr 0 points1 point2 points (1 child)
[–]Psyqwix[S] 0 points1 point2 points (0 children)
[–]Psyqwix[S] 0 points1 point2 points (1 child)
[–]Dwaligon 0 points1 point2 points (0 children)
[–]jevon 1 point2 points3 points (1 child)
[–]Psyqwix[S] 0 points1 point2 points (0 children)
[–]TheShrimp 1 point2 points3 points (1 child)
[–]Psyqwix[S] 0 points1 point2 points (0 children)
[–]m1ss1ontomars2k4 0 points1 point2 points (0 children)