Usuário com melhor resposta
Rejeicao: Assinatura difere do calculado

Pergunta
-
Olá estou assinando um xml para o servidor RecepcaoEvento, só assino as tags "evento" antes de enviar o xml para receita, ou seja, quando o xml já está todo montado, para não editar o documento após a assinatura, ficando nesse estilo:
<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Header> <nfeCabecMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/RecepcaoEvento"> <versaoDados>1.00</versaoDados> <cUF>32</cUF> </nfeCabecMsg> </soap12:Header> <soap12:Body> <nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/RecepcaoEvento"> <envEvento versao="1.00" xmlns="http://www.portalfiscal.inf.br/nfe"> <idLote>1</idLote> <evento versao="1.00"> <infEvento Id="ID2102104318039462374100017255001000513074100273529301"> <cOrgao>91</cOrgao> <tpAmb>1</tpAmb> <CNPJ>02016203000100</CNPJ> <chNFe>43180394623741000172550010005130741002735293</chNFe> <dhEvento>2018-06-14T09:29:22-03:00</dhEvento> <tpEvento>210210</tpEvento> <nSeqEvento>1</nSeqEvento> <verEvento>1.00</verEvento> <detEvento versao="1.00"> <descEvento>Ciencia da Operacao</descEvento> </detEvento> </infEvento> <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/2000/09/xmldsig#rsa-sha1" /> <Reference URI="#ID2102104318039462374100017255001000513074100273529301"> <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/2000/09/xmldsig#sha1" /> <DigestValue>------=</DigestValue> </Reference> </SignedInfo> <SignatureValue>xxxxxxxxxxxxxxx==</SignatureValue> <KeyInfo> <X509Data> <X509Certificate>xxxxxxxxxxxxxxxxxx==</X509Certificate> </X509Data> </KeyInfo> </Signature> </evento> </envEvento> </nfeDadosMsg> </soap12:Body> </soap12:Envelope>
o retorno que chega da receita é esse:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <nfeRecepcaoEventoResult xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/RecepcaoEvento"> <retEnvEvento versao="1.00" xmlns="http://www.portalfiscal.inf.br/nfe"> <idLote>1</idLote> <tpAmb>1</tpAmb> <verAplic>AN_1.0.0</verAplic> <cOrgao>91</cOrgao> <cStat>128</cStat> <xMotivo>Lote de evento processado</xMotivo> <retEvento versao="1.00"> <infEvento> <tpAmb>1</tpAmb> <verAplic>AN_1.0.0</verAplic> <cOrgao>91</cOrgao> <cStat>297</cStat> <xMotivo>Rejeicao: Assinatura difere do calculado</xMotivo> <chNFe>43180394623741000172550010005130741002735293</chNFe> <tpEvento>210210</tpEvento> <xEvento>Ciencia da Operacao</xEvento> <nSeqEvento>1</nSeqEvento> <dhRegEvento>2018-06-14T09:29:36-03:00</dhRegEvento> </infEvento> </retEvento> </retEnvEvento> </nfeRecepcaoEventoResult> </soap:Body> </soap:Envelope>
Será algum problema na hora de assinar? Eu uso o seguinte código para realizar a assinatura:
private XmlDocument assinarEventos(X509Certificate2 certificado, string envelope_eventos) { XmlDocument xml = new XmlDocument(); xml.LoadXml(envelope_eventos); XmlNodeList list_Evento = xml.GetElementsByTagName("evento"); if (certificado.HasPrivateKey && certificado.NotAfter > DateTime.Now && certificado.NotBefore < DateTime.Now) { foreach (XmlElement evento in list_Evento) { string id = evento.FirstChild.Attributes["Id"].Value; SignedXml signedXml = new SignedXml(evento); signedXml.SigningKey = certificado.PrivateKey; Reference reference = new Reference("#" + @id); reference.AddTransform(new XmlDsigEnvelopedSignatureTransform()); reference.AddTransform(new XmlDsigC14NTransform()); signedXml.AddReference(reference); signedXml.ComputeSignature(); //inclui cláusula com os dados do certificado KeyInfo keyInfo = new KeyInfo(); keyInfo.AddClause(new KeyInfoX509Data(certificado)); signedXml.KeyInfo = keyInfo; signedXml.ComputeSignature(); XmlElement xmlKeyInfo = signedXml.KeyInfo.GetXml(); XmlElement xmlSignature = xml.CreateElement("Signature", "http://www.w3.org/2000/09/xmldsig#"); XmlElement xmlSignedInfo = signedXml.SignedInfo.GetXml(); xmlSignature.AppendChild(xml.ImportNode(xmlSignedInfo, true)); XmlElement xmlSignatureValue = xml.CreateElement("SignatureValue", xmlSignature.NamespaceURI); string signBase64 = Convert.ToBase64String(signedXml.Signature.SignatureValue); XmlText text = xml.CreateTextNode(signBase64); xmlSignatureValue.AppendChild(text); xmlSignature.AppendChild(xmlSignatureValue); xmlSignature.AppendChild(xml.ImportNode(xmlKeyInfo, true)); evento.AppendChild(xmlSignature); } return xml; } else { throw new Exception("Certificado digital ausente ou vencido"); } }
Alguém já passou raiva com isso, teria como me dar uma luz? :(
Respostas
-
Depois de passar muita raiva descobri o problema:
Eu passei esse tempo todo tentando assinar quando o xml estava concluído em sua totalidade, mas daí usei o mesmo código pra assinar as tags "evento", apenas quando o xml possuia só a tag "envEvento" (xml abaixo), sem o cabeçalho e todo xml referente ao protocolo SOAP, então o servidor aceitou :lol: :lol: :lol:
Espero que isso sirva pra alguém no futuro.
<envEvento versao="1.00" xmlns="http://www.portalfiscal.inf.br/nfe"> <idLote>1</idLote> <evento versao="1.00"> <infEvento Id="ID2102104318039462374100017255001000513074100273529301"> <cOrgao>91</cOrgao> <tpAmb>1</tpAmb> <CNPJ>02016203000100</CNPJ> <chNFe>43180394623741000172550010005130741002735293</chNFe> <dhEvento>2018-06-14T09:29:22-03:00</dhEvento> <tpEvento>210210</tpEvento> <nSeqEvento>1</nSeqEvento> <verEvento>1.00</verEvento> <detEvento versao="1.00"> <descEvento>Ciencia da Operacao</descEvento> </detEvento> </infEvento> <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/2000/09/xmldsig#rsa-sha1" /> <Reference URI="#ID2102104318039462374100017255001000513074100273529301"> <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/2000/09/xmldsig#sha1" /> <DigestValue>------=</DigestValue> </Reference> </SignedInfo> <SignatureValue>xxxxxxxxxxxxxxx==</SignatureValue> <KeyInfo> <X509Data> <X509Certificate>xxxxxxxxxxxxxxxxxx==</X509Certificate> </X509Data> </KeyInfo> </Signature> </evento> </envEvento>
- Marcado como Resposta Rafael dos Santos Gomes terça-feira, 19 de junho de 2018 14:03