Skip to content

Commit 0cdd377

Browse files
authored
Merge pull request #386 from rhenium/ky/ssl-attr-default-values
ssl: initialize verify_mode and verify_hostname with default values
2 parents 3cdc23e + 87d8693 commit 0cdd377

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

lib/openssl/ssl.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class SSLContext
122122
def initialize(version = nil)
123123
self.options |= OpenSSL::SSL::OP_ALL
124124
self.ssl_version = version if version
125+
self.verify_mode = OpenSSL::SSL::VERIFY_NONE
126+
self.verify_hostname = false
125127
end
126128

127129
##

test/openssl/test_ssl.rb

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,56 @@ def test_copy_stream
246246
end
247247
end
248248

249-
def test_client_auth_failure
249+
def test_verify_mode_default
250+
ctx = OpenSSL::SSL::SSLContext.new
251+
assert_equal OpenSSL::SSL::VERIFY_NONE, ctx.verify_mode
252+
end
253+
254+
def test_verify_mode_server_cert
255+
start_server(ignore_listener_error: true) { |port|
256+
populated_store = OpenSSL::X509::Store.new
257+
populated_store.add_cert(@ca_cert)
258+
empty_store = OpenSSL::X509::Store.new
259+
260+
# Valid certificate, SSL_VERIFY_PEER
261+
assert_nothing_raised {
262+
ctx = OpenSSL::SSL::SSLContext.new
263+
ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
264+
ctx.cert_store = populated_store
265+
server_connect(port, ctx) { |ssl| ssl.puts("abc"); ssl.gets }
266+
}
267+
268+
# Invalid certificate, SSL_VERIFY_NONE
269+
assert_nothing_raised {
270+
ctx = OpenSSL::SSL::SSLContext.new
271+
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
272+
ctx.cert_store = empty_store
273+
server_connect(port, ctx) { |ssl| ssl.puts("abc"); ssl.gets }
274+
}
275+
276+
# Invalid certificate, SSL_VERIFY_PEER
277+
assert_handshake_error {
278+
ctx = OpenSSL::SSL::SSLContext.new
279+
ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
280+
ctx.cert_store = empty_store
281+
server_connect(port, ctx) { |ssl| ssl.puts("abc"); ssl.gets }
282+
}
283+
}
284+
end
285+
286+
def test_verify_mode_client_cert_required
287+
# Optional, client certificate not supplied
288+
vflag = OpenSSL::SSL::VERIFY_PEER
289+
accept_proc = -> ssl {
290+
assert_equal nil, ssl.peer_cert
291+
}
292+
start_server(verify_mode: vflag, accept_proc: accept_proc) { |port|
293+
assert_nothing_raised {
294+
server_connect(port) { |ssl| ssl.puts("abc"); ssl.gets }
295+
}
296+
}
297+
298+
# Required, client certificate not supplied
250299
vflag = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
251300
start_server(verify_mode: vflag, ignore_listener_error: true) { |port|
252301
assert_handshake_error {
@@ -282,20 +331,16 @@ def test_client_auth_success
282331
}
283332
end
284333

285-
def test_client_auth_public_key
334+
def test_client_cert_cb_ignore_error
286335
vflag = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
287336
start_server(verify_mode: vflag, ignore_listener_error: true) do |port|
288-
assert_raise(ArgumentError) {
289-
ctx = OpenSSL::SSL::SSLContext.new
290-
ctx.key = @cli_key.public_key
291-
ctx.cert = @cli_cert
292-
server_connect(port, ctx) { |ssl| ssl.puts("abc"); ssl.gets }
293-
}
294-
295337
ctx = OpenSSL::SSL::SSLContext.new
296-
ctx.client_cert_cb = Proc.new{ |ssl|
297-
[@cli_cert, @cli_key.public_key]
338+
ctx.client_cert_cb = -> ssl {
339+
raise "exception in client_cert_cb must be suppressed"
298340
}
341+
# 1. Exception in client_cert_cb is suppressed
342+
# 2. No client certificate will be sent to the server
343+
# 3. SSL_VERIFY_FAIL_IF_NO_PEER_CERT causes the handshake to fail
299344
assert_handshake_error {
300345
server_connect(port, ctx) { |ssl| ssl.puts("abc"); ssl.gets }
301346
}
@@ -879,6 +924,7 @@ def test_verify_hostname_on_connect
879924

880925
start_server(ctx_proc: ctx_proc, ignore_listener_error: true) do |port|
881926
ctx = OpenSSL::SSL::SSLContext.new
927+
assert_equal false, ctx.verify_hostname
882928
ctx.verify_hostname = true
883929
ctx.cert_store = OpenSSL::X509::Store.new
884930
ctx.cert_store.add_cert(@ca_cert)

0 commit comments

Comments
 (0)