【发布时间】:2022-03-02 23:33:25
【问题描述】:
我已将我们的一个 Web 应用程序从 .NET Framework 4.6 升级到 4.8。在代码深处的某个地方,一个 XML 字符串被签名,这个签名的 XML 被发送回客户端,客户端将验证签名。 XML 签名使用SignedXml 类。
由于其遗留性质,SHA1 仍在使用(请耐心等待!)。为了在升级 .NET Framework 时不破坏这种行为,我必须在实际签署 XML 之前设置以下 AppContext 开关:
AppContext.SetSwitch("Switch.System.Security.Cryptography.Xml.UseInsecureHashAlgorithms", true);
AppContext.SetSwitch("Switch.System.Security.Cryptography.Pkcs.UseInsecureHashAlgorithms", true);
这解决了我本地机器上的所有问题,我可以成功签署 XML。
现在,在将新代码版本部署到我们的暂存环境中时,出现了一个新问题:XML 签名失败并出现Invalid algorithm specified 异常。这是堆栈跟踪:
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash, Int32 cbHash, ObjectHandleOnStack retSignature)
at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash)
at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] rgbHash, Int32 calgHash)
at System.Security.Cryptography.Xml.SignedXml.ComputeSignature()
现在我偶然发现了this question,尤其是that particular answer。
因此,我使用 certutil 检查了 CSP,得到了以下结果:
- 本地 →
Microsoft Enhanced RSA and AES Cryptographic Provider→ 有效 - 暂存 →
Microsoft Enhanced Cryptographic Provider v1.0→ 不起作用
问题:是否有机会减轻这种行为,例如。 G。通过使用另一段代码对 XML 进行签名?还是我必须重新创建证书?
提前致谢!
【问题讨论】:
-
您是否尝试针对不同版本的网络?您可能需要构建一个类库来使用目标 Net 4.6 进行加密,然后从 Net 4.8 应用程序调用该库。
-
@jdweng 这样就足够了吗?嵌入到 .NET 4.8 进程中的 .NET 4.6 程序集真的最终会在 4.6 中执行吗?
-
源代码中有条件编译指令,用于库的目标选项工作。所以对于不同的目标,中间的 obj 文件是不同的。它仍然使用编译代码的机器上安装的 Net 版本运行。
-
@jdweng 我将签名代码移到了针对 .NET 4.6.2 的不同程序集中,但问题仍然存在。还有其他想法吗?
-
原始代码是 4.6 还是 4.6.2?