Sample code for 30+ languages & platforms
C

Find a Certificate by it's SHA-1 Thumbprint

See more Cert Store Examples

Finds a certificate by it's SHA-1 hex thumbprint.

Note: This example requires Chilkat v10.1.2 or later.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkCertStore certStore;
    const char *thumbprint;
    BOOL bReadOnly;
    HCkJsonObject json;
    HCkCert cert;
    HCkCertStore certStorePem;

    success = FALSE;

    certStore = CkCertStore_Create();

    thumbprint = "12c1dd8015f3f03f7b1fa619dc24e2493ca8b4b2";

    //  This opens the Current User certificate store on Windows,
    //  On MacOS and iOS it opens the default Keychain.
    bReadOnly = TRUE;
    success = CkCertStore_OpenCurrentUserStore(certStore,bReadOnly);
    if (success == FALSE) {
        printf("%s\n",CkCertStore_lastErrorText(certStore));
        CkCertStore_Dispose(certStore);
        return;
    }

    //  Find the certificate having a specific SHA1 thumbprint.
    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"thumbprint",thumbprint);

    cert = CkCert_Create();
    success = CkCertStore_FindCert(certStore,json,cert);
    if (success == FALSE) {
        printf("Failed to find the certificate.\n");
        CkCertStore_Dispose(certStore);
        CkJsonObject_Dispose(json);
        CkCert_Dispose(cert);
        return;
    }

    printf("Found: %s\n",CkCert_subjectCN(cert));

    //  -------------------------------------------------------------------------------------
    //  Alternatively, one could load a certificate store object with certs from a PEM file,
    //  and do the same thing..
    certStorePem = CkCertStore_Create();

    success = CkCertStore_LoadPemFile(certStorePem,"pemFiles/certificates.pem");
    if (success == FALSE) {
        printf("%s\n",CkCertStore_lastErrorText(certStorePem));
        CkCertStore_Dispose(certStore);
        CkJsonObject_Dispose(json);
        CkCert_Dispose(cert);
        CkCertStore_Dispose(certStorePem);
        return;
    }

    success = CkCertStore_FindCert(certStore,json,cert);
    if (success == FALSE) {
        printf("Failed to find the certificate.\n");
        CkCertStore_Dispose(certStore);
        CkJsonObject_Dispose(json);
        CkCert_Dispose(cert);
        CkCertStore_Dispose(certStorePem);
        return;
    }

    printf("Found: %s\n",CkCert_subjectCN(cert));


    CkCertStore_Dispose(certStore);
    CkJsonObject_Dispose(json);
    CkCert_Dispose(cert);
    CkCertStore_Dispose(certStorePem);

    }