Invoice signing reference¶
Create a signer from an X.509 certificate and an unencrypted secp256k1 private key in PKCS#8 format. Use it to sign a finalized invoice or existing XML. For a complete workflow, see the signing guide.
Inputs and behavior¶
| Operation | Expected input | Result and behavior |
|---|---|---|
| Create from PEM | Certificate PEM and private-key PEM text | A reusable signer; rejects invalid certificate/key encodings |
| Create from DER | Certificate DER bytes and private-key PKCS#8 DER bytes | A reusable signer with the same behavior as PEM construction |
| Export certificate | Live signer | Certificate as PEM text or DER bytes |
| Sign finalized invoice | A finalized invoice and a live signer | A signed invoice; consumes the finalized invoice's state |
| Sign existing XML | UBL invoice XML text | Signed XML text; preserves bytes outside the signing structures it owns. Re-signing requires the same certificate |
| Hash existing XML | XML text | Base64 invoice digest after invoice-specific exclusions and canonicalization |
Signer construction parses the certificate and key; it does not establish that they match, that the certificate is trusted, or that its dates are valid. Signing checks that the private key matches the certificate and rejects a mismatch. It does not run the complete invoice validator. Validate the resulting XML separately when you need a local compliance report.
Keep signed XML unchanged for storage and submission. Signing existing XML does not construct an invoice model or run all model-import checks.
Signed invoice values¶
All getters below leave the signed invoice usable. Signed imports verify local signature and QR consistency with the embedded certificate; certificate trust and clearance remain separate.
| Value | Returned content |
|---|---|
| XML | Original stored signed XML text |
| XML Base64 | Base64 of that stored XML text |
| QR code | Base64 TLV payload for rendering; see QR reference |
| Invoice hash | Embedded invoice digest from signature metadata |
| Computed hash | hash() recomputes the digest of the stored signed XML using ZATCA exclusions |
| Signature | Encoded invoice signature |
| Public key | Encoded public-key material |
| ZATCA key signature | Optional encoded certificate-signature material |
| Issuer / serial | Certificate issuer and serial from signed properties |
| Certificate hash | Encoded certificate digest |
| Signed properties hash | Encoded signed-properties digest |
| Signing time | UTC text in YYYY-MM-DDTHH:MM:SS format |
The signed computed hash equals the XML hash operation on xml(). Signed imports reject altered digests, signatures and inconsistent QR data. Local integrity checks do not establish certificate issuer trust. See XML reference for parsing and consuming XML access.
Methods by language¶
Import InvoiceSigner and SigningError from fatoora_core::invoice::sign,
and hash_xml from fatoora_core::invoice.
Construct, export and sign
InvoiceSigner::from_pem(cert_pem: &str, key_pem: &str) -> Result<InvoiceSigner, SigningError>
InvoiceSigner::from_der(cert_der: &[u8], key_der: &[u8]) -> Result<InvoiceSigner, SigningError>
signer.certificate_pem() -> Result<String, SigningError>
signer.certificate_der() -> Result<Vec<u8>, SigningError>
signer.sign_xml(xml: &str) -> Result<String, SigningError>
invoice.sign(signer: &InvoiceSigner) -> Result<SignedInvoice, SigningError>
hash_xml(xml: &str) -> Result<InvoiceHash, SigningError>
hash_xml returns the base64 SHA-256 invoice hash as an InvoiceHash, the value the next invoice carries as its previous invoice hash; use as_str() for the text.
invoice.sign takes a FinalizedInvoice by value. SignedInvoice::xml, qr_code, signature and public_key return borrowed &str; invoice_hash returns &InvoiceHash and uuid returns &InvoiceUuid (call .as_str() for text); zatca_key_signature returns Option<&str>. to_xml_base64 returns String; hash returns Result<InvoiceHash, SigningError>.
Use signed.signed_properties() for issuer(), serial(), cert_hash() and signed_props_hash(), all borrowed &str, plus invoice_hash() -> &InvoiceHash and signing_time() -> &InvoiceTimestamp. The XAdES SigningTime element has no zone suffix; signing_time().as_str() returns the same clock value in canonical YYYY-MM-DDTHH:MM:SSZ form. Copy borrowed strings to retain them after dropping the invoice. Failures expose SigningError::kind() and structured details through the shared error type.
| Call | Return |
|---|---|
InvoiceSigner.from_pem(cert_pem: str, key_pem: str) |
InvoiceSigner |
InvoiceSigner.from_der(cert_der: bytes, key_der: bytes) |
InvoiceSigner |
signer.certificate_pem / certificate_der |
str / bytes (properties) |
signer.sign_xml(xml: str) |
str |
finalized.sign(signer: InvoiceSigner) |
SignedInvoice; consumes finalized state |
invoice.hash() |
str; recomputes the base64 invoice hash |
hash_xml(xml: str) |
str; the same hash from XML text |
SignedInvoice string properties are xml, qr_code, invoice_hash (the hash stored in the signature), signature, public_key, issuer, serial, cert_hash and signed_props_hash; zatca_key_signature is str | None and signing_time is a timezone-aware (UTC) datetime. to_xml_base64() and hash() are methods. Strings and bytes are independent copies. Use context managers or close() for owners; errors raise the typed exceptions in errors.
| Call | Return |
|---|---|
InvoiceSigner.fromPem(certPem: string, keyPem: string) |
InvoiceSigner |
InvoiceSigner.fromDer(certDer: Uint8Array, keyDer: Uint8Array) |
InvoiceSigner |
signer.certificatePem |
string (property) |
signer.certificateDer() |
Bytes |
signer.signXml(xml: string) |
string |
signer.sign(invoice: FinalizedInvoice) |
SignedInvoice; consumes invoice state |
Xml.hash(xml: string) |
string |
SignedInvoice string properties: xml, qrCode, invoiceHash, hash, signature, publicKey, issuer, serial, certHash, signedPropsHash, signingTime; zatcaKeySignature is a string or null. toXmlBase64() is a method returning string.
Objects use garbage-collected cleanup. Bytes.toUint8Array() returns a copied Uint8Array. Native failures throw a FatooraError subclass; see error handling.
| Call | Return |
|---|---|
InvoiceSigner.FromPem(certPem: string, keyPem: string) |
InvoiceSigner |
InvoiceSigner.FromDer(certDer: byte[], keyDer: byte[]) |
InvoiceSigner |
signer.CertificatePem (property) |
string |
signer.CertificateDer() |
Bytes |
signer.SignXml(xml: string) |
string |
signer.Sign(invoice: FinalizedInvoice) |
SignedInvoice; consumes invoice state |
Xml.Hash(xml: string) |
string |
SignedInvoice string properties: Xml, QrCode, InvoiceHash, Hash, Signature, PublicKey, Issuer, Serial, CertHash, SignedPropsHash, SigningTime; ZatcaKeySignature is string?. ToXmlBase64() remains a method returning string.
Dispose native owners, including certificate Bytes; Bytes.ToArray() copies the bytes into a byte[]. Native failures throw FatooraException subclasses such as CryptoException.
| Call | Return |
|---|---|
InvoiceSigner.fromPem(certPem: String, keyPem: String) |
InvoiceSigner |
InvoiceSigner.fromDer(certDer: ByteArray, keyDer: ByteArray) |
InvoiceSigner |
signer.certificatePem |
String |
signer.certificateDer() |
Bytes |
signer.signXml(xml: String) |
String |
signer.sign(invoice: FinalizedInvoice) |
SignedInvoice; consumes invoice state |
Xml.hash(xml: String) |
String |
SignedInvoice text properties: xml, qrCode, invoiceHash, hash, signature, publicKey, issuer, serial, certHash, signedPropsHash, signingTime, each a String; zatcaKeySignature is String?. toXmlBase64() returns String.
Use use or close() for native owners, including certificate Bytes. Bytes.toByteArray() returns a copied ByteArray. Failures throw FatooraException.
| Call | Return |
|---|---|
InvoiceSigner.fromPem(certPem: String, keyPem: String) |
InvoiceSigner |
InvoiceSigner.fromDer(certDer: byte[], keyDer: byte[]) |
InvoiceSigner |
signer.getCertificatePem() |
String |
signer.certificateDer() |
Bytes |
signer.signXml(xml: String) |
String |
signer.sign(invoice: FinalizedInvoice) |
SignedInvoice; consumes invoice state |
Xml.hash(xml: String) |
String |
SignedInvoice text getters: getXml(), toXmlBase64(), getQrCode(), getInvoiceHash(), getHash(), getSignature(), getPublicKey(), getIssuer(), getSerial(), getCertHash(), getSignedPropsHash(), getSigningTime(). Each returns String. getZatcaKeySignature() returns a nullable String.
Use try-with-resources for native owners, including certificate Bytes. Bytes.toByteArray() returns a copied byte[]. Failures throw FatooraException.
Include fatoora.h. All functions return FatooraStatus; inspect outputs only after FATOORA_OK. Inputs are borrowed for the call. Release strings with fatoora_string_free, bytes with fatoora_bytes_free, errors with fatoora_error_free and objects with their corresponding free function. Optional text is NULL when absent.
#include "fatoora.h"
FatooraStatus fatoora_invoice_signer_from_pem(const char *cert_pem,
const char *key_pem,
FatooraInvoiceSigner **out,
FatooraError **error);
FatooraStatus fatoora_invoice_signer_from_der(const uint8_t *cert_der,
size_t cert_der_len,
const uint8_t *key_der,
size_t key_der_len,
FatooraInvoiceSigner **out,
FatooraError **error);
FatooraStatus fatoora_invoice_signer_certificate_pem(const FatooraInvoiceSigner *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_invoice_signer_certificate_der(const FatooraInvoiceSigner *self,
FatooraBytes **out,
FatooraError **error);
FatooraStatus fatoora_invoice_signer_sign(const FatooraInvoiceSigner *self,
FatooraFinalizedInvoice *invoice,
FatooraSignedInvoice **out,
FatooraError **error);
FatooraStatus fatoora_invoice_signer_sign_xml(const FatooraInvoiceSigner *self,
const char *xml,
char **out,
FatooraError **error);
FatooraStatus fatoora_xml_hash(const char *xml,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_xml(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_to_xml_base64(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_qr_code(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_invoice_hash(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_hash(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_signature(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_public_key(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_zatca_key_signature(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_signed_props_hash(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_cert_hash(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_signing_time(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_issuer(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
FatooraStatus fatoora_signed_invoice_serial(const FatooraSignedInvoice *self,
char **out,
FatooraError **error);
Signing consumes the finalized invoice state; its outer handle still needs fatoora_finalized_invoice_free.