Skip to content

Instantly share code, notes, and snippets.

@ab
Created June 8, 2014 02:55
Show Gist options
  • Select an option

  • Save ab/19589eb4f33e9a3db4c5 to your computer and use it in GitHub Desktop.

Select an option

Save ab/19589eb4f33e9a3db4c5 to your computer and use it in GitHub Desktop.
SSL certificate algorithm test
#!/usr/bin/env ruby
require 'socket'
require 'openssl'
def check_ssl_cert(host, port, ca_file)
sock = TCPSocket.new(host, port)
ctx = OpenSSL::SSL::SSLContext.new
ctx.set_params(:verify_mode => OpenSSL::SSL::VERIFY_PEER,
:ca_file => ca_file)
socket = OpenSSL::SSL::SSLSocket.new(sock, ctx)
socket.connect
cert = socket.peer_cert
return [host, 'OK', cert.not_before.strftime('%F'), cert.signature_algorithm]
end
if ARGV.empty?
STDERR.puts 'usage: certtest.rb DOMAIN...'
exit 1
end
def do_check(domain, add_www=true)
ca_file = '/etc/ssl/certs/ca-certificates.crt'
begin
puts check_ssl_cert(domain, '443', ca_file).join("\t")
rescue OpenSSL::SSL::SSLError, SocketError => err
STDERR.puts("#{domain}: #{err}")
puts [domain, 'ERR', '', ''].join("\t")
end
$stdout.flush
if add_www
do_check('www.' + domain, false)
end
end
if ARGV.first == '-'
STDIN.each_line do |line|
do_check(line.chomp)
end
else
ARGV.each do |domain|
do_check(domain)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment