Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

loCertStore = createobject("CkCertStore")

// On MacOS and iOS, the OpenSmartcard method opens the Keychain.
// The argument passed to OpenSmartcard is ignored.
llSuccess = loCertStore.OpenSmartcard("")
if (llSuccess = .F.) then
    ? loCertStore.LastErrorText
    release loCertStore
    return
endif

lnNumCerts = loCertStore.NumCertificates
? "numCerts = " + str(lnNumCerts)

loCert = createobject("CkCert")
i = 0
do while i < lnNumCerts
    // 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.
    loCertStore.GetCert(i,loCert)
    ? loCert.SubjectDN
    ? loCert.SubjectCN
    ? loCert.SerialNumber
    if (loCert.IsRsa() = .T.) then
        ? "key type is RSA"
    endif

    if (loCert.IsEcdsa() = .T.) then
        ? "key type is ECDSA"
    endif

    ? "has private key: " + str(loCert.HasPrivateKey())
    ? "----"
    i = i + 1
enddo

loCertStore.CloseCertStore()


release loCertStore
release loCert