Last active
January 9, 2018 00:28
-
-
Save bethesque/9a393ebd5e1f6754373fede226942f0e to your computer and use it in GitHub Desktop.
Making a request to a server with a self signed certificate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'openssl' | |
require 'uri' | |
require 'net/http' | |
uri = URI('https://self-signed.badssl.com') | |
downloaded_cert_path = '/tmp/downloaded_cert.pem' | |
puts `openssl s_client -showcerts -servername #{uri.host} -connect #{uri.host}:#{uri.port} </dev/null 2>/dev/null|openssl x509 -text` | |
command = "openssl s_client -showcerts -servername #{uri.host} -connect #{uri.host}:#{uri.port} </dev/null 2>/dev/null|openssl x509 -outform PEM > #{downloaded_cert_path}" | |
puts command | |
puts `#{command}` | |
cert_store = OpenSSL::X509::Store.new | |
puts "Adding certificate #{downloaded_cert_path}" | |
cert_store.add_file(downloaded_cert_path) | |
req = Net::HTTP::Get.new(uri) | |
options = { | |
:use_ssl => uri.scheme == 'https', | |
verify_mode: OpenSSL::SSL::VERIFY_PEER, | |
cert_store: cert_store | |
} | |
response = Net::HTTP.start(uri.hostname, uri.port, options) do |http| | |
http.request req | |
end | |
puts response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, for some reason, the certificate that openssl downloads is not the certificate that the browser displays. When I grab the certificate through the browser (shown below) this works: