Rust
Rust
Add EncapsulatedTimestamp to Already-Signed XML
See more XML Digital Signatures Examples
Demonstrates how to add an EncapsulatedTimestamp to an existing XML signature.Note: This example requires Chilkat v9.5.0.90 or greater.
Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Note: We cannot load the already-signed XML into a Chilkat XML object because it would re-format the XML when re-emitted.
// (i.e. indentation and whitespace could change, and it would invalidate the existing signature.)
// We must use a StringBuilder.
let sb_xml = chilkat::StringBuilder::new();
if sb_xml.load_file("qa_data/xml_dsig_valid_samples/encapsulatedTimestamp_not_yet_added.xml", "utf-8").is_err() {
println!("Failed to load the XML file.");
return;
}
let dsig = chilkat::XmlDSig::new();
if dsig.load_signature_sb(&sb_xml).is_err() {
println!("{}", dsig.last_error_text());
return;
}
if dsig.has_encapsulated_time_stamp() {
println!("This signed XML already has an EncapsulatedTimeStamp");
return;
}
// Specify the timestamping authority URL
let json = chilkat::JsonObject::new();
let _ = json.update_string("timestampToken.tsaUrl", "http://timestamp.digicert.com");
let _ = json.update_bool("timestampToken.requestTsaCert", true);
// Call AddEncapsulatedTimeStamp to add the EncapsulatedTimeStamp to the signature.
// Note: If the signed XML contains multiple signatures, the signature modified is the one
// indicated by the dsig.Selector property.
let sb_out = chilkat::StringBuilder::new();
if dsig.add_encapsulated_time_stamp(&json, &sb_out).is_err() {
println!("{}", dsig.last_error_text());
return;
}
let _ = sb_out.write_file("qa_output/addedEncapsulatedTimeStamp.xml", "utf-8", false);
// The EncapsulatedTimeStamp can be validated when validating the signature by adding the VerifyEncapsulatedTimeStamp
// keyword to UncommonOptions. See here:
// ----------------------------------------
// Verify the signatures we just produced...
let verifier = chilkat::XmlDSig::new();
if verifier.load_signature_sb(&sb_out).is_err() {
println!("{}", verifier.last_error_text());
return;
}
// Add "VerifyEncapsulatedTimeStamp" to the UncommonOptions to also verify any EncapsulatedTimeStamps
verifier.set_uncommon_options("VerifyEncapsulatedTimeStamp");
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.");