Skip to content

Commit 2835bb4

Browse files
committed
Enable host name verification for secure WebSocket client connections by
default. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1833757 13f79535-47bb-0310-9956-ffa450edef68
1 parent 43e1c5d commit 2835bb4

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

java/org/apache/tomcat/websocket/WsWebSocketContainer.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import javax.net.ssl.SSLContext;
5353
import javax.net.ssl.SSLEngine;
5454
import javax.net.ssl.SSLException;
55+
import javax.net.ssl.SSLParameters;
5556
import javax.net.ssl.TrustManagerFactory;
5657
import javax.websocket.ClientEndpoint;
5758
import javax.websocket.ClientEndpointConfig;
@@ -328,7 +329,7 @@ private Session connectToServerRecursive(Endpoint endpoint,
328329
// Regardless of whether a non-secure wrapper was created for a
329330
// proxy CONNECT, need to use TLS from this point on so wrap the
330331
// original AsynchronousSocketChannel
331-
SSLEngine sslEngine = createSSLEngine(userProperties);
332+
SSLEngine sslEngine = createSSLEngine(userProperties, host, port);
332333
channel = new AsyncChannelWrapperSecure(socketChannel, sslEngine);
333334
} else if (channel == null) {
334335
// Only need to wrap as this point if it wasn't wrapped to process a
@@ -866,7 +867,7 @@ private String readLine(ByteBuffer response) {
866867
}
867868

868869

869-
private SSLEngine createSSLEngine(Map<String,Object> userProperties)
870+
private SSLEngine createSSLEngine(Map<String,Object> userProperties, String host, int port)
870871
throws DeploymentException {
871872

872873
try {
@@ -904,7 +905,7 @@ private SSLEngine createSSLEngine(Map<String,Object> userProperties)
904905
}
905906
}
906907

907-
SSLEngine engine = sslContext.createSSLEngine();
908+
SSLEngine engine = sslContext.createSSLEngine(host, port);
908909

909910
String sslProtocolsValue =
910911
(String) userProperties.get(Constants.SSL_PROTOCOLS_PROPERTY);
@@ -914,6 +915,14 @@ private SSLEngine createSSLEngine(Map<String,Object> userProperties)
914915

915916
engine.setUseClientMode(true);
916917

918+
// Enable host verification
919+
// Start with current settings (returns a copy)
920+
SSLParameters sslParams = engine.getSSLParameters();
921+
// Use HTTPS since WebSocket starts over HTTP(S)
922+
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
923+
// Write the parameters back
924+
engine.setSSLParameters(sslParams);
925+
917926
return engine;
918927
} catch (Exception e) {
919928
throw new DeploymentException(sm.getString(

webapps/docs/changelog.xml

+4
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@
277277
Improve the handling of exceptions during TLS handshakes for the
278278
WebSocket client. (markt)
279279
</fix>
280+
<fix>
281+
Enable host name verification when using TLS with the WebSocket client.
282+
(markt)
283+
</fix>
280284
</changelog>
281285
</subsection>
282286
<subsection name="Web applications">

webapps/docs/web-socket-howto.xml

+15-4
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,21 @@
110110
<li><code>org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD</code></li>
111111
</ul>
112112
<p>The default truststore password is <code>changeit</code>.</p>
113-
<p>If the <code>org.apache.tomcat.websocket.SSL_CONTEXT</code> property is
114-
set then the <code>org.apache.tomcat.websocket.SSL_TRUSTSTORE</code> and
115-
<code>org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD</code> properties
116-
will be ignored.</p>
113+
114+
<p>If the <code>org.apache.tomcat.websocket.SSL_CONTEXT</code> property is
115+
set then the <code>org.apache.tomcat.websocket.SSL_TRUSTSTORE</code> and
116+
<code>org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD</code> properties
117+
will be ignored.</p>
118+
119+
<p>For secure server end points, host name verification is enabled by default.
120+
To bypass this verification (not recommended), it is necessary to provide a
121+
custom <code>SSLContext</code> via the
122+
<code>org.apache.tomcat.websocket.SSL_CONTEXT</code> user property. The
123+
custom <code>SSLContext</code> must be configured with a custom
124+
<code>TrustManager</code> that extends
125+
<code>javax.net.ssl.X509ExtendedTrustManager</code>. The desired verification
126+
(or lack of verification) can then be controlled by appropriate
127+
implementations of the individual abstract methods.</p>
117128

118129
<p>When using the WebSocket client to connect to server endpoints, the number of
119130
HTTP redirects that the client will follow is controlled by the

0 commit comments

Comments
 (0)