SQL Server Requires Chilkat v11.7.0+
SQL Server
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 SQL Server Downloads
-- 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.
-- 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.
DECLARE @path nvarchar(4000)
SELECT @path = 'C:/someDir/Deploy.ps1'
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
-- 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.
DECLARE @valid int
EXEC sp_OAMethod @codeSign, 'VerifySignature', @valid OUT, @path, @sigInfo
IF @valid = 0
BEGIN
-- The LastErrorText explains why the verification failed.
EXEC sp_OAGetProperty @codeSign, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @codeSign
EXEC @hr = sp_OADestroy @sigInfo
RETURN
END
PRINT '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.
EXEC sp_OAMethod @sigInfo, 'StringOf', @sTmp0 OUT, 'pkcs7.verify.signerInfo[0].signingAlgName'
PRINT 'signing algorithm: ' + @sTmp0
EXEC sp_OAMethod @sigInfo, 'StringOf', @sTmp0 OUT, 'pkcs7.verify.signerInfo[0].cert.digestAlgName'
PRINT 'hash algorithm: ' + @sTmp0
-- 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.
DECLARE @signerCert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @signerCert OUT
EXEC sp_OAMethod @codeSign, 'GetSignerCert', @success OUT, @signerCert
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @codeSign, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @codeSign
EXEC @hr = sp_OADestroy @sigInfo
EXEC @hr = sp_OADestroy @signerCert
RETURN
END
EXEC sp_OAGetProperty @signerCert, 'SubjectCN', @sTmp0 OUT
PRINT 'signed by: ' + @sTmp0
EXEC sp_OAGetProperty @signerCert, 'IssuerCN', @sTmp0 OUT
PRINT 'certificate issued by: ' + @sTmp0
EXEC sp_OAGetProperty @signerCert, 'SerialNumber', @sTmp0 OUT
PRINT 'certificate serial number: ' + @sTmp0
EXEC sp_OAGetProperty @signerCert, 'ValidToStr', @sTmp0 OUT
PRINT 'certificate valid until: ' + @sTmp0
EXEC @hr = sp_OADestroy @codeSign
EXEC @hr = sp_OADestroy @sigInfo
EXEC @hr = sp_OADestroy @signerCert
END
GO