Sample code for 30+ languages & platforms
SQL Server

P7S - Access Signature Information (date/time, certificate used, etc.)

See more Digital Signatures Examples

Examine a PKCS7 signature (.p7s) and get information about it.

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

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

    -- First load the .p7s file into a BinData object..
    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, 'qa_data/p7s/sample.p7s'
    IF @success <> 1
      BEGIN

        PRINT 'Failed to load .p7s file.'
        EXEC @hr = sp_OADestroy @bd
        RETURN
      END

    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT

    -- Assuming this is a signature that contains the original data that was signed..
    EXEC sp_OAMethod @crypt, 'OpaqueVerifyBd', @success OUT, @bd
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @bd
        EXEC @hr = sp_OADestroy @crypt
        RETURN
      END

    -- Examine the last JSON data after signature verification..
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @crypt, 'GetLastJsonData', NULL, @json

    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Sample output...
    -- Go to http://tools.chilkat.io/jsonParse.cshtml
    -- and paste the JSON into the online form to generate JSON parsing code.

    -- {
    --   "pkcs7": {
    --     "verify": {
    --       "digestAlgorithms": [
    --         "sha256"
    --       ],
    --       "signerInfo": [
    --         {
    --           "cert": {
    --             "serialNumber": "AAC5FC48C0FD8FBB",
    --             "issuerCN": "AC ABCDEF RFB v5",
    --             "issuerDN": "",
    --             "digestAlgOid": "2.16.840.1.101.3.4.2.1",
    --             "digestAlgName": "SHA-256"
    --           },
    --           "contentType": "1.2.840.113549.1.7.1",
    --           "signingTime": "180607195054Z",
    --           "messageDigest": "trzyxXbZ96z2M4mncyZ7BNMV4yIT92+5sS27Fu64iG8=",
    --           "signingAlgOid": "1.2.840.113549.1.1.11",
    --           "signerDigest": "trzyxXbZ96z2M4mncyZ7BNMV4yIT92+5sS27Fu64iG8="
    --         },
    --         {
    --           "cert": {
    --             "serialNumber": "324FB38ABD59723F",
    --             "issuerCN": "AC ABCDEF RFB v5",
    --             "issuerDN": "",
    --             "digestAlgOid": "2.16.840.1.101.3.4.2.1",
    --             "digestAlgName": "SHA-256"
    --           },
    --           "contentType": "1.2.840.113549.1.7.1",
    --           "signingTime": "180608182517Z",
    --           "messageDigest": "trzyxXbZ96z2M4mncyZ7BNMV4yIT92+5sS27Fu64iG8=",
    --           "signingAlgOid": "1.2.840.113549.1.1.11",
    --           "signerDigest": "trzyxXbZ96z2M4mncyZ7BNMV4yIT92+5sS27Fu64iG8="
    --         }
    --       ]
    --     }
    --   }
    -- }
    -- 

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @strVal nvarchar(4000)

    DECLARE @certSerialNumber nvarchar(4000)

    DECLARE @certIssuerCN nvarchar(4000)

    DECLARE @certIssuerDN nvarchar(4000)

    DECLARE @certDigestAlgOid nvarchar(4000)

    DECLARE @certDigestAlgName nvarchar(4000)

    DECLARE @contentType nvarchar(4000)

    DECLARE @signingTime nvarchar(4000)

    DECLARE @messageDigest nvarchar(4000)

    DECLARE @signingAlgOid nvarchar(4000)

    DECLARE @signerDigest nvarchar(4000)

    DECLARE @dt int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT

    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'pkcs7.verify.digestAlgorithms'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'pkcs7.verify.digestAlgorithms[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'pkcs7.verify.signerInfo'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @certSerialNumber OUT, 'pkcs7.verify.signerInfo[i].cert.serialNumber'
        EXEC sp_OAMethod @json, 'StringOf', @certIssuerCN OUT, 'pkcs7.verify.signerInfo[i].cert.issuerCN'
        EXEC sp_OAMethod @json, 'StringOf', @certIssuerDN OUT, 'pkcs7.verify.signerInfo[i].cert.issuerDN'
        EXEC sp_OAMethod @json, 'StringOf', @certDigestAlgOid OUT, 'pkcs7.verify.signerInfo[i].cert.digestAlgOid'
        EXEC sp_OAMethod @json, 'StringOf', @certDigestAlgName OUT, 'pkcs7.verify.signerInfo[i].cert.digestAlgName'
        EXEC sp_OAMethod @json, 'StringOf', @contentType OUT, 'pkcs7.verify.signerInfo[i].contentType'
        EXEC sp_OAMethod @json, 'StringOf', @signingTime OUT, 'pkcs7.verify.signerInfo[i].signingTime'

        -- The signingTime isin UTCTime format.
        -- UTCTime values take the form of either "YYMMDDhhmm[ss]Z" or "YYMMDDhhmm[ss](+|-)hhmm"
        -- Starting in Chilkat v9.5.0.77, the SetFromTimestamp method auto-recognizes the UTCTime format and parses it correctly.
        EXEC sp_OAMethod @dt, 'SetFromTimestamp', @success OUT, @signingTime
        -- To get the signingTime in other date/time formats, look at the online reference documentation for CkDateTime.
        -- There are numerous methods such as GetAsDateTime, GetAsIso8601, GetAsUnixTime, GetAsRfc822, etc.

        EXEC sp_OAMethod @json, 'StringOf', @messageDigest OUT, 'pkcs7.verify.signerInfo[i].messageDigest'
        EXEC sp_OAMethod @json, 'StringOf', @signingAlgOid OUT, 'pkcs7.verify.signerInfo[i].signingAlgOid'
        EXEC sp_OAMethod @json, 'StringOf', @signerDigest OUT, 'pkcs7.verify.signerInfo[i].signerDigest'
        SELECT @i = @i + 1
      END

    -- println crypt.LastErrorText;

    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @bd
    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @dt


END
GO