ruby - OpenSSL::SSL::SSLError: Ruby 客户端的服务器 ca 证书在使用 curl 时不起作用

标签 ruby ssl curl openssl

我从客户那里获得了与他们的 VPN 连接的证书,但是当它与 curl 命令一起使用时,它不适用于 ruby​​ 代码。 curl 命令如下:

curl --cacert cert.cer -d '{"acb": 123 }' -H 'Content-Type: application/json' 'https://demo.com'
在 ruby​​ 中,我正在尝试执行以下操作来连接提供给我们进行交易的客户端 API。
require 'net/http'
require 'json'
require 'uri'

full_url = "https://demo.com"
uri = URI.parse(full_url)

data = { "acb": 123 }
headers = { 'Content-Type' => "application/json" }

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

raw = File.read("path_to_the_certificate")
http.cert = OpenSSL::X509::Certificate.new(raw)

request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = data.to_json

response = http.request(request)

puts response.code
puts response.body
我们还尝试如下传递我们的服务器证书,但这也不起作用
http.ca_path='/etc/pki/tls/certs'
http.ca_file='/etc/pki/tls/certs/cert.cer'
http.cert = OpenSSL::X509::Certificate.new(File.read("/path/client.crt"))
http.key = OpenSSL::PKey::RSA.new(File.read("/path/client.key"))
在出现以下错误时
OpenSSL::SSL::SSLError (SSL_connect returned=1 errno=0 state=error: certificate verify failed (unable to get local issuer certificate))

最佳答案

我认为他们的自签名证书有问题。验证失败。
但是,您可以手动禁用它

http.verify_mode = OpenSSL::SSL::VERIFY_NONE

verify_mode[RW]

Sets the flags for server the certification verification at beginning of SSL/TLS session.

OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER are acceptable.


来自 https://ruby-doc.org/stdlib-2.7.0/libdoc/net/http/rdoc/Net/HTTP.html
我试图在本地复制它,它与这个修复程序一起工作。

关于ruby - OpenSSL::SSL::SSLError: Ruby 客户端的服务器 ca 证书在使用 curl 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71127689/

相关文章:

ruby-on-rails - 获取 Ruby on Rails 应用程序的主机/URL?扩展

php - phpcurl 是否有一个选项来检查它是否是初始连接或该连接是否被重用?

node.js - 缺少 PFX 或证书 + 私钥

linux - curl -SL 有什么作用?

curl - Logstash : send logs using curl

Ruby 在反引号 Shell 中转义参数

mysql - Rails - 安装 Gem 时出错

jquery - 客户端验证返回 "Uncaught TypeError: Cannot read property ' presence' of undefined"

ssl - Nginx 配置 : if not iPhone/iPad then 301 redirect to https

php - Docker安装cURL失败(需要7.10.5以上版本)