SSL (Server)Socket

J

JScoobyCed

Hi,

I have a working simple client/server set of classes to connect on
top of SSL. The sample code is:

<server>
System.setProperties("javax.net.ssl.keyStore","myKS");
System.setProperties("javax.net.ssl.keyStorePassword","passwd");

ServerSocketFactory ssFact = SSLServerSocketFactory.getDefault();
ServerSocket srv = ssocketFactory.createServerSocket(8443);

Socket socket = ssocket.accept();

//...
</server>

<client>
System.setProperties("javax.net.ssl.trustStore","myTS");
System.setProperties("javax.net.ssl.trustStorePassword","passwd");
SocketFactory ssFact= SSLSocketFactory.getDefault();
Socket socket = socketFactory.createSocket("localhost", 8443);
// ...
</client>

When I run from within eclipse, with the "myTS" keystore in the
classpath, everything is fine. Now I want to package my client to a jar.
I just can't find where to put the keystore file: in the JAR it fails to
be found, in the folder where the JAR is it fails.
I read that the resolution of the path to find the keystore is under the
java folders (some subfolders of the "lib" folder) or "user.home". I
know it is possible to successfully put the file anywhere as it works
from Eclipse.
I also read there is an alternative to the way of connecting through SSL

<pseudo-code>
InputStream stream = getResourceAsStream(keyStoreFile);
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(stream, "passwd".toCharArray());

KeyManagerFactory kmf = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
kmf.init( ks, "password".toCharArray());

SSLContext ctx = SSLContext.getInstance( "SSLv3" );
ctx.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
SSLSocketFactory ssf = ctx.getSocketFactory();
Socket so = ssf.createSocket();
</pseudo-code>

Is there other way t create an SSL connection? Which one is the
recommanded? It would be used to send files from client to server and
get back files to client.

Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top