XML signing error for EFD-Reinf

Asked

Viewed 3,333 times

2

The problem I mention below is similar to the problem of this link post:

REINF - Invalid Signature. Failed to verify XML document signature. Reinf using c#

What happens is that I am testing the sending of an event to REINF and I am receiving the return below the webservice:

<codResp>MS0017</codResp>
<dscResp>Assinatura do evento inválida. Assinatura Digital do documento XML é inválida</dscResp>

My XML before signing is this:

    <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v1_03_00">
        <loteEventos>
            <evento id="ID1670710010000002018030117011200001">
                <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_00">
                    <evtInfoContri id="ID1670710010000002018030117011200001">
                        <ideEvento>
                            <tpAmb>2</tpAmb>
                            <procEmi>1</procEmi>
                            <verProc>1</verProc>
                        </ideEvento>
                        <ideContri>
                            <tpInsc>1</tpInsc>
                            <nrInsc>67071001</nrInsc>
                        </ideContri>
                        <infoContri>
                            <inclusao>
                                <idePeriodo>
                                    <iniValid>2018-03</iniValid>
                                    <fimValid>2018-03</fimValid>
                                </idePeriodo>
                                <infoCadastro>
                                    <classTrib>99</classTrib>
                                    <indEscrituracao>1</indEscrituracao>
                                    <indDesoneracao>0</indDesoneracao>
                                    <indAcordoIsenMulta>0</indAcordoIsenMulta>
                                    <indSitPJ>0</indSitPJ>
                                    <contato>
                                        <nmCtt>XXXXXXXXXX</nmCtt>
                                        <cpfCtt>00000000000</cpfCtt>
                                        <foneFixo>00000000000</foneFixo>
                                    </contato>
                                    <softHouse>
                                        <cnpjSoftHouse>00000000000</cnpjSoftHouse>
                                        <nmRazao>XXXXXXXXXX</nmRazao>
                                        <nmCont>XXXXXXXXXX</nmCont>
                                        <telefone>00000000000</telefone>
                                        <email>XXXXXXXXXX</email>
                                    </softHouse>
                                </infoCadastro>
                            </inclusao>
                        </infoContri>
                    </evtInfoContri>
                </Reinf>
            </evento>
        </loteEventos>
    </Reinf>

After the signature is like this:

    <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v1_03_00">
      <loteEventos>
        <evento id="ID1670710010000002018030117011200001">
          <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_00">
            <evtInfoContri id="ID1670710010000002018030117011200001">
              <ideEvento>
                <tpAmb>2</tpAmb>
                <procEmi>1</procEmi>
                <verProc>1</verProc>
              </ideEvento>
              <ideContri>
                <tpInsc>1</tpInsc>
                <nrInsc>67071001</nrInsc>
              </ideContri>
              <infoContri>
                <inclusao>
                  <idePeriodo>
                    <iniValid>2018-03</iniValid>
                    <fimValid>2018-03</fimValid>
                  </idePeriodo>
                  <infoCadastro>
                    <classTrib>99</classTrib>
                    <indEscrituracao>1</indEscrituracao>
                    <indDesoneracao>0</indDesoneracao>
                    <indAcordoIsenMulta>0</indAcordoIsenMulta>
                    <indSitPJ>0</indSitPJ>
                    <contato>
                      <nmCtt>XXXXXXXXXX</nmCtt>
                      <cpfCtt>00000000000</cpfCtt>
                      <foneFixo>00000000000</foneFixo>
                    </contato>
                    <softHouse>
                      <cnpjSoftHouse>00000000000</cnpjSoftHouse>
                      <nmRazao>XXXXXXXXXX</nmRazao>
                      <nmCont>XXXXXXXXXX</nmCont>
                      <telefone>00000000000</telefone>
                      <email>XXXXXXXXXX</email>
                    </softHouse>
                  </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="#ID1670710010000002018030117011200001">
                  <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>+Dm6c8v+ZhocqLQ5miRzTsuDnP5TnmwCAkhvWvxiFdI=</DigestValue>
                </Reference>
              </SignedInfo>
              <SignatureValue>ncq4zPSN5EXVYh6YvrdfJn4+jMYhrHR1tM6NStnfmlDZjxCcLFCHrXLxuG6awsEtnJULWSGwbm49uEEBcvY5LHs6iB0fPZOsMlyoaA8plHidK+UQmk0C37/TMGMd1ii/xTeU+XraVCtJIw1xIGLC3q7D2iAQykuUCBNSaIBVlS8rT03BrnG+XX+fkW/F668clm5UQfnsFcID5WfIQ9u85vYCwa/J4b+LjvtuqFx6CQJdYsw1EAV98fgEWxoktbBpuda+UfFroOPog83Ogh92TI8OaqVva7YMOPSoNreMQuQCPj4HctywCpIIo7Ak10H4n2zGAFAr/tfdm9jJX3GmDQ==</SignatureValue>
              <KeyInfo>
                <X509Data>
                  <X509Certificate>MIIH2jCCBcKgAwIBAgIIWrdyPpsRHaUwDQYJKoZIhvcNAQELBQAwTDELMAkGA1UEBhMCQlIxEzARBgNVBAoTCklDUC1CcmFzaWwxKDAmBgNVBAMTH1NFUkFTQSBDZXJ0aWZpY2Fkb3JhIERpZ2l0YWwgdjIwHhcNMTcwNjE5MTgxMDAwWhcNMTgwNjE5MTgxMDAwWjCB+jELMAkGA1UEBhMCQlIxEzARBgNVBAoTCklDUC1CcmFzaWwxFDASBgNVBAsTCyhFTSBCUkFOQ08pMRgwFgYDVQQLEw8wMDAwMDEwMDc5MzA2MzUxFDASBgNVBAsTCyhFTSBCUkFOQ08pMRQwEgYDVQQLEwsoRU0gQlJBTkNPKTEUMBIGA1UECxMLKEVNIEJSQU5DTykxFDASBgNVBAsTCyhFTSBCUkFOQ08pMRQwEgYDVQQLEwsoRU0gQlJBTkNPKTE4MDYGA1UEAxMvVU5JRlkgU09MVUNPRVMgRU0gVEVDTk9MT0dJQSBEQSBJTkZPUk1BQ0FPIExUREEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCoO2g+VemAW7SHjmO5BfDejnjC/etDmV5I7u7A8b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B/wQEAwIF4DAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwQwgbgGA1UdEQSBsDCBrYEXTUFSQ09TLlRIT01BWkBVTklGWS5DT02gPgYFYEwBAwSgNRMzMjgxMDE5NzcyNjY5MjM3NDg3MTAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwoB4GBWBMAQMCoBUTE01BUkNPUyBDRVNBUiBUSE9NQVqgGQYFYEwBAwOgEBMONjcwNzEwMDEwMDAxMDagFwYFYEwBAwegDhMMMDAwMDAwMDAwMDAwMA0GCSqGSIb3DQEBCwUAA4ICAQATHgfi7caoxqobwfXRcBhZ7LkVO3GaOBYE92L/KAoWoZR7m2vREbo7mfhXIx2VZXR509Nr6FIPT28EeKAaOR6ZwonykVpicGklkZjUmDgXTb4jtaCNAQZl347Qf+81LEHHwtrL2alhr+hvxXL2Q+UzKTrqtxua4Ta96lF2s7ubcrTnsiHNPSu8cZSzirtsYIm0ExIkNVQpH3xiCIcQIkuWPAjBAuuQtAUK1klmEcNQYz01l5krDIbLiYzwUJXG7CP9wDZvn8xPFzTr9XFgGglb3fy2OKFiSoaB80z90w3Sd1opiuojANEJrQV64A47fSa6BnidA97e5Dtk+GWtbJ0vPoT9/+3HAahJFq5Uq3btghQ3IXxvRSxnsWbqXAR/RPhyQ4Sxe0f1aqtz7cPjf5Ec4m3PHtWhmU5VOPNYv6k3U3+lSXTTNZFP2aydybRatxYVg78Dws3HMhIouS8N9W/6v0eomtGRoX+S/cjN5JwflQ5KvldSt9qhBVGHwZ+W7HrlrfLGKphcFaxFWUpfNO5SBMLypPw2IMu2ZfDBh+WwGnOduRJqAASsJe+XzB8cOONKqsLEtfxLzfVRY71kXwqt3l9X2Ninu3qVr6ubyyuAgK5buKNzzZvfLBVGGziH4nQdeMxypi5hjSFci8RPp4jcCDJ/DANVFlRFG7rNr044rQ==</X509Certificate>
                </X509Data>
              </KeyInfo>
            </Signature>
          </Reinf>
        </evento>
      </loteEventos>
    </Reinf>

