在Ruby中验证HTTPS的自签名TLS证书

时间:2019-03-19 14:00:15

标签: ruby

如何在RUBY的GET Request中使用SSL证书验证(使用URL中的password参数)? ->

require 'net/https'
require 'uri'

uri = URI.parse(ARGV[0] || 'https://example.com?password=my_password')

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

if uri.scheme == "https" 

    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http.ca_file=File.join(File.dirname(__FILE__),
             "/Users/me/Desktop/demp.pem")
end

http.start do
    http.request_get(uri.path) do |res|

print res.body

end

end

->输出

SSL_connect返回= 1 errno = 0状态=错误:证书验证失败(证书链中的自签名证书)(OpenSSL :: SSL :: SSLError)

1 个答案:

答案 0 :(得分:0)

这对我有用。

require 'net/http'
require 'net/https'
require 'openssl'
require 'uri'
require 'json'


ContentURI = URI.parse("example.com")
@cert_raw = File.read('cert.pem')
TestDataPath = '.'

req = Net::HTTP::Get.new(ContentURI.path)
https = Net::HTTP.new(ContentURI.host, ContentURI.port)
https.use_ssl = true
https.cert = OpenSSL::X509::Certificate.new(@cert_raw)
https.key = OpenSSL::PKey::RSA.new(@cert_raw)
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
resp = https.start { |cx| cx.request(req) }
p resp
p resp.body