robrichards / xmlseclibs

A PHP library for XML Security
BSD 3-Clause "New" or "Revised" License
386 stars 180 forks source link

There is an invalid signature error #256

Open bcs018 opened 10 months ago

bcs018 commented 10 months ago

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"?>

1 xxxxxx970 1 202x-06 2 1 2.1.02 1 xxxxxx970 1 7xxxxxxxxx0111 xxxxxxxxxx0100 xxx01 20xx-0x-xx xxxx98,60 xxx8,60 xxxx2 20xx-0x-1x xxx,08 xxx,08 6,00 7xxx,08 x,00 xxxx,08 x,00 20xxx-0x-x0 6xxx,86 xxx3,86 1x,10 xxxx03 20xx-0x-x0 38xx0,06 3xxx,06 xxxx19 20xx-0x-1x 1xxx,00 1xxx0,00 xxx,00 1xxx0,00 9xx,50 xxx0,00 xx,25 20xx-xx-2x xxxx0,00 xxx0,00 xx,00 xx00,00 xx,00 xxx0,00 xx,00 20xx-xx-xx xx00,00 xx00,00 2xxx,00 xx00,00 xx0,00 xx00,00 xx0,00 20xx-0x-x0 xxx,00 3x00,00 x5,00 ` [ARQUIVO XML.zip](https://github.com/robrichards/xmlseclibs/files/12486681/ARQUIVO.XML.zip) 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: ............. ........... SIGNATURE HERE ................ Can someone help me please
tvdijen commented 10 months ago

You give us a whole lot of info, but you tell us nothing about the exact error you are getting...

bcs018 commented 10 months ago

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