Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_CertStore
oleobject loo_Cert
integer i
integer li_NumCerts

li_Success = 0

loo_CertStore = create oleobject
li_rc = loo_CertStore.ConnectToNewObject("Chilkat.CertStore")
if li_rc < 0 then
    destroy loo_CertStore
    MessageBox("Error","Connecting to COM object failed")
    return
end if

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

li_Success = loo_CertStore.OpenSmartcard("")
if li_Success = 0 then
    Write-Debug loo_CertStore.LastErrorText
    destroy loo_CertStore
    return
end if

Write-Debug loo_CertStore.LastErrorText

// Iterate over certificates on the smartcard.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

i = 0
li_NumCerts = loo_CertStore.NumCertificates
Write-Debug "numCerts = " + string(li_NumCerts)

do while (i < li_NumCerts)
    loo_CertStore.GetCert(i,loo_Cert)
    Write-Debug loo_Cert.SubjectCN
    Write-Debug loo_Cert.SerialNumber
    if loo_Cert.IsRsa() = 1 then
        Write-Debug "key type is RSA"
    end if

    if loo_Cert.IsEcdsa() = 1 then
        Write-Debug "key type is ECDSA"
    end if

    Write-Debug "has private key: " + string(loo_Cert.HasPrivateKey())
    Write-Debug "----"
    i = i + 1
loop


destroy loo_CertStore
destroy loo_Cert