Sample code for 30+ languages & platforms
SQL Server

MIME S/MIME Signing Algorithm Properties

See more MIME Examples

Demonstrates the properties that control S/MIME signing: SigningAlg (the RSA signature scheme, PKCS1-V1_5 or RSASSA-PSS), SigningHashAlg (the message-digest algorithm), Micalg and Protocol (the micalg and protocol parameters of a multipart/signed Content-Type), and UseXPkcs7 (whether historical x-pkcs7 media-type names are used). The properties are configured before creating a detached signature.

Background. These settings determine how the CMS/PKCS #7 signature is produced and how a multipart/signed wrapper advertises it. The defaults (PKCS1-V1_5, sha256) suit most modern uses; the properties allow interoperability with specific requirements.

Chilkat SQL Server Downloads

SQL Server
-- 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 @sTmp1 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

    --  SigningAlg is the RSA signature scheme.  The default is PKCS1-V1_5; set RSASSA-PSS (or pss) for
    --  RSASSA-PSS.
    EXEC sp_OASetProperty @mime, 'SigningAlg', 'PKCS1-V1_5'

    --  SigningHashAlg is the message-digest algorithm.  The default is sha256.
    EXEC sp_OASetProperty @mime, 'SigningHashAlg', 'sha256'

    --  Micalg and Protocol are the micalg and protocol parameters written into a multipart/signed
    --  Content-Type header field.  They are normally set automatically, but can be controlled here.
    EXEC sp_OASetProperty @mime, 'Micalg', 'sha-256'
    EXEC sp_OASetProperty @mime, 'Protocol', 'application/pkcs7-signature'

    --  UseXPkcs7 controls whether newly created S/MIME media types use the historical x-pkcs7 names.
    EXEC sp_OASetProperty @mime, 'UseXPkcs7', 0

    --  Load a signing certificate (with private key access) from a PFX.  The PFX password should come
    --  from a secure source rather than being hard-coded.
    DECLARE @pfxPassword nvarchar(4000)
    SELECT @pfxPassword = 'myPfxPassword'
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/signer.pfx', @pfxPassword
    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

    --  Create the detached signature using the algorithms configured above.
    EXEC sp_OAMethod @mime, 'AddDetachedSignature', @success OUT, @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 @mime, 'SigningAlg', @sTmp0 OUT

    EXEC sp_OAGetProperty @mime, 'SigningHashAlg', @sTmp1 OUT
    PRINT 'Signed with SigningAlg=' + @sTmp0 + ' SigningHashAlg=' + @sTmp1

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


END
GO