~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to sample/openssl/crlstore.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
begin
 
2
  require 'http-access2'
 
3
rescue LoadError
 
4
  STDERR.puts("Cannot load http-access2.  CRL might not be fetched.")
 
5
end
 
6
require 'c_rehash'
 
7
 
 
8
 
 
9
class CrlStore
 
10
  def initialize(c_store)
 
11
    @c_store = c_store
 
12
    @c_store.hash_dir(true)
 
13
  end
 
14
 
 
15
  def find_crl(cert)
 
16
    do_find_crl(cert)
 
17
  end
 
18
 
 
19
private
 
20
 
 
21
  def do_find_crl(cert)
 
22
    unless ca = find_ca(cert)
 
23
      return nil
 
24
    end
 
25
    unless crlfiles = @c_store.get_crls(ca.subject)
 
26
      if crl = renew_crl(cert, ca)
 
27
        @c_store.add_crl(crl)
 
28
        return crl
 
29
      end
 
30
      return nil
 
31
    end
 
32
    crlfiles.each do |crlfile|
 
33
      next unless crl = load_crl(crlfile)
 
34
      if crl.next_update < Time.now
 
35
        if new_crl = renew_crl(cert, ca)
 
36
          @c_store.delete_crl(crl)
 
37
          @c_store.add_crl(new_crl)
 
38
          crl = new_crl
 
39
        end
 
40
      end
 
41
      if check_valid(crl, ca)
 
42
        return crl
 
43
      end
 
44
    end
 
45
    nil
 
46
  end
 
47
 
 
48
  def find_ca(cert)
 
49
    @c_store.get_certs(cert.issuer).each do |cafile|
 
50
      ca = load_cert(cafile)
 
51
      if cert.verify(ca.public_key)
 
52
        return ca
 
53
      end
 
54
    end
 
55
    nil
 
56
  end
 
57
 
 
58
  def fetch(location)
 
59
    if /\AURI:(.*)\z/ =~ location
 
60
      begin
 
61
        c = HTTPAccess2::Client.new(ENV['http_proxy'] || ENV['HTTP_PROXY'])
 
62
        c.get_content($1)
 
63
      rescue NameError, StandardError
 
64
        nil
 
65
      end
 
66
    else
 
67
      nil
 
68
    end
 
69
  end
 
70
 
 
71
  def load_cert(certfile)
 
72
    load_cert_str(File.read(certfile))
 
73
  end
 
74
 
 
75
  def load_crl(crlfile)
 
76
    load_crl_str(File.read(crlfile))
 
77
  end
 
78
 
 
79
  def load_cert_str(cert_str)
 
80
    OpenSSL::X509::Certificate.new(cert_str)
 
81
  end
 
82
 
 
83
  def load_crl_str(crl_str)
 
84
    OpenSSL::X509::CRL.new(crl_str)
 
85
  end
 
86
 
 
87
  def check_valid(crl, ca)
 
88
    unless crl.verify(ca.public_key)
 
89
      return false
 
90
    end
 
91
    crl.last_update <= Time.now
 
92
  end
 
93
 
 
94
  RE_CDP = /\AcrlDistributionPoints\z/
 
95
  def get_cdp(cert)
 
96
    if cdp_ext = cert.extensions.find { |ext| RE_CDP =~ ext.oid }
 
97
      cdp_ext.value.chomp
 
98
    else
 
99
      false
 
100
    end
 
101
  end
 
102
 
 
103
  def renew_crl(cert, ca)
 
104
    if cdp = get_cdp(cert)
 
105
      if new_crl_str = fetch(cdp)
 
106
        new_crl = load_crl_str(new_crl_str)
 
107
        if check_valid(new_crl, ca)
 
108
          return new_crl
 
109
        end
 
110
      end
 
111
    end
 
112
    false
 
113
  end
 
114
end
 
115
 
 
116
if $0 == __FILE__
 
117
  dir = "trust_certs"
 
118
  c_store = CHashDir.new(dir)
 
119
  s = CrlStore.new(c_store)
 
120
  c = OpenSSL::X509::Certificate.new(File.read("cert_store/google_codesign.pem"))
 
121
  p s.find_crl(c)
 
122
end