Sample code for 30+ languages & platforms
SQL Server

Verify the Digital Signatures on an Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.SignaturesValid property, which is true when the email was received with one or more digital signatures and all of them validated, indicating the message was not altered. It is only meaningful when ReceivedSigned is true, so this example checks that first.

Background: A valid signature proves integrity — the bytes that were signed are exactly the bytes you received — but that is not the same as trust. A message can carry a perfectly valid signature from a certificate you have no reason to trust (self-signed, expired, or from an unknown issuer). So after confirming SignaturesValid, an application that needs assurance of who signed should separately examine the signer certificate and its chain (see LastSignerCert).

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the read-only Email.SignaturesValid property, which is true if the email
    --  was received with one or more digital signatures AND all of them validated (indicating
    --  the email was not altered).  It is only meaningful when ReceivedSigned is true.

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'qa_data/eml/signed.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    EXEC sp_OAGetProperty @email, 'ReceivedSigned', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'SignaturesValid', @iTmp0 OUT
        IF @iTmp0 = 1
          BEGIN

            PRINT 'The signature(s) verified: the email was not altered.'
          END
        ELSE
          BEGIN

            PRINT 'Signature verification FAILED.'
          END
      END
    ELSE
      BEGIN

        PRINT 'This email was not received with a digital signature.'
      END

    --  Note: Paths such as "qa_data/..." are relative local filesystem paths,
    --  relative to the current working directory of the running application.

    EXEC @hr = sp_OADestroy @email


END
GO