0

下面的代码尝试更新现有证书。证书已更新,但尽管指定了选项 X509RequestInheritOptions.InheritPrivateKey,但仍会生成新的公钥/私钥。

下面的代码有什么问题,因为目的是保留现有的私钥?在证书管理控制台中,我可以更新证书并保留现有的私钥。

string certificateSerial = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
X509Certificate certificate = getCertificate(certificateSerial);
var objPkcs7 = new CX509CertificateRequestPkcs7();
objPkcs7.InitializeFromCertificate(X509CertificateEnrollmentContext.ContextUser, true, 
Convert.ToBase64String(enrollmentAgentCertificate.GetRawCertData()), 
EncodingType.XCN_CRYPT_STRING_BASE64, 
X509RequestInheritOptions.InheritPrivateKey  & X509RequestInheritOptions.InheritValidityPeriodFlag);

IX509Enrollment ca = new CX509EnrollmentClass();
ca.InitializeFromRequest(objPkcs7);
ca.Enroll();

谢谢

4

1 回答 1

0

似乎问题出在 MSDN 文档中:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa379430%28v=vs.85%29.aspx

该页面指出:“..您还可以使用按位与运算将键继承选择与 InheritNone 或以下标志的任意组合结合起来......”。

但是,如果我们在 InheritPrivateKey = 0x00000003 和 InheritValidityPeriodFlag= 0x00000400 之间使用按位与,我们会得到 0,即 InheritDefault(即没有私钥继承)

对于我的用例,我们需要使用按位或。似乎 C++ SDK 示例也是如此:

https://github.com/theonlylawislove/WindowsSDK7-Samples/blob/master/security/x509%20certificate%20enrollment/vc/enrollpkcs7/enrollPKCS7.cpp

hr = pPkcs7->InitializeFromCertificate(
ContextUser,VARIANT_FALSE, strOldCert, 
XCN_CRYPT_STRING_BINARY,              
(X509RequestInheritOptions)(InheritPrivateKey|InheritTemplateFlag));

在这种情况下,上面的代码应修改为:

X509RequestInheritOptions.InheritPrivateKey  | X509RequestInheritOptions.InheritTemplateFlag);
于 2016-02-02T09:25:12.030 回答