[英]Ruby Imap OpenSSL::SSL::SSLError: read server certificate B: certificate verify failed
[英]Ruby Net::HTTP responds with OpenSSL::SSL::SSLError “certificate verify failed” after certificate renewal
我們最近更新了我們網站的SSL證書,並在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
我在Google和StackOverflow上的所有搜索都提出了表明Ruby安裝存在問題的答案,但它們似乎與較舊的Ruby版本有關,我認為這不是這種情況。 這是我嘗試過的:
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)"
(之前我沒有這個版本) rvm requirements
crlrefresh rpv
清除OSX系統范圍的CRL緩存。 我該如何解決這個問題?
筆記:
在BoraMa的幫助下,現在很清楚發生了什么。 COMODO添加了一個名為COMODO RSA Certification Authority
的新根,而不是之前的COMODO Certification Authority
。 新的root用戶未在Mac的鑰匙串中注冊,導致此問題。
我們嘗試調試此方法的一種方法是運行:
openssl s_client -connect www.mysite.com:443
其中顯示警告verify error:num=20:unable to get local issuer certificate
。 此警告不是問題,因為openssl s_client
默認情況下不使用任何證書。 將證書從COMODO下載到comodo.pem
( 此處為索引)后,運行以下命令可以防止出現警告:
openssl s_client -connect www.mysite.com:443 -CAfile comodo.pem
但是,這不能也不會影響Ruby OpenSSL接口。 這篇文章讓我更加清楚,其作者創建的SSL醫生腳本也很有幫助,因為它證實了這一假設。 文章建議查看OpenSSL::X509::DEFAULT_CERT_FILE
,對我來說是/usr/local/etc/openssl/cert.pem
。 該文件在我的機器上不存在,這意味着Apple的OpendSSL補丁正在使用Keychain App。 無論出於何種原因,將comodo.pem
導入我的鑰匙串並根據此帖子將其標記為受信任不起作用。
因此,解決方案是手動創建cert.pem
文件。 我去了鑰匙串應用程序,並將所有系統根證書導出到system_root.pem
。 然后: cat system_root.pem comodo.pem > cert.pem
並將該文件移動到/usr/local/etc/openssl/
就可以了。 在Ruby中運行Net::HTTP.get
不再失敗。
如果它包含COMODO_RSA_Certification_Authority.pem
證書,我會嘗試仔細檢查可信證書庫。 在我的(Linux)設置中,該站點工作正常,但是當我暫時從證書存儲中刪除COMODO證書頒發機構的證書時,我得到與您完全相同的錯誤(在瀏覽器中它仍然有效,因為它們擁有自己的證書庫)。
順便說一下,使用curl
也可以識別同樣的錯誤,因為它似乎也使用與ruby相同的可信證書庫,因此您可能首先確保該站點在curl下工作。
在linux中,cert存儲通常位於/etc/ssl/certs
而在OSX下它應該是/System/Library/OpenSSL
(有關其他選項,請參閱此文章 )。
您應該在cert store目錄中看到類似以下內容的內容:
root@apsara:/etc/ssl/certs$ ls -l | grep COMODO_RSA_Certification_Authority.pem
lrwxrwxrwx 1 root root 73 úno 28 10:24 COMODO_RSA_Certification_Authority.pem -> /usr/share/ca-certificates/mozilla/COMODO_RSA_Certification_Authority.crt
lrwxrwxrwx 1 root root 38 úno 28 10:24 d4c339cb.0 -> COMODO_RSA_Certification_Authority.pem
lrwxrwxrwx 1 root root 38 úno 28 10:24 d6325660.0 -> COMODO_RSA_Certification_Authority.pem
以下是此根CA證書的一些屬性:
$ openssl x509 -in COMODO_RSA_Certification_Authority.pem -noout -text
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
4c:aa:f9:ca:db:63:6f:e0:1f:f7:4e:d8:5b:03:86:9d
Signature Algorithm: sha384WithRSAEncryption
Issuer: C=GB, ST=Greater Manchester, L=Salford, O=COMODO CA Limited, CN=COMODO RSA Certification Authority
Validity
Not Before: Jan 19 00:00:00 2010 GMT
Not After : Jan 18 23:59:59 2038 GMT
Subject: C=GB, ST=Greater Manchester, L=Salford, O=COMODO CA Limited, CN=COMODO RSA Certification Authority
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (4096 bit)
Modulus:
00:91:e8:54:92:d2:0a:56:b1:ac:0d:24:dd:c5:cf:
...
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
BB:AF:7E:02:3D:FA:A6:F1:3C:84:8E:AD:EE:38:98:EC:D9:32:32:D4
X509v3 Key Usage: critical
Certificate Sign, CRL Sign
X509v3 Basic Constraints: critical
CA:TRUE
Signature Algorithm: sha384WithRSAEncryption
...
證書可以在這里從Comodo下載(所有證書的索引都在這里 )。
更多信息 :在調查結果時,事實證明Comodo CA實際上有兩個不同的證書認證鏈。 一個是較舊的,是上面列出的根CA的那個。 較新的驗證鏈使用鏈中的“外部CA根”證書。 該論壇帖子進一步解釋了OSX的具體說明,用於將這些證書標記為可信任。
聽起來問題在於您的OSX證書緩存。 我猜你在舊證書到期之前更新了你的證書?
嘗試通過運行此命令來清除OSX系統范圍的CRL緩存:
crlrefresh rpv
# p - purges cache, r - refreshes them, v - run in verbose mode
這是一個內置的命令行工具,可以更新和維護系統范圍的CRL緩存。 在其man
頁( mand crlrefresh
)中閱讀更多相關信息。
因為這個錯誤,我整個早上一直把頭發拉出來。 這個問題和答案使我得到了一個適合我的解決方案。 我不是在這里添加新信息,而只是我所做的具體細節,以防在類似我的平台上使用此錯誤的任何人使用它。
我正在使用:
Ubuntu 16.04
ruby 2.3.0
rails 4.2.7.1
HTTParty
我正在訪問使用COMODO SSL證書保護的API。 在我的代碼中,當我嘗試時:
HTTParty.get(secured_url).tap{|response| puts response}
我有:
SSL_connect returned=1 errno=0 state=error: certificate verify failed (OpenSSL::SSL::SSLError)
我也使用上面提到的SSL醫生腳本。 當我運行腳本(用我的實際api服務器地址代替host
)時,我得到了:
$ ruby doctor.rb host:443
/home/<redacted>/.rvm/rubies/ruby-2.3.0/bin/ruby (2.3.0-p0)
OpenSSL 1.0.2g 1 Mar 2016: /usr/lib/ssl
SSL_CERT_DIR=""
SSL_CERT_FILE=""
HEAD https://host:443
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=error: certificate verify failed
The server presented a certificate that could not be verified:
subject: <redacted>
issuer: /C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Organization Validation Secure Server CA
error code 20: unable to get local issuer certificate
在一個單獨的終端中,我進入了我的certs目錄:
$ cd /etc/ssl/certs
並且(使用從上面的issuer
文本派生的COMODO_RSA_Organization_Validation_Secure_Server_CA
):
<redacted>:/etc/ssl/certs$ openssl x509 -in COMODO_RSA_Organization_Validation_Secure_Server_CA.pem -noout -text
Error opening Certificate COMODO_RSA_Organization_Validation_Secure_Server_CA.pem
140455648364184:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('COMODO_RSA_Organization_Validation_Secure_Server_CA.pem','r')
140455648364184:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
unable to load certificate
我去了COMODO RSA組織驗證安全服務器CA pem所在的COMODO站點。 我將證書復制到我桌面上名為COMODO_RSA_Organization_Validation_Secure_Server_CA.crt
的新文件中(即使您需要pem
certicate內容,一些說明也會使用crt
擴展而不是pem
)。
然后,按照這些說明 ,我做了:
<redacted>:~/Desktop$ sudo cp COMODO_RSA_Organization_Validation_Secure_Server_CA.crt /usr/share/ca-certificates/COMODO_RSA_Organization_Validation_Secure_Server_CA.crt
<redacted>:~/Desktop$ sudo dpkg-reconfigure ca-certificates
然后我做了:
sudo dpkg-reconfigure ca-certificates
然后:
<redacted>:~/Desktop$ ruby doctor.rb host:443
/home/<redacted>/.rvm/rubies/ruby-2.3.0/bin/ruby (2.3.0-p0)
OpenSSL 1.0.2g 1 Mar 2016: /usr/lib/ssl
SSL_CERT_DIR=""
SSL_CERT_FILE=""
HEAD https://host:443
OK
之后我的代碼運行正常。 謝謝你,謝謝你,謝謝你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.