I want to access Twitter and upon using Net::HTTP's POST function I get this error.
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
Yes I know everyone gets this message.
Here are viable solutions I found.
First, manually set the cert file:
#! /usr/bin/env ruby
require 'net/https'
require 'uri'
uri = URI.parse(ARGV[0] || 'https://localhost/')
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
end
http.start {
http.request_get(uri.path) {|res|
print res.body
}
}
This was provided by Ariejan de Vroom: https://www.kabisa.nl/tech/ruby-and-ssl-certificate-validation/
Many people have given a similar answer to this. This did not work for me.
Then I found something that brought me along the right path. This guy Mislav Marohnić https://mislav.net/2013/07/ruby-openssl/ nailed the area of concern. It has to do with OpenSSL::X509::DEFAULT_CERT_FILE and OpenSSL::X509::DEFAULT_CERT_DIR. Which turns out are hard coded into my Ruby 1.9.3 through it's source code. Mislav gives his workaround like so:
require 'https'
http = Net::HTTP.new('example.com', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.cert_store = OpenSSL::X509::Store.new
http.cert_store.set_default_paths
http.cert_store.add_file('/path/to/cacert.pem')
# ...or:
cert = OpenSSL::X509::Certificate.new(File.read('mycert.pem'))
http.cert_store.add_cert(cert)
I dabbled around with this and I would always get this error:
OpenSSL::X509::StoreError: cert already in hash table
Bah humbug and all that stuff!
I should also mention he has written a script that should help debug what's going on. It may help you, but not in my case. The link is on his page.
I also set
ENV['SSL_CERT_FILE']
ENV['SSL_CERT_DIR']
in my ruby code without success.
Then I proceeded to set the environment variables in windows by Start -> Control Panel -> System -> Advanced System Settings -> Advanced(tab) -> Environment Variables -> System variables New and added the SSL_CERT_DIR and SSL_CERT_FILE. This didn't work either.
And the certified gem didn't work for me... https://github.com/stevegraham/certified
So I will now provide you with my hack answer for all you Windows 7 users out there below.