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 Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let _ = 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>
let xml_to_sign = chilkat::Xml::new();
xml_to_sign.set_tag("es:Dossier");
let _ = xml_to_sign.add_attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
let _ = xml_to_sign.add_attribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");
let _ = xml_to_sign.add_attribute("xmlns", "http://uri.etsi.org/01903/v1.3.2#");
let _ = xml_to_sign.add_attribute("xmlns:es", "https://www.microsec.hu/ds/e-szigno30#");
let _ = xml_to_sign.add_attribute("xsi:schemaLocation", "https://www.microsec.hu/ds/e-szigno30# https://www.microsec.hu/ds/e-szigno30.xsd");
let _ = xml_to_sign.update_attr_at("es:DossierProfile", true, "Id", "PObject0");
let _ = xml_to_sign.update_attr_at("es:DossierProfile", true, "OBJREF", "Object0");
xml_to_sign.update_child_content("es:DossierProfile|es:Title", "e-akta.es3");
xml_to_sign.update_child_content("es:DossierProfile|es:E-category", "electronic dossier");
xml_to_sign.update_child_content("es:DossierProfile|es:CreationDate", "2022-12-02T07:55:16Z");
let _ = xml_to_sign.update_attr_at("es:Documents", true, "Id", "Object0");
let gen_ = chilkat::XmlDSigGen::new();
gen_.set_sig_location("es:Dossier");
gen_.set_sig_location_mod(0);
gen_.set_sig_id("S9fe8096e-2cac-415d-9222-f6cf2ecb314b");
gen_.set_sig_value_id("VS9fe8096e-2cac-415d-9222-f6cf2ecb314b");
gen_.set_signed_info_id("SIS9fe8096e-2cac-415d-9222-f6cf2ecb314b");
gen_.set_signed_info_canon_alg("EXCL_C14N");
gen_.set_signed_info_digest_method("sha256");
// Set the KeyInfoId before adding references..
gen_.set_key_info_id("KS9fe8096e-2cac-415d-9222-f6cf2ecb314b");
// Create an Object to be added to the Signature.
let object1 = chilkat::Xml::new();
object1.set_tag("es:SignatureProfile");
let _ = object1.add_attribute("Id", "PS9fe8096e-2cac-415d-9222-f6cf2ecb314b");
let _ = object1.add_attribute("OBJREF", "Object0");
let _ = object1.add_attribute("SIGREF", "S9fe8096e-2cac-415d-9222-f6cf2ecb314b");
let _ = object1.add_attribute("SIGREFLIST", "#Object0 #PS9fe8096e-2cac-415d-9222-f6cf2ecb314b #PObject0 #XS9fe8096e-2cac-415d-9222-f6cf2ecb314b");
object1.update_child_content("es:SignerName", "EC Minősített-Tesztelő Péterke");
object1.update_child_content("es:SDPresented", "false");
object1.update_child_content("es:Type", "signature");
let _ = object1.update_attr_at("es:Generator|es:Program", true, "name", "e-Szigno");
let _ = object1.update_attr_at("es:Generator|es:Program", true, "version", "3.3.6.8");
let _ = object1.update_attr_at("es:Generator|es:Device", true, "name", "OpenSSL 1.1.1n 15 Mar 2022");
let _ = object1.update_attr_at("es:Generator|es:Device", true, "type", "");
let _ = gen_.add_object("O1S9fe8096e-2cac-415d-9222-f6cf2ecb314b", &object1.get_xml().unwrap_or_default(), "", "");
// Create an Object to be added to the Signature.
let object2 = chilkat::Xml::new();
object2.set_tag("QualifyingProperties");
let _ = object2.add_attribute("Target", "#S9fe8096e-2cac-415d-9222-f6cf2ecb314b");
let _ = object2.add_attribute("Id", "QPS9fe8096e-2cac-415d-9222-f6cf2ecb314b");
let _ = object2.update_attr_at("SignedProperties", true, "Id", "XS9fe8096e-2cac-415d-9222-f6cf2ecb314b");
object2.update_child_content("SignedProperties|SignedSignatureProperties|SigningTime", "TO BE GENERATED BY CHILKAT");
let _ = object2.update_attr_at("SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestMethod", true, "Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
object2.update_child_content("SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestValue", "TO BE GENERATED BY CHILKAT");
object2.update_child_content("SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|IssuerSerialV2", "TO BE GENERATED BY CHILKAT");
object2.update_child_content("SignedProperties|SignedSignatureProperties|SignaturePolicyIdentifier|SignaturePolicyImplied", "");
object2.update_child_content("SignedProperties|SignedSignatureProperties|SignerRoleV2|ClaimedRoles|ClaimedRole", "tesztelő");
// Here we have the EncapsulatedTimestamp found in the unsigned signature properties.
let _ = object2.update_attr_at("UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp", true, "Id", "T72cb4961-4326-4319-857a-7cf55e7ef899");
let _ = object2.update_attr_at("UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|ds:CanonicalizationMethod", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#");
let _ = object2.update_attr_at("UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp", true, "Id", "ET72cb4961-4326-4319-857a-7cf55e7ef899");
object2.update_child_content("UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp", "TO BE GENERATED BY CHILKAT");
let _ = object2.update_attr_at("UnsignedProperties|UnsignedSignatureProperties|TimeStampValidationData", true, "xmlns", "http://uri.etsi.org/01903/v1.4.1#");
let _ = object2.update_attr_at("UnsignedProperties|UnsignedSignatureProperties|CertificateValues", true, "Id", "CV18c7702d-d45b-44bc-853a-a720f41053cd");
let _ = object2.update_attr_at("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate", true, "Id", "EC42db04c8-1422-407b-8c42-189353a55268");
object2.update_child_content("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate", "BASE64_CONTENT");
let _ = object2.update_attr_at("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]", true, "Id", "EC04728b44-a32c-46c1-b9bb-85b1f6b3c7d3");
object2.update_child_content("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]", "BASE64_CONTENT");
let _ = gen_.add_object("O2S9fe8096e-2cac-415d-9222-f6cf2ecb314b", &object2.get_xml().unwrap_or_default(), "", "");
// -------- Reference 1 --------
let _ = gen_.add_same_doc_ref("Object0", "sha256", "EXCL_C14N", "", "");
let _ = gen_.set_ref_id_attr("Object0", "Re1f816c4-7898-4544-9b41-f4156dc0c528");
// -------- Reference 2 --------
let _ = gen_.add_object_ref("PS9fe8096e-2cac-415d-9222-f6cf2ecb314b", "sha256", "EXCL_C14N", "", "");
let _ = gen_.set_ref_id_attr("PS9fe8096e-2cac-415d-9222-f6cf2ecb314b", "Ra873b616-e568-4c38-ae94-27fbff67cc43");
// -------- Reference 3 --------
let _ = gen_.add_same_doc_ref("PObject0", "sha256", "EXCL_C14N", "", "");
let _ = gen_.set_ref_id_attr("PObject0", "Ra5d85948-5d6a-4914-8c32-242f5d6d9e81");
// -------- Reference 4 --------
let _ = gen_.add_object_ref("XS9fe8096e-2cac-415d-9222-f6cf2ecb314b", "sha256", "EXCL_C14N", "", "http://uri.etsi.org/01903#SignedProperties");
let _ = gen_.set_ref_id_attr("XS9fe8096e-2cac-415d-9222-f6cf2ecb314b", "Ra7412a43-dc05-4e0a-ac84-e9a070214757");
// Provide a certificate + private key. (PFX password is test123)
let cert = chilkat::Cert::new();
if cert.load_pfx_file("qa_data/pfx/cert_test123.pfx", "test123").is_err() {
println!("{}", cert.last_error_text());
return;
}
let _ = gen_.set_x509_cert(&cert, true);
gen_.set_key_info_type("X509Data");
gen_.set_x509_type("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:
// -------------------------------------------------------------------------------------------
let json_tsa = chilkat::JsonObject::new();
let _ = json_tsa.update_string("timestampToken.tsaUrl", "http://timestamp.digicert.com");
let _ = json_tsa.update_bool("timestampToken.requestTsaCert", true);
let _ = gen_.set_tsa(&json_tsa);
// Load XML to be signed...
let sb_xml = chilkat::StringBuilder::new();
let _ = xml_to_sign.get_xml_sb(&sb_xml);
gen_.set_behaviors("IndentedSignature,OmitAlreadyDefinedSigNamespace");
// Sign the XML...
if gen_.create_xml_d_sig_sb(&sb_xml).is_err() {
println!("{}", gen_.last_error_text());
return;
}
// -----------------------------------------------
// Save the signed XML to a file.
let _ = sb_xml.write_file("c:/temp/qa_output/signedXml.xml", "utf-8", false).is_ok();
println!("{}", sb_xml.get_as_string().unwrap_or_default());
// ----------------------------------------
// Verify the signatures we just produced...
let verifier = chilkat::XmlDSig::new();
if verifier.load_signature_sb(&sb_xml).is_err() {
println!("{}", verifier.last_error_text());
return;
}
let num_sigs = verifier.num_signatures();
let mut verify_idx = 0;
while verify_idx < num_sigs {
verifier.set_selector(verify_idx);
if verifier.verify_signature(true).is_err() {
println!("{}", verifier.last_error_text());
return;
}
verify_idx = verify_idx + 1;
}
println!("All signatures were successfully verified.");