Sample code for 30+ languages & platforms
PowerBuilder

Create XAdES-T Signed XML

See more XAdES Examples

This example signs XML using the XAdES-T profile. XAdES-T is a profile within the XAdES standard that adds support for secure timestamping of signatures.

Secure timestamping involves adding a timestamp to the signature, indicating the exact time when the signature was applied.

Timestamping enhances the long-term validity of signatures by providing evidence that the signature existed at a specific point in time, even if the signer's certificate has expired or been revoked.

XAdES-T signatures include elements for embedding timestamp data within the XML signature, along with information about the timestamp authority and the timestamp verification process.

XAdES-T signatures are suitable for scenarios where long-term validity and integrity of signatures are essential, such as in legal and regulatory contexts where archived documents may need to be validated years or decades later.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_XmlToSign
oleobject loo_Gen
oleobject loo_Object1
oleobject loo_Object2
oleobject loo_Cert
oleobject loo_JsonTsa
oleobject loo_SbXml
oleobject loo_Verifier
integer li_NumSigs
integer li_VerifyIdx
integer li_Verified

li_Success = 0

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

li_Success = 1
// Create the XML to be signed...

// Use this online tool to generate code from sample XML: 
// Generate Code to Create XML

// <?xml version="1.0" encoding="UTF-8"?>
// <es:Dossier xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns="http://uri.etsi.org/01903/v1.3.2#" xmlns:es="https://www.microsec.hu/ds/e-szigno30#" xsi:schemaLocation="https://www.microsec.hu/ds/e-szigno30# https://www.microsec.hu/ds/e-szigno30.xsd">
// 	<es:DossierProfile Id="PObject0" OBJREF="Object0">
// 	<es:Title>e-akta.es3</es:Title>
// 	<es:E-category>electronic dossier</es:E-category>
// 	<es:CreationDate>2022-12-02T07:55:16Z</es:CreationDate>
// 	</es:DossierProfile>
// 	<es:Documents Id="Object0"/>
// </es:Dossier>

loo_XmlToSign = create oleobject
li_rc = loo_XmlToSign.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_XmlToSign
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_XmlToSign.Tag = "es:Dossier"
loo_XmlToSign.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
loo_XmlToSign.AddAttribute("xmlns:ds","http://www.w3.org/2000/09/xmldsig#")
loo_XmlToSign.AddAttribute("xmlns","http://uri.etsi.org/01903/v1.3.2#")
loo_XmlToSign.AddAttribute("xmlns:es","https://www.microsec.hu/ds/e-szigno30#")
loo_XmlToSign.AddAttribute("xsi:schemaLocation","https://www.microsec.hu/ds/e-szigno30# https://www.microsec.hu/ds/e-szigno30.xsd")
loo_XmlToSign.UpdateAttrAt("es:DossierProfile",1,"Id","PObject0")
loo_XmlToSign.UpdateAttrAt("es:DossierProfile",1,"OBJREF","Object0")
loo_XmlToSign.UpdateChildContent("es:DossierProfile|es:Title","e-akta.es3")
loo_XmlToSign.UpdateChildContent("es:DossierProfile|es:E-category","electronic dossier")
loo_XmlToSign.UpdateChildContent("es:DossierProfile|es:CreationDate","2022-12-02T07:55:16Z")
loo_XmlToSign.UpdateAttrAt("es:Documents",1,"Id","Object0")

loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")

loo_Gen.SigLocation = "es:Dossier"
loo_Gen.SigLocationMod = 0
loo_Gen.SigId = "S9fe8096e-2cac-415d-9222-f6cf2ecb314b"
loo_Gen.SigValueId = "VS9fe8096e-2cac-415d-9222-f6cf2ecb314b"
loo_Gen.SignedInfoId = "SIS9fe8096e-2cac-415d-9222-f6cf2ecb314b"
loo_Gen.SignedInfoCanonAlg = "EXCL_C14N"
loo_Gen.SignedInfoDigestMethod = "sha256"

// Set the KeyInfoId before adding references..
loo_Gen.KeyInfoId = "KS9fe8096e-2cac-415d-9222-f6cf2ecb314b"

// Create an Object to be added to the Signature.
loo_Object1 = create oleobject
li_rc = loo_Object1.ConnectToNewObject("Chilkat.Xml")

