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

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  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.

    DECLARE @path nvarchar(4000)
    SELECT @path = 'C:/someDir/SomeInstaller.exe'

    DECLARE @codeSign int
    EXEC @hr = sp_OACreate 'Chilkat.CodeSign', @codeSign OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @sigInfo int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @sigInfo OUT

    DECLARE @valid int
    EXEC sp_OAMethod @codeSign, 'VerifySignature', @valid OUT, @path, @sigInfo
    IF @valid = 1
      BEGIN

        PRINT 'The Authenticode signature is valid.'
        EXEC @hr = sp_OADestroy @codeSign
        EXEC @hr = sp_OADestroy @sigInfo
        RETURN
      END

    --  The LastErrorText explains why the verification failed.
    EXEC sp_OAGetProperty @codeSign, 'LastErrorText', @sTmp0 OUT
    PRINT @sTmp0

    --  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.
    EXEC sp_OASetProperty @codeSign, 'UncommonOptions', 'codesign-allow-cert-padding'

    EXEC sp_OAMethod @codeSign, 'VerifySignature', @valid OUT, @path, @sigInfo
    IF @valid = 0
      BEGIN
        --  The file failed for a reason other than the certificate table check.
        EXEC sp_OAGetProperty @codeSign, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @codeSign
        EXEC @hr = sp_OADestroy @sigInfo
        RETURN
      END


    PRINT 'The signature is valid, apart from the extra data in the certificate table.'
    --  The failed certificate table check is still reported in the LastErrorText.
    EXEC sp_OAGetProperty @codeSign, 'LastErrorText', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @codeSign
    EXEC @hr = sp_OADestroy @sigInfo


END
GO