EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Generete Digestvalue, x509Certificate and signaturevalue

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#16250
Posted: 04/21/2011 16:28:10
by Daniel Rjeili (Priority Standard support level)
Joined: 04/18/2011
Posts: 4

hi.... i am starting now my project ... and i make search in the forum and i saw similar question about what i need... i tried to reproduce the same situation here and i have a return error ... thanks for all answers ...


function TfrmMain._Assina(XML, Uri: WideString): WideString;
var
Signer : TElXMLSigner;
HMACKeyData : TElXMLKeyInfoHMACData;
RSAKeyData : TElXMLKeyInfoRSAData;
X509KeyData : TElXMLKeyInfoX509Data;
Cert : TElX509Certificate;
SigNode : TElXMLDOMNode;
Ref : TElXMLReference;
Refs : TElXMLReferenceList;
Elementos : ElXMLDOMElement;
Document : ElXMLDOMDocument;
I : Integer;

begin

HMACKeyData:=Nil;
RSAKeyData:=Nil;
X509KeyData:=Nil;
Ref:=Nil;
Refs:=TElXMLReferenceList.Create;

Try
Ref:=TElXMLReference.Create;

FXMLDocument:=TElXMLDOMDocument.Create;
Elementos:=ParseElementFromXMLString(XML, FXMLDocument);

FXMLDocument.AppendChild(Elementos);

Ref.URINode:=FXMLDocument.DocumentElement.FindNode('infNFe');
//Ref.URINode:=FXMLDocument.DocumentElement.FindNode('infNFe');

Ref.URI:='#NFe'+Uri;
Ref.TransformChain.Add(TElXMLEnvelopedSignatureTransform.Create);
Ref.TransformChain.Add(TElXMLC14NTransform.Create);

Refs.Add(Ref);

Signer:=TElXMLSigner.Create(Self);

Try
Signer.SignatureType:=xstEnveloped;
Signer.CanonicalizationMethod:=xcmCanon;
Signer.SignatureMethodType:=xmtSig;
Signer.SignatureMethod:=xsmRSA_SHA1;
Signer.MACMethod:=xmmHMAC_SHA1;
Signer.References:=Refs;
Signer.IncludeKey:=True;

If (frmSelWinCert.ShowModal = mrOK) Then
Cert:=frmSelWinCert.Certificate;

If Assigned(Cert) and Cert.PrivateKeyExists Then Begin
X509KeyData:= TElXMLKeyInfoX509Data.Create(False);
X509KeyData.IncludeDataParams:=[xkidX509Certificate, xkidX509CRL];
X509KeyData.IncludeKeyValue:=False;
X509KeyData.Certificate:=Cert;
Signer.KeyData:=X509KeyData;
end;

Signer.UpdateReferencesDigest;

If True then begin
Signer.Sign;

SigNode := FXMLDocument.DocumentElement.FindNode('NFe');


//i := FXMLDocument.DocumentElement.FindNode('NFe').RelativeIndex;
//FXMLDocument.DocumentElement.ChildNodes.Item.OuterXML;



If SigNode is TElXMLDOMDocument Then
SigNode := TElXMLDOMDocument(SigNode).DocumentElement;

Try
Signer.Save(SigNode);

FXMLDocument.OuterXML;
signode.OuterXML;

_SalvaArq('N:\NFe2\_sbb\_com_ass\teste.xml');

except
on E: Exception do
raise EElXMLError.CreateFmt('Falha ao salvar os dados da assinatura. (%s)', [E.Message]);
end;
end;
finally
FreeAndNil(Signer);
FreeAndNil(HMACKeyData);
FreeAndNil(RSAKeyData);
FreeAndNil(X509KeyData);
end;
finally
FreeAndNil(Refs);
end;

---------------------------
xml_sem_assinar:=

