Sample code for 30+ languages & platforms
C

Load PFX/P12 File into Certificate Store Object

Demonstrates how to load a .pfx/.p12 into a certificate store object.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkCertStore certStore;
    const char *pfxPassword;
    HCkCert cert;
    int numCerts;
    int i;

    success = FALSE;

    certStore = CkCertStore_Create();

    //  This only loads the contents of the PFX file into the certStore object.
    //  It is not importing the PFX into the Windows certificate stores.
    pfxPassword = "badssl.com";
    success = CkCertStore_LoadPfxFile(certStore,"qa_data/pfx/badssl.com-client.p12",pfxPassword);
    if (success == FALSE) {
        printf("%s\n",CkCertStore_lastErrorText(certStore));
        CkCertStore_Dispose(certStore);
        return;
    }

    //  Examine each certificate (loaded from the PFX) in this certStore object
    cert = CkCert_Create();
    numCerts = CkCertStore_getNumCertificates(certStore);
    i = 0;
    while (i < numCerts) {
        CkCertStore_GetCert(certStore,i,cert);
        printf("hasPrivateKey=%d, %s\n",CkCert_HasPrivateKey(cert),CkCert_subjectCN(cert));
        i = i + 1;
    }



    CkCertStore_Dispose(certStore);
    CkCert_Dispose(cert);

    }