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

Supply a Fallback Certificate for S/MIME Verification

See more MIME Examples

Demonstrates the Chilkat Mime.SetVerifyCert method, which supplies a signer-certificate fallback for subsequent signature verification. The only argument is the Cert.

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: Most signed messages embed the signer's certificate, so Verify can find it automatically. But some signatures omit it to save space, expecting the recipient to already have it. This method provides that certificate as a fallback so verification can proceed — the S/MIME equivalent of already knowing the sender's public key out of band.

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.Cert,
  Chilkat.Mime;

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  cert: TCert;

begin
  success := False;

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

  //  Supply a signer-certificate fallback for verification.  This is useful when the signature does
  //  not embed the signer certificate and Chilkat cannot otherwise locate it.
  cert := TCert.Create;
  success := cert.LoadFromFile('qa_data/signer.cer');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  success := mime.SetVerifyCert(cert);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Now verify using the supplied certificate.
  success := mime.Verify();
  if (success <> True) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;
  WriteLn('Signature verified using the supplied certificate.');


  mime.Free;
  cert.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.