And after an envelope, it’s 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_00">
                    <loteEventos>
                        <evento id="ID1670710010000002018030117011200001">
                            <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_00">
                                <evtInfoContri id="ID1670710010000002018030117011200001">
                                    <ideEvento>
                                        <tpAmb>2</tpAmb>
                                        <procEmi>1</procEmi>
                                        <verProc>1</verProc>
                                    </ideEvento>
                                    <ideContri>
                                        <tpInsc>1</tpInsc>
                                        <nrInsc>67071001</nrInsc>
                                    </ideContri>
                                    <infoContri>
                                        <inclusao>
                                            <idePeriodo>
                                                <iniValid>2018-03</iniValid>
                                                <fimValid>2018-03</fimValid>
                                            </idePeriodo>
                                            <infoCadastro>
                                                <classTrib>99</classTrib>
                                                <indEscrituracao>1</indEscrituracao>
                                                <indDesoneracao>0</indDesoneracao>
                                                <indAcordoIsenMulta>0</indAcordoIsenMulta>
                                                <indSitPJ>0</indSitPJ>
                                                <contato>
                                                    <nmCtt>XXXXXXXXXX</nmCtt>
                                                    <cpfCtt>00000000000</cpfCtt>
                                                    <foneFixo>00000000000</foneFixo>
                                                </contato>
                                                <softHouse>
                                                    <cnpjSoftHouse>00000000000</cnpjSoftHouse>
                                                    <nmRazao>XXXXXXXXXX</nmRazao>
                                                    <nmCont>XXXXXXXXXX</nmCont>
                                                    <telefone>00000000000</telefone>
                                                    <email>XXXXXXXXXX</email>
                                                </softHouse>
                                            </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="#ID1670710010000002018030117011200001">
                                            <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>+Dm6c8v+ZhocqLQ5miRzTsuDnP5TnmwCAkhvWvxiFdI=</DigestValue>
                                        </Reference>
                                    </SignedInfo>
                                    <SignatureValue>ncq4zPSN5EXVYh6YvrdfJn4+jMYhrHR1tM6NStnfmlDZjxCcLFCHrXLxuG6awsEtnJULWSGwbm49uEEBcvY5LHs6iB0fPZOsMlyoaA8plHidK+UQmk0C37/TMGMd1ii/xTeU+XraVCtJIw1xIGLC3q7D2iAQykuUCBNSaIBVlS8rT03BrnG+XX+fkW/F668clm5UQfnsFcID5WfIQ9u85vYCwa/J4b+LjvtuqFx6CQJdYsw1EAV98fgEWxoktbBpuda+UfFroOPog83Ogh92TI8OaqVva7YMOPSoNreMQuQCPj4HctywCpIIo7Ak10H4n2zGAFAr/tfdm9jJX3GmDQ==</SignatureValue>
                                    <KeyInfo>
                                        <X509Data>
                                            <X509Certificate>MIIH2jCCBcKgAwIBAgIIWrdyPpsRHaUwDQYJKoZIhvcNAQELBQAwTDELMAkGA1UEBhMCQlIxEzARBgNVBAoTCklDUC1CcmFzaWwxKDAmBgNVBAMTH1NFUkFTQSBDZXJ0aWZpY2Fkb3JhIERpZ2l0YWwgdjIwHhcNMTcwNjE5MTgxMDAwWhcNMTgwNjE5MTgxMDAwWjCB+jELMAkGA1UEBhMCQlIxEzARBgNVBAoTCklDUC1CcmFzaWwxFDASBgNVBAsTCyhFTSBCUkFOQ08pMRgwFgYDVQQLEw8wMDAwMDEwMDc5MzA2MzUxFDASBgNVBAsTCyhFTSBCUkFOQ08pMRQ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9W/6v0eomtGRoX+S/cjN5JwflQ5KvldSt9qhBVGHwZ+W7HrlrfLGKphcFaxFWUpfNO5SBMLypPw2IMu2ZfDBh+WwGnOduRJqAASsJe+XzB8cOONKqsLEtfxLzfVRY71kXwqt3l9X2Ninu3qVr6ubyyuAgK5buKNzzZvfLBVGGziH4nQdeMxypi5hjSFci8RPp4jcCDJ/DANVFlRFG7rNr044rQ==</X509Certificate>
                                        </X509Data>
                                    </KeyInfo>
                                </Signature>
                            </Reinf>
                        </evento>
                    </loteEventos>
                </Reinf>
            </sped:loteEventos>
        </sped:ReceberLoteEventos>
    </soapenv:Body>
