1

O problema que cito abaixo é semelhante ao problema do post deste link:

REINF - Assinatura Inválida. Falha ao verificar a assinatura do documento XML. Reinf usando c#

O que ocorre é que estou testando o envio de um evento para o REINF e estou recebendo o retorno abaixo do webservice:

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

Meu XML antes de assinar é este:

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

Após a assinatura está assim:

    <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/etDmV5I7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UdDwEB/wQEAwIF4DAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwQwgbgGA1UdEQSBsDCBrYEXTUFSQ09TLlRIT01BWkBVTklGWS5DT02gPgYFYEwBAwSgNRMzMjgxMDE5NzcyNjY5MjM3NDg3MTAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwoB4GBWBMAQMCoBUTE01BUkNPUyBDRVNBUiBUSE9NQVqgGQYFYEwBAwOgEBMONjcwNzEwMDEwMDAxMDagFwYFYEwBAwegDhMMMDAwMDAwMDAwMDAwMA0GCSqGSIb3DQEBCwUAA4ICAQATHgfi7caoxqobwfXRcBhZ7LkVO3GaOBYE92L/KAoWoZR7m2vREbo7mfhXIx2VZXR509Nr6FIPT28EeKAaOR6ZwonykVpicGklkZjUmDgXTb4jtaCNAQZl347Qf+81LEHHwtrL2alhr+hvxXL2Q+UzKTrqtxua4Ta96lF2s7ubcrTnsiHNPSu8cZSzirtsYIm0ExIkNVQpH3xiCIcQIkuWPAjBAuuQtAUK1klmEcNQYz01l5krDIbLiYzwUJXG7CP9wDZvn8xPFzTr9XFgGglb3fy2OKFiSoaB80z90w3Sd1opiuojANEJrQV64A47fSa6BnidA97e5Dtk+GWtbJ0vPoT9/+3HAahJFq5Uq3btghQ3IXxvRSxnsWbqXAR/RPhyQ4Sxe0f1aqtz7cPjf5Ec4m3PHtWhmU5VOPNYv6k3U3+lSXTTNZFP2aydybRatxYVg78Dws3HMhIouS8N9W/6v0eomtGRoX+S/cjN5JwflQ5KvldSt9qhBVGHwZ+W7HrlrfLGKphcFaxFWUpfNO5SBMLypPw2IMu2ZfDBh+WwGnOduRJqAASsJe+XzB8cOONKqsLEtfxLzfVRY71kXwqt3l9X2Ninu3qVr6ubyyuAgK5buKNzzZvfLBVGGziH4nQdeMxypi5hjSFci8RPp4jcCDJ/DANVFlRFG7rNr044rQ==</X509Certificate>
                </X509Data>
              </KeyInfo>
            </Signature>
          </Reinf>
        </evento>
      </loteEventos>
    </Reinf>

E depois de envelopado, está assim:

<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+jELMAkGA1UEBhMCQlIxEzARBgNVBAoTCklDUC1CcmFzaWwxFDASBgNVBAsTCyhFTSBCUkFOQ08pMRgwFgYDVQQLEw8wMDAwMDEwMDc5MzA2MzUxFDASBgNVBAsTCyhFTSBCUkFOQ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IouS8N9W/6v0eomtGRoX+S/cjN5JwflQ5KvldSt9qhBVGHwZ+W7HrlrfLGKphcFaxFWUpfNO5SBMLypPw2IMu2ZfDBh+WwGnOduRJqAASsJe+XzB8cOONKqsLEtfxLzfVRY71kXwqt3l9X2Ninu3qVr6ubyyuAgK5buKNzzZvfLBVGGziH4nQdeMxypi5hjSFci8RPp4jcCDJ/DANVFlRFG7rNr044rQ==</X509Certificate>
                                        </X509Data>
                                    </KeyInfo>
                                </Signature>
                            </Reinf>
                        </evento>
                    </loteEventos>
                </Reinf>
            </sped:loteEventos>
        </sped:ReceberLoteEventos>
    </soapenv:Body>
</soapenv:Envelope>

E então, o retorno que recebo do webservice é este:

<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>MIIHcDCCBVigAwIBAgICTGk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hgwowxX7JA3V4OlQRw7aAjt8Kikh/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>

Não sei mais o que posso fazer para ajustar isso, agradeço muito as sugestões!

Obrigado.

Sérgio.

2 Respostas 2

1

Sérgio, pelo que eu entendi você está usando o XML completo, Lote + Evento, para gerar a assinatura:

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

Se for isso mesmo, está errado de fato. Você deve assinar apenas o XML do evento. Então, você deveria passar apenas essa parte do XML para sua rotina de assinatura:

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

Depois de gerada a assinatura, você embute ela no final do XML do evento (dentro dele):

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

Depois você pega esse XML do evento assinado e embute no XML do lote:

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

No caso do R-1000, Informações do Contribuinte, você só terá um evento por lote, mas, se fosse o caso de um evento como o R-2020, Serviços Prestados, você poderia ter mais de um evento no lote, então, você teria que assinar cada evento individualmente, para depois embutir cada evento assinado no lote:

<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>
1

Pessoal tive o mesmo problema, no meu caso eu consegui resolver passando o namespace para a assinatura, pois, quando o XML chega lá na Receita Federal, e o sistema deles deserializa o XML que enviei, o mesmo coloca o namespace e confronta como o XML que enviei, aí dá o erro MS0017.

Vou colocar abaixo um artigo falando sobre isso que me ajudou demais: EFD-Reinf: Assinatura inválida - Falha ao verificar a assinatura do documento XML (usando C#)

Vou colocar também o método que criamos para assinar qualquer tipo de documento XML, fizemos um método dinâmico. Segue abaixo:

//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: Demorei 3 dias para conseguir fazer isso!

0

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .