Function Signature
Description
Signs an XML document using a digital certificate in PKCS#12 format. This function implements XAdES-BES (XML Advanced Electronic Signatures - Basic Electronic Signature) standard, which is required for electronic invoicing in Ecuador. The function supports certificates from:- Security Data - Costa Rican certificate authority
- Banco Central del Ecuador - Ecuador’s central bank
Parameters
The PKCS#12 certificate file data as an ArrayBuffer. Use
getP12FromLocalFile() or getP12FromUrl() to obtain this.The password to decrypt the PKCS#12 certificate file.
The XML document to be signed as a string. Use
getXMLFromLocalFile() or getXMLFromLocalUrl() to obtain this.Returns
The signed XML document with the digital signature appended at the end, before the closing root tag.
Errors
- Throws
"Expired certificate"if the certificate’s validity period has expired or not yet started - May throw parsing errors if the P12 file is corrupted or password is incorrect
- May throw errors if the XML is malformed
Example Usage
Implementation Details
The function performs the following operations:- Certificate Validation: Checks that the certificate is within its validity period
- XML Normalization: Removes excess whitespace and formatting
- Certificate Processing: Extracts the appropriate signing key based on provider
- Hash Generation: Creates SHA-1 hashes of the XML content and certificate
- Signature Creation: Generates an RSA-SHA1 signature
- XAdES-BES Construction: Builds the complete signature structure with:
- SignedInfo with canonicalization method
- SignatureValue
- KeyInfo with X.509 certificate
- QualifyingProperties with signed properties
Certificate Provider Support
Security Data
For certificates issued by Security Data, the function uses the first PKCS#8 shrouded key bag.Banco Central del Ecuador
For certificates issued by Banco Central, the function searches for the key with “Signing Key” in its friendly name attribute. Source:src/services/signing.ts:62