EldoS | Feel safer!

Software components for data protection, secure storage and transfer

how to sign xml file in delphi?

Also by EldoS: BizCrypto
Components for BizTalk® and SQL Server® Integration Services that let you securely store and transfer information in your business automation solutions.
#9216
Posted: 03/11/2009 06:09:46
by felix Gonzalez (Standard support level)
Joined: 03/06/2009
Posts: 7

I´m trayin to sign xml file from delphi 5 mu code it´s like this

Code
     xmlReferenceList := TElXMLReferenceList.Create;
     xmlReference := TElxmlReference.Create;
     Signer := TElXMLSigner.Create(nil);
     X509KeyData := TElXMLKeyInfoX509Data.Create(False);
     XAdESSigner := TElXAdESSigner.Create(nil);

     xmlreference := TElXMLReference.Create;
     xmlreference.DigestMethod := xdmSHA1;
     xmlreference.URI := 'd:\f.xml';
     myFile := TFileStream.Create(origen, fmOpenRead or fmShareDenyWrite);
     filesizevalue:=FileSize('d:\f.xml');
     SetLength(dataarray, filesizevalue);
     try
       myFile.seek(0, soFromBeginning);
       myFile.ReadBuffer(dataarray[0], filesizevalue);
     finally
          myFile.free;
     end;

     xmlReference.UriData := dataarray;

     xmlreference.DigestMethod := xdmSHA1;
     xmlreference.TransformChain.Add(TElXMLEnvelopedSignatureTransform.Create);
     xmlReferenceList.Add(xmlreference);

     Signer.SignatureType := xstEnveloped;
     Signer.CanonicalizationMethod := xcmCanon;
     Signer.SignatureMethodType := xmtSig;
     Signer.SignatureMethod := xsmRSA_SHA1;
     Signer.MACMethod := xmmHMAC_SHA1;
     Signer.References := xmlReferenceList;
     Signer.KeyName := '';
     Signer.IncludeKey := true;

     cert := MyCert;

     X509KeyData.Certificate := Cert;
     Signer.KeyData := X509KeyData;

     Signer.XAdESProcessor := XAdESSigner;
     XAdESSigner.XAdESVersion := XAdES_v1_3_2;
     XAdESSigner.PolicyId.SigPolicyId.Description := '';
     XAdESSigner.PolicyId.SigPolicyId.Identifier := '';
     XAdESSigner.PolicyId.SigPolicyId.IdentifierQualifier := xqtNone;
     XAdESSigner.SigningTime := LocalTimeToUTCTime(Now);
     XAdESSigner.Generate;
     XAdESSigner.QualifyingProperties.XAdESPrefix := 'xades';

     // Signer.UpdateReferencesDigest;

     Signer.Sign;




     finally
            FreeandNil(xmlReferenceList);
            //FreeAndNil(xmlReference);
            FreeAndNil(doc);
            FreeAndNil(Signer);
            FreeAndNil(X509KeyData);
            FreeandNil(XAdESSigner);
     end;


That´s OK?
if all it´s ok, how can generate the xml sing file?

Thanks
#9217
Posted: 03/11/2009 06:26:13
by Dmytro Bogatskyy (EldoS Corp.)

Quote
That´s OK?

No.
First, what do you need to do:
to sign 'f.xml' file as binary data and then create a detached signature
or to sign 'f.xml' file as xml (for example: to sign DocumentElement node) and then add a signature into this xml document?
#9218
Posted: 03/11/2009 06:34:24
by felix Gonzalez (Standard support level)
Joined: 03/06/2009
Posts: 7

second. sign xml as xml using Xades
#9219
Posted: 03/11/2009 07:20:34
by Dmytro Bogatskyy (EldoS Corp.)

Quote
second. sign xml as xml using Xades

Then, you should first load an xml (like in XMLBlackbox\Signer sample).
After:
myFile := TFileStream.Create(origen, fmOpenRead or fmShareDenyWrite);
add
Code
  FXMLDocument := TElXMLDOMDocument.Create;
  FXMLDocument.LoadFromStream(F, 'utf-8', true);


Set URINode property for the reference, not URIData:
Code
xmlreference.URI := '';
xmlReference.UriNode := FXMLDocument.DocumentElement;


And, finally sign and save xml document:
Code
Signer.UpdateReferencesDigest;
Signer.Sign;
SigNode := FXMLDocument.DocumentElement;
Signer.Save(SigNode);

myFile := TFileStream.Create('d:\f-signed.xml', fmOpenReadWrite or fmShareDenyWrite);
try
  FXMLDocument.SaveToStream(myFile, xcmNone, 'utf-8');
finally
  myFile.free;
end;
#9221
Posted: 03/11/2009 09:28:57
by felix Gonzalez (Standard support level)
Joined: 03/06/2009
Posts: 7

but with this sample I get a problem.
I must execute UpdateXML procedure. If I don´t execute it, I get an error when I try to validate the file. and this procedure use TreeNode class that dosen´t exist in dlephi 5.

thanks. I´ll try to solve it
#9222
Posted: 03/11/2009 09:34:00
by felix Gonzalez (Standard support level)
Joined: 03/06/2009
Posts: 7

sorry. I mistake TTreenode existe on delphi 5 :-)
Also by EldoS: BizCrypto
Components for BizTalk® and SQL Server® Integration Services that let you securely store and transfer information in your business automation solutions.

Reply

Statistics

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