Class ScMinidriver
- All Implemented Interfaces:
AutoCloseable
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
Use the Windows smart-card minidriver layer to access card capabilities without working directly at the APDU level.
Certificates and containers
Enumerate and inspect smart-card certificates, key containers, container indexes, key types, and related card state.
PIN authentication
Authenticate to the card with the required PIN before accessing protected private-key operations or card objects.
On-card signing
Sign data using private keys that remain on the smart card, including workflows where the private key is non-exportable.
Key generation and import
Generate or import RSA and ECC keys into supported card containers when the card policy allows it.
Use with Chilkat signing
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.
Common pattern:
Connect to the smart card, verify the card and container state, authenticate
with the PIN, locate the certificate and private-key container, then either
sign directly or link the certificate for use by another Chilkat signing
class. Use 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.
For an extended overview, see ScMinidriver Class Overview.
</section>
Available on Windows only: the constructor throws UnsupportedOperationException elsewhere.
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidacquireContext(String readerName)Initializes communication with the card inserted in the given reader.voidcardDeleteFile(String dirName, String fileName)Deletes the file specified bydirNameandfileName.voiddeleteCert(Cert cert, boolean delPrivKey)Deletes a certificate and optionally its associated private key from the smart card.voidThis function reverses the effect of AcquireContext and severs the communication between the Base CSP/KSP and the card minidriver.voiddeleteKeyContainer(int containerIndex)Deletes the key container at the givencontainerIndex.voidenumFiles(String dirName, StringTable st)Get the list of files in the directory specified bydirName.voidFinds the certificate where the givencertPartequals thepartValue.voidgenerateKey(int containerIndex, String keySpec, String keyType, int keySize, String pinId)Generates a key to be stored in either thesignatureorkey exchangelocation within a key container.getAtr()The ATR of the card in the reader.The name of the card in the reader.voidgetCardProperties(JsonObject json)Gets all card properties and returns them injson.voidGet the certificate at the specifiedcontainerIndexandkeySpec.voidgetContainerKeys(int containerIndex, PublicKey sigKey, PublicKey kexKey)Queries a key container to get the keys that are present.voidgetCspContainerMap(JsonObject json)Returns the contents of the CSP container map file (cmapfile).intThe maximum number of key containers available.If an RSA key is used for signing, this is the hash algorithm to used in conjunction with the padding scheme.If an RSA key is used for signing, this is the padding scheme to use.This is a catch-all property to be used for uncommon needs.voidimportCert(Cert cert, int containerIndex, String keySpec, String pinId)Imports a certificate with its private key onto the smart card.voidimportKey(int containerIndex, String keySpec, PrivateKey privKey, String pinId)Imports a key to be stored in either thesignatureorkey exchangelocation within a key container.voidlistCerts(String certPart, StringTable st)Lists the certs found on the smart card.intpinAuthenticate(String pinId, String pin)Performs regular PIN authentication.intpinAuthenticateHex(String pinId, String pin)The same as PinAutheneticate, but the PIN is passed as a hex string.intChanges a PIN.voidpinDeauthenticate(String pinId)Reverses a previous PIN authentication without resetting the card.voidReads the entire file specified bydirNameandfileNameintobd.voidsetRsaPaddingHash(String value)Sets theRsaPaddingHashproperty; seegetRsaPaddingHash().voidsetRsaPaddingScheme(String value)Sets theRsaPaddingSchemeproperty; seegetRsaPaddingScheme().voidsetUncommonOptions(String value)Sets theUncommonOptionsproperty; seegetUncommonOptions().voidsignData(int containerIndex, String keySpec, String hashDataAlg, BinData bdData, BinData bdSignedData)Signs the data passed inbdData.voidWrites the entire file specified bydirNameandfileName.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
ScMinidriver
public ScMinidriver()Creates a newScMinidriverobject.
-
-
Method Details
-
getAtr
The ATR of the card in the reader. This property is set by the AquireContext method.- Returns:
- the value of the
Atrproperty (read-only)
-
getCardName
The name of the card in the reader. This property is set by the AquireContext method.- Returns:
- the value of the
CardNameproperty (read-only)
-
getMaxContainers
public int getMaxContainers()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.- Returns:
- the value of the
MaxContainersproperty (read-only)
-
getRsaPaddingHash
If an RSA key is used for signing, this is the hash algorithm to used in conjunction with the padding scheme. It can beSHA1,SHA256,SHA384, orSHA512. The default isSHA256.- Returns:
- the value of the
RsaPaddingHashproperty
-
setRsaPaddingHash
Sets theRsaPaddingHashproperty; seegetRsaPaddingHash().- Parameters:
value- the new value (notnull)
-
getRsaPaddingScheme
If an RSA key is used for signing, this is the padding scheme to use. It can bePKCSorPSS. The default isPSS.- Returns:
- the value of the
RsaPaddingSchemeproperty
-
setRsaPaddingScheme
Sets theRsaPaddingSchemeproperty; seegetRsaPaddingScheme().- Parameters:
value- the new value (notnull)
-
getUncommonOptions
This is a catch-all property to be used for uncommon needs. This property defaults to the empty string and should typically remain empty.- Returns:
- the value of the
UncommonOptionsproperty
-
setUncommonOptions
Sets theUncommonOptionsproperty; seegetUncommonOptions().- Parameters:
value- the new value (notnull)
-
acquireContext
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.- Parameters:
readerName- thereaderNameargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
cardDeleteFile
Deletes the file specified bydirNameandfileName.dirNameis the name of the directory that contains the file, or the empty string for root.- Parameters:
dirName- thedirNameargument (notnull)fileName- thefileNameargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
deleteCert
Deletes a certificate and optionally its associated private key from the smart card. IfdelPrivKeyistrue, then the associated private key, if it exists, is also deleted.- Parameters:
cert- thecertargument (notnull)delPrivKey- thedelPrivKeyargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
deleteContext
public void deleteContext()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.- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
deleteKeyContainer
public void deleteKeyContainer(int containerIndex)Deletes the key container at the givencontainerIndex. This deletes both thesignatureandkey exchangekeys that may be contained in the specified key container.- Parameters:
containerIndex- thecontainerIndexargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
enumFiles
Get the list of files in the directory specified bydirName. Pass the empty string for the root directory. The filenames are returned inst.- Parameters:
dirName- thedirNameargument (notnull)st- thestargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
findCert
Finds the certificate where the givencertPartequals thepartValue. Possible values forcertPartare:subjectDN,subjectDN_withTags,subjectCN,serial, orserial:issuerCN.The
certis loaded with the certificate if successful.Note: If successful, the
certwill 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.- Parameters:
certPart- thecertPartargument (notnull)partValue- thepartValueargument (notnull)cert- thecertargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
generateKey
public void generateKey(int containerIndex, String keySpec, String keyType, int keySize, String pinId)Generates a key to be stored in either thesignatureorkey exchangelocation within a key container. Creates the key container if it does not already exist. Otherwise replaces the key in the key container.The
keySpeccan besigorkexto specify either thesignatureorkey exchangelocation.The
keyTypecan beeccorrsa.For RSA keys, the
keySizeis 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
pinIdcan beuser, or3through7. (It is typicallyuser.)- Parameters:
containerIndex- thecontainerIndexargumentkeySpec- thekeySpecargument (notnull)keyType- thekeyTypeargument (notnull)keySize- thekeySizeargumentpinId- thepinIdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getCardProperties
Gets all card properties and returns them injson. See the example below.- Parameters:
json- thejsonargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getCert
Get the certificate at the specifiedcontainerIndexandkeySpec. ThekeySpeccan besigorkexto specify either thesignatureorkey exchangelocation within the container. ThecontainerIndexcan be -1 to choose the first key container with a certificate. ThekeySpeccan also beanyto choose eithersigorkexbased on which is present, with preference given tosigif both are present.The
certis loaded with the certificate if successful.Note: If successful, the
certwill 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.- Parameters:
containerIndex- thecontainerIndexargumentkeySpec- thekeySpecargument (notnull)cert- thecertargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getContainerKeys
Queries a key container to get the keys that are present. If the signature public key is present, it is returned insigKey. If the key exchange key is present, it is returned inkexKey.- Parameters:
containerIndex- thecontainerIndexargumentsigKey- thesigKeyargument (notnull)kexKey- thekexKeyargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getCspContainerMap
Returns the contents of the CSP container map file (cmapfile). The information is returned in thejson. 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.- Parameters:
json- thejsonargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
importCert
Imports a certificate with its private key onto the smart card. Thecertmust have an accessible private key, such as will be the case if thecertwas loaded from a .pfx/.p12, or if thecertwas loaded from a Windows certificate store where the private key exists (and can be exported from the Windows certificate store).The
containerIndexis the container index. It can range from 0 to the MaxContainers-1.The
keySpeccan besigorkexto specify either thesignatureorkey exchangelocation within the container.The
pinIdcan beuser, or3through7. (It is typicallyuser.)- Parameters:
cert- thecertargument (notnull)containerIndex- thecontainerIndexargumentkeySpec- thekeySpecargument (notnull)pinId- thepinIdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
importKey
Imports a key to be stored in either thesignatureorkey exchangelocation within a key container. Creates the key container if it does not already exist. Otherwise replaces the specified key in the key container.The
keySpeccan besigorkexto specify either thesignatureorkey exchangelocation.The
privKeyis the private key to import.The ARG5 can be
user, or3through7. (It is typicallyuser.)- Parameters:
containerIndex- thecontainerIndexargumentkeySpec- thekeySpecargument (notnull)privKey- theprivKeyargument (notnull)pinId- thepinIdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
listCerts
Lists the certs found on the smart card. ThecertPartindicates the information to be returned from each certificate. Possible values are:subjectDN,subjectDN_withTags,subjectCN,serial, orserial:issuerCN. The information is returned inst.- Parameters:
certPart- thecertPartargument (notnull)st- thestargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
pinAuthenticate
Performs regular PIN authentication. ThepinIdcan beuser,admin, or3through7. (It is typicallyuser.) Thepinis 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.
- Parameters:
pinId- thepinIdargument (notnull)pin- thepinargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
pinAuthenticateHex
The same as PinAutheneticate, but the PIN is passed as a hex string. For example, to pass a PIN of 0x01, 0x02, 0x03, 0x04, pass01020304.- Parameters:
pinId- thepinIdargument (notnull)pin- thepinargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
pinChange
Changes a PIN. ThepinIdcan beuser,admin, or3through7. (It is typicallyuser.) ThecurrentPinis the current alphanumeric PIN. ThenewPinis 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.
- Parameters:
pinId- thepinIdargument (notnull)currentPin- thecurrentPinargument (notnull)newPin- thenewPinargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
pinDeauthenticate
Reverses a previous PIN authentication without resetting the card. ThepinIdcan beuser,admin, or3through7. (It is typicallyuser.)- Parameters:
pinId- thepinIdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
readFile
Reads the entire file specified bydirNameandfileNameintobd.dirNameis the name of the directory that contains the file, or the empty string for root.- Parameters:
dirName- thedirNameargument (notnull)fileName- thefileNameargument (notnull)bd- thebdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
signData
public void signData(int containerIndex, String keySpec, String hashDataAlg, BinData bdData, BinData bdSignedData)Signs the data passed inbdData. ThehashDataAlgcan besha1,sha256,sha384,sha512, ornone. If not equal tonone, then the hash of the data passed inbdDatais signed.The
containerIndexspecifies 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. ThekeySpecindicates which of these two keys to use.keySpecshould be set tosigorkex.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
containerIndexandkeySpec). If it is an RSA key, additional options can be specified via the RsaPaddingScheme and RsaPaddingHash properties.If successful, the signature is written to
bdSignedData.- Parameters:
containerIndex- thecontainerIndexargumentkeySpec- thekeySpecargument (notnull)hashDataAlg- thehashDataAlgargument (notnull)bdData- thebdDataargument (notnull)bdSignedData- thebdSignedDataargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
writeFile
Writes the entire file specified bydirNameandfileName.dirNameis the name of the directory that contains the file, or the empty string for root. The entire contents ofbdare written to the file on the smart card.- Parameters:
dirName- thedirNameargument (notnull)fileName- thefileNameargument (notnull)bd- thebdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-