SignedXml.CheckSignature always false

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

I try to create an XmlSignature (Detached). The Sign Method in my example
works fine. Inside the Verify Method the result of SignedXml.CheckSignature
is always false. Any help or ideas is really appreciated.
How is the strategy to find such problems. Are there any more specific
details inside the debugger to find what is wrong?

Thanks
Dieter

My Example Code



using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;

namespace XmlSignatureDetached
{
class Program
{
static void Main(string[] args)
{
Init();
}

private static void Init()
{
string xmlString = "<Assertion><Issuer>Identity
Management</Issuer><Subject><NameID>MyDomain\\MyUser</NameID></Subject><AttributeStatement><Attribute
name=\"IamIdentityState\"><AttributeValue>Activated</AttributeValue></Attribute></AttributeStatement></Assertion>";

XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = true;
xdoc.LoadXml(xmlString);

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048);

XmlDocument signature = Sign(xdoc, rsa);

bool verifyResult = Verify(xdoc, signature, rsa);

}

private static XmlDocument Sign(XmlDocument content, RSA rsa)
{
XmlDocument signature = null;

// Create a SignedXml object.
SignedXml signedXml = new SignedXml(content);

// Add the key to the SignedXml document.
signedXml.SigningKey = rsa;

// Create a reference to be signed.
Reference reference = new Reference();
reference.DigestMethod =
"http://www.w3.org/2001/04/xmlenc#sha512";;

// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
reference.Uri = "";

// Add the reference to the SignedXml object.
signedXml.AddReference(reference);

// Compute the signature.
signedXml.ComputeSignature();

// Get the XML representation of the signature
signature = new XmlDocument();
XmlElement xmlDigitalSignature = signedXml.GetXml();
XmlNode xmlNodeDigSig = xmlDigitalSignature.Clone();

signature.AppendChild(signature.ImportNode(xmlNodeDigSig, true));

if (signature.FirstChild is XmlDeclaration)
{
signature.RemoveChild(signature.FirstChild);
}
return signature;
}

private static bool Verify(XmlDocument xdoc,XmlDocument
signature,RSACryptoServiceProvider rsa)
{
bool result = false;
// Create a new SignedXml and pass it to the XmlDocument.
SignedXml signedXml = new SignedXml(xdoc);


// Add the key to the SignedXml document.
signedXml.SigningKey = rsa;

// Find the "Signature" node and create a new XmlNodeList.
XmlNodeList nodeList =
signature.GetElementsByTagName("Signature");

// Load the signature node.
signedXml.LoadXml((XmlElement)nodeList[0]);

// Check the signature and set the result.
result = signedXml.CheckSignature();

return result;
}

}
}

.



Relevant Pages

  • Re: Compute XML Signature on external Xml document
    ... SignedXml signedXml = new SignedXml; ... Reference reference = new Reference; ... // insert the signature into the document ... > of an XmlDocument and then to include the signature into that XmlDocument. ...
    (microsoft.public.dotnet.security)
  • 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)
  • Problem with SignedXml and CheckSignature
    ... I've a problem with the SignedXml Class. ... when I check the signature, ... if I include the keyinfo in then xml-file which I sign. ... RSA rsa; ...
    (microsoft.public.dotnet.security)
  • RE: Problem with SignedXml and CheckSignature
    ... KeyInfo keyInfo; ... RSA rsa; ... Problem with SignedXml and CheckSignature ... > Reference reference; ...
    (microsoft.public.dotnet.security)
  • Re: RSA Decryption with public key?
    ... private key by encrypting our data with the private key and giving us back ... The thingy that with RSA often referred as ... "Decryption with public key" is actually a *Signature Verification ... encryption vs signature. ...
    (microsoft.public.dotnet.security)