EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Generete Digestvalue, x509Certificate and signaturevalue

Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.
#5949
Posted: 04/21/2008 09:27:07
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

I need one example my code is:

function TFLnEnvNFe.Assinar2(Xml, Uri:WideString; out x509, Ass,
Digest: WideString): WideString;
var
Signer: TElXMLSigner;
XAdESSigner: TElXAdESSigner;
HMACKeyData: TElXMLKeyInfoHMACData;
RSAKeyData: TElXMLKeyInfoRSAData;
X509KeyData: TElXMLKeyInfoX509Data;
PGPKeyData: TElXMLKeyInfoPGPData;
Cert: TElX509Certificate;
SigNode: TElXMLDOMNode;
El: TElXMLDOMElement;
Buf: ByteArray;
Ref: TElXMLReference;
Refs: TElXMLReferenceList;
//HTTPClient: TElHTTPSClient;
//TSPClient: TElHttpTSPClient;
NodeNivel1,
NodeNivel2,
NodeNivel3,
NodeNivelZC: IXMLNode;
iNode,i : Integer;
Elementos : ElXMLDOMElement;
Document : ElXMLDOMDocument;
F: {$ifndef DELPHI_NET}TFileStream{$else}FileStream{$endif};
S: WideString;
begin
inherited;
XAdESSigner := nil;
//TSPClient := nil;
//HTTPClient := nil;
HMACKeyData := nil;
RSAKeyData := nil;
X509KeyData := nil;
PGPKeyData := nil;
Ref := nil;
Refs := TElXMLReferenceList.Create;

try
XML;
Ref := TElXMLReference.Create;

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

// Ref.DigestMethod := xdmSHA1;

Ref.URINode := FXMLDocument.DocumentElement.FindNode('NFe').FindNode('infNFe');
// ref.LoadFromXML();
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;
Signer.OnFormatText := FormatText;

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');

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);

XMLDoc.XML.Clear;
XMLDoc.XML.Text := signode.OuterXML;
XMLDoc.Active := True;
XMLDoc.SaveToFile('C:\Documents and Settings\user\Desktop\envNfeAss.xml');

except
on E: Exception do
raise EElXMLError.CreateFmt('Signed data saving failed. (%s)', [E.Message]);
end;
end;

finally
FreeAndNil(Signer);
FreeAndNil(XAdESSigner);
FreeAndNil(HMACKeyData);
FreeAndNil(RSAKeyData);
FreeAndNil(X509KeyData);
FreeAndNil(PGPKeyData);
end;
//end;
finally
FreeAndNil(Refs);
end;
end;


Currect xml and Wrong xml:
Wrong have one Space(TAB) more in signature tags




[ Download ]

Gabriel Herdt
Consystec
#5950
Posted: 04/21/2008 09:38:23
by Dmytro Bogatskyy (EldoS Corp.)

Quote
I need one example my code is

Please explain what exactly problem do you have?

Quote
XMLDoc.XML.Clear;
XMLDoc.XML.Text := signode.OuterXML;
XMLDoc.Active := True;
XMLDoc.SaveToFile('C:\Doc­uments and Settings\user\Desktop\env­NfeAss.xml');

You can use ElXMLDOMDocument.SaveToStream method to save the xml document.
#5951
Posted: 04/21/2008 09:44:15
by Dmytro Bogatskyy (EldoS Corp.)

Sorry, I missed the end of your message.
Did you modify FormatElement and FormatText methods? Are they the same as in the demo application?
#5952
Posted: 04/21/2008 09:45:41
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

I use save only to check my xml. My function need Result one string.
My problem is my tags(signature) have one space(tab) more to right, In Example code don't.
Look the files in my last post.


Gabriel Herdt
Consystec
#5953
Posted: 04/21/2008 09:46:31
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

Yes, Ctrl+c and Ctrl+v :D


Gabriel Herdt
Consystec
#5956
Posted: 04/21/2008 12:34:07
by Dmytro Bogatskyy (EldoS Corp.)

