Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, XmlDSigGen, XmlDSig, StringBuilder, JsonObject, Cert, Xml;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
xmlToSign: HCkXml;
gen: HCkXmlDSigGen;
object1: HCkXml;
object2: HCkXml;
cert: HCkCert;
jsonTsa: HCkJsonObject;
sbXml: HCkStringBuilder;
verifier: HCkXmlDSig;
numSigs: Integer;
verifyIdx: Integer;
verified: Boolean;

begin
success := False;

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

success := True;
// 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>

xmlToSign := CkXml_Create();
CkXml_putTag(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',True,'Id','PObject0');
CkXml_UpdateAttrAt(xmlToSign,'es:DossierProfile',True,'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',True,'Id','Object0');

gen := CkXmlDSigGen_Create();

CkXmlDSigGen_putSigLocation(gen,'es:Dossier');
CkXmlDSigGen_putSigLocationMod(gen,0);
CkXmlDSigGen_putSigId(gen,'S9fe8096e-2cac-415d-9222-f6cf2ecb314b');
CkXmlDSigGen_putSigValueId(gen,'VS9fe8096e-2cac-415d-9222-f6cf2ecb314b');
CkXmlDSigGen_putSignedInfoId(gen,'SIS9fe8096e-2cac-415d-9222-f6cf2ecb314b');
CkXmlDSigGen_putSignedInfoCanonAlg(gen,'EXCL_C14N');
CkXmlDSigGen_putSignedInfoDigestMethod(gen,'sha256');

// Set the KeyInfoId before adding references..
CkXmlDSigGen_putKeyInfoId(gen,'KS9fe8096e-2cac-415d-9222-f6cf2ecb314b');

// Create an Object to be added to the Signature.
object1 := CkXml_Create();
CkXml_putTag(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',True,'name','e-Szigno');
CkXml_UpdateAttrAt(object1,'es:Generator|es:Program',True,'version','3.3.6.8');
CkXml_UpdateAttrAt(object1,'es:Generator|es:Device',True,'name','OpenSSL 1.1.1n  15 Mar 2022');
CkXml_UpdateAttrAt(object1,'es:Generator|es:Device',True,'type','');

CkXmlDSigGen_AddObject(gen,'O1S9fe8096e-2cac-415d-9222-f6cf2ecb314b',CkXml__getXml(object1),'','');

// Create an Object to be added to the Signature.
object2 := CkXml_Create();
CkXml_putTag(object2,'QualifyingProperties');
CkXml_AddAttribute(object2,'Target','#S9fe8096e-2cac-415d-9222-f6cf2ecb314b');
CkXml_AddAttribute(object2,'Id','QPS9fe8096e-2cac-415d-9222-f6cf2ecb314b');
CkXml_UpdateAttrAt(object2,'SignedProperties',True,'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',True,'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',True,'Id','T72cb4961-4326-4319-857a-7cf55e7ef899');
CkXml_UpdateAttrAt(object2,'UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|ds:CanonicalizationMethod',True,'Algorithm','http://www.w3.org/2001/10/xml-exc-c14n#');
CkXml_UpdateAttrAt(object2,'UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp',True,'Id','ET72cb4961-4326-4319-857a-7cf55e7ef899');
CkXml_UpdateChildContent(object2,'UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp','TO BE GENERATED BY CHILKAT');
CkXml_UpdateAttrAt(object2,'UnsignedProperties|UnsignedSignatureProperties|TimeStampValidationData',True,'xmlns','http://uri.etsi.org/01903/v1.4.1#');
CkXml_UpdateAttrAt(object2,'UnsignedProperties|UnsignedSignatureProperties|CertificateValues',True,'Id','CV18c7702d-d45b-44bc-853a-a720f41053cd');
CkXml_UpdateAttrAt(object2,'UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate',True,'Id','EC42db04c8-1422-407b-8c42-189353a55268');
CkXml_UpdateChildContent(object2,'UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate','BASE64_CONTENT');
CkXml_UpdateAttrAt(object2,'UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]',True,'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)
cert := CkCert_Create();
success := CkCert_LoadPfxFile(cert,'qa_data/pfx/cert_test123.pfx','test123');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkCert__lastErrorText(cert));
    Exit;
  end;
CkXmlDSigGen_SetX509Cert(gen,cert,True);

CkXmlDSigGen_putKeyInfoType(gen,'X509Data');
CkXmlDSigGen_putX509Type(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:
// -------------------------------------------------------------------------------------------

jsonTsa := CkJsonObject_Create();
CkJsonObject_UpdateString(jsonTsa,'timestampToken.tsaUrl','http://timestamp.digicert.com');
CkJsonObject_UpdateBool(jsonTsa,'timestampToken.requestTsaCert',True);
CkXmlDSigGen_SetTsa(gen,jsonTsa);

// Load XML to be signed...
sbXml := CkStringBuilder_Create();
CkXml_GetXmlSb(xmlToSign,sbXml);

CkXmlDSigGen_putBehaviors(gen,'IndentedSignature,OmitAlreadyDefinedSigNamespace');

// Sign the XML...
success := CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkXmlDSigGen__lastErrorText(gen));
    Exit;
  end;
// -----------------------------------------------

// Save the signed XML to a file.
success := CkStringBuilder_WriteFile(sbXml,'c:/temp/qa_output/signedXml.xml','utf-8',False);

Memo1.Lines.Add(CkStringBuilder__getAsString(sbXml));

// ----------------------------------------
// Verify the signatures we just produced...
verifier := CkXmlDSig_Create();
success := CkXmlDSig_LoadSignatureSb(verifier,sbXml);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkXmlDSig__lastErrorText(verifier));
    Exit;
  end;

numSigs := CkXmlDSig_getNumSignatures(verifier);
verifyIdx := 0;
while verifyIdx < numSigs do
  begin
    CkXmlDSig_putSelector(verifier,verifyIdx);
    verified := CkXmlDSig_VerifySignature(verifier,True);
    if (verified <> True) then
      begin
        Memo1.Lines.Add(CkXmlDSig__lastErrorText(verifier));
        Exit;
      end;
    verifyIdx := verifyIdx + 1;
  end;

Memo1.Lines.Add('All signatures were successfully verified.');

CkXml_Dispose(xmlToSign);
CkXmlDSigGen_Dispose(gen);
CkXml_Dispose(object1);
CkXml_Dispose(object2);
CkCert_Dispose(cert);
CkJsonObject_Dispose(jsonTsa);
CkStringBuilder_Dispose(sbXml);
CkXmlDSig_Dispose(verifier);

end;