class Win32::Certstore
Constants
- TINY
- VERSION
Attributes
Public Class Methods
# File lib/win32/certstore.rb, line 115 def self.finalize(certstore_handler) proc { certstore_handler.to_s } end
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
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
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
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
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 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
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
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
Returns all matching certificates in a store @param
# File lib/win32/certstore.rb, line 98
def search(search_token)
cert_search(certstore_handler, search_token)
end
Validates a certificate in a certificate store on the basis of time validity @param
# File lib/win32/certstore.rb, line 105
def valid?(certificate_thumbprint, store_location: "", store_name: "")
cert_validate(certificate_thumbprint, store_location: store_location, store_name: store_name)
end
Private Instance Methods
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
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
To close all open certificate store at the end
# File lib/win32/certstore.rb, line 141 def remove_finalizer ObjectSpace.undefine_finalizer(self) end