Rust Requires Chilkat v11.0.0+
Rust
Create JPK VAT metadata XML
See more RSA Examples
Demonstrates how to create the JPK VAT metadata XML (InitUpload) that will be signed using XADES.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First build an InitUpload XML template
// Use this online tool to generate the code from the sample XML below:
// Generate Code to Create XML
// <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">F9EhKFec...uWqAWUIg==</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">JFDI1pItwh6dj/Xe1uts/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">5NX0q1935fvMjLFV7E1yDw==</HashValue>
// </FileSignature>
// </FileSignatureList>
// </Document>
// </DocumentList>
// </InitUpload>
let xml = chilkat::Xml::new();
xml.set_tag("InitUpload");
let _ = xml.add_attribute("xmlns", "http://e-dokumenty.mf.gov.pl");
xml.update_child_content("DocumentType", "JPK");
xml.update_child_content("Version", "01.02.01.20160617");
let _ = xml.update_attr_at("EncryptionKey", true, "algorithm", "RSA");
let _ = xml.update_attr_at("EncryptionKey", true, "encoding", "Base64");
let _ = xml.update_attr_at("EncryptionKey", true, "mode", "ECB");
let _ = xml.update_attr_at("EncryptionKey", true, "padding", "PKCS#1");
xml.update_child_content("EncryptionKey", "TO BE DETERMINED");
let _ = xml.update_attr_at("DocumentList|Document|FormCode", true, "schemaVersion", "1-1");
let _ = xml.update_attr_at("DocumentList|Document|FormCode", true, "systemCode", "JPK_VAT (3)");
xml.update_child_content("DocumentList|Document|FormCode", "JPK_VAT");
xml.update_child_content("DocumentList|Document|FileName", "JPK_VAT_3_v1-1_20181201.xml");
xml.update_child_content("DocumentList|Document|ContentLength", "9999");
let _ = xml.update_attr_at("DocumentList|Document|HashValue", true, "algorithm", "SHA-256");
let _ = xml.update_attr_at("DocumentList|Document|HashValue", true, "encoding", "Base64");
xml.update_child_content("DocumentList|Document|HashValue", "TO BE DETERMINED");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList", true, "filesNumber", "1");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|Packaging|SplitZip", true, "mode", "zip");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|Packaging|SplitZip", true, "type", "split");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES", true, "block", "16");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES", true, "mode", "CBC");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES", true, "padding", "PKCS#7");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES", true, "size", "256");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES|IV", true, "bytes", "16");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|Encryption|AES|IV", true, "encoding", "Base64");
xml.update_child_content("DocumentList|Document|FileSignatureList|Encryption|AES|IV", "TO BE DETERMINED");
xml.update_child_content("DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber", "1");
xml.update_child_content("DocumentList|Document|FileSignatureList|FileSignature|FileName", "JPK_VAT_3_v1-1_20181201-000.xml.zip.aes");
xml.update_child_content("DocumentList|Document|FileSignatureList|FileSignature|ContentLength", "9999");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|FileSignature|HashValue", true, "algorithm", "MD5");
let _ = xml.update_attr_at("DocumentList|Document|FileSignatureList|FileSignature|HashValue", true, "encoding", "Base64");
xml.update_child_content("DocumentList|Document|FileSignatureList|FileSignature|HashValue", "TO BE DETERMINED");
// ------------------------------------------------------------
// Step 1: Load our JPK_VAT XML and update the DocumentList|Document|HashValue
// and DocumentList|Document|ContentLength
let bd_xml = chilkat::BinData::new();
if bd_xml.load_file("qa_data/xml_dsig/jpk_vat/JPK_VAT_3_v1-1_20181201-000.xml").is_err() {
println!("Failed to load XML file.");
return;
}
xml.update_child_content_int("DocumentList|Document|ContentLength", bd_xml.num_bytes());
let crypt = chilkat::Crypt2::new();
crypt.set_hash_algorithm("sha256");
crypt.set_encoding_mode("base64");
xml.update_child_content("DocumentList|Document|HashValue", &crypt.hash_bd_enc(&bd_xml).unwrap_or_default());
// ------------------------------------------------------------
// Step 2: Create a Zip archive containing the XML.
let zip = chilkat::Zip::new();
// The filename we pass here doesn't matter because we won't actually be creating a .zip file.
let _ = zip.new_zip("anything.zip");
let _ = zip.add_bd("JPK_VAT_3_v1-1_20181201-000.xml", &bd_xml);
// Write the .zip file to a BinData object.
let bd_zip = chilkat::BinData::new();
let _ = zip.write_bd(&bd_zip);
// ------------------------------------------------------------
// Step 3: Generate a random 256-bit AES key (32-bytes)
let prng = chilkat::Prng::new();
let bd_aes_key = chilkat::BinData::new();
let _ = prng.gen_random_bd(32, &bd_aes_key);
let iv_bytes = prng.gen_random(16, "base64").unwrap_or_default();
// Store the IV (base64 string) in the XML.
xml.update_child_content("DocumentList|Document|FileSignatureList|Encryption|AES|IV", &iv_bytes);
// ------------------------------------------------------------
// Step 4: AES encrypt our zip archive (the contents of bdZip)
crypt.set_cipher_mode("cbc");
crypt.set_key_length(256);
crypt.set_crypt_algorithm("aes");
crypt.set_padding_scheme(0);
crypt.set_encoded_iv(&iv_bytes, "base64");
crypt.set_encoded_key(&bd_aes_key.get_encoded("base64").unwrap_or_default(), "base64");
// AES by definition has a block size of 16.
let _ = crypt.encrypt_bd(&bd_zip);
// bdZip now contains the AES encrypted data.
// Note: This is NOT the same as a zip where the contents are AES encrypted.
// In that case, we have an unencrypted zip structure with AES encrypted files within.
// In our case, the entire zip file image is encrypted.
// Save the bdZip to a file. This is what will get sent to e-dokumenty.mf.gov.pl
let _ = bd_zip.write_file("qa_output/JPK_VAT_3_v1-1_20181201-000.xml.zip.aes").is_ok();
xml.update_child_content_int("DocumentList|Document|FileSignatureList|FileSignature|ContentLength", bd_zip.num_bytes());
// ------------------------------------------------------------
// Step 4: RSA Encrypt the AES key using the public key certificate provided by the Ministry of Finance
let cert = chilkat::Cert::new();
if cert.load_from_file("qa_data/pem/mf_public_rsa.pem").is_err() {
println!("{}", cert.last_error_text());
return;
}
let pub_key = chilkat::PublicKey::new();
let _ = cert.get_public_key(&pub_key);
let rsa = chilkat::Rsa::new();
let _ = rsa.use_public_key(&pub_key);
rsa.set_encoding_mode("base64");
rsa.set_little_endian(false);
// in-place RSA encrypt the contents of bdAesKey.
let _ = rsa.encrypt_bd(&bd_aes_key, false);
xml.update_child_content("EncryptionKey", &bd_aes_key.get_encoded("base64").unwrap_or_default());
// Step 5: We forgot to get the MD5 hash of the AES encrypted zip.
// (I'm assuming we need the MD5 of the encrypted zip as opposed to the MD5 of the pre-encrypted zip..)
crypt.set_hash_algorithm("md5");
xml.update_child_content("DocumentList|Document|FileSignatureList|FileSignature|HashValue", &crypt.hash_bd_enc(&bd_zip).unwrap_or_default());
// At this point, the XML is prepared and the AES encrypted image of the zip file is written
// to a file (and also in bdZip).
let final_xml = xml.get_xml().unwrap_or_default();
println!("{}", final_xml);
let _ = xml.save_xml("qa_output/jpk_vat.xml");
println!("Finished.");