gpt4 book ai didi

c# - 从 PKCS#12 字节数组构建 X509Certificate2 如何抛出 CryptographicException ("The system cannot find the file specified.")?

转载 作者:IT王子 更新时间:2023-10-29 04:02:23 25 4
gpt4 key购买 nike

我试图从一个字节数组中的 PKCS#12 blob 构造一个 X509Certificate2 并得到一个相当令人费解的错误。此代码在具有 Windows XP 管理员权限的桌面应用程序中运行。

堆栈跟踪如下,但我在尝试排除故障时迷路了,因为 _LoadCertFromBlob 被标记为 [MethodImpl(MethodImplOptions.InternalCall)]

System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)
at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)

编辑: blob 是由 BouncyCastle for C# 生成的真正的 PKCS#12包含 RSA 私钥和证书(自签名或最近在 CA 注册)——我要做的是将私钥和证书从 BouncyCaSTLe 库转换为 System.Security.Cryptography 库,方法是从一个并导入到另一个。这段代码适用于它所试过的绝大多数系统;我只是从未见过该构造函数抛出的特定错误。可能是那个盒子上的某种环境怪异。

编辑 2: 该错误发生在不同城市的不同环境中,我无法在本地重现它,因此我可能最终不得不将其归因于 XP 损坏安装。

不过,既然你问了,这里就是有问题的片段。该代码采用 BouncyCaSTLe 表示形式的私钥和证书,从个人 key 存储中删除相同专有名称的所有先前证书,并通过中间 PKCS#12 blob 将新的私钥和证书导入个人 key 存储。

// open the personal keystore
var msMyStore = new X509Store(StoreName.My);
msMyStore.Open(OpenFlags.MaxAllowed);

// remove any certs previously issued for the same DN
var oldCerts =
msMyStore.Certificates.Cast<X509Certificate2>()
.Where(c => X509Name
.GetInstance(Asn1Object.FromByteArray(c.SubjectName.RawData))
.Equivalent(CurrentCertificate.SubjectDN))
.ToArray();
if (oldCerts.Length > 0) msMyStore.RemoveRange(new X509Certificate2Collection(oldCerts));

// build a PKCS#12 blob from the private key and certificate
var pkcs12store = new Pkcs12StoreBuilder().Build();
pkcs12store.SetKeyEntry(_Pkcs12KeyName,
new AsymmetricKeyEntry(KeyPair.Private),
new[] {new X509CertificateEntry(CurrentCertificate)});
var pkcs12data = new MemoryStream();
pkcs12store.Save(pkcs12data, _Pkcs12Password.ToCharArray(), Random);

// and import it. this constructor call blows up
_MyCertificate2 = new X509Certificate2(pkcs12data.ToArray(),
_Pkcs12Password,
X509KeyStorageFlags.Exportable);
msMyStore.Add(_MyCertificate2);
msMyStore.Close();

最佳答案

您有 PKCS#12 还是只有 PFX 文件?在 Microsoft 世界中,它是相同的,但其他人有不同的想法(请参阅 this archived page)。

你可以试试跟随

X509Certificate2 cert = X509Certificate2(byte[] rawData, "password");
X509Certificate2 cert2 = X509Certificate2(byte[] rawData, "password",
X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet |
X509KeyStorageFlags.Exportable);

( X509Certificate2(Byte[]) ) 或

X509Certificate2 cert = X509Certificate2("C:\Path\my.pfx", "password");

(如果您需要使用一些标志,请参阅 Microsoft Docs 上的 X509Certificate2(String, String)Import(String, String, X509KeyStorageFlags))

已更新:如果您插入代码片段而不仅仅是异常堆栈跟踪,将会很有帮助。

您使用哪个 X509KeyStorageFlags?您可以使用 Process Monitor找出哪个文件找不到 X509Certificate2 构造函数。例如,在出现问题的 Windows XP 上可能没有当前用户的默认 key 容器。您可以创建它并重试导入。

关于c# - 从 PKCS#12 字节数组构建 X509Certificate2 如何抛出 CryptographicException ("The system cannot find the file specified.")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3826321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com