Re: Compute XML Signature on external Xml document

From: Ivan Medvedev [MS] (ivanmed_at_online.microsoft.com)
Date: 05/21/03


Date: Wed, 21 May 2003 09:56:37 -0700


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



Relevant Pages

  • SignedXml.CheckSignature always false
    ... RSACryptoServiceProvider rsa = new RSACryptoServiceProvider; ... XmlDocument signature = Sign; ... SignedXml signedXml = new SignedXml; ... Reference reference = new Reference; ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • CAPICOM xmldsig example error
    ... After researching Xml Signature verification ... issues using .NET classes (centered around SignedXml) ... Reference r = new Reference; ... ...Signature will fail verification from within the same ...
    (microsoft.public.dotnet.security)
  • Verifying a signature with SignedXml.CheckSignature() does not seem to work with xml-files signed wi
    ... XmlDocument xmlDocument = new XmlDocument; ... SignedXml signedXml = new SignedXml; ... <Signature xmlns="http://www.w3.org/2000/09/xmldsig #" ... <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1 "/> ...
    (microsoft.public.dotnet.security)
  • RE: SignedXML.CheckSignature()/CreateSignature() Alternative?
    ... Probably I can create a dummy reference and get the digest values for the ... the signature Value logic... ... and attach this transform to that reference. ... >> Iam using SignedXML.CheckSignaturemethod for validating XMLDSIG ...
    (microsoft.public.dotnet.security)
  • bug in c14n canonicalization ?
    ... I would like to discuss a c14n canonicalization problem here. ... When I try to validate this signature with an other XmlDSig libraries, ... referenced by the first Reference with .NET functions and with Apache ... Apache returns the same node (SignedProperties) but keeps its namespace to ...
    (microsoft.public.dotnet.security)