Skip to content

Commit 2c52279

Browse files
committed
Enable host name verification for secure WebSocket client connections by default.
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk@1833760 13f79535-47bb-0310-9956-ffa450edef68
1 parent 471f387 commit 2c52279

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import javax.net.ssl.SSLContext;
5454
import javax.net.ssl.SSLEngine;
5555
import javax.net.ssl.SSLException;
56+
import javax.net.ssl.SSLParameters;
5657
import javax.net.ssl.TrustManagerFactory;
5758
import javax.websocket.ClientEndpoint;
5859
import javax.websocket.ClientEndpointConfig;
@@ -369,7 +370,7 @@ private Session connectToServerRecursive(Endpoint endpoint,
369370
// Regardless of whether a non-secure wrapper was created for a
370371
// proxy CONNECT, need to use TLS from this point on so wrap the
371372
// original AsynchronousSocketChannel
372-
SSLEngine sslEngine = createSSLEngine(userProperties);
373+
SSLEngine sslEngine = createSSLEngine(userProperties, host, port);
373374
channel = new AsyncChannelWrapperSecure(socketChannel, sslEngine);
374375
} else if (channel == null) {
375376
// Only need to wrap as this point if it wasn't wrapped to process a
@@ -931,7 +932,7 @@ private String readLine(ByteBuffer response) {
931932
}
932933

933934

934-
private SSLEngine createSSLEngine(Map<String,Object> userProperties)
935+
private SSLEngine createSSLEngine(Map<String,Object> userProperties, String host, int port)
935936
throws DeploymentException {
936937

937938
try {
@@ -979,7 +980,7 @@ private SSLEngine createSSLEngine(Map<String,Object> userProperties)
979980
}
980981
}
981982

982-
SSLEngine engine = sslContext.createSSLEngine();
983+
SSLEngine engine = sslContext.createSSLEngine(host, port);
983984

984985
String sslProtocolsValue =
985986
(String) userProperties.get(SSL_PROTOCOLS_PROPERTY);
@@ -989,6 +990,14 @@ private SSLEngine createSSLEngine(Map<String,Object> userProperties)
989990

990991
engine.setUseClientMode(true);
991992

993+
// Enable host verification
994+
// Start with current settings (returns a copy)
995+
SSLParameters sslParams = engine.getSSLParameters();
996+
// Use HTTPS since WebSocket starts over HTTP(S)
997+
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
998+
// Write the parameters back
999+
engine.setSSLParameters(sslParams);
1000+
9921001
return engine;
9931002
} catch (Exception e) {
9941003
throw new DeploymentException(sm.getString(

webapps/docs/changelog.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@
164164
<code>DecodeException</code> instead of throwing
165165
<code>ArrayIndexOutOfBoundsException</code>. (kfujino)
166166
</fix>
167+
<fix>
168+
Enable host name verification when using TLS with the WebSocket client.
169+
(markt)
170+
</fix>
167171
</changelog>
168172
</subsection>
169173
<subsection name="Web applications">

webapps/docs/web-socket-howto.xml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,21 @@ implement its own timeout mechanism to handle these cases.</p>
148148
<li><code>org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD</code></li>
149149
</ul>
150150
<p>The default truststore password is <code>changeit</code>.</p>
151-
<p>If the <code>org.apache.tomcat.websocket.SSL_CONTEXT</code> property is
152-
set then the <code>org.apache.tomcat.websocket.SSL_TRUSTSTORE</code> and
153-
<code>org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD</code> properties
154-
will be ignored.</p>
151+
152+
<p>If the <code>org.apache.tomcat.websocket.SSL_CONTEXT</code> property is
153+
set then the <code>org.apache.tomcat.websocket.SSL_TRUSTSTORE</code> and
154+
<code>org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD</code> properties
155+
will be ignored.</p>
156+
157+
<p>For secure server end points, host name verification is enabled by default.
158+
To bypass this verification (not recommended), it is necessary to provide a
159+
custom <code>SSLContext</code> via the
160+
<code>org.apache.tomcat.websocket.SSL_CONTEXT</code> user property. The
161+
custom <code>SSLContext</code> must be configured with a custom
162+
<code>TrustManager</code> that extends
163+
<code>javax.net.ssl.X509ExtendedTrustManager</code>. The desired verification
164+
(or lack of verification) can then be controlled by appropriate
165+
implementations of the individual abstract methods.</p>
155166

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

0 commit comments

Comments
 (0)