Sign electronic documents with digital certificates and validate XML signatures in LibreDTE Core
Digital signatures are essential for Chilean electronic tax documents. LibreDTE Core handles both the electronic stamp (Timbre Electrónico/TED) and the XML digital signature using your digital certificate.
The certificate validity is checked against the timestamp. The certificate must be active at the time of signing.
// AbstractBuilderStrategy.php:201if (!$certificate->isActive($timestamp)) { throw new BuilderException( 'El certificado digital no está vigente en el tiempo especificado' );}
3
XML Signature
The document is signed using the XML-DSig standard with the certificate’s private key.
The TED (Timbre Electrónico) is created using the CAF’s private key. This happens automatically when you provide a CAF:
// The stamping process (AbstractBuilderStrategy.php:123-185)// 1. Verify folio is in CAF rangeif (!$caf->enRango($document->getFolio())) { throw new BuilderException('Folio fuera de rango');}// 2. Verify CAF is valid at timestampif (!$caf->vigente($timestamp)) { throw new BuilderException('CAF vencido');}// 3. Generate TED data structure$tedData = $document->getPlantillaTED();$tedData['TED']['DD']['CAF'] = $cafArray['AUTORIZACION']['CAF'];$tedData['TED']['DD']['TSTED'] = $timestamp;// 4. Sign with CAF private key using SHA1$timbre = $this->signatureService->sign( $ddToStamp, $caf->getPrivateKey(), OPENSSL_ALGO_SHA1);// 5. Add stamp to document$tedData['TED']['FRMT']['@value'] = $timbre;
The TED uses SHA1 algorithm as specified by SII, while the XML signature typically uses SHA256 or stronger algorithms.
You can sign a document with only a certificate (no electronic stamp):
// Create document without CAF$bag = $documentComponent->bill( data: $documentData, caf: null, // No CAF = no electronic stamp certificate: $certificate // Only digital signature);// The document will be signed but won't have a TED stamp$document = $bag->getDocument();
Documents without a TED stamp cannot be sent to SII. The CAF is required for all documents that need to be reported to the tax authority.
Ensure your certificate is valid for the timestamp you’re using. Check certificate validity dates:
if (!$certificate->isActive($timestamp)) { echo "Certificate not valid at {$timestamp}"; echo "Valid from: {$certificate->getFrom()}"; echo "Valid to: {$certificate->getTo()}";}
Invalid certificate password
If you get errors loading the certificate, verify the password is correct: