FórumCategoria: Fórum - Perguntas e RespostasComo ler a assinatura digital e incorporar no XML SPED REINF
Mauricio Carlos perguntou há 7 anos

Boa tarde pessoal,
Olá pesssoal,
Sou de uma empresa de TI, onde hoje já criamos o XML do REINF, porém, não montamos a assinatura digital no corpo do XML para envio.
Onde acho instruções técnicas para desenvolver isso?
Existe algum software que esteja realizando isto gratuitamente? Ou seja, pega o XML, le a assintura e inclui no XML e envia?
 
Obrigado!
Mauricio.
 

2 Respostas
Melhor resposta
Marcos respondeu há 7 anos

1)correto
2)é o elemento onde está o id. no caso do R1000 o elemento é o “evtInfoContri”, para o R1070 é o “evtTabProcesso” e assim por diante.
3)p_certificado_digital: vc pega atraves da rotina abaixo:
vc passa para o parâmetro o retorno da função
private X509Certificate2 SelecionarCertificado()
{
X509Certificate2 v_certificado = new X509Certificate2();
try
{
X509Certificate2Collection v_certificados = null;
X509Store v_store = new X509Store(“MY”, StoreLocation.CurrentUser);
v_store.Open(OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = v_store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, true).Find(X509FindType.FindByKeyUsage, X509KeyUsageFlags.DigitalSignature, true);
v_certificados = X509Certificate2UI.SelectFromCollection(certificates, “Certificados Digitais”, “Selecione o Certificado Digital para uso no aplicativo”, X509SelectionFlag.SingleSelection);
if ((v_certificados.Count == 0))
{
v_certificado.Reset();
throw new System.Exception(“Nenhum certificado digital foi selecionado ou o certificado selecionado está com problemas.”);
}
else
{
v_certificado = v_certificados[0];
}
v_store.Close();
}
catch (Exception ex)
{
throw ex;
}
return v_certificado;
}
 

Mauricio Carlos respondeu há 7 anos

PRONTO! basta substituir o * por um <

Obrigado!

Mauricio Carlos respondeu há 7 anos

*?xml version=”1.0″ encoding=”UTF-8″?>
*Reinf xmlns=”http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v1_01_01″>
*loteEventos>
*evento id=”ID192156926000199030520181751100001″>
*Reinf xmlns=”http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_01_01″>
*evtInfoContri id=”ID192156926000199030520181751100001″>
*ideEvento>
*tpAmb>1*/tpAmb>
*procEmi>1*/procEmi>
*verProc>*/verProc>
*/ideEvento>
*ideContri>
*tpInsc>1*/tpInsc>
*nrInsc>92156926000199*/nrInsc>
*/ideContri>
*infoContri>
*inclusao>
*idePeriodo>
*iniValid>2023*/iniValid>
*fimValid>2023*/fimValid>
*/idePeriodo>
*infoCadastro>
*classTrib>99*/classTrib>
*indEscrituracao>0*/indEscrituracao>
*indDesoneracao>0*/indDesoneracao>
*indAcordoIsenMulta>0*/indAcordoIsenMulta>
*indSitPJ>0*/indSitPJ>
*contato>
*nmCtt>*/nmCtt>
*cpfCtt>234234234*/cpfCtt>
*foneFixo>234234234*/foneFixo>
*foneCel>234324324*/foneCel>
*email>[email protected]*/email>
*/contato>
*softHouse>
*cnpjSoftHouse>10686199000107*/cnpjSoftHouse>
*nmRazao>3R INFORMATICA LTDA*/nmRazao>
*nmCont>RICARDO*/nmCont>
*telefone>3344061*/telefone>
*email>[email protected]*/email>
*/softHouse>
*/infoCadastro>
*/inclusao>
*/infoContri>
*/evtInfoContri>
*/Reinf>
*/evento>
*/loteEventos>
*/Reinf>

Mauricio Carlos respondeu há 7 anos

<!—

1
1

1
92156926000199

2023
2023

99
0
0
0
0

234234234
234234234
234324324
[email protected]

10686199000107
3R INFORMATICA LTDA
RICARDO
3344061
[email protected]

—>

Mauricio Carlos respondeu há 7 anos

1
1

1
92156926000199

2023
2023

99
0
0
0
0

234234234
234234234
234324324
[email protected]

10686199000107
3R INFORMATICA LTDA
RICARDO
3344061
[email protected]

Mauricio Carlos respondeu há 7 anos

xml version=”1.0″ encoding=”UTF-8″?>

1
1

1
92156926000199

2023
2023

99
0
0
0
0

234234234
234234234
234324324
[email protected]

10686199000107
3R INFORMATICA LTDA
RICARDO
3344061
[email protected]

</Reinf

Mauricio Carlos respondeu há 7 anos

está interpretando o xml….

Mauricio Carlos respondeu há 7 anos

/*

1
1

1
92156926000199

2023
2023

99
0
0
0
0

234234234
234234234
234324324
[email protected]

10686199000107
3R INFORMATICA LTDA
RICARDO
3344061
[email protected]

*/

