Sample code for 30+ languages & platforms
Rust

XAdES using TSA Requiring Client Certificate

See more XML Digital Signatures Examples

Demonstrates how to create an XMLDSig (XAdES) signed document which includes an EncapsulatedTimestamp using a TSA (TimeStamp Authority) server requiring client certificate authentication. One such TSA is https://www3.postsignum.cz/TSS/TSS_crt/

Chilkat Rust Downloads

Rust

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

let _ = true;

// Load the XML to be signed.  For example, the XML to be signed might contain something like this:

// <?xml version="1.0" encoding="utf-8"?>
// <TransakcniLogSystemu xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://nsess.public.cz/erms_trans/v_01_01" Id="Signature1">
//   <TransLogInfo>
//     <Identifikator>XYZ ABC</Identifikator>
//     <DatumVzniku>2022-12-20T14:39:02.3625922+01:00</DatumVzniku>
//     <DatumCasOd>2022-12-20T14:26:26.88</DatumCasOd>
//     <DatumCasDo>2022-12-20T14:39:02.287</DatumCasDo>
//     <Software>XYZ</Software>
//     <VerzeSoftware>2.0.19.32</VerzeSoftware>
//   </TransLogInfo>
//   <Udalosti>
//     <Udalost>
//       <Poradi>1</Poradi>
// ...

// Load the XML to be signed from a file.
// (XML can be loaded from other source, such as a string variable.)
let sb_xml = chilkat::StringBuilder::new();
let _ = sb_xml.load_file("xmlToSign.xml", "utf-8").is_ok();

let gen_ = chilkat::XmlDSigGen::new();

gen_.set_sig_location("TransakcniLogSystemu");
gen_.set_sig_location_mod(0);
gen_.set_sig_id("SignatureID-Signature1");
gen_.set_sig_namespace_prefix("ds");
gen_.set_sig_namespace_uri("http://www.w3.org/2000/09/xmldsig#");
gen_.set_signed_info_canon_alg("C14N");
gen_.set_signed_info_digest_method("sha256");

// Set the KeyInfoId before adding references..
gen_.set_key_info_id("KeyInfoId-Signature-Signature1");

// Create an Object to be added to the Signature.

// Note: Chilkat will automatically fill in the values marked as "TO BE GENERATED BY CHILKAT" at the time of signing.
// The EncapsulatedTimestamp will be automatically generated.

let object1 = chilkat::Xml::new();
object1.set_tag("xades:QualifyingProperties");
let _ = object1.add_attribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#");
let _ = object1.add_attribute("Target", "#Signature1");

let _ = object1.update_attr_at("xades:SignedProperties", true, "Id", "SignedProperties-Signature-Signature1");
object1.update_child_content("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", "TO BE GENERATED BY CHILKAT");
let _ = object1.update_attr_at("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod", true, "Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
object1.update_child_content("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue", "TO BE GENERATED BY CHILKAT");
object1.update_child_content("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2", "TO BE GENERATED BY CHILKAT");

// The EncapsulatedTimestamp will be included in the unsigned properties.
let _ = object1.update_attr_at("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp", true, "Id", "signature-timestamp-5561-8212-3316-5191");
let _ = object1.update_attr_at("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#");
let _ = object1.update_attr_at("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp", true, "Encoding", "http://uri.etsi.org/01903/v1.2.2#DER");
object1.update_child_content("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp", "TO BE GENERATED BY CHILKAT");

let _ = gen_.add_object("XadesObjectId-Signature1", &object1.get_xml().unwrap_or_default(), "", "");

// -------- Reference 1 --------
let _ = gen_.add_object_ref("SignedProperties-Signature-Signature1", "sha256", "EXCL_C14N", "", "http://uri.etsi.org/01903#SignedProperties");

// -------- Reference 2 --------
let _ = gen_.add_same_doc_ref("KeyInfoId-Signature-Signature1", "sha256", "EXCL_C14N", "", "");
let _ = gen_.set_ref_id_attr("KeyInfoId-Signature-Signature1", "ReferenceKeyInfo");

// -------- Reference 3 --------
let _ = gen_.add_same_doc_ref("", "sha256", "EXCL_C14N", "", "");
let _ = gen_.set_ref_id_attr("", "Reference-Signature1");

// 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");

gen_.set_behaviors("IndentedSignature");

// -------------------------------------------------------------------------------------------
// To have the EncapsulatedTimeStamp automatically added... 
// 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.  (This was accomplished in the above code.)
// 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", "https://www3.postsignum.cz/TSS/TSS_crt/");
let _ = json_tsa.update_bool("timestampToken.requestTsaCert", true);
let _ = gen_.set_tsa(&json_tsa);

// -------------------------------------------------------------------------------------------
// In this case, the TSA requires client certificate authentication.
// To provide your client certificate, the application will instantiate a Chilkat HTTP object,
// then set it up with a SSL/TLS client certificate, and then tell the XmlDSigGen object
// to use the HTTP object for connections to the TSA server.
// -------------------------------------------------------------------------------------------
let http = chilkat::Http::new();
if http.set_ssl_client_cert_pfx("/home/bob/pfxFiles/myClientSideCertWithPrivateKey.pfx", "pfxPassword").is_err() {
    println!("{}", http.last_error_text());
    return;
}

// Tell the XmlDSigGen object to use the above HTTP object for TSA communications.
gen_.set_http_obj(&http);

// 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());