Zig Requires Chilkat v11.7.0+
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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.
const path = "C:/someDir/MyApp.exe";
const code_sign = try chilkat.CodeSign.init();
defer code_sign.deinit();
const sig_info = try chilkat.JsonObject.init();
defer sig_info.deinit();
const signer_cert = try chilkat.Cert.init();
defer signer_cert.deinit();
// Find out who signed the file.
if (code_sign.verifySignature(path, sig_info)) {
code_sign.getSignerCert(signer_cert) catch {};
std.debug.print("currently signed by: {s}\n", .{try signer_cert.getSubjectCN(alloc)});
std.debug.print("certificate valid until: {s}\n", .{try signer_cert.getValidToStr(alloc)});
} else |_| {
std.debug.print("The file is not signed, or its signature is not valid.\n", .{});
}
// Load the certificate to sign with. It must have access to its private key.
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadPfxFile("C:/someDir/myNewCodeSigningCert.pfx", "pfx password") catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
// 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"
// }
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("hashAlg", "sha256") catch {};
json.updateBool("timestampToken.enabled", true) catch {};
json.updateString("timestampToken.tsaUrl", "http://timestamp.digicert.com") catch {};
json.updateBool("timestampToken.requestTsaCert", true) catch {};
json.updateString("timestampToken.hashAlg", "sha256") catch {};
// Sign the file. It is not necessary to call RemoveSignature first:
// the existing signature is removed and the new signature takes its place.
code_sign.addSignature(path, cert, json) catch {
std.debug.print("{s}\n", .{try code_sign.getLastErrorText(alloc)});
return;
};
// The file now has one signature, made with the new certificate.
code_sign.verifySignature(path, sig_info) catch {
std.debug.print("{s}\n", .{try code_sign.getLastErrorText(alloc)});
return;
};
code_sign.getSignerCert(signer_cert) catch {
std.debug.print("{s}\n", .{try code_sign.getLastErrorText(alloc)});
return;
};
std.debug.print("now signed by: {s}\n", .{try signer_cert.getSubjectCN(alloc)});
std.debug.print("certificate valid until: {s}\n", .{try signer_cert.getValidToStr(alloc)});
}