Class: Certstore::OpenSSL::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/certstore/openssl/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log = Logger.new(STDOUT), cert_store, store_name, enterprise: true) ⇒ Loader

Returns a new instance of Loader.



26
27
28
29
30
31
# File 'lib/certstore/openssl/loader.rb', line 26

def initialize(log = Logger.new(STDOUT), cert_store, store_name, enterprise: true)
  @log = log
  @cert_store = cert_store
  @store_name = store_name
  @loader = Certstore::Loader.new(@store_name, enterprise: enterprise)
end

Instance Attribute Details

#cert_store ⇒ Object (readonly)

Returns the value of attribute cert_store.



24
25
26
# File 'lib/certstore/openssl/loader.rb', line 24

def cert_store
  @cert_store
end

Instance Method Details

#add_certificate(cert_path) ⇒ Object



64
65
66
67
68
69
# File 'lib/certstore/openssl/loader.rb', line 64

def add_certificate(cert_path)
  File.readable?(cert_path)
  File.open(cert_path) do |file|
    @loader.add_cert(::OpenSSL::X509::Certificate.new(file.read).to_der)
  end
end

#cleanup_thumbprint(thumbprint) ⇒ Object



45
46
47
# File 'lib/certstore/openssl/loader.rb', line 45

def cleanup_thumbprint(thumbprint)
  thumbprint.gsub(/[^A-Za-z0-9]/, "")
end

#delete_certificate(thumbprint) ⇒ Object



71
72
73
74
# File 'lib/certstore/openssl/loader.rb', line 71

def delete_certificate(thumbprint)
  thumbprint = cleanup_thumbprint(thumbprint)
  @loader.delete_cert(thumbprint)
end

#export_pkcs12(thumbprint, password) ⇒ Object



54
55
56
57
58
# File 'lib/certstore/openssl/loader.rb', line 54

def export_pkcs12(thumbprint, password)
  thumbprint = cleanup_thumbprint(thumbprint)
  pkcs12 = @loader.export_pfx(thumbprint, password)
  ::OpenSSL::PKCS12.new(pkcs12, password)
end

#get_certificate(thumbprint) ⇒ Object



49
50
51
52
# File 'lib/certstore/openssl/loader.rb', line 49

def get_certificate(thumbprint)
  thumbprint = cleanup_thumbprint(thumbprint)
  ::OpenSSL::X509::Certificate.new(@loader.find_cert(thumbprint))
end

#load_cert_store ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/certstore/openssl/loader.rb', line 33

def load_cert_store
  @loader.each do |pem|
    begin
      x509_certificate_obj = ::OpenSSL::X509::Certificate.new(pem)
      valid_duration?(x509_certificate_obj)
      @cert_store.add_cert(x509_certificate_obj)
    rescue ::OpenSSL::X509::StoreError => e # continue to read
      @log.warn "failed to load certificate(thumbprint: #{OpenSSL::Digest::SHA1.new(x509_certificate_obj.to_der).to_s}) from certstore", error: e
    end
  end
end

#valid_duration?(x509_obj) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/certstore/openssl/loader.rb', line 60

def valid_duration?(x509_obj)
  x509_obj.not_before < Time.now.utc && x509_obj.not_after > Time.now.utc
end