Fórum Assinar Digitalmente XML #727
24/11/2008
0
Preciso assinar digitalmente o arquivo XML abaixo da seguinte forma.
Para cada tag infCTe devo ter uma assinatura digital.
O codigo para gerar a assinatura digital consegui fazer e esta funcionando, porém não consigo posicionar o XmlElement assinado junto a tag infCTe, somente consigo gerar as tags de assinatura no fim do arquivo.
Não estou encontrando o metodo que faça o append exatamente na posição que quero.
Preciso que seja como abaixo
<?xml version="1.0" encoding="UTF-8"?>
<enviCTe xmlns="http://www.portalfiscal.inf.br/cte" versao="1.01">
<idLote>200602220000001</idLote>
<CTe xmlns="http://www.portalfiscal.inf.br/cte">
<infCTe Id="CTe31060243816719000108650000000010001234567890" versao="1.01">
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
</CTe>
</enviCTe>
Note que a assinatura esta dentro do node <CTe> e no mesmo nivel de <infCte>.
Abaixo o meu codigo C# e XML exemplo.
public void AssinaDocumento(X509Certificate2 certificadoDigital, string Uri, string xmlOrigem)
{
int countTagsUri;
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
XmlNodeList xmlNodeList;
Reference reference = new Reference();
xmlDocument.Load(xmlOrigem);
XmlDocument xmlAssinado = xmlDocument;
SignedXml signedXml = new SignedXml(xmlDocument);
signedXml.SigningKey = certificadoDigital.PrivateKey;
countTagsUri = xmlDocument.GetElementsByTagName(Uri).Count;
if (countTagsUri == 0)
{
throw new Exception("Uri " + Uri + " não encontrada no XML");
}
xmlNodeList = xmlDocument.GetElementsByTagName(Uri);
foreach (XmlNode xmlnl in xmlNodeList)
{
XmlAttributeCollection attributeCollection = xmlnl.Attributes;
reference.Uri = "#" + attributeCollection["Id"].InnerText;
string res = xmlnl.OuterXml;
XmlDsigEnvelopedSignatureTransform envelope = new XmlDsigEnvelopedSignatureTransform();
XmlDsigC14NTransform c14NTransform = new XmlDsigC14NTransform();
KeyInfo keyInfo = new KeyInfo();
reference.AddTransform(envelope);
reference.AddTransform(c14NTransform);
signedXml.AddReference(reference);
keyInfo.AddClause(new KeyInfoX509Data(certificadoDigital));
signedXml.KeyInfo = keyInfo;
signedXml.ComputeSignature();
XmlElement xmlDigitalSignature = signedXml.GetXml();
xmlDocument.DocumentElement.AppendChild(xmlDocument.ImportNode(xmlDigitalSignature, true));
xmlDocument.PreserveWhitespace = true;
xmlDocument.Save(@"c:\assinado.xml");
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<enviCTe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" versao="1.02" xmlns="http://www.portalfiscal.inf.br/cte">
<idLote>000000001</idLote>
<CTe>
<infCte Id="CTe430811882462510001005700000030167800030167868" versao="1.02">
<ide>
<cUF>43</cUF>
<cCT>000301678</cCT>
<CFOP>5352</CFOP>
<natOp>RESTAçãO DE SERVIçO DE TRANSPORTE A ESTABELECIMENTO INDUSTRI</natOp>
<forPag>1</forPag>
<mod>57</mod>
<serie>0</serie>
<nCT>301678</nCT>
<dhEmi>2008-11-11T00:03:00</dhEmi>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>8</cDV>
<tpAmb>1</tpAmb>
<tpCTe>0</tpCTe>
<procEmi>0</procEmi>
<verProc>07.07</verProc>
<cMunEmi>4314902</cMunEmi>
<xMunEmi>PORTO ALEGRE</xMunEmi>
<UFEmi>RS</UFEmi>
<modal>01</modal>
<tpServ>1</tpServ>
<cMunIni>4309209</cMunIni>
<xMunIni>GRAVATAI</xMunIni>
<UFIni>RS</UFIni>
<cMunFim>4313904</cMunFim>
<xMunFim>PANAMBI</xMunFim>
<UFFim>RS</UFFim>
<retira>1</retira>
<toma03>
<toma>0</toma>
</toma03>
</ide>
<emit>
<CNPJ>88246251000100</CNPJ>
<IE>0962047104</IE>
<xNome>TRANSEICH ASSESSORIA E TRANSPORTES LTDA</xNome>
<enderEmit>
<xLgr>AVENIDA DAS INDUSTRIAS</xLgr>
<nro>389</nro>
<xBairro>ANCHIETA</xBairro>
<cMun>4314902</cMun>
<xMun>PORTO ALEGRE</xMun>
<UF>RS</UF>
<xPais>BRASIL</xPais>
<fone>05130231000</fone>
</enderEmit>
</emit>
<vPrest>
<vTPrest>9.86</vTPrest>
<vRec>9.86</vRec>
</vPrest>
<imp>
<ICMS>
<CST45>
<CST>40</CST>
</CST45>
</ICMS>
<infAdFisco>ICMS ISENTO CFE. LIVRO I ART.10 , INC. IX DEC. 37.699/97 DO RICMS.</infAdFisco>
</imp>
<infCTeNorm>
<infCarga>
<vMerc>2465.67</vMerc>
<proPred>DVS</proPred>
<infQ>
<cUnid>03</cUnid>
<tpMed>VLS</tpMed>
<qCarga>7</qCarga>
</infQ>
</infCarga>
<rodo>
<RNTRC>07082200018570</RNTRC>
<dPrev>2008-11-13</dPrev>
<lota>0</lota>
</rodo>
</infCTeNorm>
</infCte>
</CTe>
<CTe>
<infCte Id="CTe430811882462510001005700000030167900030167919" versao="1.02">
<ide>
<cUF>43</cUF>
<cCT>000301679</cCT>
<CFOP>6352</CFOP>
<natOp>RESTAçãO DE SERVIçO DE TRANSPORTE A ESTABELECIMENTO INDUSTRI</natOp>
<forPag>1</forPag>
<mod>57</mod>
<serie>0</serie>
<nCT>301679</nCT>
<dhEmi>2008-11-11T00:06:00</dhEmi>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>9</cDV>
<tpAmb>1</tpAmb>
<tpCTe>0</tpCTe>
<procEmi>0</procEmi>
<verProc>07.07</verProc>
<cMunEmi>4314902</cMunEmi>
<xMunEmi>PORTO ALEGRE</xMunEmi>
<UFEmi>RS</UFEmi>
<modal>01</modal>
<tpServ>1</tpServ>
<cMunIni>4309209</cMunIni>
<xMunIni>GRAVATAI</xMunIni>
<UFIni>RS</UFIni>
<cMunFim>1302603</cMunFim>
<xMunFim>MANAUS</xMunFim>
<UFFim>AM</UFFim>
<retira>1</retira>
<toma03>
<toma>0</toma>
</toma03>
</ide>
<emit>
<CNPJ>88246251000100</CNPJ>
<IE>0962047104</IE>
<xNome>TRANSEICH ASSESSORIA E TRANSPORTES LTDA</xNome>
<enderEmit>
<xLgr>AVENIDA DAS INDUSTRIAS</xLgr>
<nro>389</nro>
<xBairro>ANCHIETA</xBairro>
<cMun>4314902</cMun>
<xMun>PORTO ALEGRE</xMun>
<UF>RS</UF>
<xPais>BRASIL</xPais>
<fone>05130231000</fone>
</enderEmit>
</emit>
<vPrest>
<vTPrest>20.77</vTPrest>
<vRec>20.77</vRec>
</vPrest>
<imp>
<ICMS>
<CST45>
<CST>40</CST>
</CST45>
</ICMS>
<infAdFisco>ICMS ISENTO CFE. LIVRO I ART.10 , INC. IX DEC. 37.699/97 DO RICMS.</infAdFisco>
</imp>
<infCTeNorm>
<infCarga>
<vMerc>1122.70</vMerc>
<proPred>DVS</proPred>
<infQ>
<cUnid>03</cUnid>
<tpMed>VLS</tpMed>
<qCarga>2</qCarga>
</infQ>
</infCarga>
<rodo>
<RNTRC>07082200018570</RNTRC>
<dPrev>2008-12-03</dPrev>
<lota>0</lota>
</rodo>
</infCTeNorm>
</infCte>
</CTe>
</enviCTe>
Alessandro Pedroso
Curtir tópico
+ 0Posts
24/11/2008
[devmedia .net]
Gostei + 0
24/11/2008
Alessandro Pedroso
So preciso saber colocar a tag <ds:Signature> abaixo de infCTe conforme o XML abaixo
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2008 rel. 2 (http://www.altova.com)-->
<enviCTe versao="1.02" xsi:schemaLocation="http://www.portalfiscal.inf.br/cte enviCte_v1.02.xsd" xmlns="http://www.portalfiscal.inf.br/cte" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<idLote>0</idLote>
<CTe>
<infCte Id="ID_1" versao="1.02">
<ide>
<cUF>11</cUF>
<cCT>000000000</cCT>
<CFOP>1001</CFOP>
<natOp>a</natOp>
<forPag>0</forPag>
<mod>57</mod>
<serie>0</serie>
<nCT>1</nCT>
<dhEmi>2000-02-29T20:00:00</dhEmi>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>0</cDV>
<tpAmb>1</tpAmb>
<tpCTe>0</tpCTe>
<procEmi>0</procEmi>
<verProc>a</verProc>
<cMunEmi>0000000</cMunEmi>
<xMunEmi>a</xMunEmi>
<UFEmi>AC</UFEmi>
<modal>01</modal>
<tpServ>0</tpServ>
<cMunIni>0000000</cMunIni>
<xMunIni>a</xMunIni>
<UFIni>AC</UFIni>
<cMunFim>0000000</cMunFim>
<xMunFim>a</xMunFim>
<UFFim>AC</UFFim>
<retira>0</retira>
<toma03>
<toma>0</toma>
</toma03>
</ide>
<emit>
<CNPJ>00000000000000</CNPJ>
<IE>00</IE>
<xNome>!</xNome>
<enderEmit>
<xLgr>!</xLgr>
<nro>!</nro>
<xBairro>!</xBairro>
<cMun>0000000</cMun>
<xMun>!</xMun>
<UF>AC</UF>
</enderEmit>
</emit>
<vPrest>
<vTPrest>0</vTPrest>
<vRec>0</vRec>
</vPrest>
<imp>
<ICMS>
<CST00>
<CST>00</CST>
<vBC>0</vBC>
<pICMS>0</pICMS>
<vICMS>0</vICMS>
</CST00>
</ICMS>
</imp>
<infCTeNorm>
<infCarga>
<vMerc>0</vMerc>
<proPred>a</proPred>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
</infCarga>
<rodo>
<RNTRC>00000000000000</RNTRC>
<dPrev>2000-02-29</dPrev>
<lota>0</lota>
</rodo>
</infCTeNorm>
</infCte>
<ds:Signature>
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="aa">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
</CTe>
<CTe>
<infCte Id="ID_2" versao="1.02">
<ide>
<cUF>11</cUF>
<cCT>000000000</cCT>
<CFOP>1001</CFOP>
<natOp>a</natOp>
<forPag>0</forPag>
<mod>57</mod>
<serie>0</serie>
<nCT>1</nCT>
<dhEmi>2000-02-29T20:00:00</dhEmi>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>0</cDV>
<tpAmb>1</tpAmb>
<tpCTe>0</tpCTe>
<procEmi>0</procEmi>
<verProc>a</verProc>
<cMunEmi>0000000</cMunEmi>
<xMunEmi>a</xMunEmi>
<UFEmi>AC</UFEmi>
<modal>01</modal>
<tpServ>0</tpServ>
<cMunIni>0000000</cMunIni>
<xMunIni>a</xMunIni>
<UFIni>AC</UFIni>
<cMunFim>0000000</cMunFim>
<xMunFim>a</xMunFim>
<UFFim>AC</UFFim>
<retira>0</retira>
<toma03>
<toma>0</toma>
</toma03>
</ide>
<emit>
<CNPJ>00000000000000</CNPJ>
<IE>00</IE>
<xNome>!</xNome>
<enderEmit>
<xLgr>!</xLgr>
<nro>!</nro>
<xBairro>!</xBairro>
<cMun>0000000</cMun>
<xMun>!</xMun>
<UF>AC</UF>
</enderEmit>
</emit>
<vPrest>
<vTPrest>0</vTPrest>
<vRec>0</vRec>
</vPrest>
<imp>
<ICMS>
<CST00>
<CST>00</CST>
<vBC>0</vBC>
<pICMS>0</pICMS>
<vICMS>0</vICMS>
</CST00>
</ICMS>
</imp>
<infCTeNorm>
<infCarga>
<vMerc>0</vMerc>
<proPred>a</proPred>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
</infCarga>
<rodo>
<RNTRC>00000000000000</RNTRC>
<dPrev>2000-02-29</dPrev>
<lota>0</lota>
</rodo>
</infCTeNorm>
</infCte>
<ds:Signature>
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="aa">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
</CTe>
<CTe>
<infCte Id="ID_3" versao="1.02">
<ide>
<cUF>11</cUF>
<cCT>000000000</cCT>
<CFOP>1001</CFOP>
<natOp>a</natOp>
<forPag>0</forPag>
<mod>57</mod>
<serie>0</serie>
<nCT>1</nCT>
<dhEmi>2000-02-29T20:00:00</dhEmi>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>0</cDV>
<tpAmb>1</tpAmb>
<tpCTe>0</tpCTe>
<procEmi>0</procEmi>
<verProc>a</verProc>
<cMunEmi>0000000</cMunEmi>
<xMunEmi>a</xMunEmi>
<UFEmi>AC</UFEmi>
<modal>01</modal>
<tpServ>0</tpServ>
<cMunIni>0000000</cMunIni>
<xMunIni>a</xMunIni>
<UFIni>AC</UFIni>
<cMunFim>0000000</cMunFim>
<xMunFim>a</xMunFim>
<UFFim>AC</UFFim>
<retira>0</retira>
<toma03>
<toma>0</toma>
</toma03>
</ide>
<emit>
<CNPJ>00000000000000</CNPJ>
<IE>00</IE>
<xNome>!</xNome>
<enderEmit>
<xLgr>!</xLgr>
<nro>!</nro>
<xBairro>!</xBairro>
<cMun>0000000</cMun>
<xMun>!</xMun>
<UF>AC</UF>
</enderEmit>
</emit>
<vPrest>
<vTPrest>0</vTPrest>
<vRec>0</vRec>
</vPrest>
<imp>
<ICMS>
<CST00>
<CST>00</CST>
<vBC>0</vBC>
<pICMS>0</pICMS>
<vICMS>0</vICMS>
</CST00>
</ICMS>
</imp>
<infCTeNorm>
<infCarga>
<vMerc>0</vMerc>
<proPred>a</proPred>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
</infCarga>
<rodo>
<RNTRC>00000000000000</RNTRC>
<dPrev>2000-02-29</dPrev>
<lota>0</lota>
</rodo>
</infCTeNorm>
</infCte>
<ds:Signature>
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="aa">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
</CTe>
<CTe>
<infCte Id="ID_4" versao="1.02">
<ide>
<cUF>11</cUF>
<cCT>000000000</cCT>
<CFOP>1001</CFOP>
<natOp>a</natOp>
<forPag>0</forPag>
<mod>57</mod>
<serie>0</serie>
<nCT>1</nCT>
<dhEmi>2000-02-29T20:00:00</dhEmi>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>0</cDV>
<tpAmb>1</tpAmb>
<tpCTe>0</tpCTe>
<procEmi>0</procEmi>
<verProc>a</verProc>
<cMunEmi>0000000</cMunEmi>
<xMunEmi>a</xMunEmi>
<UFEmi>AC</UFEmi>
<modal>01</modal>
<tpServ>0</tpServ>
<cMunIni>0000000</cMunIni>
<xMunIni>a</xMunIni>
<UFIni>AC</UFIni>
<cMunFim>0000000</cMunFim>
<xMunFim>a</xMunFim>
<UFFim>AC</UFFim>
<retira>0</retira>
<toma03>
<toma>0</toma>
</toma03>
</ide>
<emit>
<CNPJ>00000000000000</CNPJ>
<IE>00</IE>
<xNome>!</xNome>
<enderEmit>
<xLgr>!</xLgr>
<nro>!</nro>
<xBairro>!</xBairro>
<cMun>0000000</cMun>
<xMun>!</xMun>
<UF>AC</UF>
</enderEmit>
</emit>
<vPrest>
<vTPrest>0</vTPrest>
<vRec>0</vRec>
</vPrest>
<imp>
<ICMS>
<CST00>
<CST>00</CST>
<vBC>0</vBC>
<pICMS>0</pICMS>
<vICMS>0</vICMS>
</CST00>
</ICMS>
</imp>
<infCTeNorm>
<infCarga>
<vMerc>0</vMerc>
<proPred>a</proPred>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
</infCarga>
<rodo>
<RNTRC>00000000000000</RNTRC>
<dPrev>2000-02-29</dPrev>
<lota>0</lota>
</rodo>
</infCTeNorm>
</infCte>
<ds:Signature>
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="aa">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
</CTe>
<CTe>
<infCte Id="ID_5" versao="1.02">
<ide>
<cUF>11</cUF>
<cCT>000000000</cCT>
<CFOP>1001</CFOP>
<natOp>a</natOp>
<forPag>0</forPag>
<mod>57</mod>
<serie>0</serie>
<nCT>1</nCT>
<dhEmi>2000-02-29T20:00:00</dhEmi>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>0</cDV>
<tpAmb>1</tpAmb>
<tpCTe>0</tpCTe>
<procEmi>0</procEmi>
<verProc>a</verProc>
<cMunEmi>0000000</cMunEmi>
<xMunEmi>a</xMunEmi>
<UFEmi>AC</UFEmi>
<modal>01</modal>
<tpServ>0</tpServ>
<cMunIni>0000000</cMunIni>
<xMunIni>a</xMunIni>
<UFIni>AC</UFIni>
<cMunFim>0000000</cMunFim>
<xMunFim>a</xMunFim>
<UFFim>AC</UFFim>
<retira>0</retira>
<toma03>
<toma>0</toma>
</toma03>
</ide>
<emit>
<CNPJ>00000000000000</CNPJ>
<IE>00</IE>
<xNome>!</xNome>
<enderEmit>
<xLgr>!</xLgr>
<nro>!</nro>
<xBairro>!</xBairro>
<cMun>0000000</cMun>
<xMun>!</xMun>
<UF>AC</UF>
</enderEmit>
</emit>
<vPrest>
<vTPrest>0</vTPrest>
<vRec>0</vRec>
</vPrest>
<imp>
<ICMS>
<CST00>
<CST>00</CST>
<vBC>0</vBC>
<pICMS>0</pICMS>
<vICMS>0</vICMS>
</CST00>
</ICMS>
</imp>
<infCTeNorm>
<infCarga>
<vMerc>0</vMerc>
<proPred>a</proPred>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
<infQ>
<cUnid>00</cUnid>
<tpMed>a</tpMed>
<qCarga>0</qCarga>
</infQ>
</infCarga>
<rodo>
<RNTRC>00000000000000</RNTRC>
<dPrev>2000-02-29</dPrev>
<lota>0</lota>
</rodo>
</infCTeNorm>
</infCte>
<ds:Signature>
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="aa">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
</CTe>
</enviCTe>
Gostei + 0
24/11/2008
[devmedia .net]
Gostei + 0
24/11/2008
Alessandro Pedroso
Carlos
Abaixo a chamada do metodo.
Estou passando a uri infCte.
Preciso ler o xml e para cada ocorrencia da infCte devo adicionar a signature.
private void button2_Click(object sender, EventArgs e)
{
AvaCorp.Xml.AssinaturaDigital assinaturaDigital = new AvaCorp.Xml.AssinaturaDigital();
X509Certificate2 certificadoDigital = assinaturaDigital.GetCertificado("123456");
assinaturaDigital.AssinaDocumento(certificadoDigital, "infCte", textBox1.Text);
}
Gostei + 0
24/11/2008
Alessandro Pedroso
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = @"C:\conhecimento.xml";
}
private void button2_Click(object sender, EventArgs e)
{
AvaCorp.Xml.AssinaturaDigital assinaturaDigital = new AvaCorp.Xml.AssinaturaDigital();
X509Certificate2 certificadoDigital = assinaturaDigital.GetCertificado("123456");
assinaturaDigital.AssinaDocumento(certificadoDigital, "infCte", textBox1.Text);
}
Gostei + 0
24/11/2008
[devmedia .net]
Gostei + 0
24/11/2008
Alessandro Pedroso
Estou começando a entender melhor este processo, mas ainda assim nao consigo posicionar na tag correta.
Veja meu caso
No código
xmlDocument.FirstChild.NextSibling.LastChild.InsertAfter(xmlNode, xmlnl.LastChild);
Neste codigo o recebo um erro me dizendo que o node de referencia não é filho do nó que estou posicionado.
Onde xmlDocument.FirstChild.NextSibling.LastChild aponta para tag CTe e xmlnl.LastChild aponta para a tag infCte, então se observarmos o XML creio que um é filho do outro.
Porém se eu fizer isto um nivel acima funciona perfeitamente como abaixo.
xmlDocument.FirstChild.NextSibling.InsertAfter(xmlNode, xmlnl);
Onde xmlDocument.FirstChild.NextSiblingaponta para tag enviCte e xmlnl aponta para a tag CTe.
Se eu usar AppendChild no codigo em vermelho funciona também, mas ele acumula todas as assinaturas na ultima tag infCte e preciso que seja uma para cada tag infCte.
Então minha dúvida é como adicionar o node da assinatura em cada uma das tag CTe, conforme o exemplo que enviei.
Se pegares o XML que enviei sem assinatura voce conseguirá simular, o que preciso seria adicionar o node e é aí que recebo o erro.
Tentei usar o metodo ReplaceChild, mas aí recebo o erro que a minha Collection do foreach foi alterada.
Agradeço a ajuda, só me falta isso para homologar.
Veja código abaixo com algumas alterações, inclusive com relação à chamada, agora estou passando a Uri CTe.
public void AssinaDocumento(X509Certificate2 certificadoDigital, string Uri, string xmlOrigem)
{
int countTagsUri;
XmlDocument xmlDocument = new XmlDocument();
XmlDocument xmlDocument2 = new XmlDocument();
xmlDocument.PreserveWhitespace = false;
XmlNodeList xmlNodeList;
Reference reference = new Reference();
xmlDocument.Load(xmlOrigem);
xmlDocument2 = xmlDocument;
XmlDocument xmlAssinado = xmlDocument;
SignedXml signedXml = new SignedXml(xmlDocument);
signedXml.SigningKey = certificadoDigital.PrivateKey;
countTagsUri = xmlDocument.GetElementsByTagName(Uri).Count;
if (countTagsUri == 0)
{
throw new Exception("Uri " + Uri + " não encontrada no XML");
}
xmlNodeList = xmlDocument.GetElementsByTagName(Uri);
foreach (XmlNode xmlnl in xmlNodeList)
{
XmlAttributeCollection attributeCollection = xmlnl.FirstChild.Attributes;
reference.Uri = "#" + attributeCollection["Id"].InnerText;
string res = xmlnl.OuterXml;
XmlDsigEnvelopedSignatureTransform envelope = new XmlDsigEnvelopedSignatureTransform();
XmlDsigC14NTransform c14NTransform = new XmlDsigC14NTransform();
KeyInfo keyInfo = new KeyInfo();
reference.AddTransform(envelope);
reference.AddTransform(c14NTransform);
signedXml.AddReference(reference);
keyInfo.AddClause(new KeyInfoX509Data(certificadoDigital));
signedXml.KeyInfo = keyInfo;
signedXml.ComputeSignature();
XmlElement xmlDigitalSignature = signedXml.GetXml();
XmlNode xmlNode = xmlDocument.ImportNode(xmlDigitalSignature, true);
xmlDocument.FirstChild.NextSibling.LastChild.InsertAfter(xmlNode, xmlnl.LastChild);
// xmlDocument.FirstChild.NextSibling.InsertAfter(xmlNode, xmlnl);
//.AppendChild(xmlNode);
//InsertAfter(xmlNode, xmlnl);
xmlDocument.PreserveWhitespace = true;
xmlDocument.Save(@"c:\assinado.xml");
}
}
}
}
Gostei + 0
24/11/2008
[devmedia .net]
me envie os dados para que eu possa simular o seu método.. mas sinceramente, ainda não sei sua resposta, me passe para eu eu possa dar uma estudada, pois até onde eu sei, funciona normalmente da maneira que lhe enviei.... No aguardo.. Abraços Carlos Jr
Gostei + 0
25/11/2008
Alessandro Pedroso
O primeiro argumento é um objeto do tipo X509Certificate2 que é um certificado digital.
O segundo argumento é a Uri.
O terceiro argumento é o caminho do arquivo XML que estou carregando
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = @"C:\conhecimento.xml";
}
private void button2_Click(object sender, EventArgs e)
{
AvaCorp.Xml.AssinaturaDigital assinaturaDigital = new AvaCorp.Xml.AssinaturaDigital();
X509Certificate2 certificadoDigital = assinaturaDigital.GetCertificado("123456");
assinaturaDigital.AssinaDocumento(certificadoDigital, "CTe", textBox1.Text);
}
Os dados são a representação XML que esta no inicio do chamado, é preciso fazer a leitura e adicionar o node conforme o segundo XML que enviei , que é o exemplo gerado a partir do Schema.
O Método AppendChild funciona, porém ele adiciona os nodes um abaixo do outro na ultima tag CTe, e eu preciso que seja dentro da tag CTe no mesmo nivel de infCte (vide o XML de exemplo, o segundo xml que enviei) sendo que é um para cada tag.
Se eu tiver tres tags CTes devo colocar o node logo abaixo do ultimo filho em cada um deles.
[]s Alessandro Pedroso
Gostei + 0
25/11/2008
Alessandro Pedroso
Ja consegui fazer funcionar corretamente.
O problema é que eu estava utilizando o AppendChild direto no XMLDocument, quando o correto era utilizar o xmlNode para fazer o AppendChild.
Aí foi só fazer uns pequenos ajustes.
Valeu pela força.
Podemos encerrar o chamado.
Gostei + 0
25/11/2008
[devmedia .net]
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)