Skip to content

Commit dd4336e

Browse files
NIFI-16622 Adjusted SocketUtils to create SSLSocket after Socket connect (#11959)
- Resolves potential hostname verification issues with separate creation and connect handling
1 parent 819bdfa commit dd4336e

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

  • nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket

‎nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketUtils.java‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.net.ssl.SSLParameters;
3232
import javax.net.ssl.SSLServerSocket;
3333
import javax.net.ssl.SSLSocket;
34+
import javax.net.ssl.SSLSocketFactory;
3435

3536
public final class SocketUtils {
3637

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

56-
final Socket socket;
57-
58-
final SSLContext sslContext = config.getSslContext();
59-
if (sslContext == null) {
60-
socket = new Socket();
61-
} else {
62-
/* This would ideally be refactored to a shared create method but Socket and ServerSocket
63-
* do not share a common interface; Socket is effectively "client socket" in this context
64-
*/
65-
Socket tempSocket = sslContext.getSocketFactory().createSocket();
66-
final SSLSocket sslSocket = (SSLSocket) tempSocket;
67-
// Set Preferred TLS Protocol Versions
68-
sslSocket.setEnabledProtocols(TlsPlatform.getPreferredProtocols().toArray(new String[0]));
69-
final SSLParameters sslParameters = sslSocket.getSSLParameters();
70-
sslParameters.setEndpointIdentificationAlgorithm(TLS_ENDPOINT_IDENTIFICATION_ALGORITHM);
71-
sslSocket.setSSLParameters(sslParameters);
72-
73-
socket = sslSocket;
74-
}
57+
final Socket socket = new Socket();
7558

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

119-
return socket;
102+
final Socket connectedSocket;
103+
104+
final SSLContext sslContext = config.getSslContext();
105+
if (sslContext == null) {
106+
connectedSocket = socket;
107+
} else {
108+
// Create SSLSocket after successful connection to support hostname verification
109+
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
110+
final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, address.getHostName(), address.getPort(), true);
111+
112+
sslSocket.setEnabledProtocols(TlsPlatform.getPreferredProtocols().toArray(new String[0]));
113+
final SSLParameters sslParameters = sslSocket.getSSLParameters();
114+
sslParameters.setEndpointIdentificationAlgorithm(TLS_ENDPOINT_IDENTIFICATION_ALGORITHM);
115+
sslSocket.setSSLParameters(sslParameters);
116+
117+
connectedSocket = sslSocket;
118+
}
119+
120+
return connectedSocket;
120121
}
121122

122123
/**

0 commit comments

Comments
 (0)