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

Verify an EXE or DLL that has Extra Data in its Certificate Table

See more Code Signing Examples

Starting in Chilkat v11.7.0, verifying the Authenticode signature of an EXE or DLL includes a strict check of the PE certificate table, which is not covered by the signature (see CVE-2013-3900). A file with extra data there fails verification. This example shows the codesign-allow-cert-padding option of UncommonOptions, which accepts such a file when it is known to carry the data on purpose, as some installers do.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

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

    Move False To iSuccess

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

    //  The Authenticode signature of an EXE or DLL is stored in the certificate table of the PE file.
    //  The certificate table itself is not covered by the signature, so data can be added to it
    //  after the file is signed without invalidating the signature (see CVE-2013-3900).
    //  
    //  Starting in Chilkat v11.7.0, VerifySignature checks the certificate table strictly: it must hold
    //  exactly one signature entry, containing exactly the signature that was verified, with no extra data.
    //  A file that fails this check is not verified, because anything hidden there is unsigned.

    Move "C:/someDir/SomeInstaller.exe" 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

    Get pvComObject of hoSigInfo to vSigInfo
    Get ComVerifySignature Of hoCodeSign sPath vSigInfo To iValid
    If (iValid = True) Begin
        Showln "The Authenticode signature is valid."
        Procedure_Return
    End

    //  The LastErrorText explains why the verification failed.
    Get ComLastErrorText Of hoCodeSign To sTemp1
    Showln sTemp1

    //  Some installers store configuration data in the certificate table on purpose.
    //  If the file is known to be one of these, the codesign-allow-cert-padding option
    //  prevents the verification from failing because of the certificate table check alone.
    //  
    //  The option relaxes only this one check.  The file is still required to match its signature,
    //  and the signing certificate is still validated.  Use it only for files that are known to carry such data.
    Set ComUncommonOptions Of hoCodeSign To "codesign-allow-cert-padding"

    Get pvComObject of hoSigInfo to vSigInfo
    Get ComVerifySignature Of hoCodeSign sPath vSigInfo To iValid
    If (iValid = False) Begin
        //  The file failed for a reason other than the certificate table check.
        Get ComLastErrorText Of hoCodeSign To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "The signature is valid, apart from the extra data in the certificate table."
    //  The failed certificate table check is still reported in the LastErrorText.
    Get ComLastErrorText Of hoCodeSign To sTemp1
    Showln sTemp1


End_Procedure