Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public final class SocketUtils {

Expand All @@ -53,25 +54,7 @@ public static Socket createSocket(final InetSocketAddress address, final SocketC
throw new IllegalArgumentException("Configuration may not be null.");
}

final Socket socket;

final SSLContext sslContext = config.getSslContext();
if (sslContext == null) {
socket = new Socket();
} else {
/* This would ideally be refactored to a shared create method but Socket and ServerSocket
* do not share a common interface; Socket is effectively "client socket" in this context
*/
Socket tempSocket = sslContext.getSocketFactory().createSocket();
final SSLSocket sslSocket = (SSLSocket) tempSocket;
// Set Preferred TLS Protocol Versions
sslSocket.setEnabledProtocols(TlsPlatform.getPreferredProtocols().toArray(new String[0]));
final SSLParameters sslParameters = sslSocket.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm(TLS_ENDPOINT_IDENTIFICATION_ALGORITHM);
sslSocket.setSSLParameters(sslParameters);

socket = sslSocket;
}
final Socket socket = new Socket();

if (config.getSocketTimeout() != null) {
socket.setSoTimeout(config.getSocketTimeout());
Expand Down Expand Up @@ -116,7 +99,25 @@ public static Socket createSocket(final InetSocketAddress address, final SocketC
throw e;
}

return socket;
final Socket connectedSocket;

final SSLContext sslContext = config.getSslContext();
if (sslContext == null) {
connectedSocket = socket;
} else {
// Create SSLSocket after successful connection to support hostname verification
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, address.getHostName(), address.getPort(), true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we close the connected socket if creating or configuring the SSLSocket throws, like the connect block does with closeQuietly?


sslSocket.setEnabledProtocols(TlsPlatform.getPreferredProtocols().toArray(new String[0]));
final SSLParameters sslParameters = sslSocket.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm(TLS_ENDPOINT_IDENTIFICATION_ALGORITHM);
sslSocket.setSSLParameters(sslParameters);

connectedSocket = sslSocket;
}

return connectedSocket;
}

/**
Expand Down
Loading