'<NFe xmlns="http://www.portalfiscal.inf.br/nfe">[i]<ide><cUF>35</cUF>'+
'<cNF>00002844</cNF><natOp>Remessa Para Industrializacao</natOp><indPag>2
<mod>55</mod>1<nNF>2844</nNF><dEmi>2011-04-21</dEmi>'+
'<dSaiEnt>2011-04-21</dSaiEnt><tpNF>1</tpNF><cMunFG>3550308</cMunFG><tpImp>1</tpImp><tpEmis>1</tpEmis><cDV>4</cDV><tpAmb>1</tpAmb><finNFe>1</finNFe>'+
'<procEmi>0</procEmi><verProc>1.0.0.X</verProc></ide><emit><CNPJ>01001134000106</CNPJ><xNome>Industria e Comercio</xNome><enderEmit>'+
'<xLgr>Rua</xLgr><nro>888</nro><xBairro>mooca</xBairro><cMun>3550308</cMun><xMun>Sao Paulo</xMun>SP<CEP>03109020</CEP><cPais>1058</cPais>'+
'<xPais>BRASIL</xPais><fone>1122229799</fone></enderEmit>993129062000<CRT>3</CRT></emit><dest><CNPJ>00000000000103</CNPJ>'+
'<xNome>Beneficiamnetos Texteis LTDA -</xNome><enderDest><xLgr>Rua cel. Peroba</xLgr><nro>136</nro><xBairro>Centro</xBairro><cMun>3523404</cMun>'+
'<xMun>Itatiba</xMun>SP<CEP>13256410</CEP><cPais>1058</cPais><xPais>BRASIL</xPais><fone>1145349450</fone></enderDest>382045479113</dest>'+
'<det nItem="1"><prod><cProd>00005727K</cProd><cEAN/><xProd>bermuda masculina</xProd><NCM>62034200</NCM><CFOP>5901</CFOP>pcs<qCom>382.0000</qCom>'+
'<vUnCom>7.0000000000</vUnCom><vProd>2674.00</vProd><cEANTrib/>pcs<qTrib>382.0000</qTrib><vUnTrib>7.0000000000</vUnTrib>1</prod>'+
'<ICMS><ICMS40><orig>0</orig><CST>50</CST></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS>'+
'<COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><total><vBC>0.00</vBC><vICMS>0.00</vICMS>'+
'<vBCST>0.00</vBCST><vST>0.00</vST><vProd>2674.00</vProd><vFrete>0.00</vFrete><vSeg>0.00</vSeg><vDesc>0.00</vDesc><vII>0.00</vII><vIPI>0.00</vIPI><vPIS>0.00</vPIS><vCOFINS>0.00</vCOFINS>'+
'<vOutro>0.00</vOutro><vNF>2674.00</vNF>
</total><transp><modFrete>1</modFrete><transporta><xNome>O MESMO</xNome></transporta><vol><esp>A GRANEL</esp><marca>xxx</marca>'+
'</vol></transp><infAdFisco>Suspensao do ICMS nos termos do artigo 402, Decreto No 45.490/2000.<obsCont xCampo="Quantidade Total"><xTexto>382 produtos (e)(ou) servicos.</xTexto>'+
'</obsCont></infAdic></infNFe></NFe>';

------------------------------

_Assina(xml_sem_assinar, '31080465105041000198550030000000870000000876');

------------------------------

when i try ... return " No XML element available " .... EEIXMLSecurityError ....

thanks!
#16252
Posted: 04/21/2011 17:28:06
by Dmytro Bogatskyy (EldoS Corp.)

Quote
when i try ... return " No XML element available " .... EEIXMLSecurityError ....

What does the following expression return?
FXMLDocument.DocumentElement.FindNode('NFe')

It is nil? NFe element is a document element in your case.
#16253
Posted: 04/21/2011 17:55:08
by Daniel Rjeili (Priority Standard support level)
Joined: 04/18/2011
Posts: 4

i checked:

Ref.URINode:= FXMLDocument.DocumentElement.FindNode('NFe'); //FindNode('NFe').

if Ref.URINode = nil then
showmessage('nil') ;

yes its returning nil !!!!

and i tried too:
Ref.URINode:= FXMLDocument.DocumentElement.FindNode('NFe').FindNode('infNFe');


if Ref.URINode = nil then
showmessage('nil') ;

with this i have accessviolation
#16258
Posted: 04/22/2011 03:33:36
by Dmytro Bogatskyy (EldoS Corp.)

Quote
yes its returning nil !!!

As I understand you need to place a signature under document element (NFe element), then try to use:
SigNode := FXMLDocument.DocumentElement;
#16259
Posted: 04/22/2011 10:05:22
by Daniel Rjeili (Priority Standard support level)
Joined: 04/18/2011
Posts: 4

Dmytro,

Thanks! it worked.....
and what about encoding .... for default is utf-8? if i need to "force" or even change the encode where i have to set the parameter??
#16260
Posted: 04/22/2011 11:09:08
by Dmytro Bogatskyy (EldoS Corp.)

Quote
and what about encoding .... for default is utf-8? if i need to "force" or even change the encode where i have to set the parameter??

You can use overloaded SaveToStream method (Charset parameter), see:
http://www.eldos.com/documentation/sb...tream.html
#16261
Posted: 04/22/2011 12:10:15
by Daniel Rjeili (Priority Standard support level)
Joined: 04/18/2011
Posts: 4

Ok good to know if i have to save in a encoding different from utf-8 i can do it.
until now 100%!

First Phase ok! thanks !!!

Second Phase will be:
TElHttpsClient
certificate chain
Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.

Reply

Statistics

Topic viewed 1265 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!