Skip to content

Commit 0e9f2af

Browse files
committed
Fix SNI not being set on SSLSocket when using connect timeout
Under BCJSSE (Bouncy Castle JSSE provider), creating an unconnected SSLSocket via createSocket() and then calling connect(InetSocketAddress) does not automatically propagate the hostname for SNI. This causes TLS handshake failures against endpoints that require SNI for certificate selection (e.g. AWS OpenSearch managed domains behind load balancers). The fix explicitly sets SNI server names via SSLParameters before connecting, which works correctly with both SunJSSE and BCJSSE. Closes opensearch-project#765 Signed-off-by: Sotaro Hikita <bering1814@gmail.com>
1 parent 525183e commit 0e9f2af

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

mr/src/main/java/org/opensearch/hadoop/rest/commonshttp/SSLSocketFactory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@
4141
import java.security.cert.CertificateException;
4242
import java.security.cert.X509Certificate;
4343

44+
import java.util.Collections;
45+
4446
import javax.net.SocketFactory;
4547
import javax.net.ssl.KeyManager;
4648
import javax.net.ssl.KeyManagerFactory;
49+
import javax.net.ssl.SNIHostName;
4750
import javax.net.ssl.SSLContext;
51+
import javax.net.ssl.SSLParameters;
52+
import javax.net.ssl.SSLSocket;
4853
import javax.net.ssl.TrustManager;
4954
import javax.net.ssl.TrustManagerFactory;
5055
import javax.net.ssl.X509TrustManager;
@@ -142,6 +147,12 @@ public Socket createSocket(String host, int port, InetAddress localAddress, int
142147
}
143148
else {
144149
Socket socket = socketfactory.createSocket();
150+
if (socket instanceof SSLSocket) {
151+
SSLSocket sslSocket = (SSLSocket) socket;
152+
SSLParameters sslParams = sslSocket.getSSLParameters();
153+
sslParams.setServerNames(Collections.singletonList(new SNIHostName(host)));
154+
sslSocket.setSSLParameters(sslParams);
155+
}
145156
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
146157
SocketAddress remoteaddr = new InetSocketAddress(host, port);
147158
socket.bind(localaddr);

0 commit comments

Comments
 (0)