Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C++) Create JPK VAT metadata XMLDemonstrates how to create the JPK VAT metadata XML (InitUpload) that will be signed using XADES.
#include <CkXmlW.h> #include <CkBinDataW.h> #include <CkCrypt2W.h> #include <CkZipW.h> #include <CkZipEntryW.h> #include <CkPrngW.h> #include <CkCertW.h> #include <CkPublicKeyW.h> #include <CkRsaW.h> void ChilkatSample(void) { // 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> bool success; CkXmlW xml; xml.put_Tag(L"InitUpload"); xml.AddAttribute(L"xmlns",L"http://e-dokumenty.mf.gov.pl"); xml.UpdateChildContent(L"DocumentType",L"JPK"); xml.UpdateChildContent(L"Version",L"01.02.01.20160617"); xml.UpdateAttrAt(L"EncryptionKey",true,L"algorithm",L"RSA"); xml.UpdateAttrAt(L"EncryptionKey",true,L"encoding",L"Base64"); xml.UpdateAttrAt(L"EncryptionKey",true,L"mode",L"ECB"); xml.UpdateAttrAt(L"EncryptionKey",true,L"padding",L"PKCS#1"); xml.UpdateChildContent(L"EncryptionKey",L"TO BE DETERMINED"); xml.UpdateAttrAt(L"DocumentList|Document|FormCode",true,L"schemaVersion",L"1-1"); xml.UpdateAttrAt(L"DocumentList|Document|FormCode",true,L"systemCode",L"JPK_VAT (3)"); xml.UpdateChildContent(L"DocumentList|Document|FormCode",L"JPK_VAT"); xml.UpdateChildContent(L"DocumentList|Document|FileName",L"JPK_VAT_3_v1-1_20181201.xml"); xml.UpdateChildContent(L"DocumentList|Document|ContentLength",L"9999"); xml.UpdateAttrAt(L"DocumentList|Document|HashValue",true,L"algorithm",L"SHA-256"); xml.UpdateAttrAt(L"DocumentList|Document|HashValue",true,L"encoding",L"Base64"); xml.UpdateChildContent(L"DocumentList|Document|HashValue",L"TO BE DETERMINED"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList",true,L"filesNumber",L"1"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",true,L"mode",L"zip"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",true,L"type",L"split"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"block",L"16"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"mode",L"CBC"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"padding",L"PKCS#7"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"size",L"256"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",true,L"bytes",L"16"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",true,L"encoding",L"Base64"); xml.UpdateChildContent(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",L"TO BE DETERMINED"); xml.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber",L"1"); xml.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|FileName",L"JPK_VAT_3_v1-1_20181201-000.xml.zip.aes"); xml.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|ContentLength",L"9999"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",true,L"algorithm",L"MD5"); xml.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",true,L"encoding",L"Base64"); xml.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",L"TO BE DETERMINED"); // ------------------------------------------------------------ // Step 1: Load our JPK_VAT XML and update the DocumentList|Document|HashValue // and DocumentList|Document|ContentLength CkBinDataW bdXml; success = bdXml.LoadFile(L"qa_data/xml_dsig/jpk_vat/JPK_VAT_3_v1-1_20181201-000.xml"); if (success != true) { wprintf(L"Failed to load XML file.\n"); return; } xml.UpdateChildContentInt(L"DocumentList|Document|ContentLength",bdXml.get_NumBytes()); CkCrypt2W crypt; crypt.put_HashAlgorithm(L"sha256"); crypt.put_EncodingMode(L"base64"); xml.UpdateChildContent(L"DocumentList|Document|HashValue",crypt.hashBdENC(bdXml)); // ------------------------------------------------------------ // Step 2: Create a Zip archive containing the XML. CkZipW zip; // The filename we pass here doesn't matter because we won't actually be creating a .zip file. zip.NewZip(L"anything.zip"); CkZipEntryW *e = zip.AppendBd(L"JPK_VAT_3_v1-1_20181201-000.xml",bdXml); delete e; // Write the .zip file to a BinData object. CkBinDataW bdZip; zip.WriteBd(bdZip); // ------------------------------------------------------------ // Step 3: Generate a random 256-bit AES key (32-bytes) CkPrngW prng; CkBinDataW bdAesKey; prng.GenRandomBd(32,bdAesKey); const wchar_t *ivBytes = prng.genRandom(16,L"base64"); // Store the IV (base64 string) in the XML. xml.UpdateChildContent(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",ivBytes); // ------------------------------------------------------------ // Step 4: AES encrypt our zip archive (the contents of bdZip) crypt.put_CipherMode(L"cbc"); crypt.put_KeyLength(256); crypt.put_CryptAlgorithm(L"aes"); crypt.put_PaddingScheme(0); crypt.SetEncodedIV(ivBytes,L"base64"); crypt.SetEncodedKey(bdAesKey.getEncoded(L"base64"),L"base64"); // AES by definition has a block size of 16. crypt.EncryptBd(bdZip); // 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 success = bdZip.WriteFile(L"qa_output/JPK_VAT_3_v1-1_20181201-000.xml.zip.aes"); xml.UpdateChildContentInt(L"DocumentList|Document|FileSignatureList|FileSignature|ContentLength",bdZip.get_NumBytes()); // ------------------------------------------------------------ // Step 4: RSA Encrypt the AES key using the public key certificate provided by the Ministry of Finance CkCertW cert; success = cert.LoadFromFile(L"qa_data/pem/mf_public_rsa.pem"); if (success != true) { wprintf(L"%s\n",cert.lastErrorText()); return; } CkPublicKeyW *pubKey = cert.ExportPublicKey(); CkRsaW rsa; rsa.ImportPublicKeyObj(*pubKey); delete pubKey; rsa.put_EncodingMode(L"base64"); rsa.put_LittleEndian(false); // in-place RSA encrypt the contents of bdAesKey. rsa.EncryptBd(bdAesKey,false); xml.UpdateChildContent(L"EncryptionKey",bdAesKey.getEncoded(L"base64")); // 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.put_HashAlgorithm(L"md5"); xml.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",crypt.hashBdENC(bdZip)); // 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). const wchar_t *finalXml = xml.getXml(); wprintf(L"%s\n",finalXml); xml.SaveXml(L"qa_output/jpk_vat.xml"); wprintf(L"Finished.\n"); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.