Zig Requires Chilkat v11.0.0+
Zig
Signed Zip as Base64 with XAdES-BES
See more XAdES Examples
This example is to help companies implement a solution for sending XAdES-BES to the Polish government for reporting purposes. Specifically:Przed podpisaniem deklaracja zbiorcza (PIT-11Z, PIT-8CZ, PIT-40Z, PIT-RZ) musi zostać
umieszczona w archiwum ZIP. W tym przypadku, podpisywany jest plik archiwum ZIP,
przyjmujący w podpisie XAdES-BES formę zakodowaną base64.
The example demonstrates the following:
- Zip an XML file (PIT-11Z.xml is zipped to PIT-11Z.zip)
- Create XML to be signed, where the XML contains the base64 encoded content of the PIT-11Z.zip archive.
- XAdES-BES sign the XML containing the base64 zip.
This example will also show the reverse:
- Verify the signed XML.
- Extract the PIT-11z.zip from the signed XML.
- Unzip the PIT-11Z.zip to get the original PIT-11Z.xml
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Zip the PIT-11Z.xml to create PIT-11Z.zip (not as a .zip file, but in-memory).
const sb_xml_to_zip = try chilkat.StringBuilder.init();
defer sb_xml_to_zip.deinit();
sb_xml_to_zip.loadFile("qa_data/xml/PIT-11Z.xml", "utf-8") catch {
std.debug.print("Failed to load the XML to be zipped.\n", .{});
return;
};
const zip = try chilkat.Zip.init();
defer zip.deinit();
// Initialize the zip object. No file is created in this call.
// It should always return true.
try zip.newZip("PIT-11Z.zip");
// Add the XML to be zipped.
zip.addSb("PIT-11Z.xml", sb_xml_to_zip, "utf-8") catch {};
// Write the zip to a BinData object.
const bd_zip = try chilkat.BinData.init();
defer bd_zip.deinit();
zip.writeBd(bd_zip) catch {};
// The contents of the bdZip will be retrieved in base64 format when needed below..
const gen = try chilkat.XmlDSigGen.init();
defer gen.deinit();
gen.setSigLocation("");
gen.setSigLocationMod(0);
gen.setSigId("Signature_2a8df7f8-b958-40cc-83f6-edb53b837347_19");
gen.setSigNamespacePrefix("ds");
gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
gen.setSigValueId("SignatureValue_2a8df7f8-b958-40cc-83f6-edb53b837347_52");
gen.setSignedInfoId("SignedInfo_2a8df7f8-b958-40cc-83f6-edb53b837347_41");
gen.setSignedInfoCanonAlg("C14N");
gen.setSignedInfoDigestMethod("sha1");
// Set the KeyInfoId before adding references..
gen.setKeyInfoId("KeyInfo_2a8df7f8-b958-40cc-83f6-edb53b837347_24");
// Create an Object to be added to the Signature.
const object1 = try chilkat.Xml.init();
defer object1.deinit();
object1.setTag("xades:QualifyingProperties");
object1.addAttribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#") catch {};
object1.addAttribute("Id", "QualifyingProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_43") catch {};
object1.addAttribute("Target", "#Signature_2a8df7f8-b958-40cc-83f6-edb53b837347_19") catch {};
object1.updateAttrAt("xades:SignedProperties", true, "Id", "SignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4e") catch {};
object1.updateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties", true, "Id", "SignedSignatureProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_0a") catch {};
// Chilkat will replace the strings "TO BE GENERATED BY CHILKAT" with actual values when the signature is created.
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", "TO BE GENERATED BY CHILKAT");
// Note: It may be that http://www.w3.org/2001/04/xmlenc#sha256 is needed in the following line instead of http://www.w3.org/2000/09/xmldsig#sha1
object1.updateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod", true, "Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1") catch {};
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue", "TO BE GENERATED BY CHILKAT");
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2", "TO BE GENERATED BY CHILKAT");
object1.updateAttrAt("xades:SignedProperties|xades:SignedDataObjectProperties", true, "Id", "SignedDataObjectProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4b") catch {};
object1.updateAttrAt("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat", true, "ObjectReference", "#Reference1_2a8df7f8-b958-40cc-83f6-edb53b837347_27") catch {};
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description", "MIME-Version: 1.0\r\nContent-Type: application/zip\r\nContent-Transfer-Encoding: binary\r\nContent-Disposition: filename="PIT-11Z.zip"");
object1.updateAttrAt("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier", true, "Qualifier", "OIDAsURI") catch {};
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier", "http://www.certum.pl/OIDAsURI/signedFile/1.2.616.1.113527.3.1.1.3.1");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Description", "Opis formatu dokumentu oraz jego pelna nazwa");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:DocumentationReferences|xades:DocumentationReference", "http://www.certum.pl/OIDAsURI/signedFile.pdf");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType", "application/zip");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:CommitmentTypeId|xades:Identifier", "http://uri.etsi.org/01903/v1.2.2#ProofOfApproval");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:AllSignedDataObjects", "");
object1.updateAttrAt("xades:UnsignedProperties", true, "Id", "UnsignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_55") catch {};
// Emit XML in compact (single-line) format to avoid whitespace problems.
object1.setEmitCompact(true);
gen.addObject("", try object1.getXml(alloc), "", "") catch {};
// Create an Object to be added to the Signature.
// This is where we add the base64 representation of the PIT-11Z.zip
gen.addObject("Object1_2a8df7f8-b958-40cc-83f6-edb53b837347", try bd_zip.getEncoded(alloc, "base64"), "", "http://www.w3.org/2000/09/xmldsig#base64") catch {};
// -------- Reference 1 --------
gen.addObjectRef("Object1_2a8df7f8-b958-40cc-83f6-edb53b837347", "sha1", "C14N_WithComments", "", "") catch {};
gen.setRefIdAttr("Object1_2a8df7f8-b958-40cc-83f6-edb53b837347", "Reference1_2a8df7f8-b958-40cc-83f6-edb53b837347_27") catch {};
// -------- Reference 2 --------
gen.addObjectRef("SignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4e", "sha1", "", "", "http://uri.etsi.org/01903#SignedProperties") catch {};
gen.setRefIdAttr("SignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4e", "SignedProperties-Reference_2a8df7f8-b958-40cc-83f6-edb53b837347_28") catch {};
// Provide a certificate + private key. (PFX password is test123)
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", "test123") catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
gen.setX509Cert(cert, true) catch {};
gen.setKeyInfoType("X509Data");
gen.setX509Type("Certificate");
// This will be an enveloping signature where the Signature element
// is the XML document root, the signed data is contained within Object
// tag(s) within the Signature.
// Therefore, pass an empty sbXml to CreateXmlDsigSb.
const sb_xml = try chilkat.StringBuilder.init();
defer sb_xml.deinit();
// The Polish government's XmlDSig implementation requires that we reproduce an attribute-sorting error.
// (This is an error in the XML canonicalization that is not noticed when both the signature-creation code and signature-verification code use
// the same XML canonicalization implementation w/ the bug.)
gen.setBehaviors("AttributeSortingBug,CompactSignedXml");
// Sign the XML...
gen.createXmlDSigSb(sb_xml) catch {
std.debug.print("{s}\n", .{try gen.getLastErrorText(alloc)});
return;
};
// -----------------------------------------------
// Save the signed XML to a file.
try sb_xml.writeFile("qa_output/signedXml.xml", "utf-8", false);
std.debug.print("{s}\n", .{try sb_xml.getAsString(alloc)});
// ----------------------------------------
// Verify the signature we just produced...
const verifier = try chilkat.XmlDSig.init();
defer verifier.deinit();
verifier.loadSignatureSb(sb_xml) catch {
std.debug.print("{s}\n", .{try verifier.getLastErrorText(alloc)});
return;
};
verifier.verifySignature(true) catch {
std.debug.print("{s}\n", .{try verifier.getLastErrorText(alloc)});
return;
};
std.debug.print("This signature was successfully verified.\n", .{});
// ------------------------------------
// Finally, let's extract the .zip from the signed XML, and then unzip the original PIT-11Z.xml from the in-memory zip.
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.loadSb(sb_xml, true) catch {};
// The base64 image of the PIT-11Z.zip is in the 2nd ds:Object child of the ds:Signature (the ds:Signature is the root element of the signed XML).
// (ds:Object[0] would be the 1st ds:Object child. Index 1 is the 2nd ds:Object child.)
const str_zip_base64 = try xml.getChildContent(alloc, "ds:Object[1]");
bd_zip.clear() catch {};
bd_zip.appendEncoded(str_zip_base64, "base64") catch {};
if (bd_zip.getNumBytes() == 0) {
std.debug.print("Something went wrong.. we dont' have any data..\n", .{});
return;
}
zip.openBd(bd_zip) catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
// Get the 1st file in the zip, which should be the PIT-11Z.xml
const entry = try chilkat.ZipEntry.init();
defer entry.deinit();
zip.entryAt(0, entry) catch {
std.debug.print("Zip contains no files...\n", .{});
return;
};
// Get the XML:
const orig_xml = try entry.unzipToString(alloc, 0, "utf-8");
std.debug.print("Original XML extracted from base64 zip:\n", .{});
std.debug.print("{s}\n", .{orig_xml});
}