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 <C_CkCertStoreW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCertStoreW certStore;
    int numCerts;
    HCkCertW cert;
    int i;

    success = FALSE;

    certStore = CkCertStoreW_Create();

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

    numCerts = CkCertStoreW_getNumCertificates(certStore);
    wprintf(L"numCerts = %d\n",numCerts);

    cert = CkCertW_Create();
    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.
        CkCertStoreW_GetCert(certStore,i,cert);
        wprintf(L"%s\n",CkCertW_subjectDN(cert));
        wprintf(L"%s\n",CkCertW_subjectCN(cert));
        wprintf(L"%s\n",CkCertW_serialNumber(cert));
        if (CkCertW_IsRsa(cert) == TRUE) {
            wprintf(L"key type is RSA\n");
        }

        if (CkCertW_IsEcdsa(cert) == TRUE) {
            wprintf(L"key type is ECDSA\n");
        }

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

    CkCertStoreW_CloseCertStore(certStore);


    CkCertStoreW_Dispose(certStore);
    CkCertW_Dispose(cert);

    }