Re: Compute XML Signature on external Xml document
From: J-P Meunier (fuimens_at_yahoo.fr)
Date: 05/22/03
- Next message: Calvin Walker: "Re: Code access security...."
- Previous message: Joe Kaplan: "Re: Changing AppDomain.CurrentDomain Principal"
- In reply to: Ivan Medvedev [MS]: "Re: Compute XML Signature on external Xml document"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Thu, 22 May 2003 10:16:14 +0200
Ivan,
Thank you very much for your response.
The Transform gives the expected XML output. But the verification always
returns false.
Here is my code, Where is the mistake ?
-------------- Signature -----------------
XmlDocument domContext = new XmlDocument();
domContext.PreserveWhitespace = true; <<----------- Is this
necessary ?
XmlNode xnContextRoot = domContext.CreateNode(XmlNodeType.Element, "",
"ClientContext", "");
domContext.AppendChild(xnContextRoot);
.... Creating childs and appending them ...
SignedXml signedXml = new SignedXml(domContext);
signedXml.SigningKey = rsaKeyPair; // rsaKeyPair exists
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new KeyInfoX509Data(X509Data)); // X509Data exists
signedXml.KeyInfo = keyInfo;
Reference reference = new Reference();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
signedXml.AddReference(reference);
signedXml.ComputeSignature();
XmlElement xmlDigitalSignature = signedXml.GetXml();
domContext.DocumentElement.AppendChild(domContext.ImportNode(xmlDigitalSigna
ture, true));
if (domContext.FirstChild is XmlDeclaration)
domContext.RemoveChild(domContext.FirstChild);
domContext.Save("c:\envelopedSignature.xml");
---------- End Signature ----------------
----------- Verification -------------------
XmlDocument doc2 = new XmlDocument();
doc2.PreserveWhitespace = true;
doc2.Load("c:\envelopedSignature.xml");
SignedXml signedXml2 = new SignedXml(doc2);
XmlNodeList nodeList = doc2.GetElementsByTagName("Signature");
signedXml2.LoadXml((XmlElement)nodeList[0]);
signedXml2.CheckSignature();
----------- End Verification --------------------
Cheers,
J-P
"Ivan Medvedev [MS]" <ivanmed@online.microsoft.com> a écrit dans le message
de news:uRDXHo7HDHA.3280@tk2msftngp13.phx.gbl...
> J-P -
> you will need to use an EnvelopedSignatureTransform. Here is approximately
> how you would do this:
>
> ------------------------- sign --------------------------
> XmlDocument doc = ... // your enveloping document
> SignedXml signedXml = new SignedXml(doc);
> signedXml.SigningKey = ... // your signing key
> Reference reference = new Reference();
> reference.Uri = "";
> XmlDsigEnvelopedSignatureTransform env = new
> XmlDsigEnvelopedSignatureTransform();
> reference.AddTransform(env);
> signedXml.AddReference(reference);
> ... // add key info if neccessary
> signedXml.ComputeSignature();
> // now insert the signature into the doc
> XmlElement xmlDigitalSignature = signedXml.GetXml();
> XmlTextWriter xmltw = new XmlTextWriter( _name_ , new
> UTF8Encoding(false));
> doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature,
> true)); // insert the signature into the document
> if (doc.FirstChild is XmlDeclaration) doc.RemoveChild(doc.FirstChild);
> doc.WriteTo(xmltw);
> xmltw.Close();
>
> ------------------------ verify ---------------------------
> XmlDocument xmlDocument = new XmlDocument();
> xmlDocument.PreserveWhitespace = true;
> xmlDocument.Load(_name_);
> SignedXml signedXml = new SignedXml(xmlDocument);
> XmlNodeList nodeList =
> xmlDocument.GetElementsByTagName("Signature");
> signedXml.LoadXml((XmlElement)nodeList[0]);
> return signedXml.CheckSignature();
>
> -----------------------------------------------------------
>
> Hope this helps.
> --Ivan
> This posting is provided "AS IS" with no warranties, and confers no
rights.
>
>
>
>
> "J-P Meunier" <fuimens@yahoo.fr> wrote in message
> news:eGEI053HDHA.2176@TK2MSFTNGP10.phx.gbl...
> > Hi all,
> >
> > I need to make an Xml Signature
> (System.Security.Cryptography.Xml.SignedXml)
> > of an XmlDocument and then to include the signature into that
XmlDocument.
> >
> > Example:
> > <XmlSign>
> > <Identity id="ToBeSigned">
> > <Name>myName</Name>
> > <Surname>mySignature</Surname>
> > </Identity>
> > <IdentitySignature>
> > *SignedXml*
> > </IdentitySignature>
> > <XmlSign>
> >
> > The solution would be to create a SignedXml object and insert it
> > <IdentitySignature>, to set the correct references and then to compute
> the
> > signature. But I can't to that because the XML element of SignedXml
can't
> be
> > accessed before computing the signature ...
> >
> > So how can I solve that problem?
> > Any comment would be appreciated,
> >
> > Thanks
> >
> > J-P
> >
> >
>
>
- Next message: Calvin Walker: "Re: Code access security...."
- Previous message: Joe Kaplan: "Re: Changing AppDomain.CurrentDomain Principal"
- In reply to: Ivan Medvedev [MS]: "Re: Compute XML Signature on external Xml document"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|