如何使用CertEnroll生成扩展验证的自签名证书?

4
由于Windows Azure中存在一个错误,所有在Windows 8应用程序中使用Azure REST API的自签名客户端证书必须指定为扩展验证证书。
为了提供更好的用户体验,我正在尝试在远程服务器上生成此自签名证书。我正在使用作为Windows一部分分发的COM库Certificate Enrolment API,如此答案中描述的那样,来回答如何使用C#创建自签名证书? 代码基本相同,只是稍微修改了一下以适应我的需求:
public static X509Certificate2 CreateSelfSignedCertificate(
    string cname,
    string friendlyName,
    string password)
{
    // create DN for subject and issuer
    var dn = new CX500DistinguishedName();
    dn.Encode("CN=" + cname, X500NameFlags.XCN_CERT_NAME_STR_NONE);

    // create a new private key for the certificate
    CX509PrivateKey privateKey = new CX509PrivateKey();
    privateKey.ProviderName = "Microsoft Base Cryptographic Provider v1.0";
    privateKey.MachineContext = true;
    privateKey.Length = 2048;
    privateKey.KeySpec = X509KeySpec.XCN_AT_SIGNATURE; // use is not limited
    privateKey.ExportPolicy 
        = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG;
    privateKey.Create();

    // Use the stronger SHA512 hashing algorithm
    var hashobj = new CObjectId();
    hashobj.InitializeFromAlgorithmName(
        ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID,
        ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY, 
        AlgorithmFlags.AlgorithmFlagsNone,
        "SHA512");

    // Create the self signing request
    var cert = new CX509CertificateRequestCertificate();
    cert.InitializeFromPrivateKey(
        X509CertificateEnrollmentContext.ContextMachine,
        privateKey,
        string.Empty);

    cert.Subject = dn;
    cert.Issuer = dn; // the issuer and the subject are the same
    cert.NotBefore = DateTime.Now;
    cert.NotAfter = DateTime.Now.AddYears(50); 
    cert.HashAlgorithm = hashobj;

    var clientAuthenticationOid = new CObjectId();
    clientAuthenticationOid.InitializeFromValue("1.3.6.1.5.5.7.3.2");

    // Set up cert to be used for Client Authentication.
    var oids = new CObjectIds();
    oids.Add(clientAuthenticationOid);

    var eku = new CX509ExtensionEnhancedKeyUsage();
    eku.InitializeEncode(oids);

    cert.X509Extensions.Add((CX509Extension)eku);

    // Add the certificate policy.
    var policy = new CCertificatePolicy();
    policy.Initialize(clientAuthenticationOid);

    // THIS IS WRONG - NEEDS A DIFFERENT QUALIFIER
    var qualifier = new CPolicyQualifier();
    qualifier.InitializeEncode(
        "c0",
         PolicyQualifierType.PolicyQualifierTypeUserNotice);

    policy.PolicyQualifiers.Add(qualifier);

    var policies = new CCertificatePolicies();
    policies.Add(policy);

    var ecp = new CX509ExtensionCertificatePolicies();
    ecp.InitializeEncode(policies);

    cert.X509Extensions.Add((CX509Extension)ecp);

    cert.Encode();

    // Do the final enrolment process
    var enroll = new CX509Enrollment();
    enroll.InitializeFromRequest(cert); // load the certificate
    enroll.CertificateFriendlyName = friendlyName;
    string csr = enroll.CreateRequest(); // Output the request in base64

    // and install it back as the response
    enroll.InstallResponse(
        InstallResponseRestrictionFlags.AllowUntrustedCertificate,
        csr,
        EncodingType.XCN_CRYPT_STRING_BASE64,
        ""); // no password

    // output a base64 encoded PKCS#12 for import to .NET
    var base64encoded = enroll.CreatePFX(
        password,
        PFXExportOptions.PFXExportChainWithRoot);

    // instantiate the target class with the PKCS#12 data
    return new X509Certificate2(
        System.Convert.FromBase64String(base64encoded),
        password,
        X509KeyStorageFlags.Exportable);
}

我找到了接口ICertificatePolicy,它似乎代表了正确的结构类型,但我无法推断出正确的IPolicyQualifier的使用。在我的代码中,限定符是:

为了清晰起见,这里是您在Windows 8证书管理器中配置信息的地方:

where to enable extended validation

这将在证书上产生此属性:

extended validation property on a certificate

我的代码目前生成此属性:

not quite extended validation property on a certificate

接近了,但还不够。

是否有另一种方法可以将数据加载到IPolicyQualifier中,以便它产生预期的结果,也许使用InitialiseDecode方法?


你有这个“bug”的参考资料吗? - EricLaw
@EricLaw: 这个帖子是最接近的:http://social.msdn.microsoft.com/Forums/windowsapps/en-US/0d005703-0ec3-4466-b389-663608fff053/using-client-side-certificates-in-windows-metro?forum=winappswithcsharp - Paul Turner
你所提到的帖子似乎根本没有提到“扩展验证”(Extended Validation);它只提到用于客户端身份验证的证书需要在扩展密钥用途(EKU)中包含客户端身份验证 OID,这是有道理的(并不是真正的错误)。而“EV”证书则完全是另一回事。 - EricLaw
1
抱歉 - 距离我上次考虑这个问题已经过去了18个月,而且它非常具体地涉及到需要一个很难生成的扩展验证属性。 - Paul Turner
就EV的工作方式而言,你需要在证书中放置一个OID,然后还必须在根证书存储中的条目上具有匹配的OID;该条目是附加到根条目的元数据,但实际上并不在根本身。 - EricLaw
1个回答

1
如果您想将客户端身份验证OID设置为"1.3.6.1.5.5.7.2",那么我认为您的代码可以按预期工作。您如何验证代码没有设置此值?
我使用您的代码编写了以下代码,以保存生成的证书:
X509Certificate2 xx = CreateSelfSignedCertificate("Avkash CNAME", "Test User Friendly Name", "xx");
byte[] bCertExported = xx.Export(X509ContentType.Pkcs12, "xx");
File.WriteAllBytes("c:\\Installbox\\test.pfx", bCertExported);

我在本地安装证书并检查了客户端身份验证OID,结果如下:

enter image description here


我已经更新了我的问题并提供了更多信息。您在此处提供的内容有助于确认我的代码有效,但它并没有添加我所说的信息。 - Paul Turner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接