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
(PureBasic) Create JPK VAT metadata XMLDemonstrates how to create the JPK VAT metadata XML (InitUpload) that will be signed using XADES.
IncludeFile "CkPublicKey.pb" IncludeFile "CkBinData.pb" IncludeFile "CkCert.pb" IncludeFile "CkPrng.pb" IncludeFile "CkXml.pb" IncludeFile "CkCrypt2.pb" IncludeFile "CkZip.pb" IncludeFile "CkRsa.pb" IncludeFile "CkZipEntry.pb" Procedure ChilkatExample() ; 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> success.i xml.i = CkXml::ckCreate() If xml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xml, "InitUpload") CkXml::ckAddAttribute(xml,"xmlns","http://e-dokumenty.mf.gov.pl") CkXml::ckUpdateChildContent(xml,"DocumentType","JPK") CkXml::ckUpdateChildContent(xml,"Version","01.02.01.20160617") CkXml::ckUpdateAttrAt(xml,"EncryptionKey",1,"algorithm","RSA") CkXml::ckUpdateAttrAt(xml,"EncryptionKey",1,"encoding","Base64") CkXml::ckUpdateAttrAt(xml,"EncryptionKey",1,"mode","ECB") CkXml::ckUpdateAttrAt(xml,"EncryptionKey",1,"padding","PKCS#1") CkXml::ckUpdateChildContent(xml,"EncryptionKey","TO BE DETERMINED") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FormCode",1,"schemaVersion","1-1") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FormCode",1,"systemCode","JPK_VAT (3)") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|FormCode","JPK_VAT") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|FileName","JPK_VAT_3_v1-1_20181201.xml") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|ContentLength","9999") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|HashValue",1,"algorithm","SHA-256") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|HashValue",1,"encoding","Base64") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|HashValue","TO BE DETERMINED") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList",1,"filesNumber","1") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|Packaging|SplitZip",1,"mode","zip") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|Packaging|SplitZip",1,"type","split") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|Encryption|AES",1,"block","16") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|Encryption|AES",1,"mode","CBC") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|Encryption|AES",1,"padding","PKCS#7") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|Encryption|AES",1,"size","256") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|Encryption|AES|IV",1,"bytes","16") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|Encryption|AES|IV",1,"encoding","Base64") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|FileSignatureList|Encryption|AES|IV","TO BE DETERMINED") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber","1") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|FileSignatureList|FileSignature|FileName","JPK_VAT_3_v1-1_20181201-000.xml.zip.aes") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|FileSignatureList|FileSignature|ContentLength","9999") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|FileSignature|HashValue",1,"algorithm","MD5") CkXml::ckUpdateAttrAt(xml,"DocumentList|Document|FileSignatureList|FileSignature|HashValue",1,"encoding","Base64") CkXml::ckUpdateChildContent(xml,"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 bdXml.i = CkBinData::ckCreate() If bdXml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkBinData::ckLoadFile(bdXml,"qa_data/xml_dsig/jpk_vat/JPK_VAT_3_v1-1_20181201-000.xml") If success <> 1 Debug "Failed to load XML file." CkXml::ckDispose(xml) CkBinData::ckDispose(bdXml) ProcedureReturn EndIf CkXml::ckUpdateChildContentInt(xml,"DocumentList|Document|ContentLength",CkBinData::ckNumBytes(bdXml)) crypt.i = CkCrypt2::ckCreate() If crypt.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkCrypt2::setCkHashAlgorithm(crypt, "sha256") CkCrypt2::setCkEncodingMode(crypt, "base64") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|HashValue",CkCrypt2::ckHashBdENC(crypt,bdXml)) ; ------------------------------------------------------------ ; Step 2: Create a Zip archive containing the XML. zip.i = CkZip::ckCreate() If zip.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; The filename we pass here doesn't matter because we won't actually be creating a .zip file. CkZip::ckNewZip(zip,"anything.zip") e.i = CkZip::ckAppendBd(zip,"JPK_VAT_3_v1-1_20181201-000.xml",bdXml) CkZipEntry::ckDispose(e) ; Write the .zip file to a BinData object. bdZip.i = CkBinData::ckCreate() If bdZip.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkZip::ckWriteBd(zip,bdZip) ; ------------------------------------------------------------ ; Step 3: Generate a random 256-bit AES key (32-bytes) prng.i = CkPrng::ckCreate() If prng.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bdAesKey.i = CkBinData::ckCreate() If bdAesKey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkPrng::ckGenRandomBd(prng,32,bdAesKey) ivBytes.s = CkPrng::ckGenRandom(prng,16,"base64") ; Store the IV (base64 string) in the XML. CkXml::ckUpdateChildContent(xml,"DocumentList|Document|FileSignatureList|Encryption|AES|IV",ivBytes) ; ------------------------------------------------------------ ; Step 4: AES encrypt our zip archive (the contents of bdZip) CkCrypt2::setCkCipherMode(crypt, "cbc") CkCrypt2::setCkKeyLength(crypt, 256) CkCrypt2::setCkCryptAlgorithm(crypt, "aes") CkCrypt2::setCkPaddingScheme(crypt, 0) CkCrypt2::ckSetEncodedIV(crypt,ivBytes,"base64") CkCrypt2::ckSetEncodedKey(crypt,CkBinData::ckGetEncoded(bdAesKey,"base64"),"base64") ; AES by definition has a block size of 16. CkCrypt2::ckEncryptBd(crypt,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 = CkBinData::ckWriteFile(bdZip,"qa_output/JPK_VAT_3_v1-1_20181201-000.xml.zip.aes") CkXml::ckUpdateChildContentInt(xml,"DocumentList|Document|FileSignatureList|FileSignature|ContentLength",CkBinData::ckNumBytes(bdZip)) ; ------------------------------------------------------------ ; Step 4: RSA Encrypt the AES key using the public key certificate provided by the Ministry of Finance cert.i = CkCert::ckCreate() If cert.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkCert::ckLoadFromFile(cert,"qa_data/pem/mf_public_rsa.pem") If success <> 1 Debug CkCert::ckLastErrorText(cert) CkXml::ckDispose(xml) CkBinData::ckDispose(bdXml) CkCrypt2::ckDispose(crypt) CkZip::ckDispose(zip) CkBinData::ckDispose(bdZip) CkPrng::ckDispose(prng) CkBinData::ckDispose(bdAesKey) CkCert::ckDispose(cert) ProcedureReturn EndIf pubKey.i = CkCert::ckExportPublicKey(cert) rsa.i = CkRsa::ckCreate() If rsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkRsa::ckImportPublicKeyObj(rsa,pubKey) CkPublicKey::ckDispose(pubKey) CkRsa::setCkEncodingMode(rsa, "base64") CkRsa::setCkLittleEndian(rsa, 0) ; in-place RSA encrypt the contents of bdAesKey. CkRsa::ckEncryptBd(rsa,bdAesKey,0) CkXml::ckUpdateChildContent(xml,"EncryptionKey",CkBinData::ckGetEncoded(bdAesKey,"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..) CkCrypt2::setCkHashAlgorithm(crypt, "md5") CkXml::ckUpdateChildContent(xml,"DocumentList|Document|FileSignatureList|FileSignature|HashValue",CkCrypt2::ckHashBdENC(crypt,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). finalXml.s = CkXml::ckGetXml(xml) Debug finalXml CkXml::ckSaveXml(xml,"qa_output/jpk_vat.xml") Debug "Finished." CkXml::ckDispose(xml) CkBinData::ckDispose(bdXml) CkCrypt2::ckDispose(crypt) CkZip::ckDispose(zip) CkBinData::ckDispose(bdZip) CkPrng::ckDispose(prng) CkBinData::ckDispose(bdAesKey) CkCert::ckDispose(cert) CkRsa::ckDispose(rsa) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.