Strange, I got the same formatting as in "Right.xml"
I only replaced this code:
Code
XMLDoc.XML.Clear;
XMLDoc.XML.Text := signode.OuterXML;
XMLDoc.Active := True;
XMLDoc.SaveToFile('C:\Documents and Settings\user\Desktop\envNfeAss.xml');

with
Code
F := TFileStream.Create('envNfeAssCheck.xml', fmCreate);
FXMLDocument.SaveToStream(F);
F.Free;

and result of signode.OuterXML is also correct.
Please, recheck your code.
#5966
Posted: 04/22/2008 06:33:00
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

I resolve the problem.
Only coment:
Signer.OnFormatElement := FormatElement;
Signer.OnFormatText := FormatText;



Gabriel Herdt
Consystec
#5969
Posted: 04/22/2008 09:09:45
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

Last question guys.
How i can load full xml? In my code if xml begin:
Quote
<?xml version="1.0" encoding="UTF-8"?>...

Begin one error.


Gabriel Herdt
Consystec
#5970
Posted: 04/22/2008 09:29:57
by Dmytro Bogatskyy (EldoS Corp.)

Quote
How i can load full xml? In my code if xml begin:

Use ElXMLDOMDocument.LoadFromStream method, as a stream object you can use TFileStream, TMemoryStream and so on...
#5971
Posted: 04/22/2008 09:39:28
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

LoadFrom­Stream thant ok. But i don't use stream for load.
I Need use FXMLDocument := TXMLDOCUMENT.


Quote
FXMLDocument := TElXMLDOMDocument.Create;
Elementos := ParseElementFromXMLString(Xml,FXMLDocument);
FXMLDocument.AppendChild(Elementos);



Quote
Gabriel Herdt wrote:

function TFLnEnvNFe.Assinar2(Xml, Uri:WideString; out x509, Ass,
Digest: WideString): WideString;
var
Signer: TElXMLSigner;
XAdESSigner: TElXAdESSigner;
HMACKeyData: TElXMLKeyInfoHMACData;
RSAKeyData: TElXMLKeyInfoRSAData;
X509KeyData: TElXMLKeyInfoX509Data;
PGPKeyData: TElXMLKeyInfoPGPData;
Cert: TElX509Certificate;
SigNode: TElXMLDOMNode;
El: TElXMLDOMElement;
Buf: ByteArray;
Ref: TElXMLReference;
Refs: TElXMLReferenceList;
//HTTPClient: TElHTTPSClient;
//TSPClient: TElHttpTSPClient;
NodeNivel1,
NodeNivel2,
NodeNivel3,
NodeNivelZC: IXMLNode;
iNode,i : Integer;
Elementos : ElXMLDOMElement;
Document : ElXMLDOMDocument;
F: {$ifndef DELPHI_NET}TFileStream{$else}FileStream{$endif};
S: WideString;
begin
inherited;
XAdESSigner := nil;
//TSPClient := nil;
//HTTPClient := nil;
HMACKeyData := nil;
RSAKeyData := nil;
X509KeyData := nil;
PGPKeyData := nil;
Ref := nil;
Refs := TElXMLReferenceList.Create;

try
XML;
Ref := TElXMLReference.Create;

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

// Ref.DigestMethod := xdmSHA1;

Ref.URINode := FXMLDocument.DocumentElement.FindNode('NFe').FindNode('infNFe');
// ref.LoadFromXML();
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;
Signer.OnFormatText := FormatText;

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');

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);

XMLDoc.XML.Clear;
XMLDoc.XML.Text := signode.OuterXML;
XMLDoc.Active := True;
XMLDoc.SaveToFile('C:\Documents and Settings\user\Desktop\envNfeAss.xml');

except
on E: Exception do
raise EElXMLError.CreateFmt('Signed data saving failed. (%s)', [E.Message]);
end;
end;

finally
FreeAndNil(Signer);
FreeAndNil(XAdESSigner);
FreeAndNil(HMACKeyData);
FreeAndNil(RSAKeyData);
FreeAndNil(X509KeyData);
FreeAndNil(PGPKeyData);
end;
//end;
finally
FreeAndNil(Refs);
end;
end;


Gabriel Herdt
Consystec
Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.

Reply

Statistics

Topic viewed 61042 times

Number of guests: 2, 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!