OpenSSL +自签名证书= OpenSSL :: SSL :: SSLError:SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器证书B:证书验证失败

rwb*_*rwb 2 apache ssl openssl open-uri ruby-on-rails-3

我一直在尝试为我们的内部Rails应用程序使用SSL(SAN)证书.

我使用OpenSSL创建了证书文件,并让它们与Apache很好地协同工作:

<VirtualHost *:443>
  ServerName server-name
  RailsEnv uat

  DocumentRoot /var/www/server-name/current/public
  <Directory /var/www/server-name/current/public>
    AllowOverride All
    Options -MultiViews
  </Directory>

  SSLEngine on
  SSLCertificateFile /etc/apache2/ssl/hostname.cer
  SSLCertificateKeyFile /etc/apache2/ssl/hostname.key
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

这很好用.

我的问题是当使用open-uri与某些Rails控制器通信时,我收到错误消息:

require "net/https"

uri = URI.parse("https://server-name.domain.com/controller.json?date=2013-09-03") 
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.start {
  http.request_get(uri.path) {|res|
    print res.body
  }
}

OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
Run Code Online (Sandbox Code Playgroud)

我看过许多StackOverflow文章建议只需关闭SSL验证,使用:

http.verify_mode = OpenSSL::SSL::VERIFY_NONE
Run Code Online (Sandbox Code Playgroud)

但我不认为这是一种令人满意的方法.我也看到这表明我需要指出的文章open-uri,以/etc/ssl/certs/ca-certificates.crt这显然是公共证书.所以对于我的自签名证书,我尝试了以下内容:

uri = URI.parse("https://server-name.domain.com/controller.json?date=2013-09-03") 
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"  # enable SSL/TLS
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.ca_file = "/etc/apache2/ssl/host-name.cer"
end
http.start {
  http.request_get(uri.path) {|res|
    print res.body
  }
}
Run Code Online (Sandbox Code Playgroud)

哪个有效.

我的问题是,/etc/apache2/ssl/除了当前的搜索路径之外,我如何告诉OpenSSL/OpenURI始终查看目录?我不希望在每个请求中指定路径(随着主机名的变化而改变,这取决于我们的工作地点).

谢谢

Vir*_*ren 6

这里提取

在此输入图像描述

所以你基本上所要做的就是这个

ENV['SSL_CERT_FILE'] = "your certificate path"
Run Code Online (Sandbox Code Playgroud)

我想一切都会奏效

您可能会发现此参考有用:

希望这有帮助