Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vCert
Store    Handle hoCertStore
    Variant vCert
    Handle hoCert
    Integer i
    Integer iNumCerts
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatCertStore)) To hoCertStore
    If (Not(IsComObjectCreated(hoCertStore))) Begin
        Send CreateComObject of hoCertStore
    End

    // 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.
    // ---------------------------------------------------------------------------------------------------------

    Get ComOpenSmartcard Of hoCertStore "" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCertStore To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLastErrorText Of hoCertStore To sTemp1
    Showln sTemp1

    // Iterate over certificates on the smartcard.
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Move 0 To i
    Get ComNumCertificates Of hoCertStore To iNumCerts
    Showln "numCerts = " iNumCerts

    While (i < iNumCerts)
        Get pvComObject of hoCert to vCert
        Get ComGetCert Of hoCertStore i vCert To iSuccess
        Get ComSubjectCN Of hoCert To sTemp1
        Showln sTemp1
        Get ComSerialNumber Of hoCert To sTemp1
        Showln sTemp1
        Get ComIsRsa Of hoCert To bTemp1
        If (bTemp1 = True) Begin
            Showln "key type is RSA"
        End

        Get ComIsEcdsa Of hoCert To bTemp1
        If (bTemp1 = True) Begin
            Showln "key type is ECDSA"
        End

        Get ComHasPrivateKey Of hoCert To bTemp1
        Showln "has private key: " bTemp1
        Showln "----"
        Move (i + 1) To i
    Loop



End_Procedure