Tcl
Tcl
ScMinidriver - Import a Certificate and Private Key to a Smart Card or USB Token
See more ScMinidriver Examples
Demonstrates how to import a certificate and its private key to a key container on a smart card or USB token.Note: This functionality was introduced in Chilkat v9.5.0.87.
Note: The ScMinidriver functionality is for Windows-only because ScMinidriver DLLs only exist on Windows.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set scmd [new_CkScMinidriver]
# Reader names (smart card readers or USB tokens) can be discovered
# via List Readers or Find Smart Cards
set readerName "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0"
set success [CkScMinidriver_AcquireContext $scmd $readerName]
if {$success == 0} then {
puts [CkScMinidriver_lastErrorText $scmd]
delete_CkScMinidriver $scmd
exit
}
# If successful, the name of the currently inserted smart card is available:
puts "Card name: [CkScMinidriver_cardName $scmd]"
# To import a cert + private key, we'll need to be PIN authenticated.
# For more details about smart card PIN authentication, see the Smart Card PIN Authentication Example
set pinId "user"
set retval [CkScMinidriver_PinAuthenticate $scmd $pinId "000000"]
if {$retval != 0} then {
puts "PIN Authentication failed."
CkScMinidriver_DeleteContext $scmd
delete_CkScMinidriver $scmd
exit
}
set cert [new_CkCert]
# Load the cert + private key from a .p12/.pfx
# We got this .p12 from https://badssl.com/download/
set password "badssl.com"
set success [CkCert_LoadPfxFile $cert "qa_data/pfx/badssl.com-client.p12" $password]
if {$success == 0} then {
puts [CkCert_lastErrorText $cert]
CkScMinidriver_DeleteContext $scmd
delete_CkScMinidriver $scmd
delete_CkCert $cert
exit
}
# Let's import this certificate as the "signature" key/cert in key container #6.
set containerIndex 6
set keySpec "sig"
set success [CkScMinidriver_ImportCert $scmd $cert $containerIndex $keySpec $pinId]
if {$success == 0} then {
puts [CkScMinidriver_lastErrorText $scmd]
} else {
puts "Successfully imported the cert + private key onto the smart card."
}
# When finished with operations that required authentication, you may if you wish, deauthenticate the session.
set success [CkScMinidriver_PinDeauthenticate $scmd "user"]
if {$success == 0} then {
puts [CkScMinidriver_lastErrorText $scmd]
}
# Delete the context when finished with the card.
set success [CkScMinidriver_DeleteContext $scmd]
if {$success == 0} then {
puts [CkScMinidriver_lastErrorText $scmd]
}
delete_CkScMinidriver $scmd
delete_CkCert $cert