9a9096
commit 2c522795166c930741a9cecca76797bf48cb1634
9a9096
Author: Mark Thomas <markt@apache.org>
9a9096
Date:   Mon Jun 18 19:45:13 2018 +0000
9a9096
9a9096
    Enable host name verification for secure WebSocket client connections by default.
9a9096
    
9a9096
    git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk@1833760 13f79535-47bb-0310-9956-ffa450edef68
9a9096
9a9096
diff --git a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
9a9096
index ac5122a89c..b526022958 100644
9a9096
--- java/org/apache/tomcat/websocket/WsWebSocketContainer.java
9a9096
+++ java/org/apache/tomcat/websocket/WsWebSocketContainer.java
9a9096
@@ -53,6 +53,7 @@ import java.util.concurrent.TimeoutException;
9a9096
 import javax.net.ssl.SSLContext;
9a9096
 import javax.net.ssl.SSLEngine;
9a9096
 import javax.net.ssl.SSLException;
9a9096
+import javax.net.ssl.SSLParameters;
9a9096
 import javax.net.ssl.TrustManagerFactory;
9a9096
 import javax.websocket.ClientEndpoint;
9a9096
 import javax.websocket.ClientEndpointConfig;
9a9096
@@ -363,7 +364,7 @@ public class WsWebSocketContainer
9a9096
             // proxy CONNECT, need to use TLS from this point on so wrap the
9a9096
             // original AsynchronousSocketChannel
9a9096
             SSLEngine sslEngine = createSSLEngine(
9a9096
-                    clientEndpointConfiguration.getUserProperties());
9a9096
+                    clientEndpointConfiguration.getUserProperties(), host, port);
9a9096
             channel = new AsyncChannelWrapperSecure(socketChannel, sslEngine);
9a9096
         } else if (channel == null) {
9a9096
             // Only need to wrap as this point if it wasn't wrapped to process a
9a9096
@@ -931,7 +932,7 @@ public class WsWebSocketContainer
9a9096
     }
9a9096
 
9a9096
 
9a9096
-    private SSLEngine createSSLEngine(Map<String,Object> userProperties)
9a9096
+    private SSLEngine createSSLEngine(Map<String,Object> userProperties, String host, int port)
9a9096
             throws DeploymentException {
9a9096
 
9a9096
         try {
9a9096
@@ -979,7 +980,7 @@ public class WsWebSocketContainer
9a9096
                 }
9a9096
             }
9a9096
 
9a9096
-            SSLEngine engine = sslContext.createSSLEngine();
9a9096
+            SSLEngine engine = sslContext.createSSLEngine(host, port);
9a9096
 
9a9096
             String sslProtocolsValue =
9a9096
                     (String) userProperties.get(SSL_PROTOCOLS_PROPERTY);
9a9096
@@ -989,6 +990,14 @@ public class WsWebSocketContainer
9a9096
 
9a9096
             engine.setUseClientMode(true);
9a9096
 
9a9096
+            // Enable host verification
9a9096
+            // Start with current settings (returns a copy)
9a9096
+            SSLParameters sslParams = engine.getSSLParameters();
9a9096
+            // Use HTTPS since WebSocket starts over HTTP(S)
9a9096
+            sslParams.setEndpointIdentificationAlgorithm("HTTPS");
9a9096
+            // Write the parameters back
9a9096
+            engine.setSSLParameters(sslParams);
9a9096
+
9a9096
             return engine;
9a9096
         } catch (Exception e) {
9a9096
             throw new DeploymentException(sm.getString(
9a9096
diff -up webapps/docs/changelog.xml.orig webapps/docs/changelog.xml
9a9096
--- webapps/docs/changelog.xml.orig	2019-03-01 09:38:54.202817893 -0500
9a9096
+++ webapps/docs/changelog.xml	2019-03-01 09:39:29.413704569 -0500
9a9096
@@ -84,6 +84,14 @@
9a9096
       </fix>
9a9096
     </changelog>
9a9096
   </subsection>
9a9096
+  <subsection name="WebSocket">
9a9096
+    <changelog>
9a9096
+      <fix>
9a9096
+        Enable host name verification when using TLS with the WebSocket client.
9a9096
+        (markt)
9a9096
+      </fix>
9a9096
+    </changelog>
9a9096
+  </subsection>
9a9096
 </section>
9a9096
 <section name="Tomcat 7.0.76-8 (csutherl)">
9a9096
   <subsection name="Catalina">
9a9096
diff -up webapps/docs/web-socket-howto.xml.orig webapps/docs/web-socket-howto.xml
9a9096
--- webapps/docs/web-socket-howto.xml.orig	2019-03-01 12:46:34.515904379 -0500
9a9096
+++ webapps/docs/web-socket-howto.xml	2019-03-01 12:51:00.900175575 -0500
9a9096
@@ -148,10 +148,21 @@ implement its own timeout mechanism to h
9a9096
      
  • org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD
  • 9a9096
        
    9a9096
        

    The default truststore password is changeit.

    9a9096
    -   

    If the org.apache.tomcat.websocket.SSL_CONTEXT property is

    9a9096
    -      set then the org.apache.tomcat.websocket.SSL_TRUSTSTORE and
    9a9096
    -      org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD properties
    9a9096
    -      will be ignored.

    9a9096
    +
    9a9096
    +

    If the org.apache.tomcat.websocket.SSL_CONTEXT property is

    9a9096
    +   set then the org.apache.tomcat.websocket.SSL_TRUSTSTORE and
    9a9096
    +   org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD properties
    9a9096
    +   will be ignored.

    9a9096
    +
    9a9096
    +

    For secure server end points, host name verification is enabled by default.

    9a9096
    +   To bypass this verification (not recommended), it is necessary to provide a
    9a9096
    +   custom SSLContext via the
    9a9096
    +   org.apache.tomcat.websocket.SSL_CONTEXT user property. The
    9a9096
    +   custom SSLContext must be configured with a custom
    9a9096
    +   TrustManager that extends
    9a9096
    +   javax.net.ssl.X509ExtendedTrustManager. The desired verification
    9a9096
    +   (or lack of verification) can then be controlled by appropriate
    9a9096
    +   implementations of the individual abstract methods.

    9a9096
     </section>
    9a9096
     
    9a9096
     <section name="Deprecated proprietary API">