SQL Server
SQL Server
Get Recipient Certificate Info from Encrypted S/MIME
See more MIME Examples
Demonstrates GetDecryptCertInfo, which examines an encrypted S/MIME entity and writes the recipient-certificate identifiers into a JsonObject. This reveals which certificate(s) the message was encrypted for, so the correct private key can be located before decrypting.
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.
Tip: Code to parse the returned JSON can be generated with Chilkat's online tool at https://tools.chilkat.io/jsonParse.
Background. A CMS/PKCS #7 encrypted message carries recipient identifiers (such as issuer and serial number). Inspecting them lets an application determine, without decrypting, which key it needs.
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
DECLARE @mime int
EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @mime, 'LoadMimeFile', @success OUT, 'qa_data/encrypted.eml'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mime
RETURN
END
-- Examine the encrypted entity and write the recipient-certificate identifiers to a JsonObject.
-- This tells you which certificate(s) the message was encrypted for, so you can locate the right
-- private key before attempting to decrypt.
DECLARE @certInfo int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @certInfo OUT
EXEC sp_OAMethod @mime, 'GetDecryptCertInfo', @success OUT, @certInfo
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mime
EXEC @hr = sp_OADestroy @certInfo
RETURN
END
EXEC sp_OASetProperty @certInfo, 'EmitCompact', 0
DECLARE @json nvarchar(4000)
EXEC sp_OAMethod @certInfo, 'Emit', @json OUT
PRINT @json
-- JSON parsing code for this result can be generated at Chilkat's online tool:
-- https://tools.chilkat.io/jsonParse
EXEC @hr = sp_OADestroy @mime
EXEC @hr = sp_OADestroy @certInfo
END
GO