As suggested by \[[Ware|AA. Java References#Ware 08]\], instances from the class javax.net.ssl.SSLSocket should be created instead of normal Sockets when transferring objects over communication channels. SSLSockets provide a layer of security protocols such as SSL or TLS.

According to \[[Java API|AA. Java References#API 06]\] , the principal protections included in SSLSockets that are not provided by the Socket class are:

Noncompliant code example

This example shows the use of regular Sockets for a server application. Using this kind of Sockets does not protect sensitive information while being in transit.

 
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*; 

public class EchoServer { 
    public static void main(String[] args) { 
        try { 
	    ServerSocket serverSocket = new ServerSocket(10007); 
	    Socket socket = serverSocket.accept();
			
	    PrintWriter out = new PrintWriter( socket.getOutputStream(),true); 
	    BufferedReader in = new BufferedReader(new InputStreamReader( socket.getInputStream())); 

	    String inputLine; 

	    while ((inputLine = in.readLine()) != null) { 
	        System.out.println (inputLine); 
	        out.println(inputLine); 
	    } 

	    out.close(); 
  	    in.close(); 
	    socket.close();  
	}
	catch (Exception e) { 
	    /*Handle exception*/
	}   
    }
}

Compliant solution

This is a better solution to the previous example, making use of SSLSockets. By doing so, packets are protected with SSL and TLS security protocols.

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import java.io.*;

public class EchoServer {
    public static void main(String[] args) {
        try {
            SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(9999);
            SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();

            PrintWriter out = new PrintWriter( sslsocket.getOutputStream(),true);
            BufferedReader in = new BufferedReader(new InputStreamReader( sslsocket.getInputStream()));
            
            String inputLine; 
            
   	    while ((inputLine = in.readLine()) != null) { 
		System.out.println (inputLine); 
		out.println(inputLine); 
	    } 
			
	    out.close(); 
	    in.close(); 
	    sslsocket.close(); 
        } 
	catch (Exception e) { 
	    /*Handle exception*/
	} 
    }
}

Noncompliant code example

This is a similar insecure code for the client application.

 
import java.io.*;
import java.net.*;

public class EchoClient {
    public static void main(String[] args) {
	try {
 	    Socket socket = new Socket("localhost", 9999);
						
	    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
	    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

	    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
			
	    String userInput;

	    while ((userInput = stdIn.readLine()) != null) {
		out.println(userInput);
		System.out.println(in.readLine());
	    }

	    out.close();
	    in.close();
	    stdIn.close();
	    socket.close();
	}
	catch (Exception e) { 
	    /*Handle exception*/
	} 
    }
}

Compliant solution

This is the client application making use of SSLSockets.

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;

public class EchoClient {
    public static void main(String[] args) {
        try {
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999);

  	    PrintWriter out = new PrintWriter(sslsocket.getOutputStream(), true);
	    BufferedReader in = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
			
	    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            
	    String userInput;

	    while ((userInput = stdIn.readLine()) != null) {
		out.println(userInput);
		System.out.println(in.readLine());
	    }

	    out.close();
	    in.close();
	    stdIn.close();
	    sslsocket.close();		
        } 
	catch (Exception e) { 
	    /*Handle exception*/
	} 
    }
}

Risk assesment

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

 

medium

unlikely

low

P6

L2

References

\[[API 06|AA. Java References#API 06]\] 
\[[Ware 08|AA. Java References#Ware 08]\]