Standard Tax Invoice Signature Validation Issue using ZATCA .NET SDK 3.4.6 (Simulation Environment)

Dear ZATCA Technical Support Team,

I am integrating Standard Tax Invoices (B2B) using the ZATCA .NET SDK v3.4.6 in the Simulation Environment. We are submitting invoices through the Reporting API (POST /e-invoicing/simulation/invoices/reporting/single) as documented by ZATCA. Our invoice XML is generated according to UBL 2.1, including Header, AdditionalDocumentReference (ICV and PIH only), Supplier, Customer, Delivery, PaymentMeans, TaxTotals, LegalMonetaryTotal and InvoiceLines. The QR element is intentionally not generated manually because it is inserted later by the SDK during the signing process. The certificate (PCSID) and private key were successfully issued by ZATCA, the certificate is stored in Base64 DER format exactly as received from ZATCA, and the private key is stored as Base64(PEM). We sign the invoice using EInvoiceSigner.SignDocument() from SDK 3.4.6. During debugging, we confirmed that the invoice hash generated by GenerateHashMiddleware is identical to the hash used by GenerateSignatureMiddleware; however, after SignDocument() completes, regenerating the invoice hash using EInvoiceHashGenerator produces a different value. Consequently, EInvoiceValidator.ValidateEInvoice() returns the following signature validation errors: hashedXml, invoiceSignedDataDigestValue, and signatureValue. We have thoroughly reviewed our implementation and verified XML generation, tax calculations, legal monetary totals, invoice lines, QR generation, PIH, CSR, certificate parsing, canonicalization, XSL transformation, and hash generation. We also reviewed the SDK internals and observed that after the signature is generated, the SDK executes TransformXMLResultMiddleware, which reloads the XML using LoadXml(), followed by FinalizeSignDocumentMiddleware, which modifies InnerXml before returning the signed document. We would appreciate your clarification on whether this behavior is expected in SDK 3.4.6, whether it has been corrected in SDK 3.4.8, whether the invoice hash is expected to change after SignDocument() completes, and whether there are any additional requirements for Standard Tax Invoice signing that are not documented. We would also appreciate it if you could confirm the recommended signing sequence for Standard Tax Invoices using the .NET SDK. For reference, we are using the Simulation Reporting API documented by ZATCA: https://zatcaapidocs.evinciblesolutions.com/reference/post_invoices-reporting-single

“The SDK generates hash A before signing, uses hash A inside SignedInfo, but after SignDocument() the SDK itself regenerates hash B from the returned SignedEInvoice. Could you clarify whether this behavior is expected?”

Thank you for your support.

dear @agentortech

do you have any updates on this matter, i am facing the same issue. local sdk signature validation fails but it is reported without errors or warnings, I am using 3.4.8 sdk version.

pls send me link Download 3.4.8 sdk version

I maintain Fatoority, an open-source ZATCA library with .NET support. It explicitly verifies that the invoice hash and signature remain valid after signing.

You can test your XML with:

using Fatoora;


using var signer = Signer.FromPem(certPem, keyPem);



var before = Xml.Hash(xml);

var signed = signer.SignXml(xml);

var after = Xml.Hash(signed);



Console.WriteLine(before == after); *// true*

We’ve tested this invariant for both standard and simplified invoices, including parity tests against ZATCA SDK 3.4.8.

Might be worth trying with your XML to isolate whether the issue is in the SDK’s signing process.

I managed to solve the issue, and the validator passes all the steps now.

Problem happens because in the signing of the eInvoice in the sdk it forcefully does this line
eInvoice.InnerXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + eInvoice.InnerXml;
making xDoc.ToString() a problem because it strips the <?xml ...?> declaration

Solution:

Use

public static System.Xml.XmlDocument ToXmlDocument(this XDocument xDoc)
{
    ArgumentNullException.ThrowIfNull(xDoc);

    var xmlDocument = new XmlDocument { PreserveWhitespace = true };
    using var ms = new MemoryStream();
    xDoc.Save(ms); // <--- Writes with <?xml version="1.0" encoding="utf-8"?>
    ms.Position = 0;
    xmlDocument.Load(ms);

    return xmlDocument;
}