Sample code for 30+ languages & platforms
PureBasic

Apple Keychain - List Certs on Smartcards and USB Tokens

See more Apple Keychain Examples

Iterates over the certificatse on connected smartcards and USB tokens via the Apple Keychain.

Chilkat PureBasic Downloads

PureBasic
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

    ; On MacOS and iOS, the OpenSmartcard method opens the Keychain.
    ; The argument passed to OpenSmartcard is ignored.
    success = CkCertStore::ckOpenSmartcard(certStore,"")
    If success = 0
        Debug CkCertStore::ckLastErrorText(certStore)
        CkCertStore::ckDispose(certStore)
        ProcedureReturn
    EndIf

    numCerts.i = CkCertStore::ckNumCertificates(certStore)
    Debug "numCerts = " + Str(numCerts)

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

    i.i = 0
    While i < numCerts
        ; Note: Chilkat also gets the associated private key if it exists.
        ; You can simply use the cert in other places in Chilkat where a cert w/ private key is required.
        CkCertStore::ckGetCert(certStore,i,cert)
        Debug CkCert::ckSubjectDN(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::ckCloseCertStore(certStore)


    CkCertStore::ckDispose(certStore)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure