signedXml.LoadXml((XmlElement)nodeList[0]); returns Malformed SignedInfo/Reference

别来无恙 提交于 2020-04-18 01:08:13

问题


I've been developing this console application for about close to 2-3 months now.

What I'm trying to achieve from the console application is to generate data files and sign the files into a "signature.xml" file. It is working using several custom methods to sign (without use of Regedit Key).

However, once the number of references goes over 99, it refuses to sign and gives the error "Malformed SignedInfo/Reference..". Even though previously it did work and this error has never surfaced.

I've tried googling and searching for solutions, but to no avail for signing more than XX amount of references. Note that there is no syntax error or typo in the signature file, it is generated exactly the same as it was when it is under 100 file references.

There is also no bad hash of files generated, so I really have no idea what went wrong. No exceptions caught when retrieving the hash and file paths of the generated files that are to be signed.

I hope the community here will be able to help :(

Snippet of the code:

private static byte[] GetC14NDigest(XmlDocument xmlDocument, HashAlgorithm hash)
{
    try
    {
        byte[] digestedSignedInfo;
        XmlDocument containingDocument = xmlDocument;
        XmlElement context = xmlDocument.DocumentElement;

        XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");
        SignedXml signedXml = new SignedXml();
        signedXml.LoadXml((XmlElement)nodeList[0]);

        string baseUri = (containingDocument == null ? null : containingDocument.BaseURI);
        XmlResolver resolver = new XmlSecureResolver(new XmlUrlResolver(), baseUri);
        XmlDocument doc = PreProcessElementInput(signedXml.SignedInfo.GetXml(), revolver, baseUri);

        Transform c14nMethodTransform = signedXml.SignedInfo.CanonicalizationMethodObject;
        digestedSignedInfo = c14nMethodTransform.GetDigestedOutput(hash);
        return digestedSignedInfo;    
    }
    catch(Exception ex)
    {
        Log.LogText(ex.Message);
        Log.LogText(ex.StackTrace);
        return null;
    }
}

来源:https://stackoverflow.com/questions/60541242/signedxml-loadxmlxmlelementnodelist0-returns-malformed-signedinfo-referen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!