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

Authenticode Sign a PowerShell Script (.ps1, .psm1, .psd1)

See more Code Signing Examples

Demonstrates how to Authenticode sign a PowerShell script. PowerShell scripts (.ps1), modules (.psm1) and data files (.psd1) can be signed starting in Chilkat v11.7.0. The signature is appended to the file as a comment block, and the file keeps its text encoding.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkCert cert;
    HCkJsonObject json;
    const char *scriptPath;
    HCkCodeSign signer;
    HCkJsonObject sigInfo;
    BOOL valid;

    success = FALSE;

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

    //  Signing PowerShell files requires Chilkat v11.7.0 or later.
    //  The same code signs a script (.ps1), a module (.psm1), or a data file (.psd1).

    //  Load the code signing certificate.  It must have access to its private key.
    cert = CkCert_Create();
    success = CkCert_LoadPfxFile(cert,"C:/someDir/myCodeSigningCert.pfx","pfx password");
    if (success == FALSE) {
        printf("%s\n",CkCert_lastErrorText(cert));
        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");

    scriptPath = "C:/someDir/Deploy.ps1";

    signer = CkCodeSign_Create();

    //  The script is modified in place.  The signature is appended to the end of the file as a comment block:
    //  
    //    # SIG # Begin signature block
    //    # MIIoKgYJKoZIhvcNAQcCoIIoGzCCKBcCAQExDzANBglghkgBZQMEAgEFADB5Bgor
    //    # ...
    //    # SIG # End signature block
    //  
    //  The text encoding of the file (UTF-8, UTF-8 with BOM, or UTF-16) is kept.
    //  If the script is already signed, the new signature block replaces the existing one.
    success = CkCodeSign_AddSignature(signer,scriptPath,cert,json);
    if (success == FALSE) {
        printf("%s\n",CkCodeSign_lastErrorText(signer));
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(json);
        CkCodeSign_Dispose(signer);
        return;
    }

    printf("Signed the PowerShell script.\n");

    //  Verify the signature that was just added.
    //  Any change made to the script after it is signed, even to a comment, invalidates the signature.
    sigInfo = CkJsonObject_Create();
    valid = CkCodeSign_VerifySignature(signer,scriptPath,sigInfo);
    if (valid == FALSE) {
        printf("%s\n",CkCodeSign_lastErrorText(signer));
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(json);
        CkCodeSign_Dispose(signer);
        CkJsonObject_Dispose(sigInfo);
        return;
    }

    printf("The signature is valid.\n");


    CkCert_Dispose(cert);
    CkJsonObject_Dispose(json);
    CkCodeSign_Dispose(signer);
    CkJsonObject_Dispose(sigInfo);

    }