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

PureBasic
IncludeFile "CkCodeSign.pb"
IncludeFile "CkCert.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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.

    path.s = "C:/someDir/MyApp.exe"

    codeSign.i = CkCodeSign::ckCreate()
    If codeSign.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sigInfo.i = CkJsonObject::ckCreate()
    If sigInfo.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    signerCert.i = CkCert::ckCreate()
    If signerCert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Find out who signed the file.
    valid.i = CkCodeSign::ckVerifySignature(codeSign,path,sigInfo)
    If valid = 1
        CkCodeSign::ckGetSignerCert(codeSign,signerCert)
        Debug "currently signed by: " + CkCert::ckSubjectCN(signerCert)
        Debug "certificate valid until: " + CkCert::ckValidToStr(signerCert)
    Else
        Debug "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.
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadPfxFile(cert,"C:/someDir/myNewCodeSigningCert.pfx","pfx password")
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkCodeSign::ckDispose(codeSign)
        CkJsonObject::ckDispose(sigInfo)
        CkCert::ckDispose(signerCert)
        CkCert::ckDispose(cert)
        ProcedureReturn
    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"
    ;    }
    ;  }
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"hashAlg","sha256")
    CkJsonObject::ckUpdateBool(json,"timestampToken.enabled",1)
    CkJsonObject::ckUpdateString(json,"timestampToken.tsaUrl","http://timestamp.digicert.com")
    CkJsonObject::ckUpdateBool(json,"timestampToken.requestTsaCert",1)
    CkJsonObject::ckUpdateString(json,"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.
    success = CkCodeSign::ckAddSignature(codeSign,path,cert,json)
    If success = 0
        Debug CkCodeSign::ckLastErrorText(codeSign)
        CkCodeSign::ckDispose(codeSign)
        CkJsonObject::ckDispose(sigInfo)
        CkCert::ckDispose(signerCert)
        CkCert::ckDispose(cert)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ;  The file now has one signature, made with the new certificate.
    valid = CkCodeSign::ckVerifySignature(codeSign,path,sigInfo)
    If valid = 0
        Debug CkCodeSign::ckLastErrorText(codeSign)
        CkCodeSign::ckDispose(codeSign)
        CkJsonObject::ckDispose(sigInfo)
        CkCert::ckDispose(signerCert)
        CkCert::ckDispose(cert)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    success = CkCodeSign::ckGetSignerCert(codeSign,signerCert)
    If success = 0
        Debug CkCodeSign::ckLastErrorText(codeSign)
        CkCodeSign::ckDispose(codeSign)
        CkJsonObject::ckDispose(sigInfo)
        CkCert::ckDispose(signerCert)
        CkCert::ckDispose(cert)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    Debug "now signed by: " + CkCert::ckSubjectCN(signerCert)
    Debug "certificate valid until: " + CkCert::ckValidToStr(signerCert)


    CkCodeSign::ckDispose(codeSign)
    CkJsonObject::ckDispose(sigInfo)
    CkCert::ckDispose(signerCert)
    CkCert::ckDispose(cert)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure