2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-12-20 11:21:22 -05:00
|
|
|
require "test/unit"
|
|
|
|
begin
|
|
|
|
require 'net/https'
|
2007-12-23 14:05:39 -05:00
|
|
|
require 'stringio'
|
2011-01-12 02:26:18 -05:00
|
|
|
require 'timeout'
|
2007-12-23 14:05:39 -05:00
|
|
|
require File.expand_path("utils", File.dirname(__FILE__))
|
2007-12-20 11:21:22 -05:00
|
|
|
rescue LoadError
|
|
|
|
# should skip this test
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestNetHTTPS < Test::Unit::TestCase
|
|
|
|
include TestNetHTTPUtils
|
|
|
|
|
2016-11-29 10:48:45 -05:00
|
|
|
def self.fixture(key)
|
|
|
|
File.read(File.expand_path("../fixtures/#{key}", __dir__))
|
|
|
|
end
|
|
|
|
|
|
|
|
CA_CERT = OpenSSL::X509::Certificate.new(fixture("cacert.pem"))
|
|
|
|
SERVER_KEY = OpenSSL::PKey.read(fixture("server.key"))
|
|
|
|
SERVER_CERT = OpenSSL::X509::Certificate.new(fixture("server.crt"))
|
|
|
|
DHPARAMS = OpenSSL::PKey::DH.new(fixture("dhparams.pem"))
|
|
|
|
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
|
2007-12-20 11:21:22 -05:00
|
|
|
|
|
|
|
CONFIG = {
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'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
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
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)
|
|
|
|
}
|
2016-11-29 10:48:45 -05:00
|
|
|
assert_equal(CA_CERT.to_der, certs[0].to_der)
|
|
|
|
assert_equal(SERVER_CERT.to_der, certs[1].to_der)
|
2010-09-27 02:57:39 -04:00
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_post
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
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)
|
|
|
|
}
|
2010-09-27 02:57:39 -04:00
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
2007-12-20 11:21:22 -05:00
|
|
|
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.
|
|
|
|
skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
|
|
|
|
|
2012-07-24 20:05:59 -04:00
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
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
|
|
|
|
|
|
|
http.start
|
|
|
|
http.get("/")
|
|
|
|
http.finish
|
|
|
|
|
|
|
|
http.start
|
|
|
|
http.get("/")
|
|
|
|
|
|
|
|
socket = http.instance_variable_get(:@socket).io
|
2018-08-08 10:13:55 -04:00
|
|
|
assert_equal true, socket.session_reused?
|
2014-11-26 03:22:27 -05:00
|
|
|
|
|
|
|
http.finish
|
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
|
|
|
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.
|
|
|
|
skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
|
|
|
|
|
2014-11-26 03:22:27 -05:00
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
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-26 01:54:29 -04:00
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
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
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
http.use_ssl = true
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
http.request_get("/") {|res|
|
|
|
|
assert_equal($test_net_http_data, res.body)
|
|
|
|
}
|
2010-09-27 02:57:39 -04:00
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_certificate_verify_failure
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
http.use_ssl = true
|
|
|
|
ex = assert_raise(OpenSSL::SSL::SSLError){
|
2010-09-27 02:57:39 -04:00
|
|
|
begin
|
|
|
|
http.request_get("/") {|res| }
|
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
|
|
|
end
|
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
|
2007-12-20 11:21:22 -05:00
|
|
|
http = Net::HTTP.new("127.0.0.1", config("port"))
|
|
|
|
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| }
|
|
|
|
}
|
2018-08-08 10:13:55 -04:00
|
|
|
re_msg = /certificate verify failed|hostname \"127.0.0.1\" does not match/
|
|
|
|
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
|
2011-08-01 18:35:05 -04:00
|
|
|
TCPServer.open('localhost', 0) {|server|
|
2011-01-12 02:26:18 -05:00
|
|
|
port = server.addr[1]
|
|
|
|
|
|
|
|
conn = Net::HTTP.new('localhost', port)
|
|
|
|
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
|
2018-08-08 10:13:55 -04:00
|
|
|
http = Net::HTTP.new("localhost", 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
|
|
|
|
http = Net::HTTP.new("127.0.0.1", config("port"))
|
|
|
|
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| }
|
|
|
|
}
|
2017-10-23 04:28:05 -04:00
|
|
|
re_msg = /\ASSL_connect returned=1 errno=0 |SSL_CTX_set_max_proto_version/
|
|
|
|
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)
|