Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
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;
pfxPassword: string;
cert: TCert;
begin
success := False;
mime := TMime.Create;
success := mime.SetBodyFromPlainText('This message will be signed.');
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
// SigningAlg is the RSA signature scheme. The default is PKCS1-V1_5; set RSASSA-PSS (or pss) for
// RSASSA-PSS.
mime.SigningAlg := 'PKCS1-V1_5';
// SigningHashAlg is the message-digest algorithm. The default is sha256.
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.
mime.Micalg := 'sha-256';
mime.Protocol := 'application/pkcs7-signature';
// UseXPkcs7 controls whether newly created S/MIME media types use the historical x-pkcs7 names.
mime.UseXPkcs7 := False;
// 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.
pfxPassword := 'myPfxPassword';
cert := TCert.Create;
success := cert.LoadPfxFile('qa_data/signer.pfx',pfxPassword);
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
// Create the detached signature using the algorithms configured above.
success := mime.AddDetachedSignature(cert);
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
WriteLn('Signed with SigningAlg=' + mime.SigningAlg + ' SigningHashAlg=' + mime.SigningHashAlg);
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.