SignedXml.LoadXml()引发CryptographicException

时间:2018-06-26 06:45:19

标签: c# xml signedxml cryptographicexception

我已经连接了一个外部WebService,该接口一直运行完美,直到2018年5月中旬。我突然开始收到一条CryptographicException消息,消息为“格式错误的XML签名”。我的调查告诉我以下内容:

LoadXml(signature)行抛出异常;从System.Security.Cryptography.Xml.SignedXml继承的类中

代码如下:

1
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[[1, 2, 3, 4, 5, 6, 7, 8, 9]]
[[[1, 2, 3, 4, 5, 6, 7, 8, 9]]]
[[[[1, 2, 3, 4, 5, 6, 7, 8, 9]]]]

到目前为止,我学到的是: 我和一位同事在Windows 10的两个不同版本上本地测试了该代码。他的版本比我的版本新。他得到了错误,而我没有。

代码处理的xml完全相同

该错误于2018年5月16日开始,并且在该日期服务器安装了此安全更新https://support.microsoft.com/da-dk/help/4099635/security-and-quality-rollup-for-net-framework-3-5-4-5-2-4-6-4-6-1-4-6

我们知道较早的类似更新会导致类似的问题: https://support.microsoft.com/en-us/help/3148821/after-you-apply-security-update-3141780-net-framework-applications-enc

有没有人知道如何解决这个问题(还有其他人遇到这个问题)吗?

PS。该代码在Windows srv 2012 R2上运行,并且该问题在Windows srv 2016上也存在

1 个答案:

答案 0 :(得分:0)

我们找到了解决方案!似乎System.Security更新已强制对XML签名进行更严格的验证。

在我们收到的XML中,签名元素的id属性写为“ id”。根据XML签名的架构(http://www.w3.org/2000/09/xmldsig#),它应该为“ Id”。在安全更新之前,LoadXml方法并没有抛出该错误,但是在此之后发生了。

由于收到的XML具有不适当的属性,因此我们创建了以下变通办法来更改属性:

if (signature.Attributes.GetNamedItem("id") is XmlAttribute oldId)
{
    signature.Attributes.Remove(oldId);
    if (signature.OwnerDocument != null)
    {
        var newId = signature.OwnerDocument.CreateAttribute("Id");
        newId.Value = oldId.Value;
        signature.Attributes.Append(newId);
    }
}

尽管我们不明白为什么验证应该如此严格,以至于只有“ Id”才能确定。。。不过,我怀疑我们是否可以通过Microsoft进行回滚:D