Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.7.0+

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 Rust Downloads

Rust

// 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.

let path = "C:/someDir/MyApp.exe".to_string();

let code_sign = chilkat::CodeSign::new();
let sig_info = chilkat::JsonObject::new();
let signer_cert = chilkat::Cert::new();

// Find out who signed the file.
if code_sign.verify_signature(&path, &sig_info).is_ok() {
    let _ = code_sign.get_signer_cert(&signer_cert);
    println!("currently signed by: {}", signer_cert.subject_cn());
    println!("certificate valid until: {}", signer_cert.valid_to_str());
} else {
    println!("The file is not signed, or its signature is not valid.");
}

// Load the certificate to sign with.  It must have access to its private key.
let cert = chilkat::Cert::new();
if cert.load_pfx_file("C:/someDir/myNewCodeSigningCert.pfx", "pfx password").is_err() {
    println!("{}", cert.last_error_text());
    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"
//   }
// }
let json = chilkat::JsonObject::new();
let _ = json.update_string("hashAlg", "sha256");
let _ = json.update_bool("timestampToken.enabled", true);
let _ = json.update_string("timestampToken.tsaUrl", "http://timestamp.digicert.com");
let _ = json.update_bool("timestampToken.requestTsaCert", true);
let _ = json.update_string("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.
if code_sign.add_signature(&path, &cert, &json).is_err() {
    println!("{}", code_sign.last_error_text());
    return;
}

// The file now has one signature, made with the new certificate.
if code_sign.verify_signature(&path, &sig_info).is_err() {
    println!("{}", code_sign.last_error_text());
    return;
}

if code_sign.get_signer_cert(&signer_cert).is_err() {
    println!("{}", code_sign.last_error_text());
    return;
}

println!("now signed by: {}", signer_cert.subject_cn());
println!("certificate valid until: {}", signer_cert.valid_to_str());