Wednesday 9 October 2013

SSL in java , Java scoket, SSL Socket, Adding SSL to java socket



How to add SSL to Socket in Java

// For trust Store adding to client Side
System.setProperty("javax.net.ssl.trustStore", requestTO.getKeystore());
System.setProperty("javax.net.ssl.trustStorePassword", requestTO.getStorepass());


SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
sssocket = (SSLSocket) sslsocketfactory.createSocket(requestTO.getIpaddress(), new Integer(requestTO.getPort()));
sssocket.startHandshake();
sssocket.setSoTimeout(timeout);
out = new PrintWriter(sssocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sssocket.getInputStream()));



// For adding on Server Side usngf Apache MINA
/*
* STEP 1: Create a FILE OBJECT
*/
//FILE_PATH = path of Keystore

FilePermission.checkReadFilePermission(FILE_PATH, "JKS");
File clientJKS = new File(FILE_PATH);
/*
* STEP2: Upload file in KeyStoreFactory
*/
final KeyStoreFactory keyStoreFactory = new KeyStoreFactory();
keyStoreFactory.setDataFile(clientJKS);
keyStoreFactory.setPassword(PASSWORD);

/*
* STEP3:GET JKS OBJECT and upload into SSLCONTEXT_FACTORY
*/
final KeyStore keyStore = keyStoreFactory.newInstance();
final SslContextFactory contextFactory = new SslContextFactory();
contextFactory.setKeyManagerFactoryKeyStore(keyStore);
contextFactory.setKeyManagerFactoryKeyStorePassword(PASSWORD);

/*
* STEP 4:RETURN SSLCONTEXT
*/
return contextFactory.newInstance();



If you want to add the SSL Debug mode then
-Djavax.net.debug=ssl:record as vm argument

References:

http://www.javanna.net/2011/07/common-ssl-issues-in-java/

http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#WhySSL





No comments:

Post a Comment

Please comment here