C#证书续订请求

时间:2016-02-01 11:25:47

标签: c# certificate renewal

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

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

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();

由于

1 个答案:

答案 0 :(得分:0)

似乎问题出在MSDN文档中:

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

该页面指出:“..您还可以使用按位AND操作将密钥继承选项与InheritNone或以下标志的任意组合组合......”。

但是,如果我们在InheritPrivateKey = 0x00000003和InheritValidityPeriodFlag = 0x00000400之间使用bitwise-AND,则得到0,即InheritDefault(即没有私钥继承)

对于我的用例,我们需要使用按位OR。似乎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);