Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lcPath
LOCAL loCodeSign
LOCAL loSigInfo
LOCAL loSignerCert
LOCAL lnValid
LOCAL loCert
LOCAL loJson

lnSuccess = 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.

lcPath = "C:/someDir/MyApp.exe"

loCodeSign = CreateObject('Chilkat.CodeSign')
loSigInfo = CreateObject('Chilkat.JsonObject')
loSignerCert = CreateObject('Chilkat.Cert')

*  Find out who signed the file.
lnValid = loCodeSign.VerifySignature(lcPath,loSigInfo)
IF (lnValid = 1) THEN
    loCodeSign.GetSignerCert(loSignerCert)
    ? "currently signed by: " + loSignerCert.SubjectCN
    ? "certificate valid until: " + loSignerCert.ValidToStr
ELSE
    ? "The file is not signed, or its signature is not valid."
ENDIF

*  Load the certificate to sign with.  It must have access to its private key.
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadPfxFile("C:/someDir/myNewCodeSigningCert.pfx","pfx password")
IF (lnSuccess = 0) THEN
    ? loCert.LastErrorText
    RELEASE loCodeSign
    RELEASE loSigInfo
    RELEASE loSignerCert
    RELEASE loCert
    CANCEL
ENDIF

*  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"
*    }
*  }
loJson = CreateObject('Chilkat.JsonObject')
loJson.UpdateString("hashAlg","sha256")
loJson.UpdateBool("timestampToken.enabled",1)
loJson.UpdateString("timestampToken.tsaUrl","http://timestamp.digicert.com")
loJson.UpdateBool("timestampToken.requestTsaCert",1)
loJson.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.
lnSuccess = loCodeSign.AddSignature(lcPath,loCert,loJson)
IF (lnSuccess = 0) THEN
    ? loCodeSign.LastErrorText
    RELEASE loCodeSign
    RELEASE loSigInfo
    RELEASE loSignerCert
    RELEASE loCert
    RELEASE loJson
    CANCEL
ENDIF

*  The file now has one signature, made with the new certificate.
lnValid = loCodeSign.VerifySignature(lcPath,loSigInfo)
IF (lnValid = 0) THEN
    ? loCodeSign.LastErrorText
    RELEASE loCodeSign
    RELEASE loSigInfo
    RELEASE loSignerCert
    RELEASE loCert
    RELEASE loJson
    CANCEL
ENDIF

lnSuccess = loCodeSign.GetSignerCert(loSignerCert)
IF (lnSuccess = 0) THEN
    ? loCodeSign.LastErrorText
    RELEASE loCodeSign
    RELEASE loSigInfo
    RELEASE loSignerCert
    RELEASE loCert
    RELEASE loJson
    CANCEL
ENDIF

? "now signed by: " + loSignerCert.SubjectCN
? "certificate valid until: " + loSignerCert.ValidToStr

RELEASE loCodeSign
RELEASE loSigInfo
RELEASE loSignerCert
RELEASE loCert
RELEASE loJson