Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to an https server with an invalid certificate using the latest versions of Ruby

Tags:

ruby

ssl

Consider the following code:

require 'net/https' uri = URI.parse("https://host/index.html") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(uri.path) response = http.request(request) 

where https://host/index.html is a valid address with an invalid certificate on the server.

On older versions of ruby (specifically 1.8.7-p334 and 1.9.2-p180) this code works fine. On all recent versions (1.8.7-p352, 1.9.2-p290 and 1.9.3-p0) it throws the following exception:

OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=unknown state 

on the last line.

Changing verify_mode to OpenSSL::SSL::VERIFY_PEER gives exactly the error suggesting it is attempting to verify the certificate in spite of the setting.

How can I convince ruby to ignore the invalid certificate and connect?

like image 377
Jonathan Avatar asked Nov 01 '11 16:11

Jonathan


1 Answers

uri = URI("https://host/index.html") req = Net::HTTP::Get.new(uri.path)  res = Net::HTTP.start(         uri.host, uri.port,          :use_ssl => uri.scheme == 'https',          :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|   https.request(req) end 
like image 70
tosterr Avatar answered Sep 26 '22 08:09

tosterr