【发布时间】:2012-05-16 10:39:09
【问题描述】:
我正在尝试使用 CNG API 和 Microsoft Certificate Store 中的证书通过 ECDSA 签署文件。我已经阅读了很多文档并且几乎完成了,但是我对从证书中导入私钥感到困惑。我用 RSA 做了同样的事情,但它似乎做得非常不同。这是我到目前为止的代码:
static void signFile()
{
X509Certificate2 myCert =
selectCert(StoreName.My,
StoreLocation.CurrentUser,
"Select a Certificate",
"Please select a certificate from the list below:");
Console.Write("Path for file to sign: ");
string path = Console.ReadLine();
TextReader file = null;
try
{
file = new StreamReader(path);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Write("\nPress any key to return to the main menu: ");
Console.ReadKey();
}
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(file.ReadToEnd());
ECDsaCng dsa = new ECDsaCng(
CngKey.Import(StringToByteArray(myCert.PrivateKey.ToString()),
CngKeyBlobFormat.EccPrivateBlob,
CngProvider.MicrosoftSoftwareKeyStorageProvider));
dsa.HashAlgorithm = CngAlgorithm.Sha384;
byte[] sig = dsa.SignData(data);
TextWriter signatureFile = new StreamWriter("signature.txt");
signatureFile.WriteLine("-----BEGIN SHA384 SIGNATURE-----" +
ByteArrayToString(sig) +
"-----END SHA384 SIGNATURE-----");
signatureFile.Close();
}
我得到了错误
System.NotSupportedException:不支持证书密钥算法。
我的证书是 ECDSA_P256 sha384ECDSA,具有以下扩展名:
Digital Signature, Non-repudiation, independent signing revocation list (CRL), CRL Signing (CRL) (c2)
Server Authentication (1.3.6.1.5.5.7.3.1)
Client Authentication (1.3.6.1.5.5.7.3.2)
Code Signing (1.3.6.1.5.5.7.3.3)
Unknown Key Usage (1.3.6.1.4.1.311.2.1.22)
Unknown Key Usage (1.3.6.1.4.1.311.2.1.21)
IKE-intermediary IP-security (1.3.6.1.5.5.8.2.2)
看起来好像证书是问题,但我不确定它是否可能是代码。
这是我的公钥证书:
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 2 (0x2)
Signature Algorithm: ecdsa-with-SHA384
Issuer: C=##, O=#######, OU=#####, OU=#####, CN=###########
Validity
Not Before: Apr 27 16:35:51 2012 GMT
Not After : Apr 26 16:35:51 2017 GMT
Subject: C=##, O=###########, OU=#####, CN=#############
Subject Public Key Info:
Public Key Algorithm: id-ecPublicKey
Public-Key: (256 bit)
pub:
04:fc:d5:ce:ad:1f:0c:19:b9:3d:2b:bd:7d:f0:8c:
44:46:db:e3:42:14:b1:1a:9f:7c:ab:e1:be:ad:a5:
0c:03:2d:0f:ff:3f:10:d4:69:eb:4c:82:a1:2a:61:
56:45:03:04:a6:49:f7:16:6e:dd:60:22:c6:20:c5:
4d:44:49:21:41
ASN1 OID: prime256v1
X509v3 extensions:
X509v3 Key Usage: critical
Digital Signature, Non Repudiation, CRL Sign
X509v3 Extended Key Usage: critical
TLS Web Server Authentication, TLS Web Client Authentication, Co
de Signing, Microsoft Commercial Code Signing, Microsoft Individual Code Signing
, 1.3.6.1.5.5.8.2.2
X509v3 Authority Key Identifier:
DirName:/C=##/O=#######/OU=#####/OU=#####/CN=######
serial:01
X509v3 Subject Key Identifier:
B7:A8:F9:55:9A:43:9E:BE:1C:4B:62:52:91:C2:F1:39:72:E1:CE:1B
X509v3 Basic Constraints: critical
CA:FALSE
Signature Algorithm: ecdsa-with-SHA384
30:81:88:02:42:01:75:55:f3:64:f9:aa:2a:66:55:b1:ca:dc:
86:ac:1f:7d:2a:ec:10:87:db:74:88:0e:77:e3:18:82:15:a7:
32:91:1a:2d:ea:07:2e:78:8d:dc:8a:18:3c:2b:5a:9b:6a:0f:
97:f6:f8:8d:c5:fc:0e:9f:20:e9:b0:16:90:1a:c4:58:ac:02:
42:01:dc:b3:88:ae:44:54:c4:e0:b7:c2:37:88:0b:19:6b:96:
99:f7:21:12:45:12:21:e5:ab:83:39:a6:47:3a:08:87:b0:fa:
0e:31:1b:97:83:8d:65:30:a1:43:c1:82:27:77:6e:93:89:1b:
bd:57:b1:7a:54:9e:cc:e1:44:cc:74:16:c5
【问题讨论】:
-
Microsoft 对椭圆曲线加密 (ECC) 的支持非常有限。它仅在 NIST 标准素数曲线 P-256、P-384 和 P-521 上提供“椭圆曲线 DSA (ECDSA)”。那些应该是“命名”的曲线。你知道证书中有哪些EC域参数吗?或者,您可以粘贴证书的十六进制、base64 或 ASN.1 文本,以便我们查找。
-
您可以编辑您的问题而不是回答以提供更多信息。重新阅读问题,证书本身不包含私钥。通常,您创建一个密钥对,创建一个包含公钥的证书请求并使用私钥对其进行签名。然后 CA 从中烘焙一个证书并将其发回。该证书仅包含公钥。那么当你尝试导入私钥时,它是什么格式的呢? PKCS#12?那将是
.pkf或.p12文件扩展名。 -
啊,我知道你哪里错了,应该戴上我的眼镜。 ToString 不对私钥进行编码。它只是在私钥上打印一些信息。您可以使用 PrivateKey 属性实际扩展的
DSACryptoServiceProvider,而不是对私钥进行编码/重新编码。 -
那么我应该使用
DSACryptoServiceProvider来实际完成签名还是仅仅处理EC 密钥的导入?如果您可以提供一些示例代码,我很乐意最终将其标记为已解决。再次感谢您的帮助。 -
此外,根据 Internet 工程任务组 (tools.ietf.org/search/rfc3279#page-13),公钥算法属性是正确的,因此这似乎是 Microsoft 的问题。我测试了一个 Microsoft 示例程序(丢失了 URL),我遇到了同样的错误。看起来他们实际上不能支持 ECC,不像 msdn.microsoft.com/en-us/library/windows/desktop/… 所说的那样
标签: c# .net cryptography elliptic-curve cng