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 <CkCertStore.h>
#include <CkCert.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkCertStore certStore;

    // On MacOS and iOS, the OpenSmartcard method opens the Keychain.
    // The argument passed to OpenSmartcard is ignored.
    success = certStore.OpenSmartcard("");
    if (success == false) {
        std::cout << certStore.lastErrorText() << "\r\n";
        return;
    }

    int numCerts = certStore.get_NumCertificates();
    std::cout << "numCerts = " << numCerts << "\r\n";

    CkCert 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);
        std::cout << cert.subjectDN() << "\r\n";
        std::cout << cert.subjectCN() << "\r\n";
        std::cout << cert.serialNumber() << "\r\n";
        if (cert.IsRsa() == true) {
            std::cout << "key type is RSA" << "\r\n";
        }

        if (cert.IsEcdsa() == true) {
            std::cout << "key type is ECDSA" << "\r\n";
        }

        std::cout << "has private key: " << cert.HasPrivateKey() << "\r\n";
        std::cout << "----" << "\r\n";
        i = i + 1;
    }

    certStore.CloseCertStore();
    }