0ab684
From 1dfc377ae3b174b043d3f0ed36de57b0296b34d0 Mon Sep 17 00:00:00 2001
0ab684
From: rhe <rhe@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
0ab684
Date: Wed, 8 Aug 2018 14:13:55 +0000
0ab684
Subject: [PATCH] net/http, net/ftp: fix session resumption with TLS 1.3
0ab684
0ab684
When TLS 1.3 is in use, the session ticket may not have been sent yet
0ab684
even though a handshake has finished. Also, the ticket could change if
0ab684
multiple session ticket messages are sent by the server. Use
0ab684
SSLContext#session_new_cb instead of calling SSLSocket#session
0ab684
immediately after a handshake. This way also works with earlier protocol
0ab684
versions.
0ab684
0ab684
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64234 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
0ab684
---
0ab684
 lib/net/ftp.rb              |  5 ++++-
0ab684
 lib/net/http.rb             |  7 +++++--
0ab684
 test/net/http/test_https.rb | 35 ++++++++++-------------------------
0ab684
 3 files changed, 19 insertions(+), 28 deletions(-)
0ab684
0ab684
diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb
0ab684
index c3ee47ef4d36..9902f9dc657a 100644
0ab684
--- a/lib/net/ftp.rb
0ab684
+++ b/lib/net/ftp.rb
0ab684
@@ -230,6 +230,10 @@ def initialize(host = nil, user_or_options = {}, passwd = nil, acct = nil)
0ab684
         if defined?(VerifyCallbackProc)
0ab684
           @ssl_context.verify_callback = VerifyCallbackProc
0ab684
         end
0ab684
+        @ssl_context.session_cache_mode =
0ab684
+          OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT |
0ab684
+          OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE
0ab684
+        @ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess }
0ab684
         @ssl_session = nil
0ab684
         if options[:private_data_connection].nil?
0ab684
           @private_data_connection = true
0ab684
@@ -349,7 +353,6 @@ def start_tls_session(sock)
0ab684
       if @ssl_context.verify_mode != VERIFY_NONE
0ab684
         ssl_sock.post_connection_check(@host)
0ab684
       end
0ab684
-      @ssl_session = ssl_sock.session
0ab684
       return ssl_sock
0ab684
     end
0ab684
     private :start_tls_session
0ab684
diff --git a/lib/net/http.rb b/lib/net/http.rb
0ab684
index 281b15cedff0..683a884f5dbe 100644
0ab684
--- a/lib/net/http.rb
0ab684
+++ b/lib/net/http.rb
0ab684
@@ -969,6 +969,10 @@ def connect
0ab684
         end
0ab684
         @ssl_context = OpenSSL::SSL::SSLContext.new
0ab684
         @ssl_context.set_params(ssl_parameters)
0ab684
+        @ssl_context.session_cache_mode =
0ab684
+          OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT |
0ab684
+          OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE
0ab684
+        @ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess }
0ab684
         D "starting SSL for #{conn_address}:#{conn_port}..."
0ab684
         s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
0ab684
         s.sync_close = true
0ab684
@@ -976,13 +980,12 @@ def connect
0ab684
         s.hostname = @address if s.respond_to? :hostname=
0ab684
         if @ssl_session and
0ab684
            Process.clock_gettime(Process::CLOCK_REALTIME) < @ssl_session.time.to_f + @ssl_session.timeout
0ab684
-          s.session = @ssl_session if @ssl_session
0ab684
+          s.session = @ssl_session
0ab684
         end
0ab684
         ssl_socket_connect(s, @open_timeout)
0ab684
         if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
0ab684
           s.post_connection_check(@address)
0ab684
         end
0ab684
-        @ssl_session = s.session
0ab684
         D "SSL established"
0ab684
       end
