SQL Server Requires Chilkat v11.7.0+
SQL Server
Replace the Authenticode Signature of an Already Signed File
See more Code Signing Examples
Demonstrates how to sign a file that already has an Authenticode signature, for example to sign it with a renewed code signing certificate. Starting in Chilkat v11.7.0, AddSignature removes the existing signature and replaces it with the new one. Before v11.7.0, signing an already signed EXE or DLL added a second signature entry instead.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, AddSignature replaces the signature of a file that is already signed.
-- (Before v11.7.0, signing an already signed EXE or DLL added a second signature entry
-- instead of replacing the first.)
--
-- This applies to every supported file type: EXE, DLL, MSI, PowerShell, and VBScript.
DECLARE @path nvarchar(4000)
SELECT @path = 'C:/someDir/MyApp.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 @signerCert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @signerCert OUT
-- Find out who signed the file.
DECLARE @valid int
EXEC sp_OAMethod @codeSign, 'VerifySignature', @valid OUT, @path, @sigInfo
IF @valid = 1
BEGIN
EXEC sp_OAMethod @codeSign, 'GetSignerCert', @success OUT, @signerCert
EXEC sp_OAGetProperty @signerCert, 'SubjectCN', @sTmp0 OUT
PRINT 'currently signed by: ' + @sTmp0
EXEC sp_OAGetProperty @signerCert, 'ValidToStr', @sTmp0 OUT
PRINT 'certificate valid until: ' + @sTmp0
END
ELSE
BEGIN
PRINT 'The file is not signed, or its signature is not valid.'
END
-- Load the certificate to sign with. It must have access to its private key.
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'C:/someDir/myNewCodeSigningCert.pfx', 'pfx password'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @codeSign
EXEC @hr = sp_OADestroy @sigInfo
EXEC @hr = sp_OADestroy @signerCert
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- Sign with SHA256, and ask a timestamp authority for a timestamp. A timestamp lets the
-- signature continue to validate after the code signing certificate has expired.
--
-- {
-- "hashAlg": "sha256",
-- "timestampToken": {
-- "enabled": true,
-- "tsaUrl": "http://timestamp.digicert.com",
-- "requestTsaCert": true,
-- "hashAlg": "sha256"
-- }
-- }
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'hashAlg', 'sha256'
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'timestampToken.enabled', 1
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'timestampToken.tsaUrl', 'http://timestamp.digicert.com'
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'timestampToken.requestTsaCert', 1
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'timestampToken.hashAlg', 'sha256'
-- Sign the file. It is not necessary to call RemoveSignature first:
-- the existing signature is removed and the new signature takes its place.
EXEC sp_OAMethod @codeSign, 'AddSignature', @success OUT, @path, @cert, @json
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
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @json
RETURN
END
-- The file now has one signature, made with the new certificate.
EXEC sp_OAMethod @codeSign, 'VerifySignature', @valid OUT, @path, @sigInfo
IF @valid = 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
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @json
RETURN
END
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
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @json
RETURN
END
EXEC sp_OAGetProperty @signerCert, 'SubjectCN', @sTmp0 OUT
PRINT 'now signed by: ' + @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
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @json
END
GO