2020import com .mongodb .MongoSocketReadException ;
2121import com .mongodb .MongoSocketWriteException ;
2222import com .mongodb .ServerAddress ;
23+ import com .mongodb .internal .connection .SslHelper ;
2324
2425import javax .net .ssl .SSLContext ;
26+ import javax .net .ssl .SSLParameters ;
27+ import javax .net .ssl .SSLSocket ;
2528import java .io .IOException ;
2629import java .io .InputStream ;
2730import java .io .OutputStream ;
@@ -42,14 +45,15 @@ class KeyManagementService {
4245 }
4346
4447 public InputStream stream (final String host , final ByteBuffer message ) {
45- Socket socket ;
48+ SSLSocket socket ;
4649 try {
47- socket = sslContext .getSocketFactory ().createSocket ();
50+ socket = ( SSLSocket ) sslContext .getSocketFactory ().createSocket ();
4851 } catch (IOException e ) {
4952 throw new MongoSocketOpenException ("Exception opening connection to Key Management Service" , new ServerAddress (host , port ), e );
5053 }
5154
5255 try {
56+ enableHostNameVerification (socket );
5357 socket .setSoTimeout (timeoutMillis );
5458 socket .connect (new InetSocketAddress (InetAddress .getByName (host ), port ), timeoutMillis );
5559 } catch (IOException e ) {
@@ -79,6 +83,15 @@ public InputStream stream(final String host, final ByteBuffer message) {
7983 }
8084 }
8185
86+ private void enableHostNameVerification (final SSLSocket socket ) {
87+ SSLParameters sslParameters = socket .getSSLParameters ();
88+ if (sslParameters == null ) {
89+ sslParameters = new SSLParameters ();
90+ }
91+ SslHelper .enableHostNameVerification (sslParameters );
92+ socket .setSSLParameters (sslParameters );
93+ }
94+
8295 public int getPort () {
8396 return port ;
8497 }
0 commit comments