Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkCertStore.h>
#include <C_CkCert.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCertStore certStore;
    int numCerts;
    HCkCert cert;
    int i;

    success = FALSE;

    certStore = CkCertStore_Create();

    // On MacOS and iOS, the OpenSmartcard method opens the Keychain.
    // The argument passed to OpenSmartcard is ignored.
    success = CkCertStore_OpenSmartcard(certStore,"");
    if (success == FALSE) {
        printf("%s\n",CkCertStore_lastErrorText(certStore));
        CkCertStore_Dispose(certStore);
        return;
    }

    numCerts = CkCertStore_getNumCertificates(certStore);
    printf("numCerts = %d\n",numCerts);

    cert = CkCert_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.
        CkCertStore_GetCert(certStore,i,cert);
        printf("%s\n",CkCert_subjectDN(cert));
        printf("%s\n",CkCert_subjectCN(cert));
        printf("%s\n",CkCert_serialNumber(cert));
        if (CkCert_IsRsa(cert) == TRUE) {
            printf("key type is RSA\n");
        }

        if (CkCert_IsEcdsa(cert) == TRUE) {
            printf("key type is ECDSA\n");
        }

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

    CkCertStore_CloseCertStore(certStore);


    CkCertStore_Dispose(certStore);
    CkCert_Dispose(cert);

    }