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

C
#include <C_CkCodeSign.h>
#include <C_CkJsonObject.h>
#include <C_CkCert.h>

void ChilkatSample(void)
    {
    BOOL success;
    const char *path;
    HCkCodeSign codeSign;
    HCkJsonObject sigInfo;
    HCkCert signerCert;
    BOOL valid;
    HCkCert cert;
    HCkJsonObject 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 = "C:/someDir/MyApp.exe";

    codeSign = CkCodeSign_Create();
    sigInfo = CkJsonObject_Create();
    signerCert = CkCert_Create();

    //  Find out who signed the file.
    valid = CkCodeSign_VerifySignature(codeSign,path,sigInfo);
    if (valid == TRUE) {
        CkCodeSign_GetSignerCert(codeSign,signerCert);
        printf("currently signed by: %s\n",CkCert_subjectCN(signerCert));
        printf("certificate valid until: %s\n",CkCert_validToStr(signerCert));
    }
    else {
        printf("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 = CkCert_Create();
    success = CkCert_LoadPfxFile(cert,"C:/someDir/myNewCodeSigningCert.pfx","pfx password");
    if (success == FALSE) {
        printf("%s\n",CkCert_lastErrorText(cert));
        CkCodeSign_Dispose(codeSign);
        CkJsonObject_Dispose(sigInfo);
        CkCert_Dispose(signerCert);
        CkCert_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 = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"hashAlg","sha256");
    CkJsonObject_UpdateBool(json,"timestampToken.enabled",TRUE);
    CkJsonObject_UpdateString(json,"timestampToken.tsaUrl","http://timestamp.digicert.com");
    CkJsonObject_UpdateBool(json,"timestampToken.requestTsaCert",TRUE);
    CkJsonObject_UpdateString(json,"timestampToken.hashAlg","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 = CkCodeSign_AddSignature(codeSign,path,cert,json);
    if (success == FALSE) {
        printf("%s\n",CkCodeSign_lastErrorText(codeSign));
        CkCodeSign_Dispose(codeSign);
        CkJsonObject_Dispose(sigInfo);
        CkCert_Dispose(signerCert);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(json);
        return;
    }

    //  The file now has one signature, made with the new certificate.
    valid = CkCodeSign_VerifySignature(codeSign,path,sigInfo);
    if (valid == FALSE) {
        printf("%s\n",CkCodeSign_lastErrorText(codeSign));
        CkCodeSign_Dispose(codeSign);
        CkJsonObject_Dispose(sigInfo);
        CkCert_Dispose(signerCert);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(json);
        return;
    }

    success = CkCodeSign_GetSignerCert(codeSign,signerCert);
    if (success == FALSE) {
        printf("%s\n",CkCodeSign_lastErrorText(codeSign));
        CkCodeSign_Dispose(codeSign);
        CkJsonObject_Dispose(sigInfo);
        CkCert_Dispose(signerCert);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(json);
        return;
    }

    printf("now signed by: %s\n",CkCert_subjectCN(signerCert));
    printf("certificate valid until: %s\n",CkCert_validToStr(signerCert));


    CkCodeSign_Dispose(codeSign);
    CkJsonObject_Dispose(sigInfo);
    CkCert_Dispose(signerCert);
    CkCert_Dispose(cert);
    CkJsonObject_Dispose(json);

    }