loo_Object1.Tag = "es:SignatureProfile"
loo_Object1.AddAttribute("Id","PS9fe8096e-2cac-415d-9222-f6cf2ecb314b")
loo_Object1.AddAttribute("OBJREF","Object0")
loo_Object1.AddAttribute("SIGREF","S9fe8096e-2cac-415d-9222-f6cf2ecb314b")
loo_Object1.AddAttribute("SIGREFLIST","#Object0 #PS9fe8096e-2cac-415d-9222-f6cf2ecb314b #PObject0 #XS9fe8096e-2cac-415d-9222-f6cf2ecb314b")
loo_Object1.UpdateChildContent("es:SignerName","EC Minősített-Tesztelő Péterke")
loo_Object1.UpdateChildContent("es:SDPresented","false")
loo_Object1.UpdateChildContent("es:Type","signature")
loo_Object1.UpdateAttrAt("es:Generator|es:Program",1,"name","e-Szigno")
loo_Object1.UpdateAttrAt("es:Generator|es:Program",1,"version","3.3.6.8")
loo_Object1.UpdateAttrAt("es:Generator|es:Device",1,"name","OpenSSL 1.1.1n  15 Mar 2022")
loo_Object1.UpdateAttrAt("es:Generator|es:Device",1,"type","")

loo_Gen.AddObject("O1S9fe8096e-2cac-415d-9222-f6cf2ecb314b",loo_Object1.GetXml(),"","")

// Create an Object to be added to the Signature.
loo_Object2 = create oleobject
li_rc = loo_Object2.ConnectToNewObject("Chilkat.Xml")

