Skip to content

digest is invalid because the computed digest differs from the digest in the XML #472

@MateoF01

Description

@MateoF01

Hello. Im getting a problem when i try to sign an xml document. I readed some issues related but i cant fin de solution yet.
After executing my code, y tried to validate the signature with this tool https://tools.chilkat.io/xmlDsigVerify.cshtml and i get:

Signature is Invalid
Number of Reference Digests = 1
Reference 1 digest is invalid because the computed digest differs from the digest in the XML. 

Right now my code is this:

import forge from 'node-forge'
import { readFileSync } from 'fs'
import { SignedXml } from 'xml-crypto'
import { createClient, WSSecurityCert } from 'soap'
import { EXTRACT_X509_CERTS } from 'xml-crypto/lib/utils.js'

SignedXml.getKeyInfoContent = ({ publicCert, prefix }) => {
  if (publicCert == null) return null
  prefix = prefix ? `${prefix}:` : ''

  let x509Certs = ''

  if (Buffer.isBuffer(publicCert)) {
    publicCert = publicCert.toString('latin1')
  }

  let publicCertMatches = []

  if (typeof publicCert === 'string') {
    publicCertMatches = publicCert.match(EXTRACT_X509_CERTS) || []
  }

  if (publicCertMatches.length > 0) {
    x509Certs = publicCertMatches
      .map(c => {
        const certificate = forge.pki.certificateFromPem(c)
        const issuerName = certificate.issuer.attributes.map(attr => `${attr.shortName}=${attr.value}`).join(', ')
        const serialNumber = certificate.serialNumber
        return (
          `<${prefix}X509IssuerSerial>` +
          `<${prefix}X509IssuerName>${issuerName}</${prefix}X509IssuerName>` +
          `<${prefix}X509SerialNumber>${BigInt(`0x${serialNumber}`).toString()}</${prefix}X509SerialNumber>` +
          `</${prefix}X509IssuerSerial>`
        )
      })
      .join('')
  }

  return `<${prefix}X509Data>${x509Certs}</${prefix}X509Data>`
}



