Sample code for 30+ languages & platforms
SQL Server

Verify the Timestamp Server Token (if any) while Validating a CMS Signature

Demonstrates how to also validate the timestamp server token (if any) while validating a CMS signature.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    -- Tell Chilkat to also validate the timestamp token if a timestamp exists in the CMS message's unauthenticated attributes.
    DECLARE @cmsOptions int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @cmsOptions OUT

    EXEC sp_OAMethod @cmsOptions, 'UpdateBool', @success OUT, 'ValidateTimestampTokens', 1
    EXEC sp_OAMethod @cmsOptions, 'Emit', @sTmp0 OUT
    EXEC sp_OASetProperty @crypt, 'CmsOptions', @sTmp0

    DECLARE @outputFile nvarchar(4000)
    SELECT @outputFile = 'qa_output/original.xml'
    DECLARE @inFile nvarchar(4000)
    SELECT @inFile = 'qa_data/p7m/fattura_signature.xml.p7m'

    -- Verify the signature and extract the contained file, which in this case is XML.
    EXEC sp_OAMethod @crypt, 'VerifyP7M', @success OUT, @inFile, @outputFile
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @cmsOptions
        RETURN
      END


    PRINT 'Signature validated.'

    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @cmsOptions


END
GO