Repository navigation
There is an invalid signature error #256
Description
Hello, I'm trying to validate the XML, from the EFD Reinf, from the Federal Revenue of Brazil, but I'm told that the signature is invalid.
I already use this library in other XML signatures and it signs normally, but in this specific case I am not succeeding, follow the XML and the code PHP.:
<?xml version="1.0" encoding="utf-8"?> <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/envioLoteEventosAssincrono/v1_00_00"> <envioLoteEventos> <ideContribuinte> <tpInsc>1</tpInsc> <nrInsc>xxxxxx970</nrInsc> </ideContribuinte> <eventos> <evento Id="IDxxxxxxxxxxxxxxxxx023082110493200003"> <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evt4020PagtoBeneficiarioPJ/v2_01_02"> <evtRetPJ id="IDxxxxxxxxxxxxxxxxx023082110493200003"> <ideEvento> <indRetif>1</indRetif> <perApur>202x-06</perApur> <tpAmb>2</tpAmb> <procEmi>1</procEmi> <verProc>2.1.02</verProc> </ideEvento> <ideContri> <tpInsc>1</tpInsc> <nrInsc>xxxxxx970</nrInsc> </ideContri> <ideEstab> <tpInscEstab>1</tpInscEstab> <nrInscEstab>7xxxxxxxxx0111</nrInscEstab> <ideBenef> <cnpjBenef>xxxxxxxxxx0100</cnpjBenef> <idePgto> <natRend>xxx01</natRend> <infoPgto> <dtFG>20xx-0x-xx</dtFG> <vlrBruto>xxxx98,60</vlrBruto> <retencoes> <vlrBaseIR>xxx8,60</vlrBaseIR> </retencoes> </infoPgto> </idePgto> <idePgto> <natRend>xxxx2</natRend> <infoPgto> <dtFG>20xx-0x-1x</dtFG> <vlrBruto>xxx,08</vlrBruto> <retencoes> <vlrBaseCSLL>xxx,08</vlrBaseCSLL> <vlrCSLL>6,00</vlrCSLL> <vlrBaseCofins>7xxx,08</vlrBaseCofins> <vlrCofins>x,00</vlrCofins> <vlrBasePP>xxxx,08</vlrBasePP> <vlrPP>x,00</vlrPP> </retencoes> </infoPgto> <infoPgto> <dtFG>20xxx-0x-x0</dtFG> <vlrBruto>6xxx,86</vlrBruto> <retencoes> <vlrBaseIR>xxx3,86</vlrBaseIR> <vlrIR>1x,10</vlrIR> </retencoes> </infoPgto> </idePgto> <idePgto> <natRend>xxxx03</natRend> <infoPgto> <dtFG>20xx-0x-x0</dtFG> <vlrBruto>38xx0,06</vlrBruto> <retencoes> <vlrBaseIR>3xxx,06</vlrBaseIR> </retencoes> </infoPgto> </idePgto> <idePgto> <natRend>xxxx19</natRend> <infoPgto> <dtFG>20xx-0x-1x</dtFG> <vlrBruto>1xxx,00</vlrBruto> <retencoes> <vlrBaseCSLL>1xxx0,00</vlrBaseCSLL> <vlrCSLL>xxx,00</vlrCSLL> <vlrBaseCofins>1xxx0,00</vlrBaseCofins> <vlrCofins>9xx,50</vlrCofins> <vlrBasePP>xxx0,00</vlrBasePP> <vlrPP>xx,25</vlrPP> </retencoes> </infoPgto> <infoPgto> <dtFG>20xx-xx-2x</dtFG> <vlrBruto>xxxx0,00</vlrBruto> <retencoes> <vlrBaseCSLL>xxx0,00</vlrBaseCSLL> <vlrCSLL>xx,00</vlrCSLL> <vlrBaseCofins>xx00,00</vlrBaseCofins> <vlrCofins>xx,00</vlrCofins> <vlrBasePP>xxx0,00</vlrBasePP> <vlrPP>xx,00</vlrPP> </retencoes> </infoPgto> <infoPgto> <dtFG>20xx-xx-xx</dtFG> <vlrBruto>xx00,00</vlrBruto> <retencoes> <vlrBaseCSLL>xx00,00</vlrBaseCSLL> <vlrCSLL>2xxx,00</vlrCSLL> <vlrBaseCofins>xx00,00</vlrBaseCofins> <vlrCofins>xx0,00</vlrCofins> <vlrBasePP>xx00,00</vlrBasePP> <vlrPP>xx0,00</vlrPP> </retencoes> </infoPgto> <infoPgto> <dtFG>20xx-0x-x0</dtFG> <vlrBruto>xxx,00</vlrBruto> <retencoes> <vlrBaseIR>3x00,00</vlrBaseIR> <vlrIR>x5,00</vlrIR> </retencoes> </infoPgto> </idePgto> </ideBenef> </ideEstab> </evtRetPJ> </Reinf> </evento> </eventos> </envioLoteEventos> </Reinf>
Código PHP
` public function assinar($conteudo)
{
$xmlDocument = new DOMDocument('1.0', 'UTF-8');
$xmlDocument->formatOutput = false;
$xmlDocument->preserveWhiteSpace = false;
$xmlDocument->loadXML($conteudo);
$serviceresponse_node = $xmlDocument->getElementsByTagName("evtRetPJ")->item(0);
$serviceresponse_node2 = $xmlDocument->getElementsByTagNameNS('http://www.reinf.esocial.gov.br/schemas/evt4020PagtoBeneficiarioPJ/v2_01_02', '*')->item(0);
$objDSig = new XMLSecurityDSig('');
$objDSig->setCanonicalMethod(XMLSecurityDSig::C14N);
$objDSig->addReference(
$serviceresponse_node,
XMLSecurityDSig::SHA1, // 'http://www.w3.org/2000/09/xmldsig#sha1',
['http://www.w3.org/2000/09/xmldsig#enveloped-signature', 'http://www.w3.org/2001/10/xml-exc-c14n#'],
['id_name' => 'id', 'overwrite' => false],
);
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type' => 'private'));
$objKey->loadKey('C:\site\reinf\ssl\22086849.pem', TRUE);
$objDSig->sign($objKey, $serviceresponse_node);
$objDSig->add509Cert(file_get_contents('C:\site\reinf\ssl\22086849.pem'));
// $signatures_node = $xmlDocument->getElementsByTagName("Reinf")->item(0);
$objDSig->appendSignature($serviceresponse_node2);
$signedXML = $xmlDocument->saveXML();
return $signedXML;
exit;
} `
I need to sign the XML in the tag that is inside Reinf, the signature must be inside the Reinf tag:
.............
<eventos>
<evento Id="IDxxxxxxxxxxxxxxxxx023082110493200003">
<Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evt4020PagtoBeneficiarioPJ/v2_01_02">
<evtRetPJ id="IDxxxxxxxxxxxxxxxxx023082110493200003">
...........
</evtRetPJ>
SIGNATURE HERE
</Reinf>
</evento>
</eventos>
................
Can someone help me please
Activity
tvdijen commentedon Aug 31, 2023
You give us a whole lot of info, but you tell us nothing about the exact error you are getting...
bcs018 commentedon Aug 31, 2023
the error is that the signature is not validating, it is saying that the signature is invalid
look at the lines:
$serviceresponse_node = $xmlDocument->getElementsByTagName("evtRetPJ")->item(0);
$serviceresponse_node2 = $xmlDocument->getElementsByTagNameNS('http://www.reinf.esocial.gov.br/schemas/evt4020PagtoBeneficiarioPJ/v2_01_02', '*')->item(0);
Is that right?,, $serviceresponse_node is the content I want to sign, and $serviceresponse_node2 is where I want to put the signature