const firmarXml = (xml, certPath, password) => {
  const { privateKey, certificate } = retornaCertificado(certPath, password)

  const sig = new SignedXml({ privateKey: privateKey, publicCert: certificate })
  
  sig.signatureAlgorithm = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'
  sig.canonicalizationAlgorithm = 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'

  sig.addReference({
    xpath: "//*[local-name(.)='eFact']",
    transforms: ["http://www.w3.org/2000/09/xmldsig#enveloped-signature", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"],
    digestAlgorithm: 'http://www.w3.org/2001/04/xmlenc#sha256',
    isEmptyUri: true,
  })

  sig.computeSignature(xml, {
    location: {
      reference: "//*[local-name(.)='CFE']",
      action: 'append',
    },
  })
  
  return { Datain: { xmlData: sig.getSignedXml() } }
}

and the xml im trying to sign is this, with the signature generated there

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DGICFE:EnvioCFE version="1.0" xsi:schemaLocation="http://cfe.dgi.gub.uy EnvioCFE_v1.24.xsd"
	xmlns:DGICFE="http://cfe.dgi.gub.uy"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<DGICFE:Caratula version="1.0">
		<DGICFE:RutReceptor>219999830019</DGICFE:RutReceptor>
		<DGICFE:RUCEmisor>219470980013</DGICFE:RUCEmisor>
		<DGICFE:Idemisor>3169</DGICFE:Idemisor>
		<DGICFE:CantCFE>1</DGICFE:CantCFE>
		<DGICFE:Fecha>2024-05-20T13:10:00-03:00</DGICFE:Fecha>
		<DGICFE:X509Certificate>MIIGwzCCBKugAwIBAgIVAJq2gU...</DGICFE:X509Certificate>
	</DGICFE:Caratula>
	<ns0:CFE version="1.0"
		xmlns:ns0="http://cfe.dgi.gub.uy">
		<ns0:eFact>
			<ns0:TmstFirma>2024-05-22T11:51:55-03:00</ns0:TmstFirma>
			<ns0:Encabezado>
				<ns0:IdDoc>
					<ns0:TipoCFE>111</ns0:TipoCFE>
					<ns0:Serie>A</ns0:Serie>
					<ns0:Nro>1</ns0:Nro>
					<ns0:FchEmis>2024-04-25</ns0:FchEmis>
					<ns0:FmaPago>1</ns0:FmaPago>
				</ns0:IdDoc>
				<ns0:Emisor>
					<ns0:RUCEmisor>219470980013</ns0:RUCEmisor>
					<ns0:RznSoc>DGI PRUEBA SERVICIOS WEB</ns0:RznSoc>
					<ns0:CdgDGISucur>1</ns0:CdgDGISucur>
					<ns0:DomFiscal>FERNANDEZ CRESPO AVDA. DANIEL 1534</ns0:DomFiscal>
					<ns0:Ciudad>MONTEVIDEO</ns0:Ciudad>
					<ns0:Departamento>MONTEVIDEO</ns0:Departamento>
				</ns0:Emisor>
				<ns0:Receptor>
					<ns0:TipoDocRecep>2</ns0:TipoDocRecep>
					<ns0:CodPaisRecep>UY</ns0:CodPaisRecep>
					<ns0:DocRecep>214844360018</ns0:DocRecep>
					<ns0:RznSocRecep>DGI</ns0:RznSocRecep>
					<ns0:DirRecep>Fernandez Crespo 1534</ns0:DirRecep>
					<ns0:CiudadRecep>Montevideo</ns0:CiudadRecep>
				</ns0:Receptor>
				<ns0:Totales>
					<ns0:TpoMoneda>UYU</ns0:TpoMoneda>
					<ns0:MntNetoIVATasaBasica>20000</ns0:MntNetoIVATasaBasica>
					<ns0:IVATasaMin>10</ns0:IVATasaMin>
					<ns0:IVATasaBasica>22</ns0:IVATasaBasica>
					<ns0:MntIVATasaBasica>4400</ns0:MntIVATasaBasica>
					<ns0:MntTotal>24400</ns0:MntTotal>
					<ns0:CantLinDet>3</ns0:CantLinDet>
					<ns0:MontoNF>5000</ns0:MontoNF>
					<ns0:MntPagar>29400</ns0:MntPagar>
				</ns0:Totales>
			</ns0:Encabezado>
			<ns0:Detalle>
				<ns0:Item>
					<ns0:NroLinDet>1</ns0:NroLinDet>
					<ns0:IndFact>3</ns0:IndFact>
					<ns0:NomItem>Harina</ns0:NomItem>
					<ns0:Cantidad>10</ns0:Cantidad>
					<ns0:UniMed>kg</ns0:UniMed>
					<ns0:PrecioUnitario>1000</ns0:PrecioUnitario>
					<ns0:MontoItem>10000</ns0:MontoItem>
				</ns0:Item>
				<ns0:Item>
					<ns0:NroLinDet>2</ns0:NroLinDet>
					<ns0:IndFact>3</ns0:IndFact>
					<ns0:NomItem>Azucar</ns0:NomItem>
					<ns0:Cantidad>5</ns0:Cantidad>
					<ns0:UniMed>kg</ns0:UniMed>
					<ns0:PrecioUnitario>2000</ns0:PrecioUnitario>
					<ns0:MontoItem>10000</ns0:MontoItem>
				</ns0:Item>
				<ns0:Item>
					<ns0:NroLinDet>3</ns0:NroLinDet>
					<ns0:IndFact>6</ns0:IndFact>
					<ns0:NomItem>Bicicleta</ns0:NomItem>
					<ns0:Cantidad>1</ns0:Cantidad>
					<ns0:UniMed>N/A</ns0:UniMed>
					<ns0:PrecioUnitario>5000</ns0:PrecioUnitario>
					<ns0:MontoItem>5000</ns0:MontoItem>
				</ns0:Item>
			</ns0:Detalle>
			<ns0:CAEData>
				<ns0:CAE_ID>90240894778</ns0:CAE_ID>
				<ns0:DNro>1</ns0:DNro>
				<ns0:HNro>100</ns0:HNro>
				<ns0:FecVenc>2026-04-24</ns0:FecVenc>
			</ns0:CAEData>
		</ns0:eFact>
		<Signature
			xmlns="http://www.w3.org/2000/09/xmldsig#">
			<SignedInfo>
				<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
				<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
				<Reference URI="">
					<Transforms>
						<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
						<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
					</Transforms>
					<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
					<DigestValue>Rd5Fog4wR0GkbCFCFG41hfzIViWcwzHiEAV+/zRwQZE=</DigestValue>
				</Reference>
			</SignedInfo>
			<SignatureValue>Y6yZwGr/ZrsfXXSyiQyW8fyOvbZyXKXVihlom61/H8tiafAKJm8RpRqKa1dHt2pv072W5srOTb9xYqIaySLzm45TLoKpl526msbOAcCPK+PJzCQIRJB+PkPAQ7m6QXcSvUd0NsxvwZnklBDo2q7/HVv4JfM+8FKt4cgn4KAnOJxM4A2SfQ3hhFK3Uw24dsWaBNTx8ChmBf308czgjaL2RpYWsfCbuCyVROPr7rIbFYcbSvELAdtHtcjI6NTOGciICiV02t/KqH4qk6IHqnTj3elVSkVcKweI8lRDNurMfrJlZ4iKEJo8DKIlWYm5tNoBSRLkcsZNpcRqo53o6DviOg==</SignatureValue>
			<KeyInfo>
				<X509Data>
					<X509IssuerSerial>
						<X509IssuerName>C=UY, L=Montevideo, O=Abitab S.A., OU=ID digital, CN=Abitab</X509IssuerName>
						<X509SerialNumber>883254578286120403153077429716205748582139371646</X509SerialNumber>
					</X509IssuerSerial>
				</X509Data>
			</KeyInfo>
		</Signature>
	</ns0:CFE>
</DGICFE:EnvioCFE>

If someone knows the reason off this problem please share it, im new in the word of signing files, so every help is welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions