Pages

Friday, 27 June 2014

Secure Copy of Files over a non Standard Port

This is a continuation of my blog on Transfer Files Between two Systems Over User Defined Ports. Felt the need to use SSL for the transfer. This is the first step in securing the transfer. As long as we ensure that the certificate on the server is not available to others, our transmission is secure. Will provide the changes for securing the transmission with a password later in another blog.

Setup for execution is available here.

Git available with the source code here.

Step 1: Create the certificate and the Keystore
Key can be generated using keystore. Execute the below command. Script to execute this on windows is available here.


keytool -genkey -alias filecopy -keyalg RSA  -keystore filecopykeystore.jks -storepass password -keypass password -validity 1000 -keysize 1024
Step 2: Code On Server Side to Accept Encrypted Socket


SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory
     .getDefault();
Step 3: Code On Client to initiated an SSL Connection



SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault()
    .createSocket();
socket.setSoTimeout(timeout);
socket.connect(new InetSocketAddress(host, port));
try {
 socket.startHandshake();
 return socket;
} catch (SSLException s) {
 socket.close();
 logger.info("Error in SSL Socket. Hence loading the socket to keystore::");
}

This is all it takes to establish a secure tcp connection. Refer the getSocket method in FileCopySocketConnectionUtil to fetch the public certificate of terver and add into the clientKeystore for establishing the SSL.

No comments:

Post a Comment