</soapenv:Envelope>

And then, the return I get from the webservice is this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <ReceberLoteEventosResponse xmlns="http://sped.fazenda.gov.br/">
         <ReceberLoteEventosResult>
            <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/retornoLoteEventos/v1_03_00">
               <retornoLoteEventos id="IDDD96452B198A6A0CC74D2068CBA00E26">
                  <ideTransmissor>
                     <IdTransmissor>88305859000150</IdTransmissor>
                  </ideTransmissor>
                  <status>
                     <cdStatus>0</cdStatus>
                     <descRetorno>SUCESSO</descRetorno>
                  </status>
                  <retornoEventos>
                     <evento id="ID1670710010000002018030117011200001">
                        <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtTotal/v1_03_00">
                           <evtTotal id="ID713166721">
                              <ideEvento>
                                 <perApur/>
                              </ideEvento>
                              <ideContri>
                                 <tpInsc>1</tpInsc>
                                 <nrInsc>67071001</nrInsc>
                              </ideContri>
                              <ideRecRetorno>
                                 <ideStatus>
                                    <cdRetorno>1</cdRetorno>
                                    <descRetorno>ERRO</descRetorno>
                                    <regOcorrs>
                                       <tpOcorr>1</tpOcorr>
                                       <localErroAviso/>
                                       <codResp>MS0017</codResp>
                                       <dscResp>Assinatura do evento inválida. Assinatura Digital do documento XML é inválida</dscResp>
                                    </regOcorrs>
                                 </ideStatus>
                              </ideRecRetorno>
                              <infoRecEv>
                                 <dhProcess>2018-03-15</dhProcess>
                                 <tpEv>1000</tpEv>
                                 <idEv>ID1670710010000002018030117011200001</idEv>
                                 <hash>+Dm6c8v+ZhocqLQ5miRzTsuDnP5TnmwCAkhvWvxiFdI=</hash>
                              </infoRecEv>
                           </evtTotal>
                           <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="#ID713166721">
                                    <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>MKt4YJZnZ0bIduOhNHJDtFJsJPTXxo69uxBOK/37a5E=</DigestValue>
                                 </Reference>
                              </SignedInfo>
                              <SignatureValue>cqglcHOmbiJqSigK8nGEspf1Z8joEhWu0R99axuLHTsS4fvzoKvLjEtissxCCgT6KGYtnkvagvypHMdwBkL4UgzJdc14rAYJtlIHVy9f8n/hN/Foda8MkPFTsGmh29TGamOiM8faSYL+Evj3j7v+97w1sZBGPQ0zcYxDt5a2Ksh/clP80drVXq9aTfz/tbJNy0W74p6tNANzfqTxT1OXdpKlOWzF7ULVE0oumnM4UrFlkdKssMZpmSmZaywiFyKry4MfvLJRL046wa2RbU28OWp5ScviGso+HCOa9TnxRWSGKHgiBf9Jl8CgsfbeB68ZzZMwzr2GQ0PLMX42nO9p+A==</SignatureValue>
                              <KeyInfo>
                                 <X509Data>
                                    <X509Certificate>MIIHcDCCBVigAwIBAgICTGkwDQYJKoZ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A3V4OlQRw7aAjt8Kikh/NjWYTfF/GZeEy90XCW8DbBmg4bSIdnaAds9Y6ne4CjO1fPILoDZRXBAzKrvBYyN6/GOd/u1nlMe3OvEPa8zpc565kxbUZZGFqPk5Tv4+11od7rJ+l/4KTj0ok0hRXnpEWQBsbSQ/YsFSOIOaID2uvyl/Q2C0YRbea+OrtmRgMqMAXWUn9TxMbAw2KgoV/V4/3Gv4QVZ/HRGPLidv9UVxf10n8imNwYrjo1focwIym254uUmyeRYcrCWh+UR0x3dwU5agb2uRNkoJ8aTGwpNNhlFdUDCwIulzi8EQRBIp1nuqEW7mQPOnH+PdKwAxd7ESfehnESjmrw0i55AZJF0rhn7CW8lxi2RXmjj9S</X509Certificate>
                                 </X509Data>
                              </KeyInfo>
                           </Signature>
                        </Reinf>
                     </evento>
                  </retornoEventos>
               </retornoLoteEventos>
            </Reinf>
         </ReceberLoteEventosResult>
      </ReceberLoteEventosResponse>
   </s:Body>
