2003-07-25 11:12:26 -04:00
|
|
|
begin
|
|
|
|
require 'http-access2'
|
|
|
|
rescue LoadError
|
|
|
|
STDERR.puts("Cannot load http-access2. CRL might not be fetched.")
|
|
|
|
end
|
|
|
|
require 'c_rehash'
|
|
|
|
|
|
|
|
|
|
|
|
class CrlStore
|
|
|
|
def initialize(c_store)
|
|
|
|
@c_store = c_store
|
|
|
|
@c_store.hash_dir(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_crl(cert)
|
|
|
|
do_find_crl(cert)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def do_find_crl(cert)
|
|
|
|
unless ca = find_ca(cert)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
unless crlfiles = @c_store.get_crls(ca.subject)
|
|
|
|
if crl = renew_crl(cert, ca)
|
2013-07-20 09:41:02 -04:00
|
|
|
@c_store.add_crl(crl)
|
|
|
|
return crl
|
2003-07-25 11:12:26 -04:00
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
crlfiles.each do |crlfile|
|
|
|
|
next unless crl = load_crl(crlfile)
|
|
|
|
if crl.next_update < Time.now
|
2013-07-20 09:41:02 -04:00
|
|
|
if new_crl = renew_crl(cert, ca)
|
|
|
|
@c_store.delete_crl(crl)
|
|
|
|
@c_store.add_crl(new_crl)
|
|
|
|
crl = new_crl
|
|
|
|
end
|
2003-07-25 11:12:26 -04:00
|
|
|
end
|
|
|
|
if check_valid(crl, ca)
|
2013-07-20 09:41:02 -04:00
|
|
|
return crl
|
2003-07-25 11:12:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_ca(cert)
|
|
|
|
@c_store.get_certs(cert.issuer).each do |cafile|
|
|
|
|
ca = load_cert(cafile)
|
|
|
|
if cert.verify(ca.public_key)
|
2013-07-20 09:41:02 -04:00
|
|
|
return ca
|
2003-07-25 11:12:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch(location)
|
|
|
|
if /\AURI:(.*)\z/ =~ location
|
|
|
|
begin
|
2013-07-20 09:41:02 -04:00
|
|
|
c = HTTPAccess2::Client.new(ENV['http_proxy'] || ENV['HTTP_PROXY'])
|
|
|
|
c.get_content($1)
|
2003-07-25 11:12:26 -04:00
|
|
|
rescue NameError, StandardError
|
2013-07-20 09:41:02 -04:00
|
|
|
nil
|
2003-07-25 11:12:26 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_cert(certfile)
|
|
|
|
load_cert_str(File.read(certfile))
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_crl(crlfile)
|
|
|
|
load_crl_str(File.read(crlfile))
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_cert_str(cert_str)
|
|
|
|
OpenSSL::X509::Certificate.new(cert_str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_crl_str(crl_str)
|
|
|
|
OpenSSL::X509::CRL.new(crl_str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_valid(crl, ca)
|
|
|
|
unless crl.verify(ca.public_key)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
crl.last_update <= Time.now
|
|
|
|
end
|
|
|
|
|
|
|
|
RE_CDP = /\AcrlDistributionPoints\z/
|
|
|
|
def get_cdp(cert)
|
|
|
|
if cdp_ext = cert.extensions.find { |ext| RE_CDP =~ ext.oid }
|
|
|
|
cdp_ext.value.chomp
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def renew_crl(cert, ca)
|
|
|
|
if cdp = get_cdp(cert)
|
|
|
|
if new_crl_str = fetch(cdp)
|
2013-07-20 09:41:02 -04:00
|
|
|
new_crl = load_crl_str(new_crl_str)
|
|
|
|
if check_valid(new_crl, ca)
|
|
|
|
return new_crl
|
|
|
|
end
|
2003-07-25 11:12:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
|
|
|
dir = "trust_certs"
|
|
|
|
c_store = CHashDir.new(dir)
|
|
|
|
s = CrlStore.new(c_store)
|
|
|
|
c = OpenSSL::X509::Certificate.new(File.read("cert_store/google_codesign.pem"))
|
|
|
|
p s.find_crl(c)
|
|
|
|
end
|