Swift Requires Chilkat v11.0.0+
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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 = CkoXml()!
xml.tag = "InitUpload"
xml.addAttribute(name: "xmlns", value: "http://e-dokumenty.mf.gov.pl")
xml.updateChildContent(tagPath: "DocumentType", value: "JPK")
xml.updateChildContent(tagPath: "Version", value: "01.02.01.20160617")
xml.updateAttrAt(tagPath: "EncryptionKey", autoCreate: true, attrName: "algorithm", attrValue: "RSA")
xml.updateAttrAt(tagPath: "EncryptionKey", autoCreate: true, attrName: "encoding", attrValue: "Base64")
xml.updateAttrAt(tagPath: "EncryptionKey", autoCreate: true, attrName: "mode", attrValue: "ECB")
xml.updateAttrAt(tagPath: "EncryptionKey", autoCreate: true, attrName: "padding", attrValue: "PKCS#1")
xml.updateChildContent(tagPath: "EncryptionKey", value: "TO BE DETERMINED")
xml.updateAttrAt(tagPath: "DocumentList|Document|FormCode", autoCreate: true, attrName: "schemaVersion", attrValue: "1-1")
xml.updateAttrAt(tagPath: "DocumentList|Document|FormCode", autoCreate: true, attrName: "systemCode", attrValue: "JPK_VAT (3)")
xml.updateChildContent(tagPath: "DocumentList|Document|FormCode", value: "JPK_VAT")
xml.updateChildContent(tagPath: "DocumentList|Document|FileName", value: "JPK_VAT_3_v1-1_20181201.xml")
xml.updateChildContent(tagPath: "DocumentList|Document|ContentLength", value: "9999")
xml.updateAttrAt(tagPath: "DocumentList|Document|HashValue", autoCreate: true, attrName: "algorithm", attrValue: "SHA-256")
xml.updateAttrAt(tagPath: "DocumentList|Document|HashValue", autoCreate: true, attrName: "encoding", attrValue: "Base64")
xml.updateChildContent(tagPath: "DocumentList|Document|HashValue", value: "TO BE DETERMINED")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList", autoCreate: true, attrName: "filesNumber", attrValue: "1")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|Packaging|SplitZip", autoCreate: true, attrName: "mode", attrValue: "zip")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|Packaging|SplitZip", autoCreate: true, attrName: "type", attrValue: "split")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|Encryption|AES", autoCreate: true, attrName: "block", attrValue: "16")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|Encryption|AES", autoCreate: true, attrName: "mode", attrValue: "CBC")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|Encryption|AES", autoCreate: true, attrName: "padding", attrValue: "PKCS#7")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|Encryption|AES", autoCreate: true, attrName: "size", attrValue: "256")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|Encryption|AES|IV", autoCreate: true, attrName: "bytes", attrValue: "16")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|Encryption|AES|IV", autoCreate: true, attrName: "encoding", attrValue: "Base64")
xml.updateChildContent(tagPath: "DocumentList|Document|FileSignatureList|Encryption|AES|IV", value: "TO BE DETERMINED")
xml.updateChildContent(tagPath: "DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber", value: "1")
xml.updateChildContent(tagPath: "DocumentList|Document|FileSignatureList|FileSignature|FileName", value: "JPK_VAT_3_v1-1_20181201-000.xml.zip.aes")
xml.updateChildContent(tagPath: "DocumentList|Document|FileSignatureList|FileSignature|ContentLength", value: "9999")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|FileSignature|HashValue", autoCreate: true, attrName: "algorithm", attrValue: "MD5")
xml.updateAttrAt(tagPath: "DocumentList|Document|FileSignatureList|FileSignature|HashValue", autoCreate: true, attrName: "encoding", attrValue: "Base64")
xml.updateChildContent(tagPath: "DocumentList|Document|FileSignatureList|FileSignature|HashValue", value: "TO BE DETERMINED")
// ------------------------------------------------------------
// Step 1: Load our JPK_VAT XML and update the DocumentList|Document|HashValue
// and DocumentList|Document|ContentLength
let bdXml = CkoBinData()!
success = bdXml.loadFile(path: "qa_data/xml_dsig/jpk_vat/JPK_VAT_3_v1-1_20181201-000.xml")
if success != true {
print("Failed to load XML file.")
return
}
xml.updateChildContentInt(tagPath: "DocumentList|Document|ContentLength", value: bdXml.numBytes.intValue)
let crypt = CkoCrypt2()!
crypt.hashAlgorithm = "sha256"
crypt.encodingMode = "base64"
xml.updateChildContent(tagPath: "DocumentList|Document|HashValue", value: crypt.hashBdENC(bd: bdXml))
// ------------------------------------------------------------
// Step 2: Create a Zip archive containing the XML.
let zip = CkoZip()!
// The filename we pass here doesn't matter because we won't actually be creating a .zip file.
zip.newZip(zipPath: "anything.zip")
zip.addBd(pathInZip: "JPK_VAT_3_v1-1_20181201-000.xml", bd: bdXml)
// Write the .zip file to a BinData object.
let bdZip = CkoBinData()!
zip.writeBd(binData: bdZip)
// ------------------------------------------------------------
// Step 3: Generate a random 256-bit AES key (32-bytes)
let prng = CkoPrng()!
let bdAesKey = CkoBinData()!
prng.genRandomBd(numBytes: 32, bd: bdAesKey)
var ivBytes: String? = prng.genRandom(numBytes: 16, encoding: "base64")
// Store the IV (base64 string) in the XML.
xml.updateChildContent(tagPath: "DocumentList|Document|FileSignatureList|Encryption|AES|IV", value: ivBytes)
// ------------------------------------------------------------
// Step 4: AES encrypt our zip archive (the contents of bdZip)
crypt.cipherMode = "cbc"
crypt.keyLength = 256
crypt.cryptAlgorithm = "aes"
crypt.paddingScheme = 0
crypt.setEncodedIV(ivStr: ivBytes, encoding: "base64")
crypt.setEncodedKey(keyStr: bdAesKey.getEncoded(encoding: "base64"), encoding: "base64")
// AES by definition has a block size of 16.
crypt.encryptBd(bd: 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(path: "qa_output/JPK_VAT_3_v1-1_20181201-000.xml.zip.aes")
xml.updateChildContentInt(tagPath: "DocumentList|Document|FileSignatureList|FileSignature|ContentLength", value: bdZip.numBytes.intValue)
// ------------------------------------------------------------
// Step 4: RSA Encrypt the AES key using the public key certificate provided by the Ministry of Finance
let cert = CkoCert()!
success = cert.load(fromFile: "qa_data/pem/mf_public_rsa.pem")
if success == false {
print("\(cert.lastErrorText!)")
return
}
let pubKey = CkoPublicKey()!
cert.getPublicKey(pubKey: pubKey)
let rsa = CkoRsa()!
rsa.usePublicKey(pubKey: pubKey)
rsa.encodingMode = "base64"
rsa.littleEndian = false
// in-place RSA encrypt the contents of bdAesKey.
rsa.encryptBd(bd: bdAesKey, usePrivateKey: false)
xml.updateChildContent(tagPath: "EncryptionKey", value: bdAesKey.getEncoded(encoding: "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.hashAlgorithm = "md5"
xml.updateChildContent(tagPath: "DocumentList|Document|FileSignatureList|FileSignature|HashValue", value: crypt.hashBdENC(bd: 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).
var finalXml: String? = xml.getXml()
print("\(finalXml!)")
xml.save(path: "qa_output/jpk_vat.xml")
print("Finished.")
}