Sample code for 30+ languages & platforms
Rust

Create JPK_VAT XaDES-BES Signed XML

See more XAdES Examples

Demonstrates how to sign XML for JPK_VAT.

Chilkat Rust Downloads

Rust

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

// This example will sign the following XML document:

// <?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">...</EncryptionKey>
//     <DocumentList>
//         <Document>
//             <FormCode schemaVersion="1-1" systemCode="JPK_VAT (3)">JPK_VAT</FormCode>
//             <FileName>JPK_VAT_3_v1-1_20181208.xml</FileName>
//             <ContentLength>8736</ContentLength>
//             <HashValue algorithm="SHA-256" encoding="Base64">JEEI1pItwh6dj/Xe1uts/x61qnjZ4DLHpkRMhmf1oQQ=</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">FFsCRAPYJD3J6cRvd44UDA==</IV>
//                     </AES>
//                 </Encryption>
//                 <FileSignature>
//                     <OrdinalNumber>1</OrdinalNumber>
//                     <FileName>JPK_VAT_3_v1-1_20181208-000.xml.zip.aes</FileName>
//                     <ContentLength>16</ContentLength>
//                     <HashValue algorithm="MD5" encoding="Base64">BX2DTD3ASC/zF6aq/012Cg==</HashValue>
//                 </FileSignature>
//             </FileSignatureList>
//         </Document>
//     </DocumentList>
// </InitUpload>

// First we build the XML to be signed.
// 
// Use this online tool to generate the code from sample XML: 
// Generate Code to Create XML

let _ = true;
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", "...");
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_20181208.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", "JEEI1pItwh6dj/Xe1uts/x61qnjZ4DLHpkRMhmf1oQQ=");
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", "FFsCRAPYJD3J6cRvd44UDA==");
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_20181208-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", "BX2DTD3ASC/zF6aq/012Cg==");

// Also see the online tool to generate the code from sample already-signed XML: 
// Generate XML Signature Creation Code from an Already-Signed XML Sample

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

gen_.set_sig_location("InitUpload");
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.
let object1 = chilkat::Xml::new();
object1.set_tag("xades:QualifyingProperties");
let _ = object1.add_attribute("Target", "#id-1234");
let _ = object1.add_attribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#");
let _ = object1.update_attr_at("xades:SignedProperties", true, "Id", "xades-id-1234");
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 _ = 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 --------
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 --------
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)
// See Load Certificate on Smartcard for an example showing how to load the cert from a smartcard..
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,TransformSignatureXPath,IssuerSerialHex");

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