SQL Server
SQL Server
Get the Certificate Used for Decryption
See more MIME Examples
Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.
After a successful decryption, LastDecryptCert loads the certificate(s) actually used into a Cert object, indexed from zero up to NumDecryptCerts minus one.
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. Following decryption or security unwrapping, Chilkat records which certificate(s) were used. LastDecryptCert retrieves them so the application can report or verify the identity involved.
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
-- Configure a decryption source and decrypt.
DECLARE @pfxPassword nvarchar(4000)
SELECT @pfxPassword = 'myPfxPassword'
EXEC sp_OAMethod @mime, 'AddPfxSourceFile', @success OUT, 'qa_data/recipient.pfx', @pfxPassword
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mime
RETURN
END
EXEC sp_OAMethod @mime, 'Decrypt', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mime
RETURN
END
-- After a successful decryption, load the certificate(s) that were used. The 1st argument is the
-- zero-based index and the 2nd is a Cert that receives the certificate.
DECLARE @n int
EXEC sp_OAGetProperty @mime, 'NumDecryptCerts', @n OUT
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
DECLARE @i int
SELECT @i = 0
WHILE @i <= @n - 1
BEGIN
EXEC sp_OAMethod @mime, 'LastDecryptCert', @success OUT, @i, @cert
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mime
EXEC @hr = sp_OADestroy @cert
RETURN
END
EXEC sp_OAGetProperty @cert, 'SubjectCN', @sTmp0 OUT
PRINT 'Decrypted with: ' + @sTmp0
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @mime
EXEC @hr = sp_OADestroy @cert
END
GO