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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    String sPath
    Handle hoCodeSign
    Variant vSigInfo
    Handle hoSigInfo
    Boolean iValid
    Variant vSignerCert
    Handle hoSignerCert
    String sTemp1

    Move False To iSuccess

    //  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.
    Move "C:/someDir/Deploy.ps1" To sPath

    Get Create (RefClass(cComChilkatCodeSign)) To hoCodeSign
    If (Not(IsComObjectCreated(hoCodeSign))) Begin
        Send CreateComObject of hoCodeSign
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoSigInfo
    If (Not(IsComObjectCreated(hoSigInfo))) Begin
        Send CreateComObject of hoSigInfo
    End

    //  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.
    Get pvComObject of hoSigInfo to vSigInfo
    Get ComVerifySignature Of hoCodeSign sPath vSigInfo To iValid
    If (iValid = False) Begin
        //  The LastErrorText explains why the verification failed.
        Get ComLastErrorText Of hoCodeSign To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "The Authenticode signature is valid."

    //  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.
    Get ComStringOf Of hoSigInfo "pkcs7.verify.signerInfo[0].signingAlgName" To sTemp1
    Showln "signing algorithm: " sTemp1
    Get ComStringOf Of hoSigInfo "pkcs7.verify.signerInfo[0].cert.digestAlgName" To sTemp1
    Showln "hash algorithm: " sTemp1

    //  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.
    Get Create (RefClass(cComChilkatCert)) To hoSignerCert
    If (Not(IsComObjectCreated(hoSignerCert))) Begin
        Send CreateComObject of hoSignerCert
    End
    Get pvComObject of hoSignerCert to vSignerCert
    Get ComGetSignerCert Of hoCodeSign vSignerCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCodeSign To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSubjectCN Of hoSignerCert To sTemp1
    Showln "signed by: " sTemp1
    Get ComIssuerCN Of hoSignerCert To sTemp1
    Showln "certificate issued by: " sTemp1
    Get ComSerialNumber Of hoSignerCert To sTemp1
    Showln "certificate serial number: " sTemp1
    Get ComValidToStr Of hoSignerCert To sTemp1
    Showln "certificate valid until: " sTemp1


End_Procedure