Sample code for 30+ languages & platforms
Unicode C

Load PKCS12 / PFX and Access Contents

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkPfxW.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPfxW pfx;
    int numPrivateKeys;
    HCkPrivateKeyW privKey;
    int i;
    HCkCertW cert;
    int numCerts;

    success = FALSE;

    pfx = CkPfxW_Create();

    // Load the PKCS12 from a file
    success = CkPfxW_LoadPfxFile(pfx,L"/someDir/my.p12",L"pfxFilePassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPfxW_lastErrorText(pfx));
        CkPfxW_Dispose(pfx);
        return;
    }

    numPrivateKeys = CkPfxW_getNumPrivateKeys(pfx);

    privKey = CkPrivateKeyW_Create();

    wprintf(L"Private Keys:\n");

    i = 0;
    while (i < numPrivateKeys) {
        CkPfxW_PrivateKeyAt(pfx,i,privKey);

        // Do something with the private key ...

        i = i + 1;
    }

    cert = CkCertW_Create();

    numCerts = CkPfxW_getNumCerts(pfx);

    wprintf(L"Certs:\n");
    i = 0;
    while (i < numCerts) {
        CkPfxW_CertAt(pfx,i,cert);
        wprintf(L"%s\n",CkCertW_subjectDN(cert));

        // If the certificate has a private key (one of the private keys within the PFX)
        // then it can also be obtained via the certificate object:
        if (CkCertW_HasPrivateKey(cert) == TRUE) {

            wprintf(L"Has private key!\n");

            success = CkCertW_GetPrivateKey(cert,privKey);
            // ...

        }

        i = i + 1;
    }



    CkPfxW_Dispose(pfx);
    CkPrivateKeyW_Dispose(privKey);
    CkCertW_Dispose(cert);

    }