DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
String sPath
Handle hoCodeSign
Variant vSigInfo
Handle hoSigInfo
Variant vSignerCert
Handle hoSignerCert
Boolean iValid
Variant vCert
Handle hoCert
Variant vJson
Handle hoJson
String sTemp1
Move False To iSuccess
// 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.
Move "C:/someDir/MyApp.exe" To sPath
Get Create (RefClass(cComChilkatCodeSign)) To hoCodeSign
If (Not(IsComObjectCreated(hoCodeSign))) Begin
Send CreateComObject of hoCodeSign
End
Get Create (RefClass(cComChilkatJsonObject)) To hoSigInfo
If (Not(IsComObjectCreated(hoSigInfo))) Begin
Send CreateComObject of hoSigInfo
End
Get Create (RefClass(cComChilkatCert)) To hoSignerCert
If (Not(IsComObjectCreated(hoSignerCert))) Begin
Send CreateComObject of hoSignerCert
End
// Find out who signed the file.
Get pvComObject of hoSigInfo to vSigInfo
Get ComVerifySignature Of hoCodeSign sPath vSigInfo To iValid
If (iValid = True) Begin
Get pvComObject of hoSignerCert to vSignerCert
Get ComGetSignerCert Of hoCodeSign vSignerCert To iSuccess
Get ComSubjectCN Of hoSignerCert To sTemp1
Showln "currently signed by: " sTemp1
Get ComValidToStr Of hoSignerCert To sTemp1
Showln "certificate valid until: " sTemp1
End
Else Begin
Showln "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.
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get ComLoadPfxFile Of hoCert "C:/someDir/myNewCodeSigningCert.pfx" "pfx password" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCert To sTemp1
Showln sTemp1
Procedure_Return
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"
// }
// }
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComUpdateString Of hoJson "hashAlg" "sha256" To iSuccess
Get ComUpdateBool Of hoJson "timestampToken.enabled" True To iSuccess
Get ComUpdateString Of hoJson "timestampToken.tsaUrl" "http://timestamp.digicert.com" To iSuccess
Get ComUpdateBool Of hoJson "timestampToken.requestTsaCert" True To iSuccess
Get ComUpdateString Of hoJson "timestampToken.hashAlg" "sha256" To iSuccess
// Sign the file. It is not necessary to call RemoveSignature first:
// the existing signature is removed and the new signature takes its place.
Get pvComObject of hoCert to vCert
Get pvComObject of hoJson to vJson
Get ComAddSignature Of hoCodeSign sPath vCert vJson To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCodeSign To sTemp1
Showln sTemp1
Procedure_Return
End
// The file now has one signature, made with the new certificate.
Get pvComObject of hoSigInfo to vSigInfo
Get ComVerifySignature Of hoCodeSign sPath vSigInfo To iValid
If (iValid = False) Begin
Get ComLastErrorText Of hoCodeSign To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSignerCert to vSignerCert
Get ComGetSignerCert Of hoCodeSign vSignerCert To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCodeSign To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComSubjectCN Of hoSignerCert To sTemp1
Showln "now signed by: " sTemp1
Get ComValidToStr Of hoSignerCert To sTemp1
Showln "certificate valid until: " sTemp1
End_Procedure