EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Generete Digestvalue, x509Certificate and signaturevalue

Also by EldoS: Solid File System
A virtual file system that offers a feature-rich storage for application documents and data with built-in compression and encryption.
#5993
Posted: 04/23/2008 10:20:50
by Dmytro Bogatskyy (EldoS Corp.)

Quote
I need result = <enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.10">1<NFe xmlns="http://www.portalfiscal.inf.br/nfe"><infNFeversao="1.10" Id="Nfe5108030394105200171855001000012351000012351">...

In this case use: FXMLDocument.DocumentElement.OuterXML (enviNFe - root element)
#5995
Posted: 04/23/2008 11:50:24
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

FXMLDocument.ReplaceChild­(signode.CloneNode(True),­ FXMLDocument.DocumentElem­ent.OuterXML)
This way?


Gabriel Herdt
Consystec
#5998
Posted: 04/23/2008 12:11:42
by Dmytro Bogatskyy (EldoS Corp.)

What are you trying to do?
Do you need to move Signature element under another node?
If you need following xml structure:
enviNFe
- idLote
- NFe
- - infNFe
- Signature <- here
Then replace in your last code:
SigNode := FXMLDocument.DocumentElement.FindNode('NFe');
to
SigNode := FXMLDocument.DocumentElement;

"If the signature type is enveloped, the signature is placed as a child of the passed node (SigNode)".
#5999
Posted: 04/23/2008 12:20:58
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

My Signature tag is NFe and i sign NFe tag. I Need save All xml.

enviNFe
- idLote
- NFe
- - infNFe
- - Signature <- here

Signature is child Nfe.

In my Quoted code i Only can save Signed tag(Nfe). here is problem. I need Save all Tags.

enviNFe
- idLote
- NFe
- - infNFe
- - Signature <- here


Bold is my code save. My Objective is all tags.

I Will have:
<enviNFe>
- idLote
- NFe 1
- - infNFe
- - Signature <- here
- NFe 2
- - infNFe
- - Signature <- here
</enviNFe>


Gabriel Herdt
Consystec
#6000
Posted: 04/23/2008 12:42:13
by Dmytro Bogatskyy (EldoS Corp.)

Quote
In my Quoted code i Only can save Signed tag(Nfe). here is problem. I need Save all Tags.

enviNFe
- idLote
- NFe
- - infNFe
- - Signature <- here

Ok.
You have in your last code:
XMLDoc.XML.Text := signode.OuterXML
signode - is NFe element, so this is a reason why you save only this bold tags.
You need:
XMLDoc.XML.Text := FXMLDocument.DocumentElement.OuterXML
#6001
Posted: 04/23/2008 12:53:50
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

And if i have?:

<enviNFe>
- idLote
- NFe 1 (SigNode 1)
- - infNFe
- - Signature <- here
- NFe 2 (SigNode 2)
- - infNFe
- - Signature <- here
</enviNFe>


Gabriel Herdt
Consystec
#6002
Posted: 04/23/2008 13:02:27
by Dmytro Bogatskyy (EldoS Corp.)

The same. The ElXMLDOMDocument.DocumentElement is a root xml element, in your case it is enviNFe element, so calling OuterXML you will save all tags.
#6016
Posted: 04/24/2008 08:16:14
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

