将私钥关联到 PFXExportCertStoreEx 的证书
我正在尝试将证书导出到 pfx 文件。 这就是我所做的(简化的):
h = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, NULL, CERT_STORE_CREATE_NEW_FLAG, NULL);
p = CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
CertBlob.pbData, CertBlob.cbData);
CertSetCertificateContextProperty(p, CERT_KEY_PROV_HANDLE_PROP_ID, 0, &hPrivKey);
CertAddCertificateContextToStore(h, p, CERT_STORE_ADD_ALWAYS, NULL);
PFXExportCertStoreEx(h, &SomeBlob, L"", NULL, EXPORT_PRIVATE_KEYS);
创建 PFX,不导出私钥。 有人曾经将私钥导出到 pfx 吗? 将私钥附加到证书以便导出的正确方法是什么?
I'm trying to export certificate to pfx file. Here's what I do (simplified):
h = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, NULL, CERT_STORE_CREATE_NEW_FLAG, NULL);
p = CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
CertBlob.pbData, CertBlob.cbData);
CertSetCertificateContextProperty(p, CERT_KEY_PROV_HANDLE_PROP_ID, 0, &hPrivKey);
CertAddCertificateContextToStore(h, p, CERT_STORE_ADD_ALWAYS, NULL);
PFXExportCertStoreEx(h, &SomeBlob, L"", NULL, EXPORT_PRIVATE_KEYS);
PFX created, no private key exported. Anyone ever exported private key to pfx? What's the proper way to attach private key to certificate so that it could be exported?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,
CertSetCertificateContextProperty(p, CERT_KEY_PROV_HANDLE_PROP_ID ...)
不好。 需要这样做:
提供者名称和其他废话与用于生成实际密钥的信息相匹配至关重要。 不需要设置提供者句柄或任何类似的东西。 它还必须在
CertAddCertificateContextToStore
之前完成。这是我发现将私钥附加到证书的唯一方法。
Apparently,
CertSetCertificateContextProperty(p, CERT_KEY_PROV_HANDLE_PROP_ID ...)
is not good. Need to do this instead:
It's critical that provider name and other crap match the information that was used to generate actual key. It's not needed to set provider handle or any of that stuff. It also must be done before
CertAddCertificateContextToStore
.This is the only way that I found to attach private key to a certificate.
对于后代:
该问题与
CertAddCertificateContextToStore
调用有关。事实上,它不会将
CERT_KEY_PROV_HANDLE_PROP_ID
属性复制到下一个上下文。 (这一事实已在备注中注明)解决方案:
用新上下文的句柄填充最后一个参数,并将属性从旧上下文复制到下一个上下文。
For the posterity:
The problem is related to the
CertAddCertificateContextToStore
call.Indeed, it does not copy the
CERT_KEY_PROV_HANDLE_PROP_ID
property to the next context. (this fact is noted in the remark)Solution:
Fill the last parameter with a handle to the new context and copy the property from the old context to the next one.