PureBasic
PureBasic
Open Smartcard Certificate Store (or from USB Token)
See more Certificates Examples
Demonstrates how to open the certificate store of the smart card currently in the reader (or the USB token). Iterates over the certs found on the smartcard.Chilkat PureBasic Downloads
IncludeFile "CkCert.pb"
IncludeFile "CkCertStore.pb"
Procedure ChilkatExample()
success.i = 0
certStore.i = CkCertStore::ckCreate()
If certStore.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Access the certificates on the smart card or USB token via the Chilkat certificate store class.
; Note: Always pass the empty string to OpenSmartcard.
; ---------------------------------------------------------------------------------------------------------
; The following is true only for Chilkat v10.1.1 and earlier:
; Also, the Chilkat CertStore class can only use MS CNG or CryptoAPI.
; Some smartcard/USB token drivers only support PKCS11 or ScMinidriver.
; You may get better results using Chilkat.Cert.LoadFromSmartcard because
; Cert.LoadFromSmartcard can automatically detect and utilize PKCS11, ScMinidriver, CNG, and CryptoAPI.
; ---------------------------------------------------------------------------------------------------------
; Starting in Chilkat versions after v10.1.1, OpenSmartcard also works with
; Apple Keychain and PKCS11 drivers on Windows, Linux, and MacOS.
; ---------------------------------------------------------------------------------------------------------
success = CkCertStore::ckOpenSmartcard(certStore,"")
If success = 0
Debug CkCertStore::ckLastErrorText(certStore)
CkCertStore::ckDispose(certStore)
ProcedureReturn
EndIf
Debug CkCertStore::ckLastErrorText(certStore)
; Iterate over certificates on the smartcard.
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i = 0
numCerts.i = CkCertStore::ckNumCertificates(certStore)
Debug "numCerts = " + Str(numCerts)
While (i < numCerts)
CkCertStore::ckGetCert(certStore,i,cert)
Debug CkCert::ckSubjectCN(cert)
Debug CkCert::ckSerialNumber(cert)
If CkCert::ckIsRsa(cert) = 1
Debug "key type is RSA"
EndIf
If CkCert::ckIsEcdsa(cert) = 1
Debug "key type is ECDSA"
EndIf
Debug "has private key: " + Str(CkCert::ckHasPrivateKey(cert))
Debug "----"
i = i + 1
Wend
CkCertStore::ckDispose(certStore)
CkCert::ckDispose(cert)
ProcedureReturn
EndProcedure