Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Parasoft Jtest 2020.2

The Programs must use the javax.net.ssl.SSLSocket class must be used instead of rather than the java.net.Socket class when transferring sensitive data over insecure communication channels. The class SSLSockets SSLSocket provides security protocols such as Secure Sockets Layer/Transport Layer Security (SSL/TLS) to ensure that the channel is not vulnerable to eavesdropping and malicious tampering.unmigrated-wiki-markup

The principal protections included in {{SSLSockets}} that are not provided by the {{Socket}} class are \[[Java API|AA. Bibliography#API 06]\]in SSLSocket that are not provided by the Socket class are [API 2014]:

  • Integrity Protection. : SSL protects against modification of messages by an active wiretapper.
  • Authentication. : In most modes, SSL provides peer authentication. Servers are usually authenticated, and clients may be authenticated as requested by servers.
  • Confidentiality (Privacy Protectionprivacy protection). : In most modes, SSL encrypts data being sent between client and server. This protects the confidentiality of data , so that passive wiretappers won't see sensitive data such as financial information or personal information of many kinds.

Wiki MarkupIt is also important to use SSL for secure remote method invocation (RMI) communications because RMI depends on object serialization , and serialized data must be safeguarded in transit. Gong et al. \[Gong, Ellison, and Dageforde [Gong 2003|AA. Bibliography#Gong 03]\] describe how to secure RMI communications using {{SSLSockets}}using SSLSocket.

Note that this rule makes no lacks any assumptions about the integrity of the data being sent down a socket. For information about ensuring data integrity, see SER02-J. Sign then seal sensitive objects before sending them outside a trust boundary.

Noncompliant Code Example

This noncompliant code example shows the use of regular sockets for a server application that does not fails to protect sensitive information in transit. The insecure code for the corresponding client application follows the server's code.

Code Block
bgColor#FFCCCC
  
// Exception handling has been omitted for the sake of brevity
class EchoServer { 
  public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    try {
      serverSocket = new ServerSocket(100079999); 
      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); 
      }
    } finally {
      if (serverSocket != null) {
        try {
          serverSocket.close();
        } catch (IOException x) {
          // Handle error
        }
      }
    }
  }
}

class EchoClient {
  public static void main(String[] args) 
                          throws UnknownHostException, IOException {
    Socket socket = null;
    try {
      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());
      } 

    } finally {
      if (socket != null) {
        try {
          socket.close();
        } catch (IOException x) {
          // Handle error
        }
      }
    }
  }
}

Note that the sockets are properly closed in accordance with ERR05-J. Do not let checked exceptions escape from a finally block. While merely printing close exceptions is frowned upon, the exceptions may be suppressed as per ERR00-EX0 of ERR00-J. Do not suppress or ignore checked exceptions.

Compliant Solution

This compliant solution makes use of SSLSockets uses SSLSocket to protect packets using the SSL/TLS security protocols.:

Code Block
bgColor#CCCCFF

// Exception handling has been omitted for the sake of brevity
class EchoServer {
  public static void main(String[] args) throws IOException {
    SSLServerSocket sslServerSocket = null;
    try {
      SSLServerSocketFactory sslServerSocketFactory =
          (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
      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); 
      } 

    } finally {
      if (sslServerSocket != null) {
        try {
          sslServerSocket.close();
        } catch (IOException x) {
          // Handle error
        }
      }
    }
  }
}

class EchoClient {
  public static void main(String[] args) throws IOException {
    SSLSocket sslSocket = null;
    try {
      SSLSocketFactory sslSocketFactory =
          (SSLSocketFactory) SSLSocketFactory.getDefault();
      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());
      }
    } finally {
      if (sslSocket !=  } finallynull) {
        try {
          sslSocket.close();
        } catch (IOException x) {
          // Handle error
        }
      }
    }
  }
}

Programs that use SSLSocket Note that a program that makes use of SSLSockets will block indefinitely if it tries they attempt to connect to a port that is not using SSL. Similarly, a program that does not use SSLSockets SSLSocket will block when trying attempting to establish a connection through a port that uses does use SSL.

Note that SSLSocket does not validate host names, so providing an arbitrary host name to an SSLSocket is still vulnerable to a man-in-the-middle attack. Host names should be validated separately. The HttpsURLConnection class validates host names and is a suitable solution for secure web sockets.

Exceptions

MSC00-J-EX0: Because of the mechanisms that SSLSockets provide SSLSocket provides to ensure the secure transfer of packets, significant performance overhead may result. Regular sockets are sufficient ifunder the following circumstances:

  • The the data being sent over the socket is not sensitive.
  • the The data is sensitive , but properly encrypted . See (see SER02-J. Sign then seal sensitive objects before sending them outside a trust boundary for more information).
  • the The network path of the socket never crosses a trust boundary. This could happen ifwhen, for example, the two endpoings endpoints of the socket are within a the same local network and the entire network is trusted.

Risk Assessment

Using Use of plain sockets instead of SSLSockets means that the data's confidentiality and integrity is not guaranteedfails to provide any guarantee of the confidentiality and integrity of data transmitted over those sockets.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MSC00-J

medium

Medium

likely

Likely

high

High

P6

L2

Automated Detection

The general case of automated detection appears to be infeasible because determining which specific data may be passed through the socket is not statically computable. An approach that introduces a custom API for passing sensitive data via secure sockets may be feasible. User tagging of sensitive data would be is a necessary requirement for such an approach.

ToolVersionCheckerDescription
Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
SECURITY.WSC.USCUse the SSL-enabled version of classes when possible

Related Guidelines

MITRE CWE

CWE-311,

"

Failure to Encrypt Sensitive Data

"

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c763a999-840d-4b1c-96ed-4a62fd448a92"><ac:plain-text-body><!

[

CDATA[

[[API 2006

AA. Bibliography#API 06]]

 

]]></ac:plain-text-body></ac:structured-macro>

API 2014]

Class Socket

[Gong 2003]

Section

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bc2d6166-6d9c-4e9b-b1db-6249c969ec6e"><ac:plain-text-body><![CDATA[

[[Gong 2003

AA. Bibliography#Gong 03]]

11.3.3, "Securing RMI Communications

"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id=

"

1f9be9bc-b28b-4545-a460-e767db8806ed"><ac:plain-text-body><![CDATA[

[

[

Ware 2008

AA. Bibliography#Ware 08

]

]

 

]]></ac:plain-text-body></ac:structured-macro>



...

Image Added Image Added 49. Miscellaneous (MSC)      49. Miscellaneous (MSC)