Sample code for 30+ languages & platforms
SQL Server

Verify DKIM Signatures in a Message

See more DKIM / DomainKey Examples

Demonstrates the Chilkat Dkim.DkimVerify method, which verifies the DKIM-Signature at a given zero-based index in a MIME message. The arguments are the signature index and a BinData holding the complete MIME. This example loops over every signature and reads the JSON VerifyInfo after each.

Tip: JSON parsing code for the VerifyInfo result can be generated at Chilkat Tools.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: Verifying confirms two things at once: that the message was signed by the claimed domain, and that the signed headers and body have not been altered since. Chilkat uses a cached public key when the selector and domain match one you loaded, and otherwise fetches it from DNS. The message is not modified. After each call, the VerifyInfo JSON records the details — selector, domain, and outcome — which is what you would log or surface when a signature fails. Verification is per-signature, so a message with several signatures may have some pass and others fail.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Dkim.DkimVerify method, which verifies the DKIM-Signature at a given index in
    --  a MIME message.  The 1st argument is the zero-based signature index and the 2nd is a BinData
    --  holding the complete MIME.

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

    --  Load the received MIME message.
    DECLARE @mimeData int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @mimeData OUT

    EXEC sp_OAMethod @mimeData, 'LoadFile', @success OUT, 'qa_data/received.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mimeData, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @dkim
        EXEC @hr = sp_OADestroy @mimeData
        RETURN
      END

    --  A message may carry more than one DKIM signature.  Verify each in turn.  Signatures are
    --  indexed in top-to-bottom header order.
    DECLARE @numSigs int
    EXEC sp_OAMethod @dkim, 'NumDkimSigs', @numSigs OUT, @mimeData
    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @numSigs - 1
      BEGIN
        --  DkimVerify uses a cached public key when one matches the signature's selector and domain;
        --  otherwise it retrieves the key from DNS.
        EXEC sp_OAMethod @dkim, 'DkimVerify', @success OUT, @i, @mimeData
        IF @success = 1
          BEGIN


            PRINT 'Signature ' + @i + ': verified.'
          END
        ELSE
          BEGIN


            PRINT 'Signature ' + @i + ': verification failed.'
          END

        --  VerifyInfo contains JSON describing the most recent DkimVerify call (selector, domain,
        --  result, and so on).
        EXEC sp_OAGetProperty @dkim, 'VerifyInfo', @sTmp0 OUT
        PRINT @sTmp0
        --  JSON parsing code for this result can be generated at Chilkat's online tool:
        --  https://tools.chilkat.io/jsonParse
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @dkim
    EXEC @hr = sp_OADestroy @mimeData


END
GO