We recently renewed the SSL certificate of our site, and the following occurs on Mac OS El Capitan 10.11.3:
require 'net/http'
Net::HTTP.get URI('https://www.google.com')
# => "<HTML>...</HTML>"
# The site whose certificate got renewed
Net::HTTP.get URI('https://www.example.com')
# => OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=error: certificate verify failed
All my searches on Google and StackOverflow come up with answers suggesting a problem with the Ruby installation, but they seem to be related to older Ruby versions and I don't think this is the case here. Here is what I've tried:
brew update
brew upgrade openssl
rvm osx-ssl-certs update all
rvm install ruby-2.3.1 --disable-binary --with-openssl-dir="$(brew --prefix openssl)"
(I did not have this version before)rvm requirements
crlrefresh rpv
to purge the OSX system wide CRL cache, per Uzbekjon's suggestion.
How can I resolve this?
Notes:
- The problem does not occur on a freshly installed linux Docker container that has bare Ruby 2.2.3. So maybe it's something to do with Mac OS, or SSL local caching.
- This issue might have existed before the certificate renewal. I cannot know for sure. However, the renewal did cause a similar problem with a 3rd party we're using as I discuss in this question.
- The certificate installation was verified by Namecheap to be correct, online checkers show everything works, and all major browsers show the certificate as valid.
Solution
With much help from BoraMa, it is now clear what was happening. COMODO added a new root called COMODO RSA Certification Authority
instead of the previous COMODO Certification Authority
. The new root was not registered within Mac's keychain, causing this issue.
One way we attempted to debug this was by running:
openssl s_client -connect www.mysite.com:443
Which showed a warning verify error:num=20:unable to get local issuer certificate
. This warning is not an issue, as openssl s_client
does not use any certificates by default. Running the following was able to prevent the warning after downloading the certificate from COMODO into comodo.pem
(index here):
openssl s_client -connect www.mysite.com:443 -CAfile comodo.pem
However, this could not and did not affect Ruby OpenSSL interface. This article made things much clearer for me, and the SSL doctor script created by its author was also helpful, as it confirmed the hypothesis. The article suggested to look at OpenSSL::X509::DEFAULT_CERT_FILE
, which for me was /usr/local/etc/openssl/cert.pem
. That file did not exist on my machine, which meant Apple's patch for OpendSSL was using the Keychain App. For whatever reason, importing comodo.pem
into my keychain and marking it as trusted based on this post did not work.
So, the solution was to create the cert.pem
file manually. I went to the keychain app, and exported all System Root certificates to system_root.pem
. Then: cat system_root.pem comodo.pem > cert.pem
and moving that file to /usr/local/etc/openssl/
did the trick. Running Net::HTTP.get
in Ruby no longer failed.