ok. But i need generate two signatures or more. One signature per NFe tag. How? Based in my code please.


    function TRDmConBas.Assinar2(Xml, Uri:WideString; out x509, Ass,
    Digest: WideString): WideString;
    var
    Signer: TElXMLSigner;
    HMACKeyData: TElXMLKeyInfoHMACData;
    RSAKeyData: TElXMLKeyInfoRSAData;
    X509KeyData: TElXMLKeyInfoX509Data;
    Cert: TElX509Certificate;
    SigNode: TElXMLDOMNode;
    Ref: TElXMLReference;
    Refs: TElXMLReferenceList;
    Elementos : ElXMLDOMElement;
    Document : ElXMLDOMDocument;
    S: WideString;
    i : integer;
    begin
    inherited;
    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('NFe').FindNode('infNFe');

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

    Refs.Add(Ref);
    //if True then begin
    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;

    //Signer.OnFormatElement := FormatElement; //Formata o XML
    //Signer.OnFormatText := FormatText; //Formata o XML

    Cert := ElWinCertStorage.Certificates[(Parametro.NR_ITECER-1)];

    //ate aki ok
    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');
    //teste

    i := FXMLDocument.DocumentElement.FindNode('NFe').RelativeIndex;
    FXMLDocument.DocumentElement.ChildNodes.Item[i].OuterXML;
    //teste
    if SigNode is TElXMLDOMDocument then
    SigNode := TElXMLDOMDocument(SigNode).DocumentElement;

    try
    // If the signature type is enveloping, then the signature is placed into the passed
    //node and the contents of the node are moved to inside of the signature.
    // If the signature type is enveloped, the signature is placed as a child of the passed node.
    Signer.Save(SigNode);

    FXMLDocument.OuterXML;
    signode.OuterXML;

    SalvaArq('C:\Documents and Settings\user\Desktop\envNfeAss1.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;
    end;

    Procedure TRDmConBas.SalvaArq(Caminho: string);
    var
    F: {$ifndef DELPHI_NET}TFileStream{$else}FileStream{$endif};
    begin
    {$ifndef DELPHI_NET}
    F := TFileStream.Create(Caminho, fmCreate or fmOpenWrite);
    {$else}
    F := System.IO.FileStream.Create(Caminho, FileMode.Create, FileAccess.ReadWrite);
    {$endif}
    try
    FXMLDocument.SaveToStream(F, xcmNone, 'utf-8');
    except
    on E : Exception do
    begin
    MessageDlg('Error: ' + E.Message, mtError, [mbOk], 0);
    end;
    end;

    FreeAndNil(F);
    end;




I'm trying to understand that help [url]http://www.eldos.com/sbbdev/articles/4210.php?phrase_id=264836[/url]

Quote
To sign multiple nodes, you can do one of the following:

select the nodes to the nodeset and assign the 0'th element of the nodeset to URINode property. You can select the nodes "manually" or use XPath. To execute XPath expression, call SelectNodes() method of ElXMLDOMDocument class.
set URINode property to reference the parent node to be signed and use XPath Transform for the given reference to select one or more child nodes of the given parent node.
have example?


Gabriel Herdt
Consystec
#6017
Posted: 04/24/2008 11:25:38
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37


I'm trying to understand that help [url]http://www.eldos.com/sbbdev/articles/4210.php?phrase_id=264836[/url]

Quote
To sign multiple nodes, you can do one of the following:

select the nodes to the nodeset and assign the 0'th element of the nodeset to URINode property. You can select the nodes "manually" or use XPath. To execute XPath expression, call SelectNodes() method of ElXMLDOMDocument class.
set URINode property to reference the parent node to be signed and use XPath Transform for the given reference to select one or more child nodes of the given parent node.
have example?


Gabriel Herdt
Consystec
#6019
Posted: 04/24/2008 15:14:09
by Dmytro Bogatskyy (EldoS Corp.)

As I understand you need to sign each node with own signature.
The simplest way will be to find all Nfe elements and then use your code to sign each one.
Sample code:
Code
var
NSMap : TElXMLNamespaceMap;
Nodes : TElXMLNodeSet;

NSMap := TElXMLNamespaceMap.Create;
NSMap.Add("nfe", "http://www.portalfiscal.inf.br/nfe");
try
  Nodes := FXMLDocument.SelectNodes("//nfe:NFe", NSMap); // search for all NFe elements with namespace: http://www.portalfiscal.inf.br/nfe
finally
  FreeAndNil(NSMap);
end;

// signing each NFe element
for i := 0 to Nodes.Count - 1 do
begin
  Ref.URINode := Nodes[i].FindNode('infNFe');
  // ... your code
  SigNode := Nodes[i];
  // ...
  Signer.Save(SigNode);
end;

FreeAndNil(Nodes);
Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.

Reply

Statistics

Topic viewed 60998 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!