新手问题 SSL 报错,查了好多资料,仍无法解决

killernova · March 30, 2016 · Last by ROR-Eason replied at January 23, 2018 · 9645 hits

最开始是在用 RestClient 的时候,链接 https 的 api 报错:

RestClient::SSLCertificateNotVerified (SSL_connect returned=1 errno=0 state=error: certificate verify failed):

然后尝试 curl 'https://www.baidu.com' -I, 报错:

curl: (60) SSL: invalid CA certificate

google 第一个错误,发现大部分都是state=SSLv3这种错误,与我一样的并不多,结合 curl 的错误,大概尝试了以下方法:

  • 下载 ca 证书并设置路径 export SSL_CERT_FILE=/usr/local/etc/openssl/.cacert.pem chmod 755 /usr/local/etc/openssl
  • brew install brew install curl --with-openssl && brew link curl --force
  • 删除不受信任的证书
  • brew update && brew upgrade openssl
  • Add the 'certified' gem to Gemfile
  • 其他记不住的方法

总而言之,RestClient 依然报这个错误,但 curl 报错内容变了:

curl: (77) error setting certificate verify locations:
  CAfile: /usr/local/etc/openssl/ca-cert.pem
  CApath: none

但这个文件确实是存在的,并且权限是 755.

接着尝试 rvm pkg install openssl && rvm reinstall all --force, 无效,且在安装任意一个版本的 ruby 的最后,都报错:

curl: (77) error setting certificate verify locations:
  CAfile: /usr/local/etc/openssl/.cacert.pem
  CApath: none
There was an error while trying to resolve rubygems version for 'latest'.
Halting the installation.

实在是搞不定了,特来求助!

PS: 这个是第三方的 api,必须用 https

曾经遇到类似的问题,看看是不是对你有帮助? https://ruby-china.org/topics/24840

不过你是在自己配置 pem 么?两次输出路径不一样哎。

CAfile: /usr/local/etc/openssl/ca-cert.pem
CAfile: /usr/local/etc/openssl/.cacert.pem

下面提供一下 nginx 配置 ssl 证书的经验和几个工具,你可以参考:

测试 https 的 ssl 证书是否正确请使用这个命令: openssl s_client -connect baidu.com:443

在 nginx.conf 文件中,特别要求 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 来支持 3 个版本的协议,因为我们碰到 Android 开发的时候会碰到不信任问题

ssl on;
ssl_certificate /usr/local/nginx/config/server.crt;
ssl_certificate_key /usr/local/nginx/config/server.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:HIGH:!aNULL:!MD5:!ADH:!DH;
ssl_prefer_server_ciphers on;

另外 pem 要格式转换成 crt 和 key 文件用来配置 nginx 吧 证书格式转换工具:https://www.trustasia.com/tools/cert-converter.htm

@sandy_xu @tiseheaini @lgn21st 谢谢 3 位,通过以下方法,curl 已经完全正常了:

cd /usr/local/etc/openssl
security find-certificate -a -p -Z /Library/Keychains/System.keychain/System/Library/Keychains/SystemRootCertificates.keychain > cert.pem
vim ~/.bash_profile
export SSL_CERT_FILE=/usr/local/etc/openssl/cert.pem 
source ~/.bash_profile

但目前通过 RestClient.post 方法依然报错:

RestClient::SSLCertificateNotVerified (SSL_connect returned=1 errno=0 state=error: certificate verify failed):

@killernova 这个问题解决了?,能不能分享一下?

You need to Sign in before reply, if you don't have an account, please Sign up first.