module SSLInfo

Docs to follow

Constants

VERSION

Public Class Methods

cert() click to toggle source
# File lib/ssl_info.rb, line 81
def self.cert
    @cert
end
common_name() click to toggle source
# File lib/ssl_info.rb, line 97
def self.common_name
    @common_name
end
display_cert() click to toggle source
# File lib/ssl_info.rb, line 71
def self.display_cert
    printf("Subject: #{@subject}\n")
    printf("Issuer: #{@issuer}\n")
    printf("Serial: #{@serial}\n")
    printf("Common Name: #{@common_name}\n")
    printf("Issued: #{@not_before}\n")
    printf("Expires: #{@not_after}\n")
    printf("Expires In: #{@expires_in} days\n")
end
expires_in() click to toggle source
# File lib/ssl_info.rb, line 109
def self.expires_in
    @expires_in
end
get_cert(domain_name, verify = false) click to toggle source
# File lib/ssl_info.rb, line 22
def self.get_cert(domain_name, verify = false)
    begin
        parts = domain_name.split(':')

        domain_name = parts[0]

        uri = URI::HTTPS.build(host: domain_name)

        port = if parts.length == 1
                   uri.port
               else
                   parts[1]
               end

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

        http.use_ssl = true
        http.verify_mode = verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
        http.open_timeout = 5
        http.read_timeout = 5
        http.ssl_timeout  = 5

        http.start do |h|
            @cert = h.peer_cert
        end

        process_cert
    rescue SocketError, SystemCallError => e
        printf("Bad URL? #{e.message}\n")
    rescue Net::OpenTimeout
        printf("Timed out. Is the site up?\n")
    rescue OpenSSL::SSL::SSLError => e
        printf("We're trying to validate your certificate using TLSv1 It looks like your server doesn't accept it: [#{$ERROR_INFO.message}]\n") if e.message =~ /sslv3.+tlsv1 alert/i
    end
end
issuer() click to toggle source
# File lib/ssl_info.rb, line 89
def self.issuer
    @issuer
end
not_after() click to toggle source
# File lib/ssl_info.rb, line 105
def self.not_after
    @not_after
end
not_before() click to toggle source
# File lib/ssl_info.rb, line 101
def self.not_before
    @not_before
end
process_cert() click to toggle source
# File lib/ssl_info.rb, line 58
def self.process_cert
    return if @cert.nil?

    @subject = @cert.subject.to_s
    @common_name = @cert.subject.to_a.select { |name, _data, _type| name == 'CN' }.first[1]
    @issuer = @cert.issuer.to_a.select { |name, _data, _type| name == 'O' }.first[1]
    @serial = @cert.serial
    @version = @cert.version
    @not_before = @cert.not_before
    @not_after = @cert.not_after
    @expires_in = ((@not_after - Time.now) / 864_00).to_i
end
serial() click to toggle source
# File lib/ssl_info.rb, line 93
def self.serial
    @serial
end
subject() click to toggle source
# File lib/ssl_info.rb, line 85
def self.subject
    @subject
end