Zig
Zig
UBL XAdES Enveloped Signature
See more XAdES Examples
Demonstrates how to create a UBL XAdES enveloped signature.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The following code creates the XML document to be signed.
// (It is also possible to simply load the XML into the Chilkat XML object by calling the LoadXml method.)
// A sample (already signed) of this XML is available here: External link: credit-note-en16931-xades-signed.xml
// Also, you may use this online tool to generate code from sample XML:
// Generate Code to Create XML
const xml_to_sign = try chilkat.Xml.init();
defer xml_to_sign.deinit();
xml_to_sign.setTag("CreditNote");
xml_to_sign.addAttribute("xmlns", "urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2") catch {};
xml_to_sign.addAttribute("xmlns:cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2") catch {};
xml_to_sign.addAttribute("xmlns:cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2") catch {};
xml_to_sign.addAttribute("xmlns:ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2") catch {};
xml_to_sign.updateAttrAt("ext:UBLExtensions|ext:UBLExtension", true, "xmlns:sac", "urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2") catch {};
xml_to_sign.updateAttrAt("ext:UBLExtensions|ext:UBLExtension", true, "xmlns:sig", "urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2") catch {};
xml_to_sign.updateChildContent("ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation", "");
xml_to_sign.updateChildContent("cbc:CustomizationID", "urn:cen.eu:en16931:2017");
xml_to_sign.updateChildContent("cbc:ProfileID", "P1");
xml_to_sign.updateChildContent("cbc:ID", "c2ad1540-cf12-4e83-b47c-906aac70242e");
xml_to_sign.updateChildContent("cbc:IssueDate", "2009-12-15");
xml_to_sign.updateAttrAt("cbc:Note", true, "languageID", "en") catch {};
xml_to_sign.updateChildContent("cbc:Note", "Ordered in our booth at the convention.");
xml_to_sign.updateAttrAt("cbc:DocumentCurrencyCode", true, "listAgencyID", "6") catch {};
xml_to_sign.updateAttrAt("cbc:DocumentCurrencyCode", true, "listID", "ISO 4217 Alpha") catch {};
xml_to_sign.updateChildContent("cbc:DocumentCurrencyCode", "HRK");
xml_to_sign.updateChildContent("cbc:AccountingCost", "Project cost code 123");
xml_to_sign.updateChildContent("cac:InvoicePeriod|cbc:StartDate", "2009-11-01");
xml_to_sign.updateChildContent("cac:InvoicePeriod|cbc:EndDate", "2009-11-30");
xml_to_sign.updateChildContent("cac:OrderReference|cbc:ID", "123");
xml_to_sign.updateChildContent("cac:ContractDocumentReference|cbc:ID", "Contract321");
xml_to_sign.updateChildContent("cac:ContractDocumentReference|cbc:DocumentType", "Framework agreement");
xml_to_sign.updateChildContent("cac:AdditionalDocumentReference|cbc:ID", "Doc1");
xml_to_sign.updateChildContent("cac:AdditionalDocumentReference|cbc:DocumentType", "Timesheet");
xml_to_sign.updateChildContent("cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI", "http://www.suppliersite.eu/sheet001.html");
xml_to_sign.updateChildContent("cac:AdditionalDocumentReference[1]|cbc:ID", "Doc2");
xml_to_sign.updateChildContent("cac:AdditionalDocumentReference[1]|cbc:DocumentType", "Drawing");
xml_to_sign.updateAttrAt("cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject", true, "mimeCode", "application/pdf") catch {};
xml_to_sign.updateChildContent("cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject", "UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID", "9934:18683136487::HR99:276");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName", "KATANCICEVA");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName", "ZAGREB");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone", "10000");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode", "HR");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID", "HR18683136487");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID", "FRE");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName", "MINISTARSTVO FINANCIJA");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name", "JURAJ MARKOVIC");
xml_to_sign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail", "juraj.markovic@fina.hr");
xml_to_sign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID", "9934:49811265576::HR99:NOVO1");
xml_to_sign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName", "GETALDICEVA 4");
xml_to_sign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName", "ZAGREB");
xml_to_sign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone", "10000");
xml_to_sign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode", "HR");
xml_to_sign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID", "HR49811265576");
xml_to_sign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID", "VAT");
xml_to_sign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName", "NOVO1");
xml_to_sign.updateChildContent("cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode", "HR");
xml_to_sign.updateChildContent("cac:PaymentMeans|cbc:PaymentMeansCode", "30");
xml_to_sign.updateChildContent("cac:PaymentMeans|cbc:InstructionNote", "Neki opis placanja");
xml_to_sign.updateChildContent("cac:PaymentMeans|cbc:PaymentID", "HR00 12456");
xml_to_sign.updateChildContent("cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID", "HR1210010051863000160");
xml_to_sign.updateChildContent("cac:PaymentTerms|cbc:Note", "Neki uvjeti placanja");
xml_to_sign.updateAttrAt("cac:TaxTotal|cbc:TaxAmount", true, "currencyID", "HRK") catch {};
xml_to_sign.updateChildContent("cac:TaxTotal|cbc:TaxAmount", "25.00");
xml_to_sign.updateAttrAt("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount", true, "currencyID", "HRK") catch {};
xml_to_sign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount", "100.00");
xml_to_sign.updateAttrAt("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount", true, "currencyID", "HRK") catch {};
xml_to_sign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount", "25.00");
xml_to_sign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID", "S");
xml_to_sign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent", "25");
xml_to_sign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID", "VAT");
xml_to_sign.updateAttrAt("cac:LegalMonetaryTotal|cbc:LineExtensionAmount", true, "currencyID", "HRK") catch {};
xml_to_sign.updateChildContent("cac:LegalMonetaryTotal|cbc:LineExtensionAmount", "100.00");
xml_to_sign.updateAttrAt("cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount", true, "currencyID", "HRK") catch {};
xml_to_sign.updateChildContent("cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount", "100.00");
xml_to_sign.updateAttrAt("cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount", true, "currencyID", "HRK") catch {};
xml_to_sign.updateChildContent("cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount", "125.00");
xml_to_sign.updateAttrAt("cac:LegalMonetaryTotal|cbc:PayableAmount", true, "currencyID", "HRK") catch {};
xml_to_sign.updateChildContent("cac:LegalMonetaryTotal|cbc:PayableAmount", "125.00");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cbc:ID", "1");
xml_to_sign.updateAttrAt("cac:CreditNoteLine|cbc:CreditedQuantity", true, "unitCode", "H87") catch {};
xml_to_sign.updateChildContent("cac:CreditNoteLine|cbc:CreditedQuantity", "1.000");
xml_to_sign.updateAttrAt("cac:CreditNoteLine|cbc:LineExtensionAmount", true, "currencyID", "HRK") catch {};
xml_to_sign.updateChildContent("cac:CreditNoteLine|cbc:LineExtensionAmount", "100.00");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cbc:Description", "Neki detaljniji opis proizvoda ide ovdje");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cbc:Name", "Neki proizvod");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode", "HR");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID", "S");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent", "25");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID", "VAT");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name", "Boja");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value", "Plava");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name", "Masa");
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value", "1,2 Kg");
xml_to_sign.updateAttrAt("cac:CreditNoteLine|cac:Price|cbc:PriceAmount", true, "currencyID", "HRK") catch {};
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Price|cbc:PriceAmount", "100.000000");
xml_to_sign.updateAttrAt("cac:CreditNoteLine|cac:Price|cbc:BaseQuantity", true, "unitCode", "H87") catch {};
xml_to_sign.updateChildContent("cac:CreditNoteLine|cac:Price|cbc:BaseQuantity", "1.000");
// Use this online tool to generate XAdES code from a sample signed XML:
// Generate Code to Creaet XAdES from Sample XAdES
// The following code was generated by the online tool.
const gen = try chilkat.XmlDSigGen.init();
defer gen.deinit();
gen.setSigLocation("CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation");
gen.setSigLocationMod(0);
gen.setSigId("Signature-7f4c4719515a4a0a8ce4d1b983a2ec69");
gen.setSigNamespacePrefix("");
gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
gen.setSignedInfoCanonAlg("C14N");
gen.setSignedInfoDigestMethod("sha256");
// 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:ds", "http://www.w3.org/2000/09/xmldsig#") catch {};
object1.addAttribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#") catch {};
object1.addAttribute("Target", "#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69") catch {};
object1.updateAttrAt("xades:SignedProperties", true, "Id", "SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc") catch {};
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|xades:DataObjectFormat", true, "ObjectReference", "") catch {};
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description", "document");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType", "application/xml");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding", "UTF-8");
gen.addObject("", try object1.getXml(alloc), "", "") catch {};
// -------- Reference 1 --------
gen.addSameDocRef("", "sha256", "", "", "") catch {};
// -------- Reference 2 --------
gen.addObjectRef("SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc", "sha256", "", "", "http://uri.etsi.org/01903#SignedProperties") 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+KeyValue");
gen.setX509Type("IssuerSerial,SubjectName,SKI,Certificate");
// Load XML to be signed...
const sb_xml = try chilkat.StringBuilder.init();
defer sb_xml.deinit();
xml_to_sign.getXmlSb(sb_xml) catch {};
// Adding the "UBLDocumentSignatures" behavior causes the following Transform to be used for the
// first Reference:
// <Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"><XPath>count(ancestor-or-self::sig:UBLDocumentSignatures | here()/ancestor::sig:UBLDocumentSignatures[1]) > count(ancestor-or-self::sig:UBLDocumentSignatures)</XPath></Transform>
gen.setBehaviors("IndentedSignature,UBLDocumentSignatures");
// 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;
};
// We should have just one signature..
const num_sigs = verifier.getNumSignatures();
var verify_idx: i32 = 0;
while (verify_idx < num_sigs) {
verifier.setSelector(verify_idx);
verifier.verifySignature(true) catch {
std.debug.print("{s}\n", .{try verifier.getLastErrorText(alloc)});
return;
};
verify_idx = verify_idx + 1;
}
std.debug.print("The signature was successfully verified.\n", .{});
}