Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access ssl certificate details? #747

Open
jots opened this issue Apr 8, 2023 · 4 comments
Open

access ssl certificate details? #747

jots opened this issue Apr 8, 2023 · 4 comments

Comments

@jots
Copy link

jots commented Apr 8, 2023

Is it possible to access the certificate details from the response?
Interested in Common name, issued by and validity period.

@tarcieri
Copy link
Member

tarcieri commented Apr 8, 2023

I don't believe so. Which type were you thinking of having an accessor for it?

@jots
Copy link
Author

jots commented Apr 8, 2023

some way to access peer_cert? like this

require 'openssl'
require 'net/http'
require 'uri'

url = ARGV[0] # get the URL from the command line arguments

uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true # use SSL for the request

begin
  http.start
rescue OpenSSL::SSL::SSLError => e
  puts "Error: SSL connection could not be established. #{e.message}"
  exit
end

cert = http.peer_cert
if cert.nil?
  puts "Error: No SSL certificate could be retrieved."
  exit
end

cert = OpenSSL::X509::Certificate.new(cert)

puts "Issuer: #{cert.issuer}"
puts "Common Names: #{cert.subject.to_a.select { |name, _, _| name == 'CN' }.map { |_, value, _| value }.join(', ')}"
puts "Valid From: #{cert.not_before}"
puts "Valid Until: #{cert.not_after}"

example:

$ ruby getcert.rb https://google.com
Issuer: /C=US/O=Google Trust Services LLC/CN=GTS CA 1C3
Common Names: *.google.com
Valid From: 2023-03-20 08:22:16 UTC
Valid Until: 2023-06-12 08:22:15 UTC

@jots
Copy link
Author

jots commented Apr 10, 2023

I think this should suffice:
response = HTTP.get('https://www.example.com')
pp response.connection.instance_variable_get("@socket").instance_variable_get("@socket").peer_cert

@tarcieri
Copy link
Member

Seems like you could use an accessor like HTTP::Connection#peer_cert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants