【问题标题】:C# Certificate Renewal RequestC# 证书更新请求
【发布时间】:2016-02-01 11:25:47
【问题描述】:

以下代码尝试更新现有证书。 证书已更新,但尽管指定了 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();

谢谢

【问题讨论】:

    标签: c# certificate renewal


    【解决方案1】:

    似乎问题出在 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);
    

    【讨论】:

    • 并非所有标志都可以一起使用,Microsoft 的文档也没有为您提供所有有效的组合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2012-04-18
    • 2020-12-09
    • 2011-02-19
    • 1970-01-01
    • 2014-06-06
    相关资源
    最近更新 更多