Sample code for 30+ languages & platforms
PureBasic

PKCS11 Find Driver Files

See more PKCS11 Examples

Uses Chilkat's SCard class to find smartcards/tokens present on the system (Windows/Linux/Mac/etc.) to return information about the expected PKCS11 drivers (DLL, shared lib, dylib) and the confirmed existing PKCS11 drivers present on the system.

Once the PKCS11 driver file is discovered, it can be used as a starting point with the PKCS11 class, which is shown in this example.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkPkcs11.pb"
IncludeFile "CkSCard.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    ; First discover the smart cards and tokens present on the system..
    scard.i = CkSCard::ckCreate()
    If scard.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)

    success = CkSCard::ckFindSmartcards(scard,json)
    If success = 0
        Debug CkSCard::ckLastErrorText(scard)
        CkSCard::ckDispose(scard)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    Debug CkJsonObject::ckEmit(json)

    ; Sample output:
    ; Note: This example was tested on Windows, but it can also run on Linux and MacOSX.

    ; {
    ;   "reader": [
    ;     {
    ;       "name": "Gemalto USB SmartCard Reader 0",
    ;       "state": "present,inuse",
    ;       "vendorName": "Gemalto",
    ;       "serialNumber": "3339424644374543",
    ;       "systemName": "Gemalto USB SmartCard Reader 0",
    ;       "card": {
    ;         "atr": "3B7F96000080318065B084565110120FFE829000",
    ;         "windows": {
    ;           "miniDriver": "AxaltoCM.dll",
    ;           "cryptoProvider": "Microsoft Base Smart Card Crypto Provider",
    ;           "keyStorageProvider": "Microsoft Smart Card Key Storage Provider"
    ;         },
    ;         "pkcs11_driver": {
    ;           "possible": [
    ;             "C:/Program Files/SafeNet/Authentication/SAC/x64/IDPrimePKCS1164.dll",
    ;             "C:/Program Files(x86)/Gemalto/IDGo 800 PKCS#11/IDPrimePKCS1164.dll",
    ;             "C:/Program Files/Gemalto/IDGo 800 PKCS#11/IDPrimePKCS1164.dll",
    ;             "C:/WINDOWS/SysWOW64/IDPrimePKCS1164.dll",
    ;             "C:/WINDOWS/SysWOW64/eTPKCS11.dll"
    ;           ],
    ;           "exists": [
    ;             "C:/Program Files/SafeNet/Authentication/SAC/x64/IDPrimePKCS1164.dll",
    ;             "C:/WINDOWS/SysWOW64/eTPKCS11.dll"
    ;           ]
    ;         }
    ;       }
    ;     }
    ;   ]
    ; }

    ; We're interested in the PKCS11 drivers that were found.

    ; Find the 1st reader where the card is present.  (The "state" contains the keyword "present")
    json2.i = CkJsonObject::ckCreate()
    If json2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckFindRecord2(json,"reader","state","*present*",0,json2)
    If success = 0
        Debug "No readers exist with a smart card or token present."
        CkSCard::ckDispose(scard)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    ; Get the size of the "card.pkcs11_driver.exists" array.  
    ; If it doesn't exist the size will be -1.
    numExists.i = CkJsonObject::ckSizeOfArray(json2,"card.pkcs11_driver.exists")
    If numExists < 1
        Debug "No PKCS11 driver files found for the smart card."
        CkSCard::ckDispose(scard)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    ; Get the path of the 1st available PKCS11 driver.
    pkcs11DriverPath.s = CkJsonObject::ckStringOf(json2,"card.pkcs11_driver.exists[0]")
    Debug "PKCS11 driver path = " + pkcs11DriverPath

    ; Now that we have a driver file, we can use it for PKCS11 access..
    pkcs11.i = CkPkcs11::ckCreate()
    If pkcs11.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkPkcs11::setCkSharedLibPath(pkcs11, pkcs11DriverPath)

    success = CkPkcs11::ckInitialize(pkcs11)
    If success = 0
        Debug CkPkcs11::ckLastErrorText(pkcs11)
        CkSCard::ckDispose(scard)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(json2)
        CkPkcs11::ckDispose(pkcs11)
        ProcedureReturn
    EndIf

    ; Get information about the smart card via PKCS11..
    jsonPkcs11.i = CkJsonObject::ckCreate()
    If jsonPkcs11.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPkcs11::ckDiscover(pkcs11,1,jsonPkcs11)
    If success = 0
        Debug CkPkcs11::ckLastErrorText(pkcs11)
        CkSCard::ckDispose(scard)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(json2)
        CkPkcs11::ckDispose(pkcs11)
        CkJsonObject::ckDispose(jsonPkcs11)
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(jsonPkcs11, 0)
    Debug CkJsonObject::ckEmit(jsonPkcs11)

    ; Use this online tool to generate parsing code from sample JSON: 
    ; Generate Parsing Code from JSON

    ; Sample output:

    ; {
    ;   "cryptokiVersion": {
    ;     "major": 2,
    ;     "minor": 20
    ;   },
    ;   "manufacturerID": "Gemalto",
    ;   "libraryDescription": "Gemalto PKCS11",
    ;   "libraryVersion": {
    ;     "major": 10,
    ;     "minor": 8
    ;   },
    ;   "slot": [
    ;     {
    ;       "id": 0,
    ;       "slotDescription": "Gemalto USB SmartCard Reader 0",
    ;       "manufacturerID": "Gemalto",
    ;       "tokenPresent": true,
    ;       "removableDevice": true,
    ;       "hardwareSlot": true,
    ;       "hardwareVersion": {
    ;         "major": 0,
    ;         "minor": 0
    ;       },
    ;       "firmwareVersion": {
    ;         "major": 0,
    ;         "minor": 0
    ;       },
    ;       "token": {
    ;         "label": "chilkat",
    ;         "manufacturerID": "Gemalto",
    ;         "model": "ID Prime MD",
    ;         "serialNumber": "2DD70CC46070255E",
    ;         "flags": [
    ;           "CKF_RNG",
    ;           "CKF_LOGIN_REQUIRED",
    ;           "CKF_USER_PIN_INITIALIZED",
    ;           "CKF_DUAL_CRYPTO_OPERATIONS",
    ;           "CKF_TOKEN_INITIALIZED",
    ;           "CKF_SO_PIN_COUNT_LOW",
    ;           "CKF_SO_PIN_LOCKED"
    ;         ],
    ;         "maxSessionCount": 0,
    ;         "sessionCount": 0,
    ;         "maxRwSessionCount": 0,
    ;         "rwSessionCount": 0,
    ;         "maxPinLen": 16,
    ;         "minPinLen": 4,
    ;         "totalPublicMemory": 32768,
    ;         "freePublicMemory": 32720,
    ;         "totalPrivateMemory": 32768,
    ;         "freePrivateMemory": 32720,
    ;         "hardwareVersion": {
    ;           "major": 0,
    ;           "minor": 0
    ;         },
    ;         "firmwareVersion": {
    ;           "major": 0,
    ;           "minor": 0
    ;         },
    ;         "utcTime": "",
    ;         "mechanism": [
    ;           "CKM_DES3_MAC",
    ;           "CKM_DES3_MAC_GENERAL",
    ;           "CKM_AES_MAC",
    ;           "CKM_AES_MAC_GENERAL",
    ;           "CKM_DES3_CBC",
    ;           "CKM_DES3_CBC_PAD",
    ;           "CKM_AES_CBC",
    ;           "CKM_AES_CBC_PAD",
    ;           "CKM_AES_CTR",
    ;           "CKM_AES_CCM",
    ;           "CKM_RSA_PKCS_KEY_PAIR_GEN",
    ;           "CKM_RSA_PKCS",
    ;           "CKM_RSA_PKCS_OAEP",
    ;           "CKM_RSA_PKCS_PSS",
    ;           "CKM_SHA256_RSA_PKCS_PSS",
    ;           "CKM_SHA384_RSA_PKCS_PSS",
    ;           "CKM_SHA512_RSA_PKCS_PSS",
    ;           "CKM_SHA256_RSA_PKCS",
    ;           "CKM_SHA384_RSA_PKCS",
    ;           "CKM_SHA512_RSA_PKCS",
    ;           "CKM_EC_KEY_PAIR_GEN",
    ;           "CKM_ECDSA",
    ;           "CKM_ECDSA_SHA256",
    ;           "CKM_ECDSA_SHA384",
    ;           "80000045",
    ;           "CKM_ECDSA_SHA512",
    ;           "CKM_ECDH1_DERIVE",
    ;           "CKM_DES3_KEY_GEN",
    ;           "CKM_AES_KEY_GEN",
    ;           "CKM_PBE_SHA1_DES3_EDE_CBC",
    ;           "CKM_GENERIC_SECRET_KEY_GEN",
    ;           "CKM_PBA_SHA1_WITH_SHA1_HMAC",
    ;           "CKM_PKCS5_PBKD2",
    ;           "CKM_SHA_1_HMAC_GENERAL",
    ;           "CKM_SHA_1_HMAC",
    ;           "CKM_SHA256_HMAC_GENERAL",
    ;           "CKM_SHA256_HMAC",
    ;           "CKM_SHA384_HMAC_GENERAL",
    ;           "CKM_SHA384_HMAC",
    ;           "CKM_SHA512_HMAC_GENERAL",
    ;           "CKM_SHA512_HMAC",
    ;           "CKM_SHA_1",
    ;           "CKM_SHA256",
    ;           "CKM_SHA384",
    ;           "CKM_SHA512",
    ;           "80006001"
    ;         ],
    ;         "rsa": {
    ;           "minKeySize": 2048,
    ;           "maxKeySize": 2048
    ;         }
    ;       }
    ;     }
    ;   ]
    ; }
    ; 


    CkSCard::ckDispose(scard)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(json2)
    CkPkcs11::ckDispose(pkcs11)
    CkJsonObject::ckDispose(jsonPkcs11)


    ProcedureReturn
EndProcedure