Class: RestClient::Windows::RootCerts

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Includes:
Enumerable
Defined in:
lib/restclient/windows/root_certs.rb

Overview

Represents a collection of trusted root certificates.

Defined Under Namespace

Classes: CERT_CONTEXT

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roots) ⇒ RootCerts

Returns a new instance of RootCerts.



19
20
21
# File 'lib/restclient/windows/root_certs.rb', line 19

def initialize(roots)
  @roots = roots
end

Class Method Details

.instanceRestClient::Windows::RootCerts

Returns a new instance.

Returns:



32
33
34
# File 'lib/restclient/windows/root_certs.rb', line 32

def self.instance
  new(self.load_certs)
end

.load_certsArray<[OpenSSL::X509::Certificate]>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an array of root certificates.

Returns:

  • (Array<[OpenSSL::X509::Certificate]>)

    an array of root certificates



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/restclient/windows/root_certs.rb', line 40

def self.load_certs
  certs = []

  # This is based on a patch submitted to openssl:
  # http://www.mail-archive.com/[email protected]/msg26958.html
  ptr = FFI::Pointer::NULL
  store = CertOpenSystemStoreA(nil, "ROOT")
  begin
    while (ptr = CertEnumCertificatesInStore(store, ptr)) and not ptr.null?
      context = CERT_CONTEXT.new(ptr)
      cert_buf = context[:pbCertEncoded].read_bytes(context[:cbCertEncoded])
      begin
        certs << OpenSSL::X509::Certificate.new(cert_buf)
      rescue => detail
        warn("Failed to import root certificate: #{detail.inspect}")
      end
    end
  ensure
    CertCloseStore(store, 0)
  end

  certs
end

Instance Method Details

#each {|cert| ... } ⇒ Object

Enumerates each root certificate.

Yield Parameters:

  • cert (OpenSSL::X509::Certificate)

    each root certificate



26
27
28
# File 'lib/restclient/windows/root_certs.rb', line 26

def each
  @roots.each {|cert| yield cert}
end