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 Tcl Downloads
load ./chilkat.dll
set success 0
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set 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>
set xmlToSign [new_CkXml]
CkXml_put_Tag $xmlToSign "es:Dossier"
CkXml_AddAttribute $xmlToSign "xmlns:xsi" "http://www.w3.org/2001/XMLSchema-instance"
CkXml_AddAttribute $xmlToSign "xmlns:ds" "http://www.w3.org/2000/09/xmldsig#"
CkXml_AddAttribute $xmlToSign "xmlns" "http://uri.etsi.org/01903/v1.3.2#"
CkXml_AddAttribute $xmlToSign "xmlns:es" "https://www.microsec.hu/ds/e-szigno30#"
CkXml_AddAttribute $xmlToSign "xsi:schemaLocation" "https://www.microsec.hu/ds/e-szigno30# https://www.microsec.hu/ds/e-szigno30.xsd"
CkXml_UpdateAttrAt $xmlToSign "es:DossierProfile" 1 "Id" "PObject0"
CkXml_UpdateAttrAt $xmlToSign "es:DossierProfile" 1 "OBJREF" "Object0"
CkXml_UpdateChildContent $xmlToSign "es:DossierProfile|es:Title" "e-akta.es3"
CkXml_UpdateChildContent $xmlToSign "es:DossierProfile|es:E-category" "electronic dossier"
CkXml_UpdateChildContent $xmlToSign "es:DossierProfile|es:CreationDate" "2022-12-02T07:55:16Z"
CkXml_UpdateAttrAt $xmlToSign "es:Documents" 1 "Id" "Object0"
set gen [new_CkXmlDSigGen]
CkXmlDSigGen_put_SigLocation $gen "es:Dossier"
CkXmlDSigGen_put_SigLocationMod $gen 0
CkXmlDSigGen_put_SigId $gen "S9fe8096e-2cac-415d-9222-f6cf2ecb314b"
CkXmlDSigGen_put_SigValueId $gen "VS9fe8096e-2cac-415d-9222-f6cf2ecb314b"
CkXmlDSigGen_put_SignedInfoId $gen "SIS9fe8096e-2cac-415d-9222-f6cf2ecb314b"
CkXmlDSigGen_put_SignedInfoCanonAlg $gen "EXCL_C14N"
CkXmlDSigGen_put_SignedInfoDigestMethod $gen "sha256"
# Set the KeyInfoId before adding references..
CkXmlDSigGen_put_KeyInfoId $gen "KS9fe8096e-2cac-415d-9222-f6cf2ecb314b"
# Create an Object to be added to the Signature.
set object1 [new_CkXml]
CkXml_put_Tag $object1 "es:SignatureProfile"
CkXml_AddAttribute $object1 "Id" "PS9fe8096e-2cac-415d-9222-f6cf2ecb314b"
CkXml_AddAttribute $object1 "OBJREF" "Object0"
CkXml_AddAttribute $object1 "SIGREF" "S9fe8096e-2cac-415d-9222-f6cf2ecb314b"
CkXml_AddAttribute $object1 "SIGREFLIST" "#Object0 #PS9fe8096e-2cac-415d-9222-f6cf2ecb314b #PObject0 #XS9fe8096e-2cac-415d-9222-f6cf2ecb314b"
CkXml_UpdateChildContent $object1 "es:SignerName" "EC Minősített-Tesztelő Péterke"
CkXml_UpdateChildContent $object1 "es:SDPresented" "false"
CkXml_UpdateChildContent $object1 "es:Type" "signature"
CkXml_UpdateAttrAt $object1 "es:Generator|es:Program" 1 "name" "e-Szigno"
CkXml_UpdateAttrAt $object1 "es:Generator|es:Program" 1 "version" "3.3.6.8"
CkXml_UpdateAttrAt $object1 "es:Generator|es:Device" 1 "name" "OpenSSL 1.1.1n 15 Mar 2022"
CkXml_UpdateAttrAt $object1 "es:Generator|es:Device" 1 "type" ""
CkXmlDSigGen_AddObject $gen "O1S9fe8096e-2cac-415d-9222-f6cf2ecb314b" [CkXml_getXml $object1] "" ""
# Create an Object to be added to the Signature.
set object2 [new_CkXml]
CkXml_put_Tag $object2 "QualifyingProperties"
CkXml_AddAttribute $object2 "Target" "#S9fe8096e-2cac-415d-9222-f6cf2ecb314b"
CkXml_AddAttribute $object2 "Id" "QPS9fe8096e-2cac-415d-9222-f6cf2ecb314b"
CkXml_UpdateAttrAt $object2 "SignedProperties" 1 "Id" "XS9fe8096e-2cac-415d-9222-f6cf2ecb314b"
CkXml_UpdateChildContent $object2 "SignedProperties|SignedSignatureProperties|SigningTime" "TO BE GENERATED BY CHILKAT"
CkXml_UpdateAttrAt $object2 "SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestMethod" 1 "Algorithm" "http://www.w3.org/2001/04/xmlenc#sha256"
CkXml_UpdateChildContent $object2 "SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestValue" "TO BE GENERATED BY CHILKAT"
CkXml_UpdateChildContent $object2 "SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|IssuerSerialV2" "TO BE GENERATED BY CHILKAT"
CkXml_UpdateChildContent $object2 "SignedProperties|SignedSignatureProperties|SignaturePolicyIdentifier|SignaturePolicyImplied" ""
CkXml_UpdateChildContent $object2 "SignedProperties|SignedSignatureProperties|SignerRoleV2|ClaimedRoles|ClaimedRole" "tesztelő"
# Here we have the EncapsulatedTimestamp found in the unsigned signature properties.
CkXml_UpdateAttrAt $object2 "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp" 1 "Id" "T72cb4961-4326-4319-857a-7cf55e7ef899"
CkXml_UpdateAttrAt $object2 "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|ds:CanonicalizationMethod" 1 "Algorithm" "http://www.w3.org/2001/10/xml-exc-c14n#"
CkXml_UpdateAttrAt $object2 "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp" 1 "Id" "ET72cb4961-4326-4319-857a-7cf55e7ef899"
CkXml_UpdateChildContent $object2 "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp" "TO BE GENERATED BY CHILKAT"
CkXml_UpdateAttrAt $object2 "UnsignedProperties|UnsignedSignatureProperties|TimeStampValidationData" 1 "xmlns" "http://uri.etsi.org/01903/v1.4.1#"
CkXml_UpdateAttrAt $object2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues" 1 "Id" "CV18c7702d-d45b-44bc-853a-a720f41053cd"
CkXml_UpdateAttrAt $object2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate" 1 "Id" "EC42db04c8-1422-407b-8c42-189353a55268"
CkXml_UpdateChildContent $object2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate" "BASE64_CONTENT"
CkXml_UpdateAttrAt $object2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]" 1 "Id" "EC04728b44-a32c-46c1-b9bb-85b1f6b3c7d3"
CkXml_UpdateChildContent $object2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]" "BASE64_CONTENT"
CkXmlDSigGen_AddObject $gen "O2S9fe8096e-2cac-415d-9222-f6cf2ecb314b" [CkXml_getXml $object2] "" ""
# -------- Reference 1 --------
CkXmlDSigGen_AddSameDocRef $gen "Object0" "sha256" "EXCL_C14N" "" ""
CkXmlDSigGen_SetRefIdAttr $gen "Object0" "Re1f816c4-7898-4544-9b41-f4156dc0c528"
# -------- Reference 2 --------
CkXmlDSigGen_AddObjectRef $gen "PS9fe8096e-2cac-415d-9222-f6cf2ecb314b" "sha256" "EXCL_C14N" "" ""
CkXmlDSigGen_SetRefIdAttr $gen "PS9fe8096e-2cac-415d-9222-f6cf2ecb314b" "Ra873b616-e568-4c38-ae94-27fbff67cc43"
# -------- Reference 3 --------
CkXmlDSigGen_AddSameDocRef $gen "PObject0" "sha256" "EXCL_C14N" "" ""
CkXmlDSigGen_SetRefIdAttr $gen "PObject0" "Ra5d85948-5d6a-4914-8c32-242f5d6d9e81"
# -------- Reference 4 --------
CkXmlDSigGen_AddObjectRef $gen "XS9fe8096e-2cac-415d-9222-f6cf2ecb314b" "sha256" "EXCL_C14N" "" "http://uri.etsi.org/01903#SignedProperties"
CkXmlDSigGen_SetRefIdAttr $gen "XS9fe8096e-2cac-415d-9222-f6cf2ecb314b" "Ra7412a43-dc05-4e0a-ac84-e9a070214757"
# Provide a certificate + private key. (PFX password is test123)
set cert [new_CkCert]
set success [CkCert_LoadPfxFile $cert "qa_data/pfx/cert_test123.pfx" "test123"]
if {$success != 1} then {
puts [CkCert_lastErrorText $cert]
delete_CkXml $xmlToSign
delete_CkXmlDSigGen $gen
delete_CkXml $object1
delete_CkXml $object2
delete_CkCert $cert
exit
}
CkXmlDSigGen_SetX509Cert $gen $cert 1
CkXmlDSigGen_put_KeyInfoType $gen "X509Data"
CkXmlDSigGen_put_X509Type $gen "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:
# -------------------------------------------------------------------------------------------
set jsonTsa [new_CkJsonObject]
CkJsonObject_UpdateString $jsonTsa "timestampToken.tsaUrl" "http://timestamp.digicert.com"
CkJsonObject_UpdateBool $jsonTsa "timestampToken.requestTsaCert" 1
CkXmlDSigGen_SetTsa $gen $jsonTsa
# Load XML to be signed...
set sbXml [new_CkStringBuilder]
CkXml_GetXmlSb $xmlToSign $sbXml
CkXmlDSigGen_put_Behaviors $gen "IndentedSignature,OmitAlreadyDefinedSigNamespace"
# Sign the XML...
set success [CkXmlDSigGen_CreateXmlDSigSb $gen $sbXml]
if {$success != 1} then {
puts [CkXmlDSigGen_lastErrorText $gen]
delete_CkXml $xmlToSign
delete_CkXmlDSigGen $gen
delete_CkXml $object1
delete_CkXml $object2
delete_CkCert $cert
delete_CkJsonObject $jsonTsa
delete_CkStringBuilder $sbXml
exit
}
# -----------------------------------------------
# Save the signed XML to a file.
set success [CkStringBuilder_WriteFile $sbXml "c:/temp/qa_output/signedXml.xml" "utf-8" 0]
puts [CkStringBuilder_getAsString $sbXml]
# ----------------------------------------
# Verify the signatures we just produced...
set verifier [new_CkXmlDSig]
set success [CkXmlDSig_LoadSignatureSb $verifier $sbXml]
if {$success != 1} then {
puts [CkXmlDSig_lastErrorText $verifier]
delete_CkXml $xmlToSign
delete_CkXmlDSigGen $gen
delete_CkXml $object1
delete_CkXml $object2
delete_CkCert $cert
delete_CkJsonObject $jsonTsa
delete_CkStringBuilder $sbXml
delete_CkXmlDSig $verifier
exit
}
set numSigs [CkXmlDSig_get_NumSignatures $verifier]
set verifyIdx 0
while {$verifyIdx < $numSigs} {
CkXmlDSig_put_Selector $verifier $verifyIdx
set verified [CkXmlDSig_VerifySignature $verifier 1]
if {$verified != 1} then {
puts [CkXmlDSig_lastErrorText $verifier]
delete_CkXml $xmlToSign
delete_CkXmlDSigGen $gen
delete_CkXml $object1
delete_CkXml $object2
delete_CkCert $cert
delete_CkJsonObject $jsonTsa
delete_CkStringBuilder $sbXml
delete_CkXmlDSig $verifier
exit
}
set verifyIdx [expr $verifyIdx + 1]
}
puts "All signatures were successfully verified."
delete_CkXml $xmlToSign
delete_CkXmlDSigGen $gen
delete_CkXml $object1
delete_CkXml $object2
delete_CkCert $cert
delete_CkJsonObject $jsonTsa
delete_CkStringBuilder $sbXml
delete_CkXmlDSig $verifier