Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Verify an S/MIME Signed Message

See more MIME Examples

Demonstrates the Chilkat Mime.Verify method, which verifies a CMS/PKCS #7 signed MIME entity and removes the signature wrapper. It takes no arguments and supports both detached (multipart/signed) and opaque (signed-data) messages.

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: Verification confirms two things: that the content was signed by the holder of the signer's certificate, and that it has not been altered since. On success Chilkat strips the signature layer so the object holds the original content, ready to read — effectively "open and check" in one step. A false return means the signature is invalid or the content was tampered with, which is exactly the case your code must not treat as trusted.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Mime;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  body: string;

begin
  success := False;

  mime := TMime.Create;
  success := mime.LoadMimeFile('qa_data/signed.eml');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Verify the signature and remove the signature wrapper, restoring the underlying content.  Both
  //  detached (multipart/signed) and opaque (CMS signed-data) messages are supported.
  success := mime.Verify();
  if (success <> True) then
    begin
      WriteLn('Signature verification failed.');
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn('Signature verified. Number of signers: ' + mime.NumSignerCerts);

  //  The object now holds the original content with the signature removed.
  body := mime.GetBodyDecoded();
  if (mime.LastMethodSuccess = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;
  WriteLn(body);


  mime.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.