2010-12-15 14:50:00 -05:00
|
|
|
require_relative "utils"
|
2004-05-26 14:14:27 -04:00
|
|
|
|
|
|
|
if defined?(OpenSSL)
|
|
|
|
|
2011-06-21 23:40:08 -04:00
|
|
|
class OpenSSL::TestSSL < OpenSSL::SSLTestCase
|
2008-04-20 18:32:06 -04:00
|
|
|
def test_ctx_setup
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
assert_equal(ctx.setup, true)
|
|
|
|
assert_equal(ctx.setup, nil)
|
|
|
|
end
|
|
|
|
|
2011-08-14 21:10:26 -04:00
|
|
|
def test_ctx_setup_no_compression
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_COMPRESSION
|
|
|
|
assert_equal(ctx.setup, true)
|
|
|
|
assert_equal(ctx.setup, nil)
|
|
|
|
assert_equal(OpenSSL::SSL::OP_NO_COMPRESSION,
|
|
|
|
ctx.options & OpenSSL::SSL::OP_NO_COMPRESSION)
|
|
|
|
end if defined?(OpenSSL::SSL::OP_NO_COMPRESSION)
|
|
|
|
|
2010-11-14 16:46:08 -05:00
|
|
|
def test_not_started_session
|
2010-12-01 01:09:22 -05:00
|
|
|
skip "non socket argument of SSLSocket.new is not supported on this platform" if /mswin|mingw/ =~ RUBY_PLATFORM
|
2010-11-14 21:37:23 -05:00
|
|
|
open(__FILE__) do |f|
|
2010-11-14 21:46:55 -05:00
|
|
|
assert_nil OpenSSL::SSL::SSLSocket.new(f).cert
|
2010-11-14 21:37:23 -05:00
|
|
|
end
|
2010-11-14 16:46:08 -05:00
|
|
|
end
|
|
|
|
|
2008-12-04 02:21:10 -05:00
|
|
|
def test_ssl_read_nonblock
|
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true) { |server, port|
|
2012-05-07 07:57:01 -04:00
|
|
|
server_connect(port) { |ssl|
|
|
|
|
assert_raise(IO::WaitReadable) { ssl.read_nonblock(100) }
|
|
|
|
ssl.write("abc\n")
|
|
|
|
IO.select [ssl]
|
|
|
|
assert_equal('a', ssl.read_nonblock(1))
|
|
|
|
assert_equal("bc\n", ssl.read_nonblock(100))
|
|
|
|
assert_raise(IO::WaitReadable) { ssl.read_nonblock(100) }
|
|
|
|
}
|
2008-12-04 02:21:10 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2004-05-26 14:14:27 -04:00
|
|
|
def test_connect_and_close
|
2007-12-17 07:18:28 -05:00
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
|
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
2004-05-26 14:14:27 -04:00
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock)
|
|
|
|
assert(ssl.connect)
|
|
|
|
ssl.close
|
|
|
|
assert(!sock.closed?)
|
|
|
|
sock.close
|
|
|
|
|
2007-12-17 07:18:28 -05:00
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
2004-05-26 14:14:27 -04:00
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock)
|
|
|
|
ssl.sync_close = true # !!
|
|
|
|
assert(ssl.connect)
|
|
|
|
ssl.close
|
|
|
|
assert(sock.closed?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_and_write
|
2007-12-17 07:18:28 -05:00
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
|
2012-05-07 07:57:01 -04:00
|
|
|
server_connect(port) { |ssl|
|
|
|
|
# syswrite and sysread
|
|
|
|
ITERATIONS.times{|i|
|
|
|
|
str = "x" * 100 + "\n"
|
|
|
|
ssl.syswrite(str)
|
|
|
|
assert_equal(str, ssl.sysread(str.size))
|
|
|
|
|
|
|
|
str = "x" * i * 100 + "\n"
|
|
|
|
buf = ""
|
|
|
|
ssl.syswrite(str)
|
|
|
|
assert_equal(buf.object_id, ssl.sysread(str.size, buf).object_id)
|
|
|
|
assert_equal(str, buf)
|
|
|
|
}
|
2004-05-26 14:14:27 -04:00
|
|
|
|
2012-05-07 07:57:01 -04:00
|
|
|
# puts and gets
|
|
|
|
ITERATIONS.times{
|
|
|
|
str = "x" * 100 + "\n"
|
|
|
|
ssl.puts(str)
|
|
|
|
assert_equal(str, ssl.gets)
|
2007-12-17 02:06:16 -05:00
|
|
|
|
2012-05-07 07:57:01 -04:00
|
|
|
str = "x" * 100
|
|
|
|
ssl.puts(str)
|
|
|
|
assert_equal(str, ssl.gets("\n", 100))
|
|
|
|
assert_equal("\n", ssl.gets)
|
|
|
|
}
|
2004-05-26 14:14:27 -04:00
|
|
|
|
2012-05-07 07:57:01 -04:00
|
|
|
# read and write
|
|
|
|
ITERATIONS.times{|i|
|
|
|
|
str = "x" * 100 + "\n"
|
|
|
|
ssl.write(str)
|
|
|
|
assert_equal(str, ssl.read(str.size))
|
|
|
|
|
|
|
|
str = "x" * i * 100 + "\n"
|
|
|
|
buf = ""
|
|
|
|
ssl.write(str)
|
|
|
|
assert_equal(buf.object_id, ssl.read(str.size, buf).object_id)
|
|
|
|
assert_equal(str, buf)
|
|
|
|
}
|
2004-05-26 14:14:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-03-09 05:45:42 -05:00
|
|
|
def test_client_auth
|
|
|
|
vflag = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
|
2007-12-17 07:18:28 -05:00
|
|
|
start_server(PORT, vflag, true){|server, port|
|
2010-05-19 01:14:41 -04:00
|
|
|
assert_raise(OpenSSL::SSL::SSLError, Errno::ECONNRESET){
|
2007-12-17 07:18:28 -05:00
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
2005-03-09 05:45:42 -05:00
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock)
|
|
|
|
ssl.connect
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.key = @cli_key
|
|
|
|
ctx.cert = @cli_cert
|
2012-05-07 07:57:01 -04:00
|
|
|
|
|
|
|
server_connect(port, ctx) { |ssl|
|
|
|
|
ssl.puts("foo")
|
|
|
|
assert_equal("foo\n", ssl.gets)
|
|
|
|
}
|
2005-03-09 05:45:42 -05:00
|
|
|
|
|
|
|
called = nil
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
2008-04-20 18:32:06 -04:00
|
|
|
ctx.client_cert_cb = Proc.new{ |sslconn|
|
2005-03-09 05:45:42 -05:00
|
|
|
called = true
|
|
|
|
[@cli_cert, @cli_key]
|
|
|
|
}
|
2012-05-07 07:57:01 -04:00
|
|
|
|
|
|
|
server_connect(port, ctx) { |ssl|
|
|
|
|
assert(called)
|
|
|
|
ssl.puts("foo")
|
|
|
|
assert_equal("foo\n", ssl.gets)
|
|
|
|
}
|
2005-03-09 05:45:42 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-06-30 10:48:52 -04:00
|
|
|
def test_client_ca
|
|
|
|
ctx_proc = Proc.new do |ctx|
|
|
|
|
ctx.client_ca = [@ca_cert]
|
|
|
|
end
|
|
|
|
|
|
|
|
vflag = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
|
|
|
|
start_server(PORT, vflag, true, :ctx_proc => ctx_proc){|server, port|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
client_ca_from_server = nil
|
|
|
|
ctx.client_cert_cb = Proc.new do |sslconn|
|
|
|
|
client_ca_from_server = sslconn.client_ca
|
|
|
|
[@cli_cert, @cli_key]
|
|
|
|
end
|
2012-05-07 13:18:55 -04:00
|
|
|
server_connect(port, ctx) { |ssl| assert_equal([@ca], client_ca_from_server) }
|
2011-06-30 10:48:52 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2004-05-26 14:14:27 -04:00
|
|
|
def test_starttls
|
2007-12-17 07:18:28 -05:00
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, false){|server, port|
|
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
2004-05-26 14:14:27 -04:00
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock)
|
|
|
|
ssl.sync_close = true
|
|
|
|
str = "x" * 1000 + "\n"
|
|
|
|
|
2010-09-10 04:54:50 -04:00
|
|
|
OpenSSL::TestUtils.silent do
|
|
|
|
ITERATIONS.times{
|
|
|
|
ssl.puts(str)
|
|
|
|
assert_equal(str, ssl.gets)
|
|
|
|
}
|
|
|
|
starttls(ssl)
|
|
|
|
end
|
2004-05-26 14:14:27 -04:00
|
|
|
|
|
|
|
ITERATIONS.times{
|
|
|
|
ssl.puts(str)
|
|
|
|
assert_equal(str, ssl.gets)
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl.close
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parallel
|
2005-12-12 01:56:08 -05:00
|
|
|
GC.start
|
2007-12-17 07:18:28 -05:00
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
|
2004-05-26 14:14:27 -04:00
|
|
|
ssls = []
|
|
|
|
10.times{
|
2007-12-17 07:18:28 -05:00
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
2004-05-26 14:14:27 -04:00
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock)
|
|
|
|
ssl.connect
|
|
|
|
ssl.sync_close = true
|
|
|
|
ssls << ssl
|
|
|
|
}
|
|
|
|
str = "x" * 1000 + "\n"
|
|
|
|
ITERATIONS.times{
|
|
|
|
ssls.each{|ssl|
|
|
|
|
ssl.puts(str)
|
|
|
|
assert_equal(str, ssl.gets)
|
|
|
|
}
|
|
|
|
}
|
2004-06-20 11:57:40 -04:00
|
|
|
ssls.each{|ssl| ssl.close }
|
2004-05-26 14:14:27 -04:00
|
|
|
}
|
|
|
|
end
|
2005-02-13 23:14:39 -05:00
|
|
|
|
2007-12-17 07:18:28 -05:00
|
|
|
def test_verify_result
|
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
|
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
2007-12-22 03:31:53 -05:00
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.set_params
|
2007-12-17 07:18:28 -05:00
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
|
|
|
assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
|
|
|
|
assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, ssl.verify_result)
|
|
|
|
|
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
2007-12-22 03:31:53 -05:00
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.set_params(
|
2007-12-17 07:18:28 -05:00
|
|
|
:verify_callback => Proc.new do |preverify_ok, store_ctx|
|
|
|
|
store_ctx.error = OpenSSL::X509::V_OK
|
|
|
|
true
|
|
|
|
end
|
|
|
|
)
|
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
|
|
|
ssl.connect
|
|
|
|
assert_equal(OpenSSL::X509::V_OK, ssl.verify_result)
|
|
|
|
|
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
2007-12-22 03:31:53 -05:00
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.set_params(
|
2007-12-17 07:18:28 -05:00
|
|
|
:verify_callback => Proc.new do |preverify_ok, store_ctx|
|
|
|
|
store_ctx.error = OpenSSL::X509::V_ERR_APPLICATION_VERIFICATION
|
|
|
|
false
|
|
|
|
end
|
|
|
|
)
|
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
|
|
|
assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
|
|
|
|
assert_equal(OpenSSL::X509::V_ERR_APPLICATION_VERIFICATION, ssl.verify_result)
|
|
|
|
}
|
|
|
|
end
|
2011-07-14 01:41:05 -04:00
|
|
|
|
|
|
|
def test_exception_in_verify_callback_is_ignored
|
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
|
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.set_params(
|
|
|
|
:verify_callback => Proc.new do |preverify_ok, store_ctx|
|
|
|
|
store_ctx.error = OpenSSL::X509::V_OK
|
|
|
|
raise RuntimeError
|
|
|
|
end
|
|
|
|
)
|
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
|
|
|
OpenSSL::TestUtils.silent do
|
|
|
|
# SSLError, not RuntimeError
|
|
|
|
assert_raise(OpenSSL::SSL::SSLError) { ssl.connect }
|
|
|
|
end
|
|
|
|
assert_equal(OpenSSL::X509::V_ERR_CERT_REJECTED, ssl.verify_result)
|
|
|
|
ssl.close
|
|
|
|
}
|
|
|
|
end
|
2007-12-17 07:18:28 -05:00
|
|
|
|
2007-12-22 03:31:53 -05:00
|
|
|
def test_sslctx_set_params
|
2007-12-17 07:18:28 -05:00
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
|
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
2007-12-22 03:31:53 -05:00
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.set_params
|
2007-12-17 07:18:28 -05:00
|
|
|
assert_equal(OpenSSL::SSL::VERIFY_PEER, ctx.verify_mode)
|
|
|
|
assert_equal(OpenSSL::SSL::OP_ALL, ctx.options)
|
|
|
|
ciphers = ctx.ciphers
|
|
|
|
ciphers_versions = ciphers.collect{|_, v, _, _| v }
|
|
|
|
ciphers_names = ciphers.collect{|v, _, _, _| v }
|
|
|
|
assert(ciphers_names.all?{|v| /ADH/ !~ v })
|
|
|
|
assert(ciphers_versions.all?{|v| /SSLv2/ !~ v })
|
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
|
|
|
assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
|
|
|
|
assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, ssl.verify_result)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-02-13 23:14:39 -05:00
|
|
|
def test_post_connection_check
|
|
|
|
sslerr = OpenSSL::SSL::SSLError
|
|
|
|
|
2007-12-17 07:18:28 -05:00
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
|
2012-05-07 07:57:01 -04:00
|
|
|
server_connect(port) { |ssl|
|
|
|
|
assert_raise(sslerr){ssl.post_connection_check("localhost.localdomain")}
|
|
|
|
assert_raise(sslerr){ssl.post_connection_check("127.0.0.1")}
|
|
|
|
assert(ssl.post_connection_check("localhost"))
|
|
|
|
assert_raise(sslerr){ssl.post_connection_check("foo.example.com")}
|
|
|
|
|
|
|
|
cert = ssl.peer_cert
|
|
|
|
assert(!OpenSSL::SSL.verify_certificate_identity(cert, "localhost.localdomain"))
|
|
|
|
assert(!OpenSSL::SSL.verify_certificate_identity(cert, "127.0.0.1"))
|
|
|
|
assert(OpenSSL::SSL.verify_certificate_identity(cert, "localhost"))
|
|
|
|
assert(!OpenSSL::SSL.verify_certificate_identity(cert, "foo.example.com"))
|
|
|
|
}
|
2005-02-13 23:14:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
now = Time.now
|
|
|
|
exts = [
|
|
|
|
["keyUsage","keyEncipherment,digitalSignature",true],
|
|
|
|
["subjectAltName","DNS:localhost.localdomain",false],
|
|
|
|
["subjectAltName","IP:127.0.0.1",false],
|
|
|
|
]
|
|
|
|
@svr_cert = issue_cert(@svr, @svr_key, 4, now, now+1800, exts,
|
|
|
|
@ca_cert, @ca_key, OpenSSL::Digest::SHA1.new)
|
2007-12-17 07:18:28 -05:00
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
|
2012-05-07 07:57:01 -04:00
|
|
|
server_connect(port) { |ssl|
|
|
|
|
assert(ssl.post_connection_check("localhost.localdomain"))
|
|
|
|
assert(ssl.post_connection_check("127.0.0.1"))
|
|
|
|
assert_raise(sslerr){ssl.post_connection_check("localhost")}
|
|
|
|
assert_raise(sslerr){ssl.post_connection_check("foo.example.com")}
|
|
|
|
|
|
|
|
cert = ssl.peer_cert
|
|
|
|
assert(OpenSSL::SSL.verify_certificate_identity(cert, "localhost.localdomain"))
|
|
|
|
assert(OpenSSL::SSL.verify_certificate_identity(cert, "127.0.0.1"))
|
|
|
|
assert(!OpenSSL::SSL.verify_certificate_identity(cert, "localhost"))
|
|
|
|
assert(!OpenSSL::SSL.verify_certificate_identity(cert, "foo.example.com"))
|
|
|
|
}
|
2005-02-13 23:14:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
now = Time.now
|
|
|
|
exts = [
|
|
|
|
["keyUsage","keyEncipherment,digitalSignature",true],
|
|
|
|
["subjectAltName","DNS:*.localdomain",false],
|
|
|
|
]
|
|
|
|
@svr_cert = issue_cert(@svr, @svr_key, 5, now, now+1800, exts,
|
|
|
|
@ca_cert, @ca_key, OpenSSL::Digest::SHA1.new)
|
2007-12-17 07:18:28 -05:00
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
|
2012-05-07 07:57:01 -04:00
|
|
|
server_connect(port) { |ssl|
|
|
|
|
assert(ssl.post_connection_check("localhost.localdomain"))
|
|
|
|
assert_raise(sslerr){ssl.post_connection_check("127.0.0.1")}
|
|
|
|
assert_raise(sslerr){ssl.post_connection_check("localhost")}
|
|
|
|
assert_raise(sslerr){ssl.post_connection_check("foo.example.com")}
|
|
|
|
cert = ssl.peer_cert
|
|
|
|
assert(OpenSSL::SSL.verify_certificate_identity(cert, "localhost.localdomain"))
|
|
|
|
assert(!OpenSSL::SSL.verify_certificate_identity(cert, "127.0.0.1"))
|
|
|
|
assert(!OpenSSL::SSL.verify_certificate_identity(cert, "localhost"))
|
|
|
|
assert(!OpenSSL::SSL.verify_certificate_identity(cert, "foo.example.com"))
|
|
|
|
}
|
2005-02-13 23:14:39 -05:00
|
|
|
}
|
|
|
|
end
|
2008-04-20 18:32:06 -04:00
|
|
|
|
2009-01-24 16:45:42 -05:00
|
|
|
def test_tlsext_hostname
|
|
|
|
return unless OpenSSL::SSL::SSLSocket.instance_methods.include?(:hostname)
|
|
|
|
|
|
|
|
ctx_proc = Proc.new do |ctx, ssl|
|
|
|
|
foo_ctx = ctx.dup
|
|
|
|
|
2010-01-25 18:12:50 -05:00
|
|
|
ctx.servername_cb = Proc.new do |ssl2, hostname|
|
2009-01-24 16:45:42 -05:00
|
|
|
case hostname
|
|
|
|
when 'foo.example.com'
|
|
|
|
foo_ctx
|
|
|
|
when 'bar.example.com'
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
raise "unknown hostname #{hostname.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
server_proc = Proc.new do |ctx, ssl|
|
|
|
|
readwrite_loop(ctx, ssl)
|
|
|
|
end
|
|
|
|
|
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, :ctx_proc => ctx_proc, :server_proc => server_proc) do |server, port|
|
|
|
|
2.times do |i|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
if defined?(OpenSSL::SSL::OP_NO_TICKET)
|
|
|
|
# disable RFC4507 support
|
|
|
|
ctx.options = OpenSSL::SSL::OP_NO_TICKET
|
|
|
|
end
|
2012-05-07 07:57:01 -04:00
|
|
|
server_connect(port, ctx) { |ssl|
|
|
|
|
ssl.hostname = (i & 1 == 0) ? 'foo.example.com' : 'bar.example.com'
|
|
|
|
str = "x" * 100 + "\n"
|
|
|
|
ssl.puts(str)
|
|
|
|
assert_equal(str, ssl.gets)
|
|
|
|
}
|
2009-01-24 16:45:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-19 16:05:21 -04:00
|
|
|
|
|
|
|
def test_multibyte_read_write
|
|
|
|
#German a umlaut
|
|
|
|
auml = [%w{ C3 A4 }.join('')].pack('H*')
|
|
|
|
auml.force_encoding(Encoding::UTF_8)
|
|
|
|
|
2011-10-22 07:35:58 -04:00
|
|
|
[10, 1000, 100000].each {|i|
|
|
|
|
str = nil
|
|
|
|
num_written = nil
|
|
|
|
server_proc = Proc.new {|ctx, ssl|
|
|
|
|
cmp = ssl.read
|
|
|
|
raw_size = cmp.size
|
|
|
|
cmp.force_encoding(Encoding::UTF_8)
|
|
|
|
assert_equal(str, cmp)
|
|
|
|
assert_equal(num_written, raw_size)
|
|
|
|
ssl.close
|
|
|
|
}
|
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, :server_proc => server_proc){|server, port|
|
2012-05-07 07:57:01 -04:00
|
|
|
server_connect(port) { |ssl|
|
|
|
|
str = auml * i
|
|
|
|
num_written = ssl.write(str)
|
|
|
|
}
|
2011-10-19 16:05:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2012-02-08 00:27:14 -05:00
|
|
|
|
|
|
|
def test_unset_OP_ALL
|
|
|
|
ctx_proc = Proc.new { |ctx|
|
|
|
|
ctx.options = OpenSSL::SSL::OP_ALL & ~OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS
|
|
|
|
}
|
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, :ctx_proc => ctx_proc){|server, port|
|
2012-05-07 07:57:01 -04:00
|
|
|
server_connect(port) { |ssl|
|
|
|
|
ssl.puts('hello')
|
|
|
|
assert_equal("hello\n", ssl.gets)
|
|
|
|
}
|
2012-02-08 00:27:14 -05:00
|
|
|
}
|
|
|
|
end
|
2012-05-06 18:26:08 -04:00
|
|
|
|
2012-05-25 10:44:15 -04:00
|
|
|
# different OpenSSL versions react differently when being faced with a
|
|
|
|
# SSL/TLS version that has been marked as forbidden, therefore either of
|
|
|
|
# these may be raised
|
|
|
|
FORBIDDEN_PROTOCOL_ERRORS = [OpenSSL::SSL::SSLError, Errno::ECONNRESET]
|
|
|
|
|
2012-05-07 07:57:01 -04:00
|
|
|
if OpenSSL::SSL::SSLContext::METHODS.include? :TLSv1
|
|
|
|
|
|
|
|
def test_forbid_ssl_v3_for_client
|
|
|
|
ctx_proc = Proc.new { |ctx| ctx.options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_SSLv3 }
|
|
|
|
start_server_version(:SSLv23, ctx_proc) { |server, port|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.ssl_version = :SSLv3
|
2012-05-25 10:44:15 -04:00
|
|
|
assert_raise(*FORBIDDEN_PROTOCOL_ERRORS) { server_connect(port, ctx) }
|
2012-05-07 07:57:01 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forbid_ssl_v3_from_server
|
|
|
|
start_server_version(:SSLv3) { |server, port|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_SSLv3
|
2012-05-25 10:44:15 -04:00
|
|
|
assert_raise(*FORBIDDEN_PROTOCOL_ERRORS) { server_connect(port, ctx) }
|
2012-05-07 07:57:01 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if OpenSSL::SSL::SSLContext::METHODS.include? :TLSv1_1
|
|
|
|
|
|
|
|
def test_tls_v1_1
|
|
|
|
start_server_version(:TLSv1_1) { |server, port|
|
|
|
|
server_connect(port) { |ssl| assert_equal("TLSv1.1", ssl.ssl_version) }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forbid_tls_v1_for_client
|
|
|
|
ctx_proc = Proc.new { |ctx| ctx.options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_TLSv1 }
|
|
|
|
start_server_version(:SSLv23, ctx_proc) { |server, port|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.ssl_version = :TLSv1
|
2012-05-25 10:44:15 -04:00
|
|
|
assert_raise(*FORBIDDEN_PROTOCOL_ERRORS) { server_connect(port, ctx) }
|
2012-05-07 07:57:01 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forbid_tls_v1_from_server
|
|
|
|
start_server_version(:TLSv1) { |server, port|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_TLSv1
|
2012-05-25 10:44:15 -04:00
|
|
|
assert_raise(*FORBIDDEN_PROTOCOL_ERRORS) { server_connect(port, ctx) }
|
2012-05-07 07:57:01 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if OpenSSL::SSL::SSLContext::METHODS.include? :TLSv1_2
|
|
|
|
|
|
|
|
def test_tls_v1_2
|
|
|
|
start_server_version(:TLSv1_2) { |server, port|
|
|
|
|
server_connect(port) { |ssl| assert_equal("TLSv1.2", ssl.ssl_version) }
|
|
|
|
}
|
2012-05-21 10:03:18 -04:00
|
|
|
end if OpenSSL::OPENSSL_VERSION_NUMBER > 0x10001000
|
2012-05-07 07:57:01 -04:00
|
|
|
|
|
|
|
def test_forbid_tls_v1_1_for_client
|
|
|
|
ctx_proc = Proc.new { |ctx| ctx.options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_TLSv1_1 }
|
|
|
|
start_server_version(:SSLv23, ctx_proc) { |server, port|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
2012-05-06 18:26:08 -04:00
|
|
|
ctx.ssl_version = :TLSv1_1
|
2012-05-25 10:44:15 -04:00
|
|
|
assert_raise(*FORBIDDEN_PROTOCOL_ERRORS) { server_connect(port, ctx) }
|
2012-05-06 18:26:08 -04:00
|
|
|
}
|
2012-05-07 13:18:55 -04:00
|
|
|
end if defined?(OpenSSL::SSL::OP_NO_TLSv1_1)
|
2012-05-07 07:57:01 -04:00
|
|
|
|
|
|
|
def test_forbid_tls_v1_1_from_server
|
|
|
|
start_server_version(:TLSv1_1) { |server, port|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_TLSv1_1
|
2012-05-25 10:44:15 -04:00
|
|
|
assert_raise(*FORBIDDEN_PROTOCOL_ERRORS) { server_connect(port, ctx) }
|
2012-05-06 18:26:08 -04:00
|
|
|
}
|
2012-05-07 13:18:55 -04:00
|
|
|
end if defined?(OpenSSL::SSL::OP_NO_TLSv1_1)
|
2012-05-06 18:26:08 -04:00
|
|
|
|
2012-05-07 07:57:01 -04:00
|
|
|
def test_forbid_tls_v1_2_for_client
|
|
|
|
ctx_proc = Proc.new { |ctx| ctx.options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_TLSv1_2 }
|
|
|
|
start_server_version(:SSLv23, ctx_proc) { |server, port|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
2012-05-06 18:26:08 -04:00
|
|
|
ctx.ssl_version = :TLSv1_2
|
2012-05-25 10:44:15 -04:00
|
|
|
assert_raise(*FORBIDDEN_PROTOCOL_ERRORS) { server_connect(port, ctx) }
|
2012-05-06 18:26:08 -04:00
|
|
|
}
|
2012-05-07 13:18:55 -04:00
|
|
|
end if defined?(OpenSSL::SSL::OP_NO_TLSv1_2)
|
2012-05-07 07:57:01 -04:00
|
|
|
|
|
|
|
def test_forbid_tls_v1_2_from_server
|
|
|
|
start_server_version(:TLSv1_2) { |server, port|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_TLSv1_2
|
2012-05-25 10:44:15 -04:00
|
|
|
assert_raise(*FORBIDDEN_PROTOCOL_ERRORS) { server_connect(port, ctx) }
|
2012-05-06 18:26:08 -04:00
|
|
|
}
|
2012-05-07 13:18:55 -04:00
|
|
|
end if defined?(OpenSSL::SSL::OP_NO_TLSv1_2)
|
2012-05-07 07:57:01 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-05-06 18:26:08 -04:00
|
|
|
|
2012-05-07 07:57:01 -04:00
|
|
|
def start_server_version(version, ctx_proc=nil, &blk)
|
2012-05-07 13:18:55 -04:00
|
|
|
ctx_wrap = Proc.new { |ctx|
|
2012-05-07 07:57:01 -04:00
|
|
|
ctx.ssl_version = version
|
|
|
|
ctx_proc.call(ctx) if ctx_proc
|
|
|
|
}
|
|
|
|
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, :ctx_proc => ctx_wrap, &blk)
|
|
|
|
end
|
|
|
|
|
|
|
|
def server_connect(port, ctx=nil)
|
|
|
|
sock = TCPSocket.new("127.0.0.1", port)
|
|
|
|
ssl = ctx ? OpenSSL::SSL::SSLSocket.new(sock, ctx) : OpenSSL::SSL::SSLSocket.new(sock)
|
|
|
|
ssl.sync_close = true
|
|
|
|
ssl.connect
|
|
|
|
yield ssl
|
|
|
|
ensure
|
|
|
|
ssl.close
|
|
|
|
end
|
2004-05-26 14:14:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|