PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
string ls_Path
oleobject loo_CodeSign
oleobject loo_SigInfo
oleobject loo_SignerCert
long li_Valid
oleobject loo_Cert
oleobject loo_Json
li_Success = 0
// 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.
ls_Path = "C:/someDir/MyApp.exe"
loo_CodeSign = create oleobject
li_rc = loo_CodeSign.ConnectToNewObject("Chilkat.CodeSign")
if li_rc < 0 then
destroy loo_CodeSign
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_SigInfo = create oleobject
li_rc = loo_SigInfo.ConnectToNewObject("Chilkat.JsonObject")
loo_SignerCert = create oleobject
li_rc = loo_SignerCert.ConnectToNewObject("Chilkat.Cert")
// Find out who signed the file.
li_Valid = loo_CodeSign.VerifySignature(ls_Path,loo_SigInfo)
if li_Valid = 1 then
loo_CodeSign.GetSignerCert(loo_SignerCert)
Write-Debug "currently signed by: " + loo_SignerCert.SubjectCN
Write-Debug "certificate valid until: " + loo_SignerCert.ValidToStr
else
Write-Debug "The file is not signed, or its signature is not valid."
end if
// Load the certificate to sign with. It must have access to its private key.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Cert.LoadPfxFile("C:/someDir/myNewCodeSigningCert.pfx","pfx password")
if li_Success = 0 then
Write-Debug loo_Cert.LastErrorText
destroy loo_CodeSign
destroy loo_SigInfo
destroy loo_SignerCert
destroy loo_Cert
return
end if
// 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"
// }
// }
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.UpdateString("hashAlg","sha256")
loo_Json.UpdateBool("timestampToken.enabled",1)
loo_Json.UpdateString("timestampToken.tsaUrl","http://timestamp.digicert.com")
loo_Json.UpdateBool("timestampToken.requestTsaCert",1)
loo_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.
li_Success = loo_CodeSign.AddSignature(ls_Path,loo_Cert,loo_Json)
if li_Success = 0 then
Write-Debug loo_CodeSign.LastErrorText
destroy loo_CodeSign
destroy loo_SigInfo
destroy loo_SignerCert
destroy loo_Cert
destroy loo_Json
return
end if
// The file now has one signature, made with the new certificate.
li_Valid = loo_CodeSign.VerifySignature(ls_Path,loo_SigInfo)
if li_Valid = 0 then
Write-Debug loo_CodeSign.LastErrorText
destroy loo_CodeSign
destroy loo_SigInfo
destroy loo_SignerCert
destroy loo_Cert
destroy loo_Json
return
end if
li_Success = loo_CodeSign.GetSignerCert(loo_SignerCert)
if li_Success = 0 then
Write-Debug loo_CodeSign.LastErrorText
destroy loo_CodeSign
destroy loo_SigInfo
destroy loo_SignerCert
destroy loo_Cert
destroy loo_Json
return
end if
Write-Debug "now signed by: " + loo_SignerCert.SubjectCN
Write-Debug "certificate valid until: " + loo_SignerCert.ValidToStr
destroy loo_CodeSign
destroy loo_SigInfo
destroy loo_SignerCert
destroy loo_Cert
destroy loo_Json