Rust
Rust
Create XAdES using Smart Card or USB Token
See more XAdES Examples
Demonstrates how to create an XAdES signed XML document using a certificate located on a smartcard or USB token.Chilkat Rust Downloads
// Load the XML to be signed.
let xml_to_sign = chilkat::Xml::new();
if xml_to_sign.load_xml_file("qa_data/fattura_electronica/docToSign.xml").is_err() {
println!("{}", xml_to_sign.last_error_text());
return;
}
let gen_ = chilkat::XmlDSigGen::new();
gen_.set_sig_location("p:FatturaElettronica");
gen_.set_sig_id("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504");
gen_.set_sig_namespace_prefix("ds");
gen_.set_sig_namespace_uri("http://www.w3.org/2000/09/xmldsig#");
gen_.set_sig_value_id("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-sigvalue");
gen_.set_signed_info_canon_alg("C14N");
gen_.set_signed_info_digest_method("sha256");
// Create an Object to be added to the Signature.
// Note: Chilkat will automatically populate the strings indicated by "TO BE GENERATED BY CHILKAT" with actual/correct values
// when the XML is signed.
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("xmlns:xades141", "http://uri.etsi.org/01903/v1.4.1#");
let _ = object1.add_attribute("Target", "#xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504");
let _ = object1.update_attr_at("xades:SignedProperties", true, "Id", "xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops");
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");
let _ = gen_.add_object("", &object1.get_xml().unwrap_or_default(), "", "");
// -------- Reference 1 --------
gen_.set_key_info_id("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo");
let _ = gen_.add_same_doc_ref("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo", "sha256", "", "", "");
// -------- Reference 2 --------
let _ = gen_.add_same_doc_ref("", "sha256", "", "", "");
let _ = gen_.set_ref_id_attr("", "xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-ref0");
// -------- Reference 3 --------
let _ = gen_.add_object_ref("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops", "sha256", "", "", "http://uri.etsi.org/01903#SignedProperties");
// ----------------------------------------------------------------
// Load a certificate that has been pre-installed on the Windows system
// This includes certificates on smartcards and USB tokens
let cert = chilkat::Cert::new();
// You may provide the PIN here..
cert.set_smart_card_pin("000000");
// Load the certificate on the smartcard currently in the reader (or on the USB token).
// Pass an empty string to allow Chilkat to automatically choose the CSP (Cryptographi Service Provider).
// See Load Certificate on Smartcard for information about explicitly selecting a particular CSP.
if cert.load_from_smartcard("").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");
// Load XML to be signed...
let sb_xml = chilkat::StringBuilder::new();
let _ = xml_to_sign.get_xml_sb(&sb_xml);
gen_.set_behaviors("IndentedSignature,ForceAddEnvelopedSignatureTransform");
// 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("qa_output/signedXml.xml", "utf-8", false).is_ok();
println!("{}", sb_xml.get_as_string().unwrap_or_default());
// ----------------------------------------
// Verify the signature we just produced...
let verifier = chilkat::XmlDSig::new();
if verifier.load_signature_sb(&sb_xml).is_err() {
println!("{}", verifier.last_error_text());
return;
}
if verifier.verify_signature(true).is_err() {
println!("{}", verifier.last_error_text());
return;
}
println!("This signature was successfully verified.");