All good?
I'm trying to make the EFD-REINF digital signature shell (the most recent version 1.03.02 ) of the event XML in this case I'm testing with the R1000) with .Net C # , but framework 4.0 .
I do not really know if I'm doing something wrong, but I've tried several things to try signing the event xml and I can not, I'm always getting the MS0017 - Invalid event signature. Digital signature of XML document is invalid .
Look below, it's the code I'm using to sign the event block.
public static System.Xml.XmlElement XmlAssinado(object reinf, string cpfCnpj, string id)
{
var certificate = GetCertificado(cpfCnpj);
var xml = reinf.ToXmlString();
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
if (xmlDoc.FirstChild is XmlDeclaration)
xmlDoc.RemoveChild(xmlDoc.FirstChild);
//if (xmlDoc.FirstChild.Attributes["xmlns:xsi"] != null)
// xmlDoc.FirstChild.Attributes.Remove(xmlDoc.FirstChild.Attributes["xmlns:xsi"]);
//if (xmlDoc.FirstChild.Attributes["xmlns:xsd"] != null)
// xmlDoc.FirstChild.Attributes.Remove(xmlDoc.FirstChild.Attributes["xmlns:xsd"]);
Reference reference = new Reference($"#{id}");
// 1 - Transformação exigida - Envelop
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// 2 - Transformação exigida - C14N
XmlDsigC14NTransform c14 = new XmlDsigC14NTransform();
reference.AddTransform(c14);
// Message Digest
reference.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256";
// Load the certificate into a KeyInfoX509Data object
// and add it to the KeyInfo object.
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new KeyInfoX509Data(certificate));
//XmlDocument documentoUTF8 = ReconstruirComoUTF8(xmlDoc);
// Instancia a classe de criptografia
//var signedXml = new SignedXml(documentoUTF8);
var signedXml = new SignedXml(xmlDoc);
// Define as propriedades da assinatura
// Adiciona a chave ao documento assinado
RSACryptoServiceProvider key = new RSACryptoServiceProvider();
key.FromXmlString(certificate.PrivateKey.ToXmlString(true));
signedXml.SigningKey = key; //.GetRSAPrivateKey();http://www.w3.org/2000/09/xmldsig#rsa-sha1
// Adiciona a referência ao documento assinado
signedXml.AddReference(reference);
signedXml.KeyInfo = keyInfo;
signedXml.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
// Gera a assinatura
signedXml.ComputeSignature();
// Obtém a representação em XML da assinatura e salva em um objeto XmlElement
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
if (xmlDoc.FirstChild is XmlDeclaration)
xmlDoc.RemoveChild(xmlDoc.FirstChild);
return xmlDoc.DocumentElement;
}
This object is the structure of the event in Object .
Below is the output xml of how XML is getting:
<Reinf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v1_03_02">
<loteEventos>
<evento id="ID1111111110000002017102714215100001">
<Reinf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_02">
<evtInfoContri id="ID1111111110000002017102714215100001">
<ideEvento>
<tpAmb>2</tpAmb>
<procEmi>1</procEmi>
<verProc>SAP ECC 6.0</verProc>
</ideEvento>
<ideContri>
<tpInsc>1</tpInsc>
<nrInsc>15622088000150</nrInsc>
</ideContri>
<infoContri>
<inclusao>
<idePeriodo>
<iniValid>2018-01</iniValid>
</idePeriodo>
<infoCadastro>
<classTrib>99</classTrib>
<indEscrituracao>1</indEscrituracao>
<indDesoneracao>0</indDesoneracao>
<indAcordoIsenMulta>0</indAcordoIsenMulta>
<contato>
<nmCtt>xxxxxxxxxxx</nmCtt>
<cpfCtt>111111111</cpfCtt>
<foneFixo>11111111111</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="#ID1111111110000002017102714215100001">
<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>yBmhSIzReZE0+WLAqgxjoSqq2tZZEThdZIHx5uRiffI=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>gF/YxdCbbPM8dd0qdBdcnALwk0zvMRWSxLqqdda/qbeYFDQ30/1m38oO80EXeZgab1e79SxLYDwOQleWknUKFxa5r+u+3mmCdy2Bgq0YJxdZ16PSEQnVTwDJDguXMyaWY76OoqIQdcqm35m11hmPRJZtkuKatgBce/pksWDZqI/2GdEUrWFPx+FB6Luto9IFt3RRxon6gQN6M3jsmd8c3BMu34TCD53JfgyTm1WqQBNtGj+fLYbC/eyItG2DI+8x3mb4WtKS3KqDUSqzZvOKj9GKTtg92+TsJurJrVsq3CZVGgClvcTCOsmP0ByES1ninFDaMEeFPKOzz8Mirz2Xsw==</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>MIIHrzCCBZegAwIBAgIIH5Hxrxq9rGAwDQYJKoZIhvcNAQELBQAwdTELMAkGA1UEBhMCQlIxEzARBgNVBAoMCklDUC1CcmFzaWwxNjA0BgNVBAsMLVNlY3JldGFyaWEgZGEgUmVjZWl0YSBGZWRlcmFsIGRvIEJyYXNpbCAtIFJGQjEZMBcGA1UEAwwQQUMgU0VSQVNBIFJGQiB2NTAeFw0xNzEyMDQxODE3MDBaFw0xODEyMDQxODE3MDBaMIHgMQswCQYDVQQGEwJCUjELMAkGA1UECAwCU1AxEDAOBgNVBAcMB0JBUlVFUkkxEzARBgNVBAoMCklDUC1CcmFza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ae5aB/R3+lDwWyAfocxoGPuOj1lzCErkjYSyetctYLx+bJQ+tNVTSqNGR/UBlKFks7zVZh/9DqbDDKNjPMHx0CbRCOO5Y7jgcVLV7EluIcyulQ/7R6P6Lu05nji2RQC9VI0gxA+gFXPnPsBMmkKNRg24rtIQfhftJ4aHC4W+tumAlPLQkJlQvKM</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</Reinf>
</evento>
</loteEventos>
If someone can help me, I thank them immensely! Very grateful already.