ScMinidriver Lua Reference Documentation
chilkat.ScMinidriver
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.ScMinidriver
Use the Windows smart-card minidriver layer to access card capabilities
without working directly at the APDU level.
Enumerate and inspect smart-card certificates, key containers, container
indexes, key types, and related card state.
Authenticate to the card with the required PIN before accessing protected
private-key operations or card objects.
Sign data using private keys that remain on the smart card, including
workflows where the private key is non-exportable.
Generate or import RSA and ECC keys into supported card containers when
the card policy allows it.
Link a smart-card certificate to other Chilkat classes so PDF, XML,
CMS/PKCS7, S/MIME, or other signing operations can use the card-backed
private key.
For an extended overview, see
ScMinidriver Class Overview.
Access smart-card certificates, key containers, PINs, files, and on-card signing.
Chilkat.ScMinidriver is a smart-card integration class for
applications that need card minidriver access to certificates, key
containers, PIN authentication, card files, and on-card signing. It can
retrieve and link smart-card certificates for use in other Chilkat signing
classes, generate or import RSA and ECC keys, sign data directly, inspect
card and container state, and manage selected card files and certificates.
Connect through minidriver
Certificates and containers
PIN authentication
On-card signing
Key generation and import
Use with Chilkat signing
ScMinidriver for Windows card-minidriver certificate
and key-container operations; use Chilkat.SCard for direct
PC/SC APDU-level work, and Chilkat.Pkcs11 for PKCS#11 token or
HSM sessions.
Object Creation
local chilkat = require("chilkat")
local sc_minidriver = chilkat.ScMinidriver.new()Creates the object (chilkat.ScMinidriver() does the same). Lua's garbage collector frees it when it is no longer referenced; sc_minidriver:dispose() frees it at once, and in Lua 5.4 and later local sc_minidriver <close> = chilkat.ScMinidriver.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = sc_minidriver:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (sc_minidriver.verbose_logging = true) and methods use the colon (sc_minidriver:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
Atr
local strVal = sc_minidriver.atr
The ATR of the card in the reader. This property is set by the AquireContext method.
topCardName
local strVal = sc_minidriver.card_name
The name of the card in the reader. This property is set by the AquireContext method.
topDebugLogFilePath
local strVal = sc_minidriver.debug_log_file_path
sc_minidriver.debug_log_file_path = strVal
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
LastErrorHtml
local strVal = sc_minidriver.last_error_html
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
local strVal = sc_minidriver.last_error_text
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
local strVal = sc_minidriver.last_error_xml
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
local boolVal = sc_minidriver.last_method_success
sc_minidriver.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
MaxContainers
local intVal = sc_minidriver.max_containers
The maximum number of key containers available. The 1st key container is at index 0. Each key container can potentially contain one signature key, and one key exchange key.
topRsaPaddingHash
local strVal = sc_minidriver.rsa_padding_hash
sc_minidriver.rsa_padding_hash = strVal
If an RSA key is used for signing, this is the hash algorithm to used in conjunction with the padding scheme. It can be SHA1, SHA256, SHA384, or SHA512. The default is SHA256.
RsaPaddingScheme
local strVal = sc_minidriver.rsa_padding_scheme
sc_minidriver.rsa_padding_scheme = strVal
If an RSA key is used for signing, this is the padding scheme to use. It can be PKCS or PSS. The default is PSS.
UncommonOptions
local strVal = sc_minidriver.uncommon_options
sc_minidriver.uncommon_options = strVal
This is a catch-all property to be used for uncommon needs. This property defaults to the empty string and should typically remain empty.
topVerboseLogging
local boolVal = sc_minidriver.verbose_logging
sc_minidriver.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = sc_minidriver.version
Methods
AcquireContext
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:acquire_context(reader_name)
Initializes communication with the card inserted in the given reader. Reader names can be discovered via the SCard.ListReaders or SCard.FindSmartcards methods. If successful, the Atr and CardName properties will be set.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
CardDeleteFile
-- file_name is a string
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:card_delete_file(dir_name, file_name)
Deletes the file specified by dir_name and file_name. dir_name is the name of the directory that contains the file, or the empty string for root.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
DeleteCert
-- del_priv_key is a boolean
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:delete_cert(cert, del_priv_key)
Deletes a certificate and optionally its associated private key from the smart card. If del_priv_key is true, then the associated private key, if it exists, is also deleted.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
DeleteContext
local ok, err = sc_minidriver:delete_context()
This function reverses the effect of AcquireContext and severs the communication between the Base CSP/KSP and the card minidriver. The Atr and CardName properties are cleared.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
DeleteKeyContainer
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:delete_key_container(container_index)
Deletes the key container at the given container_index. This deletes both the signature and key exchange keys that may be contained in the specified key container.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
EnumFiles
-- st is a chilkat.StringTable
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:enum_files(dir_name, st)
Get the list of files in the directory specified by dir_name. Pass the empty string for the root directory. The filenames are returned in st.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
FindCert
-- part_value is a string
-- cert is a chilkat.Cert
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:find_cert(cert_part, part_value, cert)
Finds the certificate where the given cert_part equals the part_value. Possible values for cert_part are: subjectDN, subjectDN_withTags, subjectCN, serial, or serial:issuerCN.
The cert is loaded with the certificate if successful.
Note: If successful, the cert will be linked internally with this ScMinidriver session such that certificate can be used for signing on the smart card when used in other Chilkat classes such as XmlDSigGen, Pdf, Crypt2, Mime, MailMan, etc.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GenerateKey
-- key_spec is a string
-- key_type is a string
-- key_size is an integer
-- pin_id is a string
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:generate_key(container_index, key_spec, key_type, key_size, pin_id)
Generates a key to be stored in either the signature or key exchange location within a key container. Creates the key container if it does not already exist. Otherwise replaces the key in the key container.
The key_spec can be sig or kex to specify either the signature or key exchange location.
The key_type can be ecc or rsa.
For RSA keys, the key_size is the size of the key in bits, such as 1024, 2048, 4096, etc. (2048 is a typical value.) For ECC keys, the size can be 256, 384, or 521.
The pin_id can be user, or 3 through 7. (It is typically user.)
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetCardProperties
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:get_card_properties(json)
Gets all card properties and returns them in json. See the example below.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetCert
-- key_spec is a string
-- cert is a chilkat.Cert
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:get_cert(container_index, key_spec, cert)
Get the certificate at the specified container_index and key_spec. The key_spec can be sig or kex to specify either the signature or key exchange location within the container. The container_index can be -1 to choose the first key container with a certificate. The key_spec can also be any to choose either sig or kex based on which is present, with preference given to sig if both are present.
The cert is loaded with the certificate if successful.
Note: If successful, the cert will be linked internally with this ScMinidriver session such that certificate can be used for signing on the smart card when used in other Chilkat classes such as XmlDSigGen, Pdf, Crypt2, Mime, MailMan, etc.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetContainerKeys
-- sig_key is a chilkat.PublicKey
-- kex_key is a chilkat.PublicKey
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:get_container_keys(container_index, sig_key, kex_key)
Queries a key container to get the keys that are present. If the signature public key is present, it is returned in sig_key. If the key exchange key is present, it is returned in kex_key.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetCspContainerMap
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:get_csp_container_map(json)
Returns the contents of the CSP container map file (cmapfile). The information is returned in the json. This gives an overview of what key containers and certificates exist in the smart card from a CSP's point of view. See the example linked below.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
ImportCert
-- container_index is an integer
-- key_spec is a string
-- pin_id is a string
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:import_cert(cert, container_index, key_spec, pin_id)
Imports a certificate with its private key onto the smart card. The cert must have an accessible private key, such as will be the case if the cert was loaded from a .pfx/.p12, or if the cert was loaded from a Windows certificate store where the private key exists (and can be exported from the Windows certificate store).
The container_index is the container index. It can range from 0 to the MaxContainers-1.
The key_spec can be sig or kex to specify either the signature or key exchange location within the container.
The pin_id can be user, or 3 through 7. (It is typically user.)
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
ImportKey
-- key_spec is a string
-- priv_key is a chilkat.PrivateKey
-- pin_id is a string
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:import_key(container_index, key_spec, priv_key, pin_id)
Imports a key to be stored in either the signature or key exchange location within a key container. Creates the key container if it does not already exist. Otherwise replaces the specified key in the key container.
The key_spec can be sig or kex to specify either the signature or key exchange location.
The priv_key is the private key to import.
The ARG5 can be user, or 3 through 7. (It is typically user.)
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
ListCerts
-- st is a chilkat.StringTable
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:list_certs(cert_part, st)
Lists the certs found on the smart card. The cert_part indicates the information to be returned from each certificate. Possible values are: subjectDN, subjectDN_withTags, subjectCN, serial, or serial:issuerCN. The information is returned in st.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
PinAuthenticate
-- pin is a string
-- n is an integer
local n = sc_minidriver:pin_authenticate(pin_id, pin)
Performs regular PIN authentication. The pin_id can be user, admin, or 3 through 7. (It is typically user.) The pin is the alphanumeric PIN.
Returns 0 for success. If not successful, the return value indicates the number of attempts remaining before the PIN is locked. (The number of times an incorrect PIN may be presented to the card before the PIN is blocked, and requires the admin to unblock it.) If the PIN is already blocked, the return value is -1. If the method fails for some other reason, such as if a context has not yet been acquired, the return value is -2.
PinAuthenticateHex
-- pin is a string
-- n is an integer
local n = sc_minidriver:pin_authenticate_hex(pin_id, pin)
The same as PinAutheneticate, but the PIN is passed as a hex string. For example, to pass a PIN of 0x01, 0x02, 0x03, 0x04, pass 01020304.
PinChange
-- current_pin is a string
-- new_pin is a string
-- n is an integer
local n = sc_minidriver:pin_change(pin_id, current_pin, new_pin)
Changes a PIN. The pin_id can be user, admin, or 3 through 7. (It is typically user.) The current_pin is the current alphanumeric PIN. The new_pin is the new PIN.
Returns 0 for success. If not successful, the return value indicates the number of attempts remaining before the PIN is locked. (The number of times an incorrect PIN may be presented to the card before the PIN is blocked, and requires the admin to unblock it.) If the PIN is already blocked, the return value is -1. If the method fails for some other reason, such as if a context has not yet been acquired, the return value is -2.
PinDeauthenticate
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:pin_deauthenticate(pin_id)
Reverses a previous PIN authentication without resetting the card. The pin_id can be user, admin, or 3 through 7. (It is typically user.)
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
ReadFile
-- file_name is a string
-- bd is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:read_file(dir_name, file_name, bd)
Reads the entire file specified by dir_name and file_name into bd. dir_name is the name of the directory that contains the file, or the empty string for root.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SignData
-- key_spec is a string
-- hash_data_alg is a string
-- bd_data is a chilkat.BinData
-- bd_signed_data is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:sign_data(container_index, key_spec, hash_data_alg, bd_data, bd_signed_data)
Signs the data passed in bd_data. The hash_data_alg can be sha1, sha256, sha384, sha512, or none. If not equal to none, then the hash of the data passed in bd_data is signed.
The container_index specifies the key container. By specifying the key container, you are almost specifying the key. A key container can contain two keys: A signature key, and a key-exchange key. The key_spec indicates which of these two keys to use. key_spec should be set to sig or kex.
Note: The type of signature created, such as RSA or ECC, is determined by the type of key that exists in the key container (specified by container_index and key_spec). If it is an RSA key, additional options can be specified via the RsaPaddingScheme and RsaPaddingHash properties.
If successful, the signature is written to bd_signed_data.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
WriteFile
-- file_name is a string
-- bd is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = sc_minidriver:write_file(dir_name, file_name, bd)
Writes the entire file specified by dir_name and file_name. dir_name is the name of the directory that contains the file, or the empty string for root. The entire contents of bd are written to the file on the smart card.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.