20
20
import com .mongodb .MongoSocketReadException ;
21
21
import com .mongodb .MongoSocketWriteException ;
22
22
import com .mongodb .ServerAddress ;
23
+ import com .mongodb .internal .connection .SslHelper ;
23
24
24
25
import javax .net .ssl .SSLContext ;
26
+ import javax .net .ssl .SSLParameters ;
27
+ import javax .net .ssl .SSLSocket ;
25
28
import java .io .IOException ;
26
29
import java .io .InputStream ;
27
30
import java .io .OutputStream ;
@@ -42,14 +45,15 @@ class KeyManagementService {
42
45
}
43
46
44
47
public InputStream stream (final String host , final ByteBuffer message ) {
45
- Socket socket ;
48
+ SSLSocket socket ;
46
49
try {
47
- socket = sslContext .getSocketFactory ().createSocket ();
50
+ socket = ( SSLSocket ) sslContext .getSocketFactory ().createSocket ();
48
51
} catch (IOException e ) {
49
52
throw new MongoSocketOpenException ("Exception opening connection to Key Management Service" , new ServerAddress (host , port ), e );
50
53
}
51
54
52
55
try {
56
+ enableHostNameVerification (socket );
53
57
socket .setSoTimeout (timeoutMillis );
54
58
socket .connect (new InetSocketAddress (InetAddress .getByName (host ), port ), timeoutMillis );
55
59
} catch (IOException e ) {
@@ -79,6 +83,15 @@ public InputStream stream(final String host, final ByteBuffer message) {
79
83
}
80
84
}
81
85
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
+
82
95
public int getPort () {
83
96
return port ;
84
97
}
0 commit comments