Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.7.0+

Replace the Authenticode Signature of an Already Signed File

See more Code Signing Examples

Demonstrates how to sign a file that already has an Authenticode signature, for example to sign it with a renewed code signing certificate. Starting in Chilkat v11.7.0, AddSignature removes the existing signature and replaces it with the new one. Before v11.7.0, signing an already signed EXE or DLL added a second signature entry instead.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkCodeSignW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *path;
    HCkCodeSignW codeSign;
    HCkJsonObjectW sigInfo;
    HCkCertW signerCert;
    BOOL valid;
    HCkCertW cert;
    HCkJsonObjectW json;

    success = FALSE;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  Starting in Chilkat v11.7.0, AddSignature replaces the signature of a file that is already signed.
    //  (Before v11.7.0, signing an already signed EXE or DLL added a second signature entry
    //  instead of replacing the first.)
    //  
    //  This applies to every supported file type: EXE, DLL, MSI, PowerShell, and VBScript.

    path = L"C:/someDir/MyApp.exe";

    codeSign = CkCodeSignW_Create();
    sigInfo = CkJsonObjectW_Create();
    signerCert = CkCertW_Create();

    //  Find out who signed the file.
    valid = CkCodeSignW_VerifySignature(codeSign,path,sigInfo);
    if (valid == TRUE) {
        CkCodeSignW_GetSignerCert(codeSign,signerCert);
        wprintf(L"currently signed by: %s\n",CkCertW_subjectCN(signerCert));
        wprintf(L"certificate valid until: %s\n",CkCertW_validToStr(signerCert));
    }
    else {
        wprintf(L"The file is not signed, or its signature is not valid.\n");
    }

    //  Load the certificate to sign with.  It must have access to its private key.
    cert = CkCertW_Create();
    success = CkCertW_LoadPfxFile(cert,L"C:/someDir/myNewCodeSigningCert.pfx",L"pfx password");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkCodeSignW_Dispose(codeSign);
        CkJsonObjectW_Dispose(sigInfo);
        CkCertW_Dispose(signerCert);
        CkCertW_Dispose(cert);
        return;
    }

    //  Sign with SHA256, and ask a timestamp authority for a timestamp.  A timestamp lets the
    //  signature continue to validate after the code signing certificate has expired.
    //  
    //  {
    //    "hashAlg": "sha256",
    //    "timestampToken": {
    //      "enabled": true,
    //      "tsaUrl": "http://timestamp.digicert.com",
    //      "requestTsaCert": true,
    //      "hashAlg": "sha256"
    //    }
    //  }
    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"hashAlg",L"sha256");
    CkJsonObjectW_UpdateBool(json,L"timestampToken.enabled",TRUE);
    CkJsonObjectW_UpdateString(json,L"timestampToken.tsaUrl",L"http://timestamp.digicert.com");
    CkJsonObjectW_UpdateBool(json,L"timestampToken.requestTsaCert",TRUE);
    CkJsonObjectW_UpdateString(json,L"timestampToken.hashAlg",L"sha256");

    //  Sign the file.  It is not necessary to call RemoveSignature first:
    //  the existing signature is removed and the new signature takes its place.
    success = CkCodeSignW_AddSignature(codeSign,path,cert,json);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCodeSignW_lastErrorText(codeSign));
        CkCodeSignW_Dispose(codeSign);
        CkJsonObjectW_Dispose(sigInfo);
        CkCertW_Dispose(signerCert);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(json);
        return;
    }

    //  The file now has one signature, made with the new certificate.
    valid = CkCodeSignW_VerifySignature(codeSign,path,sigInfo);
    if (valid == FALSE) {
        wprintf(L"%s\n",CkCodeSignW_lastErrorText(codeSign));
        CkCodeSignW_Dispose(codeSign);
        CkJsonObjectW_Dispose(sigInfo);
        CkCertW_Dispose(signerCert);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(json);
        return;
    }

    success = CkCodeSignW_GetSignerCert(codeSign,signerCert);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCodeSignW_lastErrorText(codeSign));
        CkCodeSignW_Dispose(codeSign);
        CkJsonObjectW_Dispose(sigInfo);
        CkCertW_Dispose(signerCert);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(json);
        return;
    }

    wprintf(L"now signed by: %s\n",CkCertW_subjectCN(signerCert));
    wprintf(L"certificate valid until: %s\n",CkCertW_validToStr(signerCert));


    CkCodeSignW_Dispose(codeSign);
    CkJsonObjectW_Dispose(sigInfo);
    CkCertW_Dispose(signerCert);
    CkCertW_Dispose(cert);
    CkJsonObjectW_Dispose(json);

    }