Skip to content

Instantly share code, notes, and snippets.

@apsoto
Forked from claudijd/example5.rb
Last active December 25, 2015 01:49
Show Gist options
  • Save apsoto/6897455 to your computer and use it in GitHub Desktop.
Save apsoto/6897455 to your computer and use it in GitHub Desktop.
SSL wrapped socket connection in ruby. NOTE: * default is to NOT verify the certs (ssl_context.verify_mode => nil) * Default is also to have an empty cert store (ssl_context.cert_store => nil) This gist fixes that
>> 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment