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

Verify the Authenticode Signature of an MSI, PowerShell, or VBScript File

See more Code Signing Examples

Demonstrates how to verify the Authenticode signature of a Windows Installer package (.msi), a PowerShell file (.ps1, .psm1, .psd1), or a VBScript file (.vbs), and how to get the certificate of the signer. Verifying these file types requires Chilkat v11.7.0 or later.

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;
    BOOL valid;
    HCkCertW signerCert;

    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, VerifySignature can verify the Authenticode signature of
    //    - Windows Installer packages (.msi)
    //    - PowerShell scripts, modules, and data files (.ps1, .psm1, .psd1)
    //    - VBScript files (.vbs)
    //  in addition to EXE and DLL files.

    //  This example verifies a signed PowerShell script.
    //  To verify an .msi or a .vbs, pass the path of that file instead.
    path = L"C:/someDir/Deploy.ps1";

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

    //  VerifySignature returns true if the file has a valid Authenticode signature.
    //  It returns false if the file is not signed, if the file was changed after it was signed,
    //  or if the signing certificate is not trusted.
    valid = CkCodeSignW_VerifySignature(codeSign,path,sigInfo);
    if (valid == FALSE) {
        //  The LastErrorText explains why the verification failed.
        wprintf(L"%s\n",CkCodeSignW_lastErrorText(codeSign));
        CkCodeSignW_Dispose(codeSign);
        CkJsonObjectW_Dispose(sigInfo);
        return;
    }

    wprintf(L"The Authenticode signature is valid.\n");

    //  The details of the signature are returned in the JSON, in the same layout as for an EXE or DLL.
    //  See Verify Authenticode Signature of EXE or DLL for a sample of the JSON and code that parses it.
    wprintf(L"signing algorithm: %s\n",CkJsonObjectW_stringOf(sigInfo,L"pkcs7.verify.signerInfo[0].signingAlgName"));
    wprintf(L"hash algorithm: %s\n",CkJsonObjectW_stringOf(sigInfo,L"pkcs7.verify.signerInfo[0].cert.digestAlgName"));

    //  Get the certificate of the signer.
    //  The signer certificate is available only after a call to VerifySignature that returned true.
    //  It is the public certificate only.  It does not include the signer's private key.
    signerCert = CkCertW_Create();
    success = CkCodeSignW_GetSignerCert(codeSign,signerCert);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCodeSignW_lastErrorText(codeSign));
        CkCodeSignW_Dispose(codeSign);
        CkJsonObjectW_Dispose(sigInfo);
        CkCertW_Dispose(signerCert);
        return;
    }

    wprintf(L"signed by: %s\n",CkCertW_subjectCN(signerCert));
    wprintf(L"certificate issued by: %s\n",CkCertW_issuerCN(signerCert));
    wprintf(L"certificate serial number: %s\n",CkCertW_serialNumber(signerCert));
    wprintf(L"certificate valid until: %s\n",CkCertW_validToStr(signerCert));


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

    }