loo_Object2.Tag = "QualifyingProperties"
loo_Object2.AddAttribute("Target","#S9fe8096e-2cac-415d-9222-f6cf2ecb314b")
loo_Object2.AddAttribute("Id","QPS9fe8096e-2cac-415d-9222-f6cf2ecb314b")
loo_Object2.UpdateAttrAt("SignedProperties",1,"Id","XS9fe8096e-2cac-415d-9222-f6cf2ecb314b")
loo_Object2.UpdateChildContent("SignedProperties|SignedSignatureProperties|SigningTime","TO BE GENERATED BY CHILKAT")
loo_Object2.UpdateAttrAt("SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestMethod",1,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256")
loo_Object2.UpdateChildContent("SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT")
loo_Object2.UpdateChildContent("SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|IssuerSerialV2","TO BE GENERATED BY CHILKAT")
loo_Object2.UpdateChildContent("SignedProperties|SignedSignatureProperties|SignaturePolicyIdentifier|SignaturePolicyImplied","")
loo_Object2.UpdateChildContent("SignedProperties|SignedSignatureProperties|SignerRoleV2|ClaimedRoles|ClaimedRole","tesztelő")

// Here we have the EncapsulatedTimestamp found in the unsigned signature properties.
loo_Object2.UpdateAttrAt("UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp",1,"Id","T72cb4961-4326-4319-857a-7cf55e7ef899")
loo_Object2.UpdateAttrAt("UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|ds:CanonicalizationMethod",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#")
loo_Object2.UpdateAttrAt("UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp",1,"Id","ET72cb4961-4326-4319-857a-7cf55e7ef899")
loo_Object2.UpdateChildContent("UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp","TO BE GENERATED BY CHILKAT")
loo_Object2.UpdateAttrAt("UnsignedProperties|UnsignedSignatureProperties|TimeStampValidationData",1,"xmlns","http://uri.etsi.org/01903/v1.4.1#")
loo_Object2.UpdateAttrAt("UnsignedProperties|UnsignedSignatureProperties|CertificateValues",1,"Id","CV18c7702d-d45b-44bc-853a-a720f41053cd")
loo_Object2.UpdateAttrAt("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate",1,"Id","EC42db04c8-1422-407b-8c42-189353a55268")
loo_Object2.UpdateChildContent("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate","BASE64_CONTENT")
loo_Object2.UpdateAttrAt("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]",1,"Id","EC04728b44-a32c-46c1-b9bb-85b1f6b3c7d3")
loo_Object2.UpdateChildContent("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]","BASE64_CONTENT")

loo_Gen.AddObject("O2S9fe8096e-2cac-415d-9222-f6cf2ecb314b",loo_Object2.GetXml(),"","")

// -------- Reference 1 --------
loo_Gen.AddSameDocRef("Object0","sha256","EXCL_C14N","","")
loo_Gen.SetRefIdAttr("Object0","Re1f816c4-7898-4544-9b41-f4156dc0c528")

// -------- Reference 2 --------
loo_Gen.AddObjectRef("PS9fe8096e-2cac-415d-9222-f6cf2ecb314b","sha256","EXCL_C14N","","")
loo_Gen.SetRefIdAttr("PS9fe8096e-2cac-415d-9222-f6cf2ecb314b","Ra873b616-e568-4c38-ae94-27fbff67cc43")

// -------- Reference 3 --------
loo_Gen.AddSameDocRef("PObject0","sha256","EXCL_C14N","","")
loo_Gen.SetRefIdAttr("PObject0","Ra5d85948-5d6a-4914-8c32-242f5d6d9e81")

// -------- Reference 4 --------
loo_Gen.AddObjectRef("XS9fe8096e-2cac-415d-9222-f6cf2ecb314b","sha256","EXCL_C14N","","http://uri.etsi.org/01903#SignedProperties")
loo_Gen.SetRefIdAttr("XS9fe8096e-2cac-415d-9222-f6cf2ecb314b","Ra7412a43-dc05-4e0a-ac84-e9a070214757")

// Provide a certificate + private key. (PFX password is test123)
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Object1
    destroy loo_Object2
    destroy loo_Cert
    return
end if

loo_Gen.SetX509Cert(loo_Cert,1)

loo_Gen.KeyInfoType = "X509Data"
loo_Gen.X509Type = "Certificate"

// -------------------------------------------------------------------------------------------
// To have the EncapsulatedTimeStamp automatically added, we only need to do 2 things.
// 1) Add the <xades:EncapsulatedTimeStamp Encoding="http://uri.etsi.org/01903/v1.2.2#DER">TO BE GENERATED BY CHILKAT</xades:EncapsulatedTimeStamp>
//    to the unsigned properties.
// 2) Specify the TSA URL (Timestamping Authority URL).
//    Here we specify the TSA URL:
// -------------------------------------------------------------------------------------------

loo_JsonTsa = create oleobject
li_rc = loo_JsonTsa.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonTsa.UpdateString("timestampToken.tsaUrl","http://timestamp.digicert.com")
loo_JsonTsa.UpdateBool("timestampToken.requestTsaCert",1)
loo_Gen.SetTsa(loo_JsonTsa)

// Load XML to be signed...
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

loo_XmlToSign.GetXmlSb(loo_SbXml)

loo_Gen.Behaviors = "IndentedSignature,OmitAlreadyDefinedSigNamespace"

// Sign the XML...
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Object1
    destroy loo_Object2
    destroy loo_Cert
    destroy loo_JsonTsa
    destroy loo_SbXml
    return
end if

// -----------------------------------------------

// Save the signed XML to a file.
li_Success = loo_SbXml.WriteFile("c:/temp/qa_output/signedXml.xml","utf-8",0)

Write-Debug loo_SbXml.GetAsString()

// ----------------------------------------
// Verify the signatures we just produced...
loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.XmlDSig")

li_Success = loo_Verifier.LoadSignatureSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Object1
    destroy loo_Object2
    destroy loo_Cert
    destroy loo_JsonTsa
    destroy loo_SbXml
    destroy loo_Verifier
    return
end if

li_NumSigs = loo_Verifier.NumSignatures
li_VerifyIdx = 0
do while li_VerifyIdx < li_NumSigs
    loo_Verifier.Selector = li_VerifyIdx
    li_Verified = loo_Verifier.VerifySignature(1)
    if li_Verified <> 1 then
        Write-Debug loo_Verifier.LastErrorText
        destroy loo_XmlToSign
        destroy loo_Gen
        destroy loo_Object1
        destroy loo_Object2
        destroy loo_Cert
        destroy loo_JsonTsa
        destroy loo_SbXml
        destroy loo_Verifier
        return
    end if

    li_VerifyIdx = li_VerifyIdx + 1
loop
Write-Debug "All signatures were successfully verified."


destroy loo_XmlToSign
destroy loo_Gen
destroy loo_Object1
destroy loo_Object2
destroy loo_Cert
destroy loo_JsonTsa
destroy loo_SbXml
destroy loo_Verifier