2010-03-24 26 views
16

Come posso verificare i certificati di un sito come https://processing.ukash.com/ in ruby ​​con net/http?Come convalidare la catena di certificati SSL in ruby ​​con net/http

https = Net::HTTP.new('processing.ukash.com', 443) 
https.use_ssl = true 
https.verify_mode = OpenSSL::SSL::VERIFY_NONE 

Funziona finora, ma come verificare che sia il certificato giusto ora? Ho salvato il certificato da firefox, ma il file .pem risultante ha molti certificati e net/http non sembra gradirlo.

risposta

22

Dalla mia Frammenti di codice collezione:

#!/usr/bin/env ruby 
# How to: 
# ======= 
# Use Ruby's net/https library, to verify a SSL certificate. 
# ========================================================== 
# - Without verification the following code will lead to: 
# warning: peer certificate won't be verified in this SSL session 
# 
# #------------------begin example----------------------------- 
# require 'net/http' 
# require 'net/https' 
# require 'uri' 
# 
# url = URI.parse 'https://myname:[email protected]/' 
# http = Net::HTTP.new(url.host, url.port) 
# http.use_ssl = (url.scheme == 'https') 
# request = Net::HTTP::Get.new(url.path) 
# request.basic_auth url.user, url.password 
# response = http.request(request) 
# #-------------------end example------------------------------ 
# 
# To verify the ssl cert cosider adapting the following. 
# Status: Untested 
# ======= 
# 
# References: 
# =========== 
# [1] http://mimori.org/%7Eh/tdiary/20080301.html#p03 
# [2] http://redcorundum.blogspot.com/2008/03/ssl-certificates-and-nethttps.html 
# 
require 'net/http' 
require 'net/https' 
require 'uri' 

RootCA = '/etc/ssl/certs' 

url = URI.parse 'https://myname:[email protected]/' 
http = Net::HTTP.new(url.host, url.port) 
http.use_ssl = (url.scheme == 'https') 
if (File.directory?(RootCA) && http.use_ssl?) 
    http.ca_path = RootCA 
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER 
    http.verify_depth = 5 
else 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
end 
request = Net::HTTP::Get.new(url.path) 
request.basic_auth url.user, url.password 
response = http.request(request) 

Spero che questo aiuti?

+0

mi sembra di ottenere gli errori dal codice. forse PEBKAC. – Thufir

+1

Puoi puntare a un punto con gli errori? – Hedgehog

+0

Ho chiesto alla pasta di restare per sempre, ma non so. comunque, link: http://pastebin.mozilla.org/1460391 potrebbe facilmente essere me stesso. Non l'ho ancora studiato veramente. – Thufir

Problemi correlati