CngKey.Create 不支持请求的操作

2024-06-28

我正在尝试在 C# 程序集中(以编程方式)动态生成自签名证书(目标.NET 4.0),作为根CA来生成其他证书。该证书不需要保留在 Windows 证书存储中,我会将其导出为文件。

通读这个问题 https://stackoverflow.com/q/13806299/3136474(特别是,@dthorpe的回答 https://stackoverflow.com/a/13830861/3136474),我决定尝试一下公共运行库安全 http://clrsecurity.codeplex.com/.

The CLR Security库添加了扩展方法CngKey类 https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngkey(v=vs.110).aspx生成自签名证书,但我无法成功创建实例CngKey with:

var key = CngKey.Create(CngAlgorithm.Sha1); //same with Sha256, Sha512 and MD5
//or
var key = CngKey.Create(CngAlgorithm.Sha1, null, new CngKeyCreationParameters()
{
    ExportPolicy = CngExportPolicies.AllowExport,
    KeyUsage = CngKeyUsages.AllUsages,
    KeyCreationOptions = CngKeyCreationOptions.MachineKey,
});

这些行中的任何一行都会引发异常:

System.Security.Cryptography.CryptographicException 未处理
HResult=-2146893783
消息=不支持请求的操作。

Source=System.Core  
StackTrace:  
  at System.Security.Cryptography.NCryptNative.CreatePersistedKey(SafeNCryptProviderHandle provider, String algorithm, String name, CngKeyCreationOptions options)  
  at System.Security.Cryptography.CngKey.Create(CngAlgorithm algorithm,  String keyName, CngKeyCreationParameters creationParameters)  
  at System.Security.Cryptography.CngKey.Create(CngAlgorithm algorithm)  
  at Tests.Program.Main(String[] args) at Program.cs:line 51

通过SO和互联网搜索,我检查了以下内容:

  • 我正在运行 Windows 7 机器(因此它支持 RPC)MSDN https://msdn.microsoft.com/en-us/library/ms838991.aspx)
  • 在 Windows Server 2012 机器上尝试过,同样的错误
  • 该进程以管理员身份运行(因此它可以访问所有证书存储)
  • 服务CNG Key Isolation and Remote Procedure Call (RPC)正在跑步

任何帮助,将不胜感激。


小题外话:在google搜索这个问题时发现一个网站HRESULT描述 http://codeswamp.tolon.co.uk/codeswamp_80090029.htmlSO 和 MSDN 上方便的搜索工具(我只是用谷歌搜索了你的HRESULT code -2146893783)


我找到了一个MSDN 上的主题 https://social.msdn.microsoft.com/Forums/en-US/6ab5a1e5-2a0c-40cf-b558-aa842859f067/ncryptcreatepersistedkey-operation-not-supported?forum=windowssecurity其中包含类似失败的代码HRESULT,并且作者提供了一个链接到有关 CNG 的 MSDN 文章 https://msdn.microsoft.com/en-us/library/windows/desktop/aa376242.aspx:

NCRYPT_ALGORITHM_GROUP_PROPERTYL“算法组”
一个以 null 结尾的 Unicode 字符串,其中包含对象的算法组的名称。此属性仅适用于键。 Microsoft 密钥存储提供商返回以下标识符:

  • NCRYPT_RSA_ALGORITHM_GROUP
    “RSA”,RSA算法组。
  • NCRYPT_DH_ALGORITHM_GROUP
    “DH”,Diffie-Hellman算法组。
  • NCRYPT_DSA_ALGORITHM_GROUP
    “DSA”,DSA算法组。
  • NCRYPT_ECDSA_ALGORITHM_GROUP
    “ECDSA”,elliptic curve DSA算法组。
  • NCRYPT_ECDH_ALGORITHM_GROUP
    “ECDH”,elliptic curve Diffie-Hellman算法组。

我还在 MSDN 上找到了一篇关于CNG关键存储提供商 https://msdn.microsoft.com/en-us/library/windows/desktop/bb931355.aspx,其中包含类似的算法列表:

  • 迪菲-赫尔曼 (DH)
    秘密协议和密钥交换,512 to 4096 in 64-bit increments
  • 数字签名算法(DSA) 签名,512 to 1024 in 64-bit increments
  • 椭圆曲线 Diffie-Hellman (ECDH) 秘密协议和密钥交换,P256, P384, P521
  • 椭圆曲线数字签名算法(ECDSA) 签名,P256, P384, P521
  • RSA 非对称加密和签名,512 to 16384 in 64-bit increments

所以,正如你所说,你只尝试过Sha1, Sha256, Sha512 and MD5,也许你只是使用另一个可用列表中的算法 https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngalgorithm.aspx?您可以找到上面提到的那些:

  • RSA https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngalgorithm.rsa.aspx
  • ECDsa
    • P256 https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngalgorithm.ecdsap256.aspx
    • P384 https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngalgorithm.ecdsap384.aspx
    • P521 https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngalgorithm.ecdsap521.aspx
  • ECDiffieHellman
    • P256 https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngalgorithm.ecdiffiehellmanp256.aspx
    • P384 https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngalgorithm.ecdiffiehellmanp384.aspx
    • P521 https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngalgorithm.ecdiffiehellmanp521.aspx

这里其他开发者成功创建 https://stackoverflow.com/a/20505976/213550其中之一并能够导出它:

var cngKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256, null,
    new CngKeyCreationParameters { ExportPolicy = CngExportPolicies.AllowPlaintextExport });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CngKey.Create 不支持请求的操作 的相关文章

随机推荐