Sample code for 30+ languages & platforms
Xbase++

Add EncapsulatedTimestamp to Already-Signed XML

See more XML Digital Signatures Examples

Demonstrates how to add an EncapsulatedTimestamp to an existing XML signature.

Note: This example requires Chilkat v9.5.0.90 or greater.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSbXml
LOCAL oDsig
LOCAL oJson
LOCAL oSbOut
LOCAL oVerifier
LOCAL nNumSigs
LOCAL nVerifyIdx
LOCAL nVerified

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Note: We cannot load the already-signed XML into a Chilkat XML object because it would re-format the XML when re-emitted.
//  (i.e. indentation and whitespace could change, and it would invalidate the existing signature.)
//  We must use a StringBuilder.
oSbXml := CreateObject("Chilkat.StringBuilder")
nSuccess := oSbXml:LoadFile("qa_data/xml_dsig_valid_samples/encapsulatedTimestamp_not_yet_added.xml", "utf-8")
IF (nSuccess == 0)
    ? "Failed to load the XML file."
    oSbXml:destroy()
    RETURN
ENDIF

oDsig := CreateObject("Chilkat.XmlDSig")
nSuccess := oDsig:LoadSignatureSb(oSbXml)
IF (nSuccess == 0)
    ? oDsig:LastErrorText
    oSbXml:destroy()
    oDsig:destroy()
    RETURN
ENDIF

IF (oDsig:HasEncapsulatedTimeStamp() == 1)
    ? "This signed XML already has an EncapsulatedTimeStamp"
    oSbXml:destroy()
    oDsig:destroy()
    RETURN
ENDIF

//  Specify the timestamping authority URL
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("timestampToken.tsaUrl", "http://timestamp.digicert.com")
oJson:UpdateBool("timestampToken.requestTsaCert", 1)

//  Call AddEncapsulatedTimeStamp to add the EncapsulatedTimeStamp to the signature.
//  Note: If the signed XML contains multiple signatures, the signature modified is the one 
//  indicated by the dsig.Selector property.
oSbOut := CreateObject("Chilkat.StringBuilder")
nSuccess := oDsig:AddEncapsulatedTimeStamp(oJson, oSbOut)
IF (nSuccess == 0)
    ? oDsig:LastErrorText
    oSbXml:destroy()
    oDsig:destroy()
    oJson:destroy()
    oSbOut:destroy()
    RETURN
ENDIF

oSbOut:WriteFile("qa_output/addedEncapsulatedTimeStamp.xml", "utf-8", 0)

//  The EncapsulatedTimeStamp can be validated when validating the signature by adding the VerifyEncapsulatedTimeStamp
//  keyword to UncommonOptions.  See here:

//  ----------------------------------------
//  Verify the signatures we just produced...
oVerifier := CreateObject("Chilkat.XmlDSig")
nSuccess := oVerifier:LoadSignatureSb(oSbOut)
IF (nSuccess != 1)
    ? oVerifier:LastErrorText
    oSbXml:destroy()
    oDsig:destroy()
    oJson:destroy()
    oSbOut:destroy()
    oVerifier:destroy()
    RETURN
ENDIF

//  Add "VerifyEncapsulatedTimeStamp" to the UncommonOptions to also verify any EncapsulatedTimeStamps
oVerifier:UncommonOptions := "VerifyEncapsulatedTimeStamp"

nNumSigs := oVerifier:NumSignatures
nVerifyIdx := 0
DO WHILE nVerifyIdx < nNumSigs
    oVerifier:Selector := nVerifyIdx
    nVerified := oVerifier:VerifySignature(1)
    IF (nVerified != 1)
        ? oVerifier:LastErrorText
        oSbXml:destroy()
        oDsig:destroy()
        oJson:destroy()
        oSbOut:destroy()
        oVerifier:destroy()
        RETURN
    ENDIF

    nVerifyIdx := nVerifyIdx + 1
ENDDO
? "All signatures were successfully verified."

oSbXml:destroy()
oDsig:destroy()
oJson:destroy()
oSbOut:destroy()
oVerifier:destroy()