Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkCertStore.h>
#include <C_CkZip.h>
#include <C_CkBinData.h>
#include <C_CkStringBuilder.h>
#include <C_CkCert.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCertStore certStore;
    BOOL readOnly;
    const char *pfxPassword;
    BOOL allSuccess;
    int numSuccess;
    HCkZip zip;
    HCkBinData certData;
    HCkStringBuilder sbFilename;
    HCkCert cert;
    int numCerts;
    int i;
    BOOL bHasPrivateKey;

    success = FALSE;

    certStore = CkCertStore_Create();

    readOnly = TRUE;
    success = CkCertStore_OpenCurrentUserStore(certStore,readOnly);
    if (!success) {
        printf("%s\n",CkCertStore_lastErrorText(certStore));
        CkCertStore_Dispose(certStore);
        return;
    }

    pfxPassword = "secret";

    allSuccess = TRUE;
    numSuccess = 0;

    zip = CkZip_Create();
    CkZip_NewZip(zip,"qa_output/personalCerts.zip");

    certData = CkBinData_Create();
    sbFilename = CkStringBuilder_Create();

    // Iterate over the certificates in the Current User store.
    cert = CkCert_Create();
    numCerts = CkCertStore_getNumCertificates(certStore);
    i = 0;
    while (i < numCerts) {
        CkCertStore_GetCert(certStore,i,cert);
        printf("DN = %s\n",CkCert_subjectDN(cert));

        CkStringBuilder_SetString(sbFilename,"cert");
        CkStringBuilder_AppendInt(sbFilename,i + 1);

        bHasPrivateKey = CkCert_HasPrivateKey(cert);
        if ((bHasPrivateKey == TRUE) && (CkCert_getPrivateKeyExportable(cert) == TRUE)) {
            // Export to a .pfx
            success = CkCert_ExportToPfxBd(cert,pfxPassword,TRUE,certData);
            if (success == TRUE) {
                CkStringBuilder_Append(sbFilename,".pfx");
                CkZip_AddBd(zip,CkStringBuilder_getAsString(sbFilename),certData);
            }

        }
        else {
            // Export to a .cer
            success = CkCert_ExportCertDerBd(cert,certData);
            if (success == TRUE) {
                CkStringBuilder_Append(sbFilename,".cer");
                CkZip_AddBd(zip,CkStringBuilder_getAsString(sbFilename),certData);
            }

        }

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

        i = i + 1;
    }

    if (numSuccess > 0) {
        success = CkZip_WriteZipAndClose(zip);
        if (success != TRUE) {
            printf("%s\n",CkZip_lastErrorText(zip));
            allSuccess = FALSE;
        }

    }

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


    CkCertStore_Dispose(certStore);
    CkZip_Dispose(zip);
    CkBinData_Dispose(certData);
    CkStringBuilder_Dispose(sbFilename);
    CkCert_Dispose(cert);

    }