Unicode C++ Requires Chilkat v11.7.0+
Unicode C++
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 Unicode C++ Downloads
#include <CkCodeSignW.h>
#include <CkJsonObjectW.h>
#include <CkCertW.h>
void ChilkatSample(void)
{
bool 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.
const wchar_t *path = L"C:/someDir/MyApp.exe";
CkCodeSignW codeSign;
CkJsonObjectW sigInfo;
CkCertW signerCert;
// Find out who signed the file.
bool valid = codeSign.VerifySignature(path,sigInfo);
if (valid == true) {
codeSign.GetSignerCert(signerCert);
wprintf(L"currently signed by: %s\n",signerCert.subjectCN());
wprintf(L"certificate valid until: %s\n",signerCert.validToStr());
}
else {
wprintf(L"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.
CkCertW cert;
success = cert.LoadPfxFile(L"C:/someDir/myNewCodeSigningCert.pfx",L"pfx password");
if (success == false) {
wprintf(L"%s\n",cert.lastErrorText());
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"
// }
// }
CkJsonObjectW json;
json.UpdateString(L"hashAlg",L"sha256");
json.UpdateBool(L"timestampToken.enabled",true);
json.UpdateString(L"timestampToken.tsaUrl",L"http://timestamp.digicert.com");
json.UpdateBool(L"timestampToken.requestTsaCert",true);
json.UpdateString(L"timestampToken.hashAlg",L"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) {
wprintf(L"%s\n",codeSign.lastErrorText());
return;
}
// The file now has one signature, made with the new certificate.
valid = codeSign.VerifySignature(path,sigInfo);
if (valid == false) {
wprintf(L"%s\n",codeSign.lastErrorText());
return;
}
success = codeSign.GetSignerCert(signerCert);
if (success == false) {
wprintf(L"%s\n",codeSign.lastErrorText());
return;
}
wprintf(L"now signed by: %s\n",signerCert.subjectCN());
wprintf(L"certificate valid until: %s\n",signerCert.validToStr());
}