Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Replace the Authenticode Signature of an Already Signed File
See more Code Signing Examples
Demonstrates how to sign a file that already has an Authenticode signature, for example to sign it with a renewed code signing certificate. Starting in Chilkat v11.7.0, AddSignature removes the existing signature and replaces it with the new one. Before v11.7.0, signing an already signed EXE or DLL added a second signature entry instead.Chilkat Lazarus Pascal 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.CodeSign,
Chilkat.Cert,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
path: string;
codeSign: TCodeSign;
sigInfo: TJsonObject;
signerCert: TCert;
valid: Boolean;
cert: TCert;
json: TJsonObject;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Starting in Chilkat v11.7.0, AddSignature replaces the signature of a file that is already signed.
// (Before v11.7.0, signing an already signed EXE or DLL added a second signature entry
// instead of replacing the first.)
//
// This applies to every supported file type: EXE, DLL, MSI, PowerShell, and VBScript.
path := 'C:/someDir/MyApp.exe';
codeSign := TCodeSign.Create;
sigInfo := TJsonObject.Create;
signerCert := TCert.Create;
// Find out who signed the file.
valid := codeSign.VerifySignature(path,sigInfo);
if (valid = True) then
begin
codeSign.GetSignerCert(signerCert);
WriteLn('currently signed by: ' + signerCert.SubjectCN);
WriteLn('certificate valid until: ' + signerCert.ValidToStr);
end
else
begin
WriteLn('The file is not signed, or its signature is not valid.');
end;
// Load the certificate to sign with. It must have access to its private key.
cert := TCert.Create;
success := cert.LoadPfxFile('C:/someDir/myNewCodeSigningCert.pfx','pfx password');
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
// Sign with SHA256, and ask a timestamp authority for a timestamp. A timestamp lets the
// signature continue to validate after the code signing certificate has expired.
//
// {
// "hashAlg": "sha256",
// "timestampToken": {
// "enabled": true,
// "tsaUrl": "http://timestamp.digicert.com",
// "requestTsaCert": true,
// "hashAlg": "sha256"
// }
// }
json := TJsonObject.Create;
json.UpdateString('hashAlg','sha256');
json.UpdateBool('timestampToken.enabled',True);
json.UpdateString('timestampToken.tsaUrl','http://timestamp.digicert.com');
json.UpdateBool('timestampToken.requestTsaCert',True);
json.UpdateString('timestampToken.hashAlg','sha256');
// Sign the file. It is not necessary to call RemoveSignature first:
// the existing signature is removed and the new signature takes its place.
success := codeSign.AddSignature(path,cert,json);
if (success = False) then
begin
WriteLn(codeSign.LastErrorText);
Exit;
end;
// The file now has one signature, made with the new certificate.
valid := codeSign.VerifySignature(path,sigInfo);
if (valid = False) then
begin
WriteLn(codeSign.LastErrorText);
Exit;
end;
success := codeSign.GetSignerCert(signerCert);
if (success = False) then
begin
WriteLn(codeSign.LastErrorText);
Exit;
end;
WriteLn('now signed by: ' + signerCert.SubjectCN);
WriteLn('certificate valid until: ' + signerCert.ValidToStr);
codeSign.Free;
sigInfo.Free;
signerCert.Free;
cert.Free;
json.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.