XML signing error for EFD-Reinf in Elixir

Asked

Viewed 301 times

1

I followed the instructions posted on this link:

This is quite enlightening, but I’m still having trouble signing REINF XML, using Elixir as a language.

In the example I put below, I am trying to sign the event "evtInfoContri", the format I am using to sign is this way:

<Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_02"><evtInfoContri id="ID1142933600000002018051414491100281"><ideEvento><tpAmb>2</tpAmb><procEmi>1</procEmi><verProc>V2R010</verProc></ideEvento><ideContri><tpInsc>1</tpInsc><nrInsc>14293360</nrInsc></ideContri><infoContri><inclusao><idePeriodo><iniValid>2018-01</iniValid></idePeriodo><infoCadastro><classTrib>99</classTrib><indEscrituracao>1</indEscrituracao><indDesoneracao>1</indDesoneracao><indAcordoIsenMulta>0</indAcordoIsenMulta><indSitPJ>0</indSitPJ><contato><nmCtt>CARLOS DOMIENIKAN</nmCtt><cpfCtt>18293491809</cpfCtt><foneFixo>1149021845</foneFixo><email>[email protected]</email></contato></infoCadastro></inclusao></infoContri></evtInfoContri></Reinf>

As I try to show, this XML is in a single line. For signature and creation of Digest, I am using the Excrypto module, performing the following instructions:

rsa_priv_key     = ExPublicKey.load!("priv/cert/private_rsa.key")
{:ok, hash}      = ExCrypto.Hash.sha256(evento())
digest           = "#{Base.encode64 hash}"
{:ok, signature} = ExPublicKey.sign(eventoXML(), :sha256, rsa_priv_key)
base64           = "#{Base.encode64 signature}"

After this is done, my signed XML is this way (Addendum: For security reasons because they are my client’s private information, the data has been modified here, so it was not necessarily assembled in this way)

<Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_02"><evtInfoContri id="ID1142933600000002018051414491100281"><ideEvento><tpAmb>2</tpAmb><procEmi>1</procEmi><verProc>V2R010</verProc></ideEvento><ideContri><tpInsc>1</tpInsc><nrInsc>12345678</nrInsc></ideContri><infoContri><inclusao><idePeriodo><iniValid>2018-01</iniValid></idePeriodo><infoCadastro><classTrib>99</classTrib><indEscrituracao>1</indEscrituracao><indDesoneracao>1</indDesoneracao><indAcordoIsenMulta>0</indAcordoIsenMulta><indSitPJ>0</indSitPJ><contato><nmCtt>Empresa XPTO</nmCtt><cpfCtt>12345678999</cpfCtt><foneFixo>1123456789</foneFixo><email>[email protected]</email></contato></infoCadastro></inclusao></infoContri></evtInfoContri><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="#ID1142933600000002018051414491100281"><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>Valor do digest em Base64</DigestValue></Reference></SignedInfo><SignatureValue>Valor da assinatura em Base64</SignatureValue><KeyInfo><X509Data><X509Certificate>Valor do certificado</X509Data></KeyInfo></Signature></Reinf>

and after envelopment it was like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sped="http://sped.fazenda.gov.br/"><soapenv:Header/><soapenv:Body><sped:ReceberLoteEventos><sped:loteEventos><Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v1_03_02"><loteEventos><evento id="ID1142933600000002018051414491100281"><Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_02"><evtInfoContri id="ID1142933600000002018051414491100281"><ideEvento><tpAmb>2</tpAmb><procEmi>1</procEmi><verProc>V2R010</verProc></ideEvento><ideContri><tpInsc>1</tpInsc><nrInsc>12345678</nrInsc></ideContri><infoContri><inclusao><idePeriodo><iniValid>2018-01</iniValid></idePeriodo><infoCadastro><classTrib>99</classTrib><indEscrituracao>1</indEscrituracao><indDesoneracao>1</indDesoneracao><indAcordoIsenMulta>0</indAcordoIsenMulta><indSitPJ>0</indSitPJ><contato><nmCtt>Empresa XPTO</nmCtt><cpfCtt>12345678999</cpfCtt><foneFixo>1123456789</foneFixo><email>[email protected]</email></contato></infoCadastro></inclusao></infoContri></evtInfoContri><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="#ID1142933600000002018051414491100281"><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>Valor do digest em Base64</DigestValue></Reference></SignedInfo><SignatureValue>Valor da assinatura em Base64</SignatureValue><KeyInfo><X509Data><X509Certificate>Valor do certificado</X509Data></KeyInfo></Signature></Reinf></evento></loteEventos></Reinf></sped:loteEventos></sped:ReceberLoteEventos></soapenv:Body></soapenv:Envelope>

Even doing so, I get as a result the message MS0017.

What could be wrong?

Thank you very much for your attention,

  • Pacheco, maybe the problem is in this namespace sped, and these two elements, sped:ReceberLoteEventos and sped:loteEventos, that appear before the first element Reinf, in the final SOAP package. Take this example XML (it is in a super outdated version, but, you can get an idea of how it should be), it worked at the time it was sent: http://supporte.quarta.com.br/eSocial/arquivos/R1000_EnvioLote.xml

  • So... I tested with and without these tags and different errors occur... Without them I have error MS0028 - Invalid batch and with them I have error MS0017 - Invalid signature. I have already tried to sign the whole XML (the string) and also only the ID, in both occur the MS0017 error, as said. I leave a question: What would be correct, to sign the XML? Use the whole string or only the ID value (e.g. "ID1142933600000002018051414491100281")?

  • Check out this other answer I gave, so you have a better idea of how you have to do it: https://answall.com/a/295159/86952

  • I came to see and followed exactly this answer https://answall.com/questions/283260/erro-signed-by-do-xml-para-o-efd-reinf/283284#283284, even though my signature has an error. I took XML as I put it here and generated his signature and Digest tbm as requested by the recipe.

  • Pacheco, I’ve now noticed that in your signed XML example, you missed closing the element <X509Certificate>. You unintentionally removed this closing tag when you edited the value of this element, or your XML has this error even?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.