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) ZATCA QR Code TLV Encoding and ECDSA Cryptographic StampSee more ZATCA ExamplesDemonstrates how to create the TLV encoding of the QR code fields, then apply the ECDSA signature and ECDSA public key, and finally insert into the previously signed XML E-Invoice. Note: This example requires Chilkat v9.5.0.92 or greater.
#include <C_CkDateTimeW.h> #include <C_CkBinDataW.h> #include <C_CkXmlW.h> #include <C_CkStringBuilderW.h> #include <C_CkCertW.h> #include <C_CkXmlDSigW.h> void ChilkatSample(void) { BOOL success; const wchar_t *sellerName; const wchar_t *vatNumber; HCkDateTimeW dt; const wchar_t *timeStamp; const wchar_t *invoiceTotal; const wchar_t *vatTotal; HCkBinDataW bdTlv; const wchar_t *charset; int tag; const wchar_t *signedXmlFilePath; HCkXmlW xmlSigned; HCkStringBuilderW sbDigestValue; HCkStringBuilderW sbSignatureValue; const wchar_t *x509Certificate; HCkCertW cert; HCkBinDataW bdPubKey; HCkBinDataW bdCertSig; const wchar_t *qr_base64; HCkXmlW xmlQR; HCkStringBuilderW sbSignedXml; HCkStringBuilderW sbReplaceStr; HCkXmlDSigW verifier; int numSigs; int verifyIdx; BOOL verified; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // In Step 1, we applied a signature to an e-invoice Zakat, Tax and Customs Authority (ZATCA) of Saudi Arabia // This example is Step 2, where we compose the TLV encoding of the QR code, apply the ECDSA signature, and then insert into the signed XML without disturbing the signed XML. // Construct TLV (Tag-Length-Value) encoding of the QR data. // We have 5 pieces of data: Seller Name, VAT Number, Time Stamp, Invoice Total, and VAT Total. // For example: sellerName = L"Firoz Ashraf"; vatNumber = L"1234567891"; // The timestamp in QR Code should be based on Invoice Issue Time (KSA-25) and Invoice Issue Date (BT-2). // It can either be in format YYYY-MM-DD'T'HH:MM:SS (for local time in KSA) or YYYY-MM-DD'T'HH:MM:SS'Z' (for UTC time or Zulu time) // Get the current UTC date/time in this format: YYYY-MM-DD'T'HH:MM:SS'Z' dt = CkDateTimeW_Create(); CkDateTimeW_SetFromCurrentSystemTime(dt); timeStamp = CkDateTimeW_getAsTimestamp(dt,FALSE); invoiceTotal = L"100.00"; vatTotal = L"15.00"; // TLV encode into a Chilkat BinData object. bdTlv = CkBinDataW_Create(); charset = L"utf-8"; tag = 1; CkBinDataW_AppendByte(bdTlv,tag); CkBinDataW_AppendCountedString(bdTlv,1,FALSE,sellerName,charset); tag = tag + 1; // This is tag 2 CkBinDataW_AppendByte(bdTlv,tag); CkBinDataW_AppendCountedString(bdTlv,1,FALSE,vatNumber,charset); tag = tag + 1; // This is tag 3 CkBinDataW_AppendByte(bdTlv,tag); CkBinDataW_AppendCountedString(bdTlv,1,FALSE,timeStamp,charset); tag = tag + 1; // This is tag 4 CkBinDataW_AppendByte(bdTlv,tag); CkBinDataW_AppendCountedString(bdTlv,1,FALSE,invoiceTotal,charset); tag = tag + 1; // This is tag 5 CkBinDataW_AppendByte(bdTlv,tag); CkBinDataW_AppendCountedString(bdTlv,1,FALSE,vatTotal,charset); // ---------------------------------------------------------------------------------------------------------------------------------------------- // For tag 6, we need the SHA256 hash from the signed XML. This is the DigestValue as shown in the fragment of the XML Signature below: // <ds:Reference Id="invoiceSignedData" URI=""> // <ds:Transforms> // <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"> // <ds:XPath>not(//ancestor-or-self::ext:UBLExtensions)</ds:XPath> // </ds:Transform> // <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"> // <ds:XPath>not(//ancestor-or-self::cac:Signature)</ds:XPath> // </ds:Transform> // <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"> // <ds:XPath>not(//ancestor-or-self::cac:AdditionalDocumentReference[cbc:ID='QR'])</ds:XPath> // </ds:Transform> // <ds:Transform Algorithm="http://www.w3.org/2006/12/xml-c14n11"/> // </ds:Transforms> // <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/> // <ds:DigestValue>zCp+kF1qzNgXD6AhLq69/CYCklMFSXUmmVPbm4v/76U=</ds:DigestValue> // </ds:Reference> // To get this information, we'll need to load the signed XML into a Chilkat XML object, and then access it. // Load the XML we previously signed... signedXmlFilePath = L"qa_data/zatca/testing/SignedXML.xml"; xmlSigned = CkXmlW_Create(); success = CkXmlW_LoadXmlFile(xmlSigned,signedXmlFilePath); if (success == FALSE) { wprintf(L"%s\n",CkXmlW_lastErrorText(xmlSigned)); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); return; } // digestValue is a base64 string. sbDigestValue = CkStringBuilderW_Create(); success = CkXmlW_GetChildContentSb(xmlSigned,L"ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation|ds:Signature|ds:SignedInfo|ds:Reference[0]|ds:DigestValue",sbDigestValue); if (success == FALSE) { wprintf(L"Failed to get DigestValue from signed XML.\n"); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); return; } wprintf(L"DigestValue = %s\n",CkStringBuilderW_getAsString(sbDigestValue)); // Append the DigestValue base64 string to the TLV. tag = 6; CkBinDataW_AppendByte(bdTlv,tag); CkBinDataW_AppendByte(bdTlv,CkStringBuilderW_getLength(sbDigestValue)); CkBinDataW_AppendSb(bdTlv,sbDigestValue,L"utf-8"); // ---------------------------------------------------------------------------------------------------------------------------------------------- // Tag 7 will contain the <ds:Signature> value from the signed XML. // PS> To get the XML path passed to GetChildContentSb, you can copy/paste the signed XML into Chilkat's online tool at https://tools.chilkat.io/xmlParse // The parsing code that is generated by the online tool will reveal the required path to the element in the XML. sbSignatureValue = CkStringBuilderW_Create(); success = CkXmlW_GetChildContentSb(xmlSigned,L"ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation|ds:Signature|ds:SignatureValue",sbSignatureValue); if (success == FALSE) { wprintf(L"Failed to get SignatureValue from signed XML.\n"); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); return; } wprintf(L"SignatureValue = %s\n",CkStringBuilderW_getAsString(sbSignatureValue)); // Append the SignatureValue base64 string to the TLV. tag = 7; CkBinDataW_AppendByte(bdTlv,tag); CkBinDataW_AppendByte(bdTlv,CkStringBuilderW_getLength(sbSignatureValue)); CkBinDataW_AppendSb(bdTlv,sbSignatureValue,L"utf-8"); // ---------------------------------------------------------------------------------------------------------------------------------------------- // Tag 8 will contain the public key of the signing certificate. // The signing certificate is available within the signed XML at <ds:X509Certificate>. x509Certificate = CkXmlW_getChildContent(xmlSigned,L"ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation|ds:Signature|ds:KeyInfo|ds:X509Data|ds:X509Certificate"); if (CkXmlW_getLastMethodSuccess(xmlSigned) == FALSE) { wprintf(L"Failed to get X509Certificate from the signed XML.\n"); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); return; } cert = CkCertW_Create(); success = CkCertW_SetFromEncoded(cert,x509Certificate); if (success == FALSE) { wprintf(L"Failed to load signing certificate from base64.\n"); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); CkCertW_Dispose(cert); return; } // We want to get the cert's public key bytes having this ASN.1 format. // SEQUENCE (2 elem) // SEQUENCE (2 elem) // OBJECT IDENTIFIER 1.2.840.10045.2.1 ecPublicKey (ANSI X9.62 public key type) // OBJECT IDENTIFIER 1.3.132.0.10 secp256k1 (SECG (Certicom) named elliptic curve) // BIT STRING (520 bit) 0000010001110000110111000001111110100110101110110101111000110001110100 // Note: The cert.GetPubKeyDer method was added in Chilkat v9.5.0.92 bdPubKey = CkBinDataW_Create(); success = CkCertW_GetPubKeyDer(cert,TRUE,bdPubKey); if (success == FALSE) { wprintf(L"Failed to get certificate's public key.\n"); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPubKey); return; } // We want to add the binary bytes of the public key (not the base64 string) to the QR code. tag = 8; CkBinDataW_AppendByte(bdTlv,tag); CkBinDataW_AppendByte(bdTlv,CkBinDataW_getNumBytes(bdPubKey)); CkBinDataW_AppendBd(bdTlv,bdPubKey); // Show the public key in base64 format: wprintf(L"Certificate public key:\n"); wprintf(L"%s\n",CkBinDataW_getEncoded(bdPubKey,L"base64")); // ---------------------------------------------------------------------------------------------------------------------------------------------- // Tag 9 will contain the signature contained in the signing certificate. // Note: The cert.GetPubKeyDer method was added in Chilkat v9.5.0.92 bdCertSig = CkBinDataW_Create(); success = CkCertW_GetSignature(cert,bdCertSig); if (success == FALSE) { wprintf(L"Failed to get certificate's signature.\n"); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPubKey); CkBinDataW_Dispose(bdCertSig); return; } // We want to add the binary bytes of the signature (not the base64 string) to the QR code. tag = 9; CkBinDataW_AppendByte(bdTlv,tag); CkBinDataW_AppendByte(bdTlv,CkBinDataW_getNumBytes(bdCertSig)); CkBinDataW_AppendBd(bdTlv,bdCertSig); // Show the cert's signature in hex format: wprintf(L"Certificate signature:\n"); wprintf(L"%s\n",CkBinDataW_getEncoded(bdCertSig,L"hex")); // ---------------------------------------------------------------------------------------------------------------------------------------------- // At this point the full QR code is contained in bdTlv. // Get it as a base64 string qr_base64 = CkBinDataW_getEncoded(bdTlv,L"base64"); wprintf(L"QR: %s\n",qr_base64); // ---------------------------------------------------------------------------------------------------------------------------------------------- // Insert the QR XML fragment into the previously signed XML -- without disturbing (invalidating) the signature. // We need to build and insert the following XML fragment under the // as a cac:AdditionalDocumentReference just before the "<cac:Signature>" opening tag. // <cac:AdditionalDocumentReference> // <cbc:ID>QR</cbc:ID> // <cac:Attachment> // <cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">BASE64_TLV_CONTENT</cbc:EmbeddedDocumentBinaryObject> // </cac:Attachment> // </cac:AdditionalDocumentReference> xmlQR = CkXmlW_Create(); CkXmlW_putTag(xmlQR,L"cac:AdditionalDocumentReference"); CkXmlW_UpdateChildContent(xmlQR,L"cbc:ID",L"QR"); CkXmlW_UpdateAttrAt(xmlQR,L"cac:Attachment|cbc:EmbeddedDocumentBinaryObject",TRUE,L"mimeCode",L"text/plain"); CkXmlW_UpdateChildContent(xmlQR,L"cac:Attachment|cbc:EmbeddedDocumentBinaryObject",CkBinDataW_getEncoded(bdTlv,L"base64")); // Load our previously signed XML into a Chilkat StringBuilder. // We should not load the previously signed XML into a Chilkat XML object because the XML gets loaded into an internal DOM (Document Object Model). // When re-emitted from the DOM, formatting can change and it would break the XML signature. // Therefore, we must load into a StringBuilder and insert the new fragment without disturbing the remainder. // The inserted fragment is ignored because the following transform was included in the XML signature reference: // <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"> // <ds:XPath>not(//ancestor-or-self::cac:AdditionalDocumentReference[cbc:ID='QR'])</ds:XPath> // </ds:Transform> sbSignedXml = CkStringBuilderW_Create(); success = CkStringBuilderW_LoadFile(sbSignedXml,signedXmlFilePath,L"utf-8"); if (success == FALSE) { wprintf(L"Failed to load previously signed XML file.\n"); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPubKey); CkBinDataW_Dispose(bdCertSig); CkXmlW_Dispose(xmlQR); CkStringBuilderW_Dispose(sbSignedXml); return; } sbReplaceStr = CkStringBuilderW_Create(); CkXmlW_putEmitXmlDecl(xmlQR,FALSE); CkXmlW_putEmitCompact(xmlQR,TRUE); CkStringBuilderW_Append(sbReplaceStr,CkXmlW_getXml(xmlQR)); CkStringBuilderW_Append(sbReplaceStr,L"<cac:Signature>"); success = CkStringBuilderW_ReplaceFirst(sbSignedXml,L"<cac:Signature>",CkStringBuilderW_getAsString(sbReplaceStr)); if (success == FALSE) { wprintf(L"Did not find <cac:Signature> in the signed XML\n"); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPubKey); CkBinDataW_Dispose(bdCertSig); CkXmlW_Dispose(xmlQR); CkStringBuilderW_Dispose(sbSignedXml); CkStringBuilderW_Dispose(sbReplaceStr); return; } // Save the updated signed XML. success = CkStringBuilderW_WriteFile(sbSignedXml,L"qa_output/signedXML_withQR.xml",L"utf-8",FALSE); // ---------------------------------------- // Verify the updated signed XML to make sure we didn't invalidate the signature... verifier = CkXmlDSigW_Create(); success = CkXmlDSigW_LoadSignatureSb(verifier,sbSignedXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier)); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPubKey); CkBinDataW_Dispose(bdCertSig); CkXmlW_Dispose(xmlQR); CkStringBuilderW_Dispose(sbSignedXml); CkStringBuilderW_Dispose(sbReplaceStr); CkXmlDSigW_Dispose(verifier); return; } // ---------------- This is important ----------------------------------------- // Starting in Chilkat v9.5.0.92, specify "ZATCA" in uncommon options // to validate signed XML according to ZATCA needs. // ---------------------------------------------------------------------------- CkXmlDSigW_putUncommonOptions(verifier,L"ZATCA"); numSigs = CkXmlDSigW_getNumSignatures(verifier); verifyIdx = 0; while (verifyIdx < numSigs) { CkXmlDSigW_putSelector(verifier,verifyIdx); verified = CkXmlDSigW_VerifySignature(verifier,TRUE); if (verified != TRUE) { wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier)); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPubKey); CkBinDataW_Dispose(bdCertSig); CkXmlW_Dispose(xmlQR); CkStringBuilderW_Dispose(sbSignedXml); CkStringBuilderW_Dispose(sbReplaceStr); CkXmlDSigW_Dispose(verifier); return; } verifyIdx = verifyIdx + 1; } wprintf(L"All signatures were successfully verified.\n"); CkDateTimeW_Dispose(dt); CkBinDataW_Dispose(bdTlv); CkXmlW_Dispose(xmlSigned); CkStringBuilderW_Dispose(sbDigestValue); CkStringBuilderW_Dispose(sbSignatureValue); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPubKey); CkBinDataW_Dispose(bdCertSig); CkXmlW_Dispose(xmlQR); CkStringBuilderW_Dispose(sbSignedXml); CkStringBuilderW_Dispose(sbReplaceStr); CkXmlDSigW_Dispose(verifier); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.