2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-12-20 11:21:22 -05:00
|
|
|
require "test/unit"
|
2021-04-28 11:55:55 -04:00
|
|
|
require_relative "utils"
|
2007-12-20 11:21:22 -05:00
|
|
|
begin
|
|
|
|
require 'net/https'
|
|
|
|
rescue LoadError
|
|
|
|
# should skip this test
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestNetHTTPS < Test::Unit::TestCase
|
|
|
|
include TestNetHTTPUtils
|
|
|
|
|
2020-02-21 07:21:14 -05:00
|
|
|
def self.read_fixture(key)
|
2016-11-29 10:48:45 -05:00
|
|
|
File.read(File.expand_path("../fixtures/#{key}", __dir__))
|
|
|
|
end
|
|
|
|
|
2021-04-28 11:55:55 -04:00
|
|
|
HOST = 'localhost'
|
|
|
|
HOST_IP = '127.0.0.1'
|
2020-02-21 07:21:14 -05:00
|
|
|
CA_CERT = OpenSSL::X509::Certificate.new(read_fixture("cacert.pem"))
|
|
|
|
SERVER_KEY = OpenSSL::PKey.read(read_fixture("server.key"))
|
|
|
|
SERVER_CERT = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
|
|
|
|
DHPARAMS = OpenSSL::PKey::DH.new(read_fixture("dhparams.pem"))
|
2016-11-29 10:48:45 -05:00
|
|
|
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
|
2007-12-20 11:21:22 -05:00
|
|
|
|
|
|
|
CONFIG = {
|
2021-04-28 11:55:55 -04:00
|
|
|
'host' => HOST,
|
2007-12-20 11:21:22 -05:00
|
|
|
'proxy_host' => nil,
|
|
|
|
'proxy_port' => nil,
|
|
|
|
'ssl_enable' => true,
|
2016-11-29 10:48:45 -05:00
|
|
|
'ssl_certificate' => SERVER_CERT,
|
|
|
|
'ssl_private_key' => SERVER_KEY,
|
|
|
|
'ssl_tmp_dh_callback' => proc { DHPARAMS },
|
2007-12-20 11:21:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
def test_get
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST, config("port"))
|
2007-12-20 11:21:22 -05:00
|
|
|
http.use_ssl = true
|
2016-11-29 10:48:45 -05:00
|
|
|
http.cert_store = TEST_STORE
|
|
|
|
certs = []
|
2007-12-20 11:21:22 -05:00
|
|
|
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
|
2016-11-29 10:48:45 -05:00
|
|
|
certs << store_ctx.current_cert
|
|
|
|
preverify_ok
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
http.request_get("/") {|res|
|
|
|
|
assert_equal($test_net_http_data, res.body)
|
|
|
|
}
|
2020-09-24 06:20:17 -04:00
|
|
|
# TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
|
2021-04-28 11:55:55 -04:00
|
|
|
certs.zip([CA_CERT, SERVER_CERT][-certs.size..-1]) do |actual, expected|
|
2020-09-24 06:39:51 -04:00
|
|
|
assert_equal(expected.to_der, actual.to_der)
|
2020-09-24 06:20:17 -04:00
|
|
|
end
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
|
2019-12-09 06:19:11 -05:00
|
|
|
def test_get_SNI
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST, config("port"))
|
2019-12-09 06:19:11 -05:00
|
|
|
http.ipaddr = config('host')
|
|
|
|
http.use_ssl = true
|
|
|
|
http.cert_store = TEST_STORE
|
|
|
|
certs = []
|
|
|
|
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
|
|
|
|
certs << store_ctx.current_cert
|
|
|
|
preverify_ok
|
|
|
|
end
|
|
|
|
http.request_get("/") {|res|
|
|
|
|
assert_equal($test_net_http_data, res.body)
|
|
|
|
}
|
2020-09-24 06:20:17 -04:00
|
|
|
# TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
|
2021-04-28 11:55:55 -04:00
|
|
|
certs.zip([CA_CERT, SERVER_CERT][-certs.size..-1]) do |actual, expected|
|
2020-09-24 06:39:51 -04:00
|
|
|
assert_equal(expected.to_der, actual.to_der)
|
2020-09-24 06:20:17 -04:00
|
|
|
end
|
2019-12-09 06:19:11 -05:00
|
|
|
end
|
|
|
|
|
2019-12-10 05:06:13 -05:00
|
|
|
def test_get_SNI_proxy
|
2021-04-28 11:55:55 -04:00
|
|
|
TCPServer.open(HOST_IP, 0) {|serv|
|
2019-12-10 05:06:13 -05:00
|
|
|
_, port, _, _ = serv.addr
|
|
|
|
client_thread = Thread.new {
|
2021-04-28 11:55:55 -04:00
|
|
|
proxy = Net::HTTP.Proxy(HOST_IP, port, 'user', 'password')
|
2019-12-10 05:06:13 -05:00
|
|
|
http = proxy.new("foo.example.org", 8000)
|
|
|
|
http.ipaddr = "192.0.2.1"
|
|
|
|
http.use_ssl = true
|
|
|
|
http.cert_store = TEST_STORE
|
|
|
|
certs = []
|
|
|
|
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
|
|
|
|
certs << store_ctx.current_cert
|
|
|
|
preverify_ok
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
http.start
|
|
|
|
rescue EOFError
|
|
|
|
end
|
|
|
|
}
|
|
|
|
server_thread = Thread.new {
|
|
|
|
sock = serv.accept
|
|
|
|
begin
|
|
|
|
proxy_request = sock.gets("\r\n\r\n")
|
|
|
|
assert_equal(
|
|
|
|
"CONNECT 192.0.2.1:8000 HTTP/1.1\r\n" +
|
|
|
|
"Host: foo.example.org:8000\r\n" +
|
|
|
|
"Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" +
|
|
|
|
"\r\n",
|
|
|
|
proxy_request,
|
|
|
|
"[ruby-dev:25673]")
|
|
|
|
ensure
|
|
|
|
sock.close
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_join_threads([client_thread, server_thread])
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-12-09 06:19:11 -05:00
|
|
|
def test_get_SNI_failure
|
2019-12-09 19:41:33 -05:00
|
|
|
TestNetHTTPUtils.clean_http_proxy_env do
|
2020-07-29 15:34:07 -04:00
|
|
|
http = Net::HTTP.new("invalidservername", config("port"))
|
2019-12-09 19:41:33 -05:00
|
|
|
http.ipaddr = config('host')
|
|
|
|
http.use_ssl = true
|
|
|
|
http.cert_store = TEST_STORE
|
|
|
|
certs = []
|
|
|
|
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
|
|
|
|
certs << store_ctx.current_cert
|
|
|
|
preverify_ok
|
|
|
|
end
|
|
|
|
@log_tester = lambda {|_| }
|
|
|
|
assert_raise(OpenSSL::SSL::SSLError){ http.start }
|
2019-12-09 06:19:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-20 11:21:22 -05:00
|
|
|
def test_post
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST, config("port"))
|
2007-12-20 11:21:22 -05:00
|
|
|
http.use_ssl = true
|
2016-11-29 10:48:45 -05:00
|
|
|
http.cert_store = TEST_STORE
|
2007-12-20 11:21:22 -05:00
|
|
|
data = config('ssl_private_key').to_der
|
2014-08-05 15:08:26 -04:00
|
|
|
http.request_post("/", data, {'content-type' => 'application/x-www-form-urlencoded'}) {|res|
|
2007-12-20 11:21:22 -05:00
|
|
|
assert_equal(data, res.body)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-07-24 20:05:59 -04:00
|
|
|
def test_session_reuse
|
2018-08-09 06:00:19 -04:00
|
|
|
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
|
|
|
|
# See https://github.com/openssl/openssl/pull/5967 for details.
|
2022-01-13 05:46:27 -05:00
|
|
|
omit if OpenSSL::OPENSSL_LIBRARY_VERSION.include?('OpenSSL 1.1.0h')
|
2018-08-09 06:00:19 -04:00
|
|
|
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST, config("port"))
|
2012-07-24 20:05:59 -04:00
|
|
|
http.use_ssl = true
|
2016-11-29 10:48:45 -05:00
|
|
|
http.cert_store = TEST_STORE
|
2012-07-24 20:05:59 -04:00
|
|
|
|
2020-07-30 14:37:16 -04:00
|
|
|
if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /LibreSSL (\d+\.\d+)/ && $1.to_f > 3.19
|
|
|
|
# LibreSSL 3.2 defaults to TLSv1.3 in server and client, which doesn't currently
|
|
|
|
# support session resuse. Limiting the version to the TLSv1.2 stack allows
|
|
|
|
# this test to continue to work on LibreSSL 3.2+. LibreSSL may eventually
|
|
|
|
# support session reuse, but there are no current plans to do so.
|
|
|
|
http.ssl_version = :TLSv1
|
|
|
|
end
|
|
|
|
|
2012-07-24 20:05:59 -04:00
|
|
|
http.start
|
2022-04-20 00:01:02 -04:00
|
|
|
session_reused = http.instance_variable_get(:@socket).io.session_reused?
|
|
|
|
assert_false session_reused unless session_reused.nil? # can not detect re-use under JRuby
|
2012-07-24 20:05:59 -04:00
|
|
|
http.get("/")
|
|
|
|
http.finish
|
|
|
|
|
|
|
|
http.start
|
2022-04-20 00:01:02 -04:00
|
|
|
session_reused = http.instance_variable_get(:@socket).io.session_reused?
|
|
|
|
assert_true session_reused unless session_reused.nil? # can not detect re-use under JRuby
|
2021-04-28 11:55:55 -04:00
|
|
|
assert_equal $test_net_http_data, http.get("/").body
|
2014-11-26 03:22:27 -05:00
|
|
|
http.finish
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_session_reuse_but_expire
|
2018-08-09 06:00:19 -04:00
|
|
|
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
|
2022-01-13 05:46:27 -05:00
|
|
|
omit if OpenSSL::OPENSSL_LIBRARY_VERSION.include?('OpenSSL 1.1.0h')
|
2018-08-09 06:00:19 -04:00
|
|
|
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST, config("port"))
|
2014-11-26 03:22:27 -05:00
|
|
|
http.use_ssl = true
|
2016-11-29 10:48:45 -05:00
|
|
|
http.cert_store = TEST_STORE
|
2014-11-26 03:22:27 -05:00
|
|
|
|
|
|
|
http.ssl_timeout = -1
|
|
|
|
http.start
|
|
|
|
http.get("/")
|
|
|
|
http.finish
|
|
|
|
|
|
|
|
http.start
|
|
|
|
http.get("/")
|
|
|
|
|
|
|
|
socket = http.instance_variable_get(:@socket).io
|
|
|
|
assert_equal false, socket.session_reused?
|
|
|
|
|
2014-05-30 11:03:34 -04:00
|
|
|
http.finish
|
2012-07-24 20:05:59 -04:00
|
|
|
end
|
|
|
|
|
2007-12-20 11:21:22 -05:00
|
|
|
if ENV["RUBY_OPENSSL_TEST_ALL"]
|
|
|
|
def test_verify
|
|
|
|
http = Net::HTTP.new("ssl.netlab.jp", 443)
|
|
|
|
http.use_ssl = true
|
|
|
|
assert(
|
2007-12-22 03:31:53 -05:00
|
|
|
(http.request_head("/"){|res| } rescue false),
|
2007-12-20 11:21:22 -05:00
|
|
|
"The system may not have default CA certificate store."
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_none
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST, config("port"))
|
2007-12-20 11:21:22 -05:00
|
|
|
http.use_ssl = true
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
http.request_get("/") {|res|
|
|
|
|
assert_equal($test_net_http_data, res.body)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-07-29 15:34:07 -04:00
|
|
|
def test_skip_hostname_verification
|
2020-01-23 03:23:17 -05:00
|
|
|
TestNetHTTPUtils.clean_http_proxy_env do
|
2020-07-29 15:34:07 -04:00
|
|
|
http = Net::HTTP.new('invalidservername', config('port'))
|
2020-01-23 03:23:17 -05:00
|
|
|
http.ipaddr = config('host')
|
|
|
|
http.use_ssl = true
|
|
|
|
http.cert_store = TEST_STORE
|
|
|
|
http.verify_hostname = false
|
|
|
|
assert_nothing_raised { http.start }
|
2020-03-04 09:26:57 -05:00
|
|
|
ensure
|
2020-07-29 15:34:07 -04:00
|
|
|
http.finish if http&.started?
|
2020-01-23 03:23:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fail_if_verify_hostname_is_true
|
|
|
|
TestNetHTTPUtils.clean_http_proxy_env do
|
2020-07-29 15:34:07 -04:00
|
|
|
http = Net::HTTP.new('invalidservername', config('port'))
|
2020-01-23 03:23:17 -05:00
|
|
|
http.ipaddr = config('host')
|
|
|
|
http.use_ssl = true
|
|
|
|
http.cert_store = TEST_STORE
|
|
|
|
http.verify_hostname = true
|
|
|
|
@log_tester = lambda { |_| }
|
|
|
|
assert_raise(OpenSSL::SSL::SSLError) { http.start }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-20 11:21:22 -05:00
|
|
|
def test_certificate_verify_failure
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST, config("port"))
|
2007-12-20 11:21:22 -05:00
|
|
|
http.use_ssl = true
|
|
|
|
ex = assert_raise(OpenSSL::SSL::SSLError){
|
2021-04-28 11:55:55 -04:00
|
|
|
http.request_get("/") {|res| }
|
2007-12-20 11:21:22 -05:00
|
|
|
}
|
|
|
|
assert_match(/certificate verify failed/, ex.message)
|
2014-12-09 12:51:43 -05:00
|
|
|
unless /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
# on Windows, Errno::ECONNRESET will be raised, and it'll be eaten by
|
|
|
|
# WEBrick
|
|
|
|
@log_tester = lambda {|log|
|
|
|
|
assert_equal(1, log.length)
|
|
|
|
assert_match(/ERROR OpenSSL::SSL::SSLError:/, log[0])
|
|
|
|
}
|
|
|
|
end
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_identity_verify_failure
|
2018-08-08 10:13:55 -04:00
|
|
|
# the certificate's subject has CN=localhost
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST_IP, config("port"))
|
2007-12-20 11:21:22 -05:00
|
|
|
http.use_ssl = true
|
2018-08-08 10:13:55 -04:00
|
|
|
http.cert_store = TEST_STORE
|
|
|
|
@log_tester = lambda {|_| }
|
2007-12-20 11:21:22 -05:00
|
|
|
ex = assert_raise(OpenSSL::SSL::SSLError){
|
|
|
|
http.request_get("/") {|res| }
|
|
|
|
}
|
2022-01-04 13:22:37 -05:00
|
|
|
re_msg = /certificate verify failed|hostname \"#{HOST_IP}\" does not match/
|
2018-08-08 10:13:55 -04:00
|
|
|
assert_match(re_msg, ex.message)
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
2011-01-12 02:26:18 -05:00
|
|
|
|
|
|
|
def test_timeout_during_SSL_handshake
|
|
|
|
bug4246 = "expected the SSL connection to have timed out but have not. [ruby-core:34203]"
|
|
|
|
|
|
|
|
# listen for connections... but deliberately do not complete SSL handshake
|
2021-04-28 11:55:55 -04:00
|
|
|
TCPServer.open(HOST, 0) {|server|
|
2011-01-12 02:26:18 -05:00
|
|
|
port = server.addr[1]
|
|
|
|
|
2021-04-28 11:55:55 -04:00
|
|
|
conn = Net::HTTP.new(HOST, port)
|
2011-01-12 02:26:18 -05:00
|
|
|
conn.use_ssl = true
|
2012-02-27 23:51:37 -05:00
|
|
|
conn.read_timeout = 0.01
|
|
|
|
conn.open_timeout = 0.01
|
2011-01-12 02:26:18 -05:00
|
|
|
|
|
|
|
th = Thread.new do
|
2012-02-28 00:15:54 -05:00
|
|
|
assert_raise(Net::OpenTimeout) {
|
2011-01-12 02:26:18 -05:00
|
|
|
conn.get('/')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
assert th.join(10), bug4246
|
|
|
|
}
|
|
|
|
end
|
2017-10-21 12:25:22 -04:00
|
|
|
|
|
|
|
def test_min_version
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST, config("port"))
|
2017-10-21 12:25:22 -04:00
|
|
|
http.use_ssl = true
|
2017-11-25 09:12:07 -05:00
|
|
|
http.min_version = :TLS1
|
2018-08-08 10:13:55 -04:00
|
|
|
http.cert_store = TEST_STORE
|
|
|
|
http.request_get("/") {|res|
|
|
|
|
assert_equal($test_net_http_data, res.body)
|
2017-10-21 12:25:22 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_max_version
|
2021-04-28 11:55:55 -04:00
|
|
|
http = Net::HTTP.new(HOST_IP, config("port"))
|
2017-10-21 12:25:22 -04:00
|
|
|
http.use_ssl = true
|
2017-11-25 09:12:07 -05:00
|
|
|
http.max_version = :SSL2
|
2017-10-21 12:25:22 -04:00
|
|
|
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
|
|
|
|
true
|
|
|
|
end
|
2017-10-21 12:44:56 -04:00
|
|
|
@log_tester = lambda {|_| }
|
2017-10-21 12:25:22 -04:00
|
|
|
ex = assert_raise(OpenSSL::SSL::SSLError){
|
|
|
|
http.request_get("/") {|res| }
|
|
|
|
}
|
2022-04-20 00:01:02 -04:00
|
|
|
re_msg = /\ASSL_connect returned=1 errno=0 |SSL_CTX_set_max_proto_version|No appropriate protocol/
|
2017-10-23 04:28:05 -04:00
|
|
|
assert_match(re_msg, ex.message)
|
2017-10-21 12:25:22 -04:00
|
|
|
end
|
|
|
|
|
2016-11-29 10:48:45 -05:00
|
|
|
end if defined?(OpenSSL::SSL)
|