Sample code for 30+ languages & platforms
SQL Server

Add a Detached Signature (Separate Key, Header Control)

See more MIME Examples

Demonstrates the Chilkat Mime.AddDetachedSignaturePk2 method, which creates a detached signature with a separately supplied certificate and private key, and a third argument controlling transfer of the existing message header fields to the new outer entity.

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: This combines the two variations — certificate and key supplied separately, plus explicit header placement — for the case that needs both. It is the most flexible of the detached-signature methods; choose it when your key material is separate and you also care about where the message headers end up on the signed result.

Chilkat SQL Server Downloads

SQL Server
--
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, 'SetBodyFromPlainText', @success OUT, 'This message will be signed.'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END
    --  Load the certificate and its private key separately.
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'qa_data/signer.cer'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    DECLARE @privKey int
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT

    EXEC sp_OAMethod @privKey, 'LoadPemFile', @success OUT, 'qa_data/signer_privkey.pem'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    --  Create a detached signature with separately supplied cert and private key, where the 3rd
    --  argument controls whether the existing message header fields are transferred to the new outer
    --  multipart/signed entity.
    DECLARE @transferHeaderFields int
    SELECT @transferHeaderFields = 1
    EXEC sp_OAMethod @mime, 'AddDetachedSignaturePk2', @success OUT, @cert, @privKey, @transferHeaderFields
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    EXEC sp_OAMethod @mime, 'SaveMime', @success OUT, 'qa_output/signed.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    PRINT 'Detached signature added (headers transferred).'

    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @privKey


END
GO