SignedXml.LoadXML((XmlElement)nodeList[0]) throwing "Malformed element SignedInfo/Reference"

298 Views Asked by At

I am having trouble when reading SignedInfo from XML of a signed document, when calling

SignedXml signedXml;
Directory.SetCurrentDirectory(TmpDir);
XmlDocument xmlDoc = new XmlDocument();

foreach(Relationship signature in signs)
{
   if(_InMemoryData.ContainsKey(signature.FilePath) || File.Exists(signature.FilePath))
    {
        using(Stream xmlStream = LoadDataItem(signature.FilePath))
        {
            xmlDoc.Load(xmlStream);
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature");
            if(nodeList.Count == 1)
            {
                signedXml = new SignedXml(xmlDoc);
                signedXml.LoadXml((XmlElement)nodeList[0]); //throws

on line signedXml.LoadXml((XmlElement)nodeList[0]); it is throwing exception with message "System.Security.Cryptography.CryptographicException: Malformed element SignedInfo/Reference".

The only odd thing about this XML that it have 145 recipients. However other software manages to read this without any issues, so I don't think this is an issue.

Searching for exception with "Malformed element..." mostly suggests that element's Id value has numbers in it and that is not allowed. Checked other similar XML files and they all do have numbers in them, so that should not be the case.

1

There are 1 best solutions below

0
On BEST ANSWER

After some setting up and debuging system libraries I found out where it throws the excpetion and apperantly by default it only allow 100 recipients. To change that, I needed to add a new registry key named "SignedXmlMaxReferencesPerSignedInfo" with DWord value to be greater than 145 in my case to "Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework\Security" and it started working.