Class: Win32::Certstore
- Inherits:
-
Object
- Object
- Win32::Certstore
- Extended by:
- Mixin::Assertions
- Includes:
- Mixin::Crypto, Mixin::String, StoreBase
- Defined in:
- lib/win32/certstore.rb,
lib/win32/certstore/version.rb,
lib/win32/certstore/store_base.rb,
lib/win32/certstore/mixin/crypto.rb,
lib/win32/certstore/mixin/helper.rb,
lib/win32/certstore/mixin/string.rb,
lib/win32/certstore/mixin/assertions.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.6.16".freeze
Constants included from Mixin::Crypto
Mixin::Crypto::BLOB, Mixin::Crypto::BOOL, Mixin::Crypto::BYTE, Mixin::Crypto::CERT_CLOSE_STORE_CHECK_FLAG, Mixin::Crypto::CERT_CLOSE_STORE_FORCE_FLAG, Mixin::Crypto::CERT_COMPARE_ANY, Mixin::Crypto::CERT_COMPARE_NAME_STR_W, Mixin::Crypto::CERT_COMPARE_SHA1_HASH, Mixin::Crypto::CERT_COMPARE_SHIFT, Mixin::Crypto::CERT_FIND_ANY, Mixin::Crypto::CERT_FIND_SHA1_HASH, Mixin::Crypto::CERT_FIND_SUBJECT_STR, Mixin::Crypto::CERT_INFO_SUBJECT_FLAG, Mixin::Crypto::CERT_NAME_ATTR_TYPE, Mixin::Crypto::CERT_NAME_DISABLE_IE4_UTF8_FLAG, Mixin::Crypto::CERT_NAME_DNS_TYPE, Mixin::Crypto::CERT_NAME_EMAIL_TYPE, Mixin::Crypto::CERT_NAME_FRIENDLY_DISPLAY_TYPE, Mixin::Crypto::CERT_NAME_ISSUER_FLAG, Mixin::Crypto::CERT_NAME_RDN_TYPE, Mixin::Crypto::CERT_NAME_SEARCH_ALL_NAMES_FLAG, Mixin::Crypto::CERT_NAME_SIMPLE_DISPLAY_TYPE, Mixin::Crypto::CERT_NAME_STR_ENABLE_PUNYCODE_FLAG, Mixin::Crypto::CERT_NAME_UPN_TYPE, Mixin::Crypto::CERT_NAME_URL_TYPE, Mixin::Crypto::CERT_STORE_ADD_REPLACE_EXISTING, Mixin::Crypto::CERT_STORE_ADD_USE_EXISTING, Mixin::Crypto::CERT_STORE_PROV_SYSTEM, Mixin::Crypto::CERT_SYSTEM_STORE_CURRENT_USER, Mixin::Crypto::CERT_SYSTEM_STORE_LOCAL_MACHINE, Mixin::Crypto::CERT_SYSTEM_STORE_SERVICES, Mixin::Crypto::CERT_SYSTEM_STORE_USERS, Mixin::Crypto::CRYPT_ASN_ENCODING, Mixin::Crypto::CRYPT_NDR_ENCODING, Mixin::Crypto::DWORD, Mixin::Crypto::ENCODING_TYPE, Mixin::Crypto::HCERTSTORE, Mixin::Crypto::HCRYPTPROV_LEGACY, Mixin::Crypto::INT_PTR, Mixin::Crypto::LMSTR, Mixin::Crypto::LONG, Mixin::Crypto::LPCTSTR, Mixin::Crypto::LPFILETIME, Mixin::Crypto::LPSTR, Mixin::Crypto::LPTSTR, Mixin::Crypto::LPVOID, Mixin::Crypto::PCCERT_CONTEXT, Mixin::Crypto::PCERT_INFO, Mixin::Crypto::PCTL_USAGE, Mixin::Crypto::PCTL_VERIFY_USAGE_PARA, Mixin::Crypto::PCTL_VERIFY_USAGE_STATUS, Mixin::Crypto::PKCS_7_ASN_ENCODING, Mixin::Crypto::PKCS_7_NDR_ENCODING, Mixin::Crypto::PKCS_7_OR_X509_ASN_ENCODING, Mixin::Crypto::PWSTR, Mixin::Crypto::X509_ASN_ENCODING, Mixin::Crypto::X509_NDR_ENCODING
Instance Attribute Summary collapse
-
#store_location ⇒ Object
Returns the value of attribute store_location.
-
#store_name ⇒ Object
Returns the value of attribute store_name.
Class Method Summary collapse
- .finalize(certstore_handler) ⇒ Object
-
.open(store_name, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE) ⇒ Object
To open given certificate store.
Instance Method Summary collapse
-
#add(certificate_obj) ⇒ true, false
Adds a new certificate to an open certificate store.
-
#add_pfx(path, password, key_properties = 0) ⇒ Boolean
Adds a PFX certificate to certificate store.
-
#close ⇒ Object
To close and destroy pointer of open certificate store handler.
-
#delete(certificate_thumbprint) ⇒ true, false
Delete existing certificate from open certificate store.
-
#get(certificate_thumbprint) ⇒ Object
Return ‘OpenSSL::X509` certificate object.
-
#get!(certificate_thumbprint) ⇒ Object
Return ‘OpenSSL::X509` certificate object if present otherwise raise a “Certificate not found!” error.
- #get_thumbprint(search_token) ⇒ Object
-
#initialize(store_name, store_location) ⇒ Certstore
constructor
Initializes a new instance of a certificate store.
-
#list ⇒ Array
Returns all the certificates in a store.
-
#search(search_token) ⇒ Array
Returns all matching certificates in a store.
-
#valid?(certificate_thumbprint) ⇒ true, false
Validates a certificate in a certificate store on the basis of time validity.
Methods included from Mixin::Assertions
lookup_error, validate!, validate_certificate, validate_certificate_obj, validate_store, validate_thumbprint
Methods included from StoreBase
#cert_add, #cert_add_pfx, #cert_delete, #cert_get, #cert_list, #cert_lookup_by_token, #cert_search, #cert_validate, #close_cert_store
Methods included from Mixin::Helper
#cert_ps_cmd, #valid_duration?
Methods included from Mixin::String
#utf8_to_wide, #wide_to_utf8, #wstring
Methods included from FFI::Library
Constructor Details
#initialize(store_name, store_location) ⇒ Certstore
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
36 37 38 39 40 |
# File 'lib/win32/certstore.rb', line 36 def initialize(store_name, store_location) @store_name = store_name @store_location = store_location @certstore_handler = open(store_name, store_location) end |
Instance Attribute Details
#store_location ⇒ Object
Returns the value of attribute store_location.
32 33 34 |
# File 'lib/win32/certstore.rb', line 32 def store_location @store_location end |
#store_name ⇒ Object
Returns the value of attribute store_name.
32 33 34 |
# File 'lib/win32/certstore.rb', line 32 def store_name @store_name end |
Class Method Details
.finalize(certstore_handler) ⇒ Object
130 131 132 |
# File 'lib/win32/certstore.rb', line 130 def self.finalize(certstore_handler) proc { certstore_handler.to_s } end |
.open(store_name, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE) ⇒ Object
To open given certificate store
43 44 45 46 47 48 49 50 |
# 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) else new(store_name, store_location) end end |
Instance Method Details
#add(certificate_obj) ⇒ true, false
Adds a new certificate to an open certificate store
55 56 57 |
# 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) ⇒ Boolean
Unlike other certificates, PFX can be password protected and may contain a private key. Therefore we need a different approach to import them.
Adds a PFX certificate to certificate store
70 71 72 |
# 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 ⇒ Object
To close and destroy pointer of open certificate store handler
125 126 127 128 |
# File 'lib/win32/certstore.rb', line 125 def close close_cert_store remove_finalizer end |
#delete(certificate_thumbprint) ⇒ true, false
Delete existing certificate from open certificate store
102 103 104 |
# File 'lib/win32/certstore.rb', line 102 def delete(certificate_thumbprint) cert_delete(certstore_handler, certificate_thumbprint) end |
#get(certificate_thumbprint) ⇒ Object
Return ‘OpenSSL::X509` certificate object
77 78 79 |
# File 'lib/win32/certstore.rb', line 77 def get(certificate_thumbprint) cert_get(certificate_thumbprint) end |
#get!(certificate_thumbprint) ⇒ Object
Return ‘OpenSSL::X509` certificate object if present otherwise raise a “Certificate not found!” error
84 85 86 87 88 89 90 |
# File 'lib/win32/certstore.rb', line 84 def get!(certificate_thumbprint) cert_pem = cert_get(certificate_thumbprint) raise ArgumentError, "Unable to retrieve the certificate" if cert_pem.empty? || cert_pem == "Certificate Not Found" cert_pem end |
#get_thumbprint(search_token) ⇒ Object
113 114 115 |
# File 'lib/win32/certstore.rb', line 113 def get_thumbprint(search_token) cert_lookup_by_token(search_token) end |
#list ⇒ Array
Returns all the certificates in a store
95 96 97 |
# File 'lib/win32/certstore.rb', line 95 def list cert_list(certstore_handler) end |
#search(search_token) ⇒ Array
Returns all matching certificates in a store
109 110 111 |
# File 'lib/win32/certstore.rb', line 109 def search(search_token) cert_search(certstore_handler, search_token) end |
#valid?(certificate_thumbprint) ⇒ true, false
Validates a certificate in a certificate store on the basis of time validity
120 121 122 |
# File 'lib/win32/certstore.rb', line 120 def valid?(certificate_thumbprint) cert_validate(certificate_thumbprint) end |