Rust
Rust
XML-DSig Add Object Reference with Transforms Specified Explicitly
Demonstrates how to use the new AddObjectRef2 method to explicitly specify the XML Transforms fragment.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let _ = true;
// Build the following XML to be signed:
// <?xml version="1.0" encoding="utf-8"?>
// <InitUpload xmlns="http://e-dokumenty.mf.gov.pl">
// <DocumentType>JPK</DocumentType>
// <Version>01.02.01.20160617</Version>
// <EncryptionKey algorithm="RSA" encoding="Base64" mode="ECB" padding="PKCS#1">xxxx</EncryptionKey>
// <DocumentList>
// <Document>
// <FormCode schemaVersion="1-1" systemCode="JPK_VAT (3)">JPK_VAT</FormCode>
// <FileName>JPK_VAT_3_v1-1_20181201.xml</FileName>
// <ContentLength>8736</ContentLength>
// <HashValue algorithm="SHA-256" encoding="Base64">JEDI1pItwh6dj/Xx1uts/x61qnjZ4DLHpkZMhmf1oKQ=</HashValue>
// <FileSignatureList filesNumber="1">
// <Packaging>
// <SplitZip mode="zip" type="split"/>
// </Packaging>
// <Encryption>
// <AES block="16" mode="CBC" padding="PKCS#7" size="256">
// <IV bytes="16" encoding="Base64">z64oN9zXHt1+S3XACRSCYw==</IV>
// </AES>
// </Encryption>
// <FileSignature>
// <OrdinalNumber>1</OrdinalNumber>
// <FileName>JPK_VAT_3_v1-1_20181201-000.xml.zip.aes</FileName>
// <ContentLength>16</ContentLength>
// <HashValue algorithm="MD5" encoding="Base64">5MX0q1935fvMjLFV7E1yDw==</HashValue>
// </FileSignature>
// </FileSignatureList>
// </Document>
// </DocumentList>
// </InitUpload>
// Use this online tool to generate code from sample XML:
// Generate Code to Create XML
let xml_to_sign = chilkat::Xml::new();
xml_to_sign.set_tag("InitUpload");
let _ = xml_to_sign.add_attribute("xmlns", "http://e-dokumenty.mf.gov.pl");
xml_to_sign.update_child_content("DocumentType", "JPK");
xml_to_sign.update_child_content("Version", "01.02.01.20160617");
let _ = xml_to_sign.update_attr_at("EncryptionKey", true, "algorithm", "RSA");
let _ = xml_to_sign.update_attr_at("EncryptionKey", true, "encoding", "Base64");
let _ = xml_to_sign.update_attr_at("EncryptionKey", true, "mode", "ECB");
let _ = xml_to_sign.update_attr_at("EncryptionKey", true, "padding", "PKCS#1");
xml_to_sign.update_child_content("EncryptionKey", "xxxx");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FormCode", true, "schemaVersion", "1-1");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FormCode", true, "systemCode", "JPK_VAT (3)");
xml_to_sign.update_child_content("DocumentList|Document|FormCode", "JPK_VAT");
xml_to_sign.update_child_content("DocumentList|Document|FileName", "JPK_VAT_3_v1-1_20181201.xml");
xml_to_sign.update_child_content("DocumentList|Document|ContentLength", "8736");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|HashValue", true, "algorithm", "SHA-256");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|HashValue", true, "encoding", "Base64");
xml_to_sign.update_child_content("DocumentList|Document|HashValue", "JEDI1pItwh6dj/Xx1uts/x61qnjZ4DLHpkZMhmf1oKQ=");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList", true, "filesNumber", "1");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|Packaging|SplitZip", true, "mode", "zip");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|Packaging|SplitZip", true, "type", "split");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES", true, "block", "16");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES", true, "mode", "CBC");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES", true, "padding", "PKCS#7");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES", true, "size", "256");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES|IV", true, "bytes", "16");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES|IV", true, "encoding", "Base64");
xml_to_sign.update_child_content("DocumentList|Document|FileSignatureList|Encryption|AES|IV", "z64oN9zXHt1+S3XACRSCYw==");
xml_to_sign.update_child_content("DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber", "1");
xml_to_sign.update_child_content("DocumentList|Document|FileSignatureList|FileSignature|FileName", "JPK_VAT_3_v1-1_20181201-000.xml.zip.aes");
xml_to_sign.update_child_content("DocumentList|Document|FileSignatureList|FileSignature|ContentLength", "16");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|FileSignature|HashValue", true, "algorithm", "MD5");
let _ = xml_to_sign.update_attr_at("DocumentList|Document|FileSignatureList|FileSignature|HashValue", true, "encoding", "Base64");
xml_to_sign.update_child_content("DocumentList|Document|FileSignatureList|FileSignature|HashValue", "5MX0q1935fvMjLFV7E1yDw==");
let gen_ = chilkat::XmlDSigGen::new();
gen_.set_sig_location("InitUpload");
gen_.set_sig_location_mod(0);
gen_.set_sig_id("id-1234");
gen_.set_sig_namespace_prefix("ds");
gen_.set_sig_namespace_uri("http://www.w3.org/2000/09/xmldsig#");
gen_.set_signed_info_canon_alg("EXCL_C14N");
gen_.set_signed_info_digest_method("sha256");
// Create an Object to be added to the Signature.
// <xades:QualifyingProperties Target="#id-1234" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#">
// <xades:SignedProperties Id="xades-id-1234">
// <xades:SignedSignatureProperties>
// <xades:SigningTime>TO BE GENERATED BY CHILKAT</xades:SigningTime>
// <xades:SigningCertificate>
// <xades:Cert>
// <xades:CertDigest>
// <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
// <ds:DigestValue>TO BE GENERATED BY CHILKAT</ds:DigestValue>
// </xades:CertDigest>
// <xades:IssuerSerial>
// <ds:X509IssuerName>TO BE GENERATED BY CHILKAT</ds:X509IssuerName>
// <ds:X509SerialNumber>TO BE GENERATED BY CHILKAT</ds:X509SerialNumber>
// </xades:IssuerSerial>
// </xades:Cert>
// </xades:SigningCertificate>
// </xades:SignedSignatureProperties>
// <xades:SignedDataObjectProperties>
// <xades:DataObjectFormat ObjectReference="#r-id-1">
// <xades:MimeType>text/xml</xades:MimeType>
// </xades:DataObjectFormat>
// </xades:SignedDataObjectProperties>
// </xades:SignedProperties>
// </xades:QualifyingProperties>
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", "#id-1234");
let _ = object1.update_attr_at("xades:SignedProperties", true, "Id", "xades-id-1234");
// Note: It may be that http://www.w3.org/2001/04/xmlenc#sha256 is needed in the following line instead of http://www.w3.org/2000/09/xmldsig#sha1
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/2000/09/xmldsig#sha1");
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 _ = object1.update_attr_at("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat", true, "ObjectReference", "#r-id-1");
object1.update_child_content("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType", "text/xml");
let _ = gen_.add_object("", &object1.get_xml().unwrap_or_default(), "", "");
// -------- Reference 1 --------
// Build the following Transforms fragment:
// <ds:Transforms>
// <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
// <ds:XPath>not(ancestor-or-self::ds:Signature)</ds:XPath>
// </ds:Transform>
// <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
// </ds:Transforms>
let xml1 = chilkat::Xml::new();
xml1.set_tag("ds:Transforms");
let _ = xml1.update_attr_at("ds:Transform", true, "Algorithm", "http://www.w3.org/TR/1999/REC-xpath-19991116");
xml1.update_child_content("ds:Transform|ds:XPath", "not(ancestor-or-self::ds:Signature)");
let _ = xml1.update_attr_at("ds:Transform[1]", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#");
let _ = gen_.add_same_doc_ref2("", "sha256", &xml1, "");
let _ = gen_.set_ref_id_attr("", "r-id-1");
// -------- Reference 2 --------
// Build the following Transforms fragment:
// <ds:Transforms>
// <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
// </ds:Transforms>
let xml2 = chilkat::Xml::new();
xml2.set_tag("ds:Transforms");
let _ = xml2.update_attr_at("ds:Transform", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#");
let _ = gen_.add_object_ref2("xades-id-1234", "sha256", &xml2, "http://uri.etsi.org/01903#SignedProperties");
// 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");
// Load XML to be signed...
let sb_xml = chilkat::StringBuilder::new();
let _ = xml_to_sign.get_xml_sb(&sb_xml);
gen_.set_behaviors("IndentedSignature");
// 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 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.");