SQL Server
SQL Server
Create a DKIM-Signature for a MIME Message
See more DKIM / DomainKey Examples
Demonstrates the Chilkat Dkim.DkimSign method, which creates a DKIM-Signature header and prepends it to a complete MIME message held in a BinData, modifying it in place. The DkimAlg, DkimCanon, DkimDomain, DkimSelector, DkimHeaders, and DkimBodyLengthCount properties configure the generated signature.
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: The signature is computed over a hash of the body plus a chosen set of headers, so the message must be completely built — every header, encoding, and boundary — before signing; any later change invalidates it. The
d= (domain) and s= (selector) tags tell a verifier where to find the public key: at selector._domainkey.domain in DNS. relaxed/relaxed canonicalization tolerates the minor whitespace changes mail systems make in transit, which is why it is the common choice.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the Dkim.DkimSign method, which creates a DKIM-Signature header and prepends it to
-- a complete MIME message. The only argument is a BinData holding the MIME, which is modified in
-- place.
--
-- The DkimAlg, DkimCanon, DkimDomain, DkimSelector, DkimHeaders, and DkimBodyLengthCount
-- properties configure the signature that is generated.
DECLARE @dkim int
EXEC @hr = sp_OACreate 'Chilkat.Dkim', @dkim OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Configure the DKIM signature.
EXEC sp_OASetProperty @dkim, 'DkimDomain', 'example.com'
EXEC sp_OASetProperty @dkim, 'DkimSelector', 'myselector'
-- Signing algorithm written to the a= tag.
EXEC sp_OASetProperty @dkim, 'DkimAlg', 'rsa-sha256'
-- Canonicalization for headers and body, written to the c= tag.
EXEC sp_OASetProperty @dkim, 'DkimCanon', 'relaxed/relaxed'
-- Colon-separated list of header fields to sign, written to the h= tag.
EXEC sp_OASetProperty @dkim, 'DkimHeaders', 'From:To:Subject:Date:Message-ID'
-- Maximum number of canonicalized body bytes to hash. 0 means the entire body.
EXEC sp_OASetProperty @dkim, 'DkimBodyLengthCount', 0
-- Load the RSA private key and give it to the Dkim object.
DECLARE @privKey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT
EXEC sp_OAMethod @privKey, 'LoadPemFile', @success OUT, 'qa_data/dkim_private.pem'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @dkim
EXEC @hr = sp_OADestroy @privKey
RETURN
END
EXEC sp_OAMethod @dkim, 'SetDkimPrivateKey', @success OUT, @privKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @dkim, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @dkim
EXEC @hr = sp_OADestroy @privKey
RETURN
END
-- Load the complete MIME message to be signed. It must already contain all headers, transfer
-- encodings, MIME boundaries, and body bytes.
DECLARE @mimeData int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @mimeData OUT
EXEC sp_OAMethod @mimeData, 'LoadFile', @success OUT, 'qa_data/message.eml'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mimeData, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @dkim
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @mimeData
RETURN
END
-- Sign. The DKIM-Signature header is prepended to the MIME in place.
EXEC sp_OAMethod @dkim, 'DkimSign', @success OUT, @mimeData
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @dkim, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @dkim
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @mimeData
RETURN
END
-- Save the signed message.
EXEC sp_OAMethod @mimeData, 'WriteFile', @success OUT, 'qa_output/signed.eml'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mimeData, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @dkim
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @mimeData
RETURN
END
PRINT 'Message signed.'
EXEC @hr = sp_OADestroy @dkim
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @mimeData
END
GO