@@ -752,6 +752,46 @@ def test_verify_hostname_on_connect
752752 end
753753 end
754754
755+ def test_verify_hostname_failure_error_code
756+ ctx_proc = proc { |ctx |
757+ exts = [
758+ [ "keyUsage" , "keyEncipherment,digitalSignature" , true ] ,
759+ [ "subjectAltName" , "DNS:a.example.com" ] ,
760+ ]
761+ ctx . cert = issue_cert ( @svr , @svr_key , 4 , exts , @ca_cert , @ca_key )
762+ ctx . key = @svr_key
763+ }
764+
765+ start_server ( ctx_proc : ctx_proc , ignore_listener_error : true ) do |port |
766+ verify_callback_ok = verify_callback_err = nil
767+
768+ ctx = OpenSSL ::SSL ::SSLContext . new
769+ ctx . verify_hostname = true
770+ ctx . cert_store = OpenSSL ::X509 ::Store . new
771+ ctx . cert_store . add_cert ( @ca_cert )
772+ ctx . verify_mode = OpenSSL ::SSL ::VERIFY_PEER
773+ ctx . verify_callback = -> ( preverify_ok , store_ctx ) {
774+ verify_callback_ok = preverify_ok
775+ verify_callback_err = store_ctx . error
776+ preverify_ok
777+ }
778+
779+ begin
780+ sock = TCPSocket . new ( "127.0.0.1" , port )
781+ ssl = OpenSSL ::SSL ::SSLSocket . new ( sock , ctx )
782+ ssl . hostname = "b.example.com"
783+ assert_handshake_error { ssl . connect }
784+ assert_equal false , verify_callback_ok
785+ code_expected = openssl? ( 1 , 0 , 2 ) || defined? ( OpenSSL ::X509 ::V_ERR_HOSTNAME_MISMATCH ) ?
786+ OpenSSL ::X509 ::V_ERR_HOSTNAME_MISMATCH :
787+ OpenSSL ::X509 ::V_ERR_CERT_REJECTED
788+ assert_equal code_expected , verify_callback_err
789+ ensure
790+ sock &.close
791+ end
792+ end
793+ end
794+
755795 def test_unset_OP_ALL
756796 ctx_proc = Proc . new { |ctx |
757797 # If OP_DONT_INSERT_EMPTY_FRAGMENTS is not defined, this test is
0 commit comments