Sample code for 30+ languages & platforms
Unicode C

Backup Windows Current User / Personal Certificates to a .zip

See more Certificates Examples

Demonstrates how to backup the certificates in the Windows registry-based Current User certificate store (in the "Personal" Logical Store as seen in certmgr.msc), to a zip archive. Certificates having an exportable private key are exported to .pfx files. Certificates with no private key, or with a non-exportable private key, are exported to .cer files.

Obviously, this example only runs on Windows computers.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkCertStoreW.h>
#include <C_CkZipW.h>
#include <C_CkBinDataW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCertStoreW certStore;
    BOOL readOnly;
    const wchar_t *pfxPassword;
    BOOL allSuccess;
    int numSuccess;
    HCkZipW zip;
    HCkBinDataW certData;
    HCkStringBuilderW sbFilename;
    HCkCertW cert;
    int numCerts;
    int i;
    BOOL bHasPrivateKey;

    success = FALSE;

    certStore = CkCertStoreW_Create();

    readOnly = TRUE;
    success = CkCertStoreW_OpenCurrentUserStore(certStore,readOnly);
    if (!success) {
        wprintf(L"%s\n",CkCertStoreW_lastErrorText(certStore));
        CkCertStoreW_Dispose(certStore);
        return;
    }

    pfxPassword = L"secret";

    allSuccess = TRUE;
    numSuccess = 0;

    zip = CkZipW_Create();
    CkZipW_NewZip(zip,L"qa_output/personalCerts.zip");

    certData = CkBinDataW_Create();
    sbFilename = CkStringBuilderW_Create();

    // Iterate over the certificates in the Current User store.
    cert = CkCertW_Create();
    numCerts = CkCertStoreW_getNumCertificates(certStore);
    i = 0;
    while (i < numCerts) {
        CkCertStoreW_GetCert(certStore,i,cert);
        wprintf(L"DN = %s\n",CkCertW_subjectDN(cert));

        CkStringBuilderW_SetString(sbFilename,L"cert");
        CkStringBuilderW_AppendInt(sbFilename,i + 1);

        bHasPrivateKey = CkCertW_HasPrivateKey(cert);
        if ((bHasPrivateKey == TRUE) && (CkCertW_getPrivateKeyExportable(cert) == TRUE)) {
            // Export to a .pfx
            success = CkCertW_ExportToPfxBd(cert,pfxPassword,TRUE,certData);
            if (success == TRUE) {
                CkStringBuilderW_Append(sbFilename,L".pfx");
                CkZipW_AddBd(zip,CkStringBuilderW_getAsString(sbFilename),certData);
            }

        }
        else {
            // Export to a .cer
            success = CkCertW_ExportCertDerBd(cert,certData);
            if (success == TRUE) {
                CkStringBuilderW_Append(sbFilename,L".cer");
                CkZipW_AddBd(zip,CkStringBuilderW_getAsString(sbFilename),certData);
            }

        }

        if (success != TRUE) {
            allSuccess = FALSE;
        }
        else {
            numSuccess = numSuccess + 1;
        }

        i = i + 1;
    }

    if (numSuccess > 0) {
        success = CkZipW_WriteZipAndClose(zip);
        if (success != TRUE) {
            wprintf(L"%s\n",CkZipW_lastErrorText(zip));
            allSuccess = FALSE;
        }

    }

    wprintf(L"All success = %d\n",allSuccess);


    CkCertStoreW_Dispose(certStore);
    CkZipW_Dispose(zip);
    CkBinDataW_Dispose(certData);
    CkStringBuilderW_Dispose(sbFilename);
    CkCertW_Dispose(cert);

    }