</s:Envelope>

I don’t know what else I can do to adjust it, I really appreciate the suggestions!

Thank you.

Sergio.

2 answers

2

I had the same problem, in my case I was able to solve, passing the namespace to the signature, because when the xml gets there in the IRS and their system deserializes the xml I sent, it puts the namespace and confronts how the xml I sent, then gives the error MS0017.

I’ll put down an article talking about it that helped me so much: EFD-Reinf: Invalid signature - Failed to verify XML document signature (using C#)

I will also put the method we created to sign any type of XML document, we made a dynamic method. Follows below:

//object xml é o XML que você deseja assinar.
//object Id é o ID do evento que você vai assinar. Por exemplo (R1000, R1070, R2010, etc...)

public static object SignXML(object xml, string Id, string prefixo, string nameSpace)
    {
        StringWriter sw = new StringWriter();
        System.Xml.Serialization.XmlSerializer x1 = new System.Xml.Serialization.XmlSerializer(xml.GetType());
        var stream = new MemoryStream();

        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

        //Para o sistema EFD Reinf, a variavel prefixo sempre será ""
        //A variavel nameSpace deve possuir o namespace que informa no manual do desenvolvedor.
        //Ex: Evento R-1000 o nameSpace atual é: http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_02
        ns.Add(prefixo, nameSpace);

        x1.Serialize(stream, xml, ns);

        var buffer = stream.GetBuffer();

        //A conversão deve ser UTF-8, caso não seja, será rejeitado pela receita federal
        var enc = Encoding.GetEncoding("UTF-8");
        var content = enc.GetString(buffer);
        System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
        xmldoc.LoadXml(content);

        RSACryptoServiceProvider key = new RSACryptoServiceProvider();
        SignedXml SignedDocument = default(SignedXml);
        KeyInfo keyInfo = new KeyInfo();

        //Localize seu certificado, o método Certificado.CertificateChoose é apenas
        //uma tela com um grid que lista todos os certificados instalados na maquina.
        //Ao dar duplo clique sobre a linha desejada, o sistema que você instala no
        //computador que gerencia os certificados pedirá a senha do mesmo automaticamente
        var cert = Certificado.CertificateChoose("", "My", StoreLocation.CurrentUser);
        if (cert != null)
        {
            try
            {
                key = (RSACryptoServiceProvider)cert.PrivateKey;
            }
            catch
            {
                return "CL"; //CL = Cartão não localizado na leitora
            }

            keyInfo.AddClause(new KeyInfoX509Data(cert, X509IncludeOption.EndCertOnly));

            SignedDocument = new SignedXml(xmldoc);

            SignedDocument.SigningKey = key;
            SignedDocument.KeyInfo = keyInfo;
            SignedDocument.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";

            Reference reference = new Reference();
            reference.Uri = "#" + Id;
            reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
            reference.AddTransform(new XmlDsigC14NTransform(false));
            reference.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256";

            SignedDocument.AddReference(reference);
            SignedDocument.ComputeSignature();

            System.Xml.XmlElement xmlDigitalSignature = SignedDocument.GetXml();
            xmldoc.DocumentElement.AppendChild(xmldoc.ImportNode(xmlDigitalSignature, true));

            XmlTextReader r = new XmlTextReader(new StringReader(xmldoc.OuterXml));

            object obj = new XmlSerializer(xml.GetType()).Deserialize(r);

            return obj;
        }
        else
            return null;
    }


//Este método retorna um objeto, ou seja, por ele ser dinâmico ele deverá retornar
//o mesmo tipo de arquivo que recebeu. Para instanciar o método eu fiz assim:

[...] Seu código C# do evento

//R1000 é a classe do evento R1000 preenchida com as informações do banco de dados.
//Id é o ID do Evento R1000 que deverá seguir a regra REGRA_VALIDA_ID_EVENTO do manual de orientações do EFD Reinf
var EFDAssinado = SignXML(R1000, Id,"", "http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_02");

PS.: It took me 3 days to get this kkk done

  • Sorry for the formatting of the text, this is the first time I put in Stackoverflow.

1


Sergio, from what I understand you are using the full XML, Batch + Event, to generate the signature:

<Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v1_03_00">
    <loteEventos>
        <evento id="ID1670710010000002018030117011200001">
            <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_00">
                <evtInfoContri id="ID1670710010000002018030117011200001">
                    [...]
                </evtInfoContri>
            </Reinf>
        </evento>
    </loteEventos>
</Reinf>

If that is so, it is indeed wrong. You must sign only the XML of the event. So, you should just pass this part of XML to your signature routine:

            <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_00">
                <evtInfoContri id="ID1670710010000002018030117011200001">
                    [...]
                </evtInfoContri>
            </Reinf>

After the signature is generated, you embed it at the end of the event’s XML (inside it):

        <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_00">
            <evtInfoContri id="ID1670710010000002018030117011200001">
                [...]
            </evtInfoContri>
            <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                [...]
            </Signature>            
        </Reinf>

Then you take this signed event XML and embed it into the batch XML:

<Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v1_03_00">
    <loteEventos>
        <evento id="ID1670710010000002018030117011200001">

            <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_00">
                <evtInfoContri id="ID1670710010000002018030117011200001">
                    [...]
                </evtInfoContri>
                <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                    [...]
                </Signature>            
            </Reinf>

        </evento>
    </loteEventos>
</Reinf>

In the case of the R-1000, Taxpayer Information, you will only have one event per lot, but if it were the case for an event like the R-2020, Rendered Services, you could have more than one event on the lot, then you would have to sign each event individually, and then embed each signed event in the batch:

<Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v1_03_00">
    <loteEventos>

        <!-- Primeiro evento do lote - INÍCIO -->
        <evento id="ID1670710010000002018030117011200001">

            <!-- XML do primeiro evento do lote, assinado - INÍCIO -->
            <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtPrestadorServicos/v1_03_00">
                <evtServPrest id="ID1670710010000002018030117011200001">
                    [...]
                </evtServPrest>
                <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                    [...]
                </Signature>            
            </Reinf>
            <!-- XML do primeiro evento do lote, assinado - FIM -->

        </evento>
        <!-- Primeiro evento do lote - FIM -->

        <!-- Segundo evento do lote - INÍCIO -->
        <evento id="ID1670710010000002018030117011200002">

            <!-- XML do segundo evento do lote, assinado - INÍCIO -->
            <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtPrestadorServicos/v1_03_00">
                <evtServPrest id="ID1670710010000002018030117011200002">
                    [...]
                </evtServPrest>
                <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                    [...]
                </Signature>            
            </Reinf>
            <!-- XML do segundo evento do lote, assinado - FIM -->

        </evento>
        <!-- Segundo evento do lote - FIM -->

    </loteEventos>
</Reinf>

Browser other questions tagged

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