Mauricio Carlos respondeu há 7 anos

1
1

1
92156926000199

2023
2023

99
0
0
0
0

234234234
234234234
234324324
[email protected]

10686199000107
3R INFORMATICA LTDA
RICARDO
3344061
[email protected]

Mauricio Carlos respondeu há 7 anos

Olá Amigo. Reproduzi o código e está funcionando parcial.
Como quero mandar evento por evento, queria mandar um xml para abertura, um para cada nota gerada e um de fechamento.
Sei que preciso assinar todos eles, porém no de abertura está me dando erro.
segue o código. Poderia validar? Obrigado.

1
1

1
92156926000199

2023
2023

99
0
0
0
0

234234234
234234234
234324324
[email protected]

10686199000107
3R INFORMATICA LTDA
RICARDO
3344061
[email protected]

Mauricio Carlos respondeu há 7 anos

Obrigado Amigo. Amanhã mesmo vou iniciar o desenv.
Qualquer coisa volto, aqui 🙂

Obrigado.

Marcos respondeu há 7 anos

Que linguagem vc está utilizando?

Mauricio Carlos respondeu há 7 anos

Para os parâmetros abaixo:
private XmlDocument AssinarXmlSha256(XmlDocument p_xml_document, string p_uri, X509Certificate2 p_certificado_digital)

1)p_xml_document: seria o xml com todos dados pronto para entrega, exceto sem assinatura, certo?
2)p_uri: o que seria? onde pego?
3)p_certificado_digital: como pego este número? supondo que tenho o certificado digital (A1 ou A3).

Mauricio Carlos respondeu há 7 anos

Acredita que o que preciso seja esta implementação?
coloca assinatura digital no XML.
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures

Marcos respondeu há 7 anos

Tem o manual do desenvolvedor: http://sped.rfb.gov.br/pasta/show/2225

Mauricio Carlos respondeu há 7 anos

Obrigado, mas está um pouco amplo. não tens uma manual ou algo assim?
Obrigado mais uma vez.

Marcos respondeu há 7 anos

Essa é a rotina que assina o documento:

No parametro vc passa o documento Xml, a elemento onde está o ID (por exemplo “evtInfoContri”) e o certificado digital:

private XmlDocument AssinarXmlSha256(XmlDocument p_xml_document, string p_uri, X509Certificate2 p_certificado_digital)
{
XmlElement xmlDigitalSignature = null;

// Create a new XML document.
XmlDocument v_xml_document = new XmlDocument();

try
{
// Format the document to ignore white spaces.
v_xml_document.PreserveWhitespace = false;

// Load the passed XML file using it’s name.
v_xml_document = p_xml_document;

// Create a SignedXml object.
SignedXml signedXml = new SignedXml(v_xml_document);

// Add the key to the SignedXml document.
signedXml.SigningKey = p_certificado_digital.GetRSAPrivateKey();
signedXml.SignedInfo.SignatureMethod = “http://www.w3.org/2001/04/xmldsig-more#rsa-sha256”;

// Create a reference to be signed.
Reference referencia = new Reference();

// pega o uri que deve ser assinada
if (p_uri != “”)
{
XmlAttributeCollection _Uri = v_xml_document.GetElementsByTagName(p_uri).Item(0).Attributes;

foreach (XmlAttribute _atributo in _Uri)
{
if (_atributo.Name == “id”)
{
referencia.Uri = “#” + _atributo.InnerText;
}
}
}
else
{
referencia.Uri = “”;
}

// Add an enveloped transformation to the reference.
referencia.AddTransform(new XmlDsigEnvelopedSignatureTransform());
referencia.AddTransform(new XmlDsigC14NTransform());
referencia.DigestMethod = “http://www.w3.org/2001/04/xmlenc#sha256”;

// Add the reference to the SignedXml object.
signedXml.AddReference(referencia);

// Create a new KeyInfo object.
System.Security.Cryptography.Xml.KeyInfo keyInfo = new System.Security.Cryptography.Xml.KeyInfo();

// Load the certificate into a KeyInfoX509Data object
// and add it to the KeyInfo object.
keyInfo.AddClause(new KeyInfoX509Data(p_certificado_digital));

// Add the KeyInfo object to the SignedXml object.
signedXml.KeyInfo = keyInfo;

// Compute the signature.
signedXml.ComputeSignature();

// Get the XML representation of the signature and save
// it to an XmlElement object.
xmlDigitalSignature = signedXml.GetXml();

// Append the element to the XML document.
v_xml_document.DocumentElement.AppendChild(v_xml_document.ImportNode(xmlDigitalSignature, true));

if (v_xml_document.FirstChild is XmlDeclaration)
{
v_xml_document.RemoveChild(v_xml_document.FirstChild);
}
//mensagem = “Certificado Digital adicionado com sucesso.”;
}
catch (Exception ex)
{
throw ex;
}

return v_xml_document;
}

Mauricio Carlos respondeu há 7 anos

Gero o XML em VB, mas não teria problema em realizar esta parte no c#