class Win32::Certstore

Constants

TINY
VERSION

Attributes

certstore_handler[R]
store_name[RW]

Public Class Methods

finalize(certstore_handler) click to toggle source
# File lib/win32/certstore.rb, line 115
def self.finalize(certstore_handler)
  proc { certstore_handler.to_s }
end
new(store_name, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE) click to toggle source

Initializes a new instance of a certificate store. takes 2 parameters - the store name (My, Root, etc) and the location (CurrentUser or LocalMachine), it defaults to LocalMachine for backwards compatibility

# File lib/win32/certstore.rb, line 36
def initialize(store_name, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE)
  @store_name = store_name
  @store_location = store_location
  @certstore_handler = open(store_name, store_location: store_location)
end
open(store_name, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE) { |new(store_name, store_location: store_location)| ... } click to toggle source

To open given certificate store

# File lib/win32/certstore.rb, line 43
def self.open(store_name, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE)
  validate_store(store_name)
  if block_given?
    yield new(store_name, store_location: store_location)
  else
    new(store_name, store_location: store_location)
  end
end

Public Instance Methods

add(certificate_obj) click to toggle source

Adds a new certificate to an open certificate store @param request [Object] of certificate in OpenSSL::X509::Certificate.new format @return [true, false] only true or false

# File lib/win32/certstore.rb, line 55
def add(certificate_obj)
  cert_add(certstore_handler, certificate_obj)
end
add_pfx(path, password, key_properties = 0) click to toggle source

Adds a PFX certificate to certificate store

@note Unlike other certificates, PFX can be password protected and may contain a private key.

Therefore we need a different approach to import them.

@param path [String] Path of the certificate that should be imported @param password [String] Password of the certificate if it is protected @param key_properties [Integer] dwFlags used to specify properties of the pfx key, see certstore/store_base.rb cert_add_pfx function

@return [Boolean]

# File lib/win32/certstore.rb, line 70
def add_pfx(path, password, key_properties = 0)
  cert_add_pfx(certstore_handler, path, password, key_properties)
end
close() click to toggle source

To close and destroy pointer of open certificate store handler

# File lib/win32/certstore.rb, line 110
def close
  close_cert_store
  remove_finalizer
end
delete(certificate_thumbprint) click to toggle source

Delete existing certificate from open certificate store @param request [thumbprint<string>] of certificate @return [true, false] only true or false

# File lib/win32/certstore.rb, line 91
def delete(certificate_thumbprint)
  cert_delete(certstore_handler, certificate_thumbprint)
end
get(certificate_thumbprint, store_name: @store_name, store_location: @store_location) click to toggle source

Return `OpenSSL::X509` certificate object @param request [thumbprint<string>] of certificate @return [Object] of certificates in OpenSSL::X509 format

# File lib/win32/certstore.rb, line 77
def get(certificate_thumbprint, store_name: @store_name, store_location: @store_location)
  cert_get(certificate_thumbprint, store_name: store_name, store_location: store_location)
end
list() click to toggle source

Returns all the certificates in a store @param [nil] @return [Array] array of certificates list

# File lib/win32/certstore.rb, line 84
def list
  cert_list(certstore_handler)
end

Private Instance Methods

add_finalizer(certstore_handler) click to toggle source

Get all open certificate store handler

# File lib/win32/certstore.rb, line 136
def add_finalizer(certstore_handler)
  ObjectSpace.define_finalizer(self, self.class.finalize(certstore_handler))
end
open(store_name, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE) click to toggle source

To open certstore and return open certificate store pointer

# File lib/win32/certstore.rb, line 125
def open(store_name, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE)
  certstore_handler = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, nil, store_location, wstring(store_name))
  unless certstore_handler
    last_error = FFI::LastError.error
    raise SystemCallError.new("Unable to open the Certificate Store `#{store_name}`.", last_error)
  end
  add_finalizer(certstore_handler)
  certstore_handler
end
remove_finalizer() click to toggle source

To close all open certificate store at the end

# File lib/win32/certstore.rb, line 141
def remove_finalizer
  ObjectSpace.undefine_finalizer(self)
end