Search code examples
rubyrestsslopensslnet-http

Getting OpenSSL::SSL::SSLError: SSL_set_tlsext_host_name when trying to make a post request through https


I'm trying to send a post request through https using net/http class in ruby and I'm getting this error:

 OpenSSL::SSL::SSLError: SSL_set_tlsext_host_name
        from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/http.rb:920:in `connect'
        from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/http.rb:920:in `block in connect'
        from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/timeout.rb:76:in `timeout'
        from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/http.rb:920:in `connect'
        from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/http.rb:863:in `do_start'
        from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/http.rb:852:in `start'
        from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/http.rb:1369:in `request'

Here's the code example i'm using:

uri = URI.parse('https://somehost/1')
proxy = "proxy-chain.domain.com"
port = 911
http = Net::HTTP.new(uri.host, uri.port, proxy, port)
http.use_ssl = true
http.ssl_version = 'SSLv2'
http.ciphers = OpenSSL::Cipher.ciphers      
req = Net::HTTP::Post.new(uri.request_uri)
req.set_form_data(JSON.parse(some_jsonstring_data))
res = http.request(req)

More Info:

I know that the host I'm trying to hit uses TLS1.2

I've tried to set the ssl_options as suggested in @jww's answer below with no luck... If I don't set the ssl_version I get this error:

OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A

Any help is super welcome. Thank you all!!


Solution

  • http.ssl_version = 'SSLv2'

    Server Name Indication, or SNI, is a TLS extension. Extensions were not available until TLS 1.0.

    And SSLv2 is insecure. You should probably avoid it (and SSLv3) in 2015. Also see How to set TLS context options in Ruby (like OpenSSL::SSL::SSL_OP_NO_SSLv2).