Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkCertStoreW.h>
#include <CkCertW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkCertStoreW certStore;

    // On MacOS and iOS, the OpenSmartcard method opens the Keychain.
    // The argument passed to OpenSmartcard is ignored.
    success = certStore.OpenSmartcard(L"");
    if (success == false) {
        wprintf(L"%s\n",certStore.lastErrorText());
        return;
    }

    int numCerts = certStore.get_NumCertificates();
    wprintf(L"numCerts = %d\n",numCerts);

    CkCertW cert;
    int 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.
        certStore.GetCert(i,cert);
        wprintf(L"%s\n",cert.subjectDN());
        wprintf(L"%s\n",cert.subjectCN());
        wprintf(L"%s\n",cert.serialNumber());
        if (cert.IsRsa() == true) {
            wprintf(L"key type is RSA\n");
        }

        if (cert.IsEcdsa() == true) {
            wprintf(L"key type is ECDSA\n");
        }

        wprintf(L"has private key: %d\n",cert.HasPrivateKey());
        wprintf(L"----\n");
        i = i + 1;
    }

    certStore.CloseCertStore();
    }