Skip to content

Instantly share code, notes, and snippets.

@claudijd
Created June 28, 2013 14:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save claudijd/5885193 to your computer and use it in GitHub Desktop.
Save claudijd/5885193 to your computer and use it in GitHub Desktop.
Ruby OpenSSL using verify peer and system cert store.
>> require 'socket'
=> true
>> require 'openssl'
=> true
>>
?> ssl_context = OpenSSL::SSL::SSLContext.new
=> #<OpenSSL::SSL::SSLContext:0x007ffc9a9deb00>
>> ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
=> 1
>> cert_store = OpenSSL::X509::Store.new
=> #<OpenSSL::X509::Store:0x007ffc9a9eb328>
>> cert_store.set_default_paths
=> nil
>> ssl_context.cert_store = cert_store
=> #<OpenSSL::X509::Store:0x007ffc9a9eb328>
>>
?> tcp_client = TCPSocket.new ‘server.trustwave.com', 443
=> #<TCPSocket:fd 5>
>> ssl_client = OpenSSL::SSL::SSLSocket.new tcp_client, ssl_context
=> #<OpenSSL::SSL::SSLSocket:0x007ffc9aa05520>
>> ssl_client.connect
=> #<OpenSSL::SSL::SSLSocket:0x007ffc9aa05520>
@awfulcooking
Copy link

For anyone else who arrives here.. this won't verify the cert's hostname.

ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
ssl_context.verify_hostname = true # ❗
cert_store = OpenSSL::X509::Store.new
cert_store.set_default_paths
ssl_context.cert_store = cert_store
tcp_client = TCPSocket.new host, 443
ssl_client = OpenSSL::SSL::SSLSocket.new tcp_client, ssl_context
ssl_client.hostname = host         #❗
ssl_client.connect

The two lines marked ❗ are also required.

@claudijd
Copy link
Author

Thanks @awfulcooking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment