使用 RSACryptoServiceProvider 从 SignedInfo 计算 SignatureValue

Calculating SignatureValue from SignedInfo using RSACryptoServiceProvider

我是 XML 数字签名主题的新手。阅读了一些教程后,我决定动手做一些 activity 并尝试遵循一些示例来提高我的理解。有一点我需要帮助。

这是我正在尝试的示例:

https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-verify-the-digital-signatures-of-xml-documents

要签名的数据很简单(copy/paste 来自站点的 TAB 字符,所以我在将文件保存到磁盘之前将所有 TAB 变成单个 SPACE):

<root>  
 <creditcard>  
  <number>19834209</number>  
  <expiry>02/02/2002</expiry>  
 </creditcard>  
</root>

当我 运行 使用此数据的示例代码时,将生成以下 Signature 元素并将其添加到根元素内的文件中:

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /><DigestValue>AjI/9vkjyZWM9noiF0J0RRZEUAjBil96ETvT9Cl85rI=</DigestValue></Reference></SignedInfo><SignatureValue>YY5B15W2W9TyoV4HjZTh554Z/yh06iz7+yKM/AgsdzU6QvakH54rPJZ9rQkIrLehxJlX76xXgp9Pe+ougMA6QSj7NvqRBXoxgQcpf1W/m4cfCzTd7iT7A93xhqCdU1F4AohvJZRqwbeApOqxjuXmcF9kqLFDcSTuRZSgIPJdSS8=</SignatureValue></Signature>

我手动将输入规范化并编写了一些代码,并验证了规范化输入的 SHA256 摘要确实与 Signature 中的 DigestValue 匹配(即我对事物在covers,至少在规范化方面,到目前为止是正确的)。但是,到目前为止,我验证签名值的尝试都失败了。无论我做什么,我都无法获得 API 产生的签名值。当然,示例的验证部分(使用 API)起作用并表明签名有效。

这是“手动”验证的代码:

CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = "XML_DSIG_RSA_KEY";

RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);

byte[] originalData = File.ReadAllBytes("signedinfo.xml");
byte[] signedBytes;

try
{
    SHA256 mySHA256 = SHA256.Create();
    byte[] digest=mySHA256.ComputeHash(originalData);
    signedBytes = rsaKey.SignHash(digest, CryptoConfig.MapNameToOID("SHA256"));
    Console.WriteLine(System.Convert.ToBase64String(signedBytes));
}
catch (CryptographicException e)
{
    Console.WriteLine(e.Message);
}

SignedInfo.xml如下:

<SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /><DigestValue>AjI/9vkjyZWM9noiF0J0RRZEUAjBil96ETvT9Cl85rI=</DigestValue></Reference></SignedInfo>

我在 https://www.di-mgt.com.au/xmldsig2.html 找到了一份指南,其中提到了在规范化 SignedInfo 期间传播命名空间,所以我也尝试了以下 SignedInfo.xml

<SignedInfo xmlns="http://www.w3.org/2000/09/xmldsig#><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /><DigestValue>AjI/9vkjyZWM9noiF0J0RRZEUAjBil96ETvT9Cl85rI=</DigestValue></Reference></SignedInfo>

这仍然不会产生与示例中的 API 相同的签名。

如果能帮助我找出我做错了什么,我将不胜感激。

我没有完整阅读指南是我的错误。我忘了将元素从 <tag /> 形式扩展到 <tag></tag> 形式。因此,正确的规范 SignedInfo

<SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></CanonicalizationMethod><SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"></SignatureMethod><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"></Transform></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"></DigestMethod><DigestValue>AjI/9vkjyZWM9noiF0J0RRZEUAjBil96ETvT9Cl85rI=</DigestValue></Reference></SignedInfo>

有了这个 SignedInfo,我可以“手动”验证签名。

虽然我在做,但让我再提供一点信息,这些信息可能会对看到这个问题的其他人有所帮助:There's a note 关于 .Net 如何实现包容性 c14n 算法 (Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315")不同于规范和其他实现。这种差异对跨平台验证有影响。在我的学习中这并没有影响到我,但是万一需要做跨平台验证,这是一个需要注意的问题。