0ab684
       @socket = BufferedIO.new(s, read_timeout: @read_timeout,
0ab684
diff --git a/test/net/http/test_https.rb b/test/net/http/test_https.rb
0ab684
index 8004d5c5f29f..a5182a1fe9db 100644
0ab684
--- a/test/net/http/test_https.rb
0ab684
+++ b/test/net/http/test_https.rb
0ab684
@@ -71,20 +71,11 @@ def test_session_reuse
0ab684
     http.get("/")
0ab684
     http.finish
0ab684
 
0ab684
-    http.start
0ab684
-    http.get("/")
0ab684
-    http.finish # three times due to possible bug in OpenSSL 0.9.8
0ab684
-
0ab684
-    sid = http.instance_variable_get(:@ssl_session).id
0ab684
-
0ab684
     http.start
0ab684
     http.get("/")
0ab684
 
0ab684
     socket = http.instance_variable_get(:@socket).io
0ab684
-
0ab684
-    assert socket.session_reused?
0ab684
-
0ab684
-    assert_equal sid, http.instance_variable_get(:@ssl_session).id
0ab684
+    assert_equal true, socket.session_reused?
0ab684
 
0ab684
     http.finish
0ab684
   rescue SystemCallError
0ab684
@@ -101,16 +92,12 @@ def test_session_reuse_but_expire
0ab684
     http.get("/")
0ab684
     http.finish
0ab684
 
0ab684
-    sid = http.instance_variable_get(:@ssl_session).id
0ab684
-
0ab684
     http.start
0ab684
     http.get("/")
0ab684
 
0ab684
     socket = http.instance_variable_get(:@socket).io
0ab684
     assert_equal false, socket.session_reused?
0ab684
 
0ab684
-    assert_not_equal sid, http.instance_variable_get(:@ssl_session).id
0ab684
-
0ab684
     http.finish
0ab684
   rescue SystemCallError
0ab684
     skip $!
0ab684
@@ -160,15 +147,16 @@ def test_certificate_verify_failure
0ab684
   end
0ab684
 
0ab684
   def test_identity_verify_failure
0ab684
+    # the certificate's subject has CN=localhost
0ab684
     http = Net::HTTP.new("127.0.0.1", config("port"))
0ab684
     http.use_ssl = true
0ab684
-    http.verify_callback = Proc.new do |preverify_ok, store_ctx|
0ab684
-      true
0ab684
-    end
0ab684
+    http.cert_store = TEST_STORE
0ab684
+    @log_tester = lambda {|_| }
0ab684
     ex = assert_raise(OpenSSL::SSL::SSLError){
0ab684
       http.request_get("/") {|res| }
0ab684
     }
0ab684
-    assert_match(/hostname \"127.0.0.1\" does not match/, ex.message)
0ab684
+    re_msg = /certificate verify failed|hostname \"127.0.0.1\" does not match/
0ab684
+    assert_match(re_msg, ex.message)
0ab684
   end
0ab684
 
0ab684
   def test_timeout_during_SSL_handshake
0ab684
@@ -193,16 +181,13 @@ def test_timeout_during_SSL_handshake
0ab684
   end
0ab684
 
0ab684
   def test_min_version
0ab684
-    http = Net::HTTP.new("127.0.0.1", config("port"))
0ab684
+    http = Net::HTTP.new("localhost", config("port"))
0ab684
     http.use_ssl = true
0ab684
     http.min_version = :TLS1
0ab684
-    http.verify_callback = Proc.new do |preverify_ok, store_ctx|
0ab684
-      true
0ab684
-    end
0ab684
-    ex = assert_raise(OpenSSL::SSL::SSLError){
0ab684
-      http.request_get("/") {|res| }
0ab684
+    http.cert_store = TEST_STORE
0ab684
+    http.request_get("/") {|res|
0ab684
+      assert_equal($test_net_http_data, res.body)
0ab684
     }
0ab684
-    assert_match(/hostname \"127.0.0.1\" does not match/, ex.message)
0ab684
   end
0ab684
 
0ab684
   def test_max_version