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.
#5972
Posted: 04/22/2008 12:53:01
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

XML PARSE ERROR AT POSITION 39 (0x27): you cam't use "<?xml"
Why?


Gabriel Herdt
Consystec
#5973
Posted: 04/22/2008 13:10:39
by Dmytro Bogatskyy (EldoS Corp.)

Are you trying to use ParseElementFromXMLString? This method can parse only xml element, not a XML Declaration.
Quote
I Need use FXMLDocument := TXMLDOCUMENT.

You cann't do this.
You can:
Code
Stream := TMemoryStream.Create();
(instance of TXMLDOCUMENT).SaveToStream(Stream);
Stream.Position := 0;
FXMLDocument.LoadFromStream(Stream);
Stream.Free;

or use ParseElementFromXMLString to parse result of
(instance of TXMLDOCUMENT).DocumentElement.OuterXML
There is no methods like LoadFromXML(string) at the moment.
#5974
Posted: 04/22/2008 13:44:12
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

Ok i Resolve the Problem.
But when i save.
I saving only signode. I need save All Elements.


Quote
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;
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;
//I Fink here is the problem.
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.Active := True;
XMLDoc.Version := '1.0';
XMLDoc.Encoding := 'utf-8';
XMLDoc.XML.Text := signode.OuterXML; XMLDoc.Active := True;
XMLDoc.SaveToFile('C:\Documents and Settings\user\Desktop\envNfeAss.xml');
{New TXmlDocument receive xml Eldos compoment, But i need save all tags in xml: FXMLDocument.AppendChild(Elementos);}


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;


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

Read last post too. please!

You have example using FXMLDocument.DocumentElement.ReplaceChild ?


Gabriel Herdt
Consystec
#5977
Posted: 04/22/2008 14:40:36
by Dmytro Bogatskyy (EldoS Corp.)

Quote
I saving only signode. I need save All Elements.

As I understand, you need FXMLDocument.DocumentElement.OuterXML not signode.OuterXML
Quote
You have example using FXMLDocument.DocumentElement.ReplaceChild ?

See: ElXMLDOMNode.ReplaceChild
E.g. Element.ReplaceChild(FXMLDocument.CreateElement("test"), Element.FirstChild);
#5978
Posted: 04/22/2008 14:46:06
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

Quote
As I understand, you need FXMLDocument.DocumentElem­ent.OuterXML not signode.OuterXML

I Need FXMLDocument.DocumentElem­­ent.OuterXML Signed. In my code only have signode.OuterXML signed

I think use signode.OuterXML replacechild in FXMLDocument.DocumentElem­ent.OuterXML tags signed.
How you can help me?


Gabriel Herdt
Consystec
#5979
Posted: 04/22/2008 15:01:33
by Dmytro Bogatskyy (EldoS Corp.)

Ok. If you need the same xml as in XMLDoc, then:
FXMLDocument.ReplaceChild(signode.CloneNode(True), FXMLDocument.DocumentElement)
// you need to clone signode
#5980
Posted: 04/22/2008 15:09:12
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

How i can switch encoding? for utf-8?


Gabriel Herdt
Consystec
#5981
Posted: 04/23/2008 01:43:31
by Dmytro Bogatskyy (EldoS Corp.)

Quote
How i can switch encoding? for utf-8?

You can specify encoding in SaveToStream method.
See: http://eldos.com/documentation/sbb/do...tream.html
"Use aEncoding OR aCharset parameter to specify, what encoding and/or charset is used for the document. If nothing is specified, UTF8 charset is used."
#5992
Posted: 04/23/2008 10:11:28
by Gabriel Herdt (Standard support level)
Joined: 04/09/2008
Posts: 37

Quote
Bogatskyy wrote:
Ok. If you need the same xml as in XMLDoc, then:
FXMLDocument.ReplaceChild(signode.CloneNode(True), FXMLDocument.DocumentElement)
// you need to clone signode


Have problem.


    FXMLDocument.OuterXML =
    '<?xml version="1.0" encoding="utf-8"?><enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.10">1<NFe xmlns="http://www.portalfiscal.inf.br/nfe">...

    signode.OuterXML;

    '<NFe xmlns="http://www.portalfiscal.inf.br/nfe"><infNFe versao="1.10" Id="Nfe5108030394105200171855001000012351000012351">


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

If i Use your replace my result is '<NFe xmlns="http://www.portalfiscal.inf.br/nfe"><infNFe versao="1.10" Id="Nfe5108030394105200171855001000012351000012351">...

help me please.



Gabriel Herdt
Consystec
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.

Reply

Statistics

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