【问题标题】:Generated CSR has unexpected extension attributes using CertificateEnrollmentManager使用 CertificateEnrollmentManager 生成的 CSR 具有意外的扩展属性
【发布时间】:2022-07-18 16:59:04
【问题描述】:

我目前正在使用 Windows.Security.Cryptography.Certificates 中的 CertificateEnrollmentManager 生成证书请求。 CSR 已成功生成,但其扩展的属性与我在 CertificateRequestProperties 中指定的属性不同。

这是请求属性:

           var basicConstraint = new CertificateExtension
            {
                ObjectId = Constants.Oids.BASIC_CONSTRAINT,
                IsCritical = false,
                Value = new BasicConstraints(false).GetEncoded()
            };

            var extendedKeyUsage = new CertificateExtension
            {
                ObjectId = Constants.Oids.EXTENDED_KEY_USAGE,
                IsCritical = false,
                Value = new ExtendedKeyUsage(
                    new[] {KeyPurposeID.IdKPClientAuth}
                ).GetEncoded()
            };

            var keyUsage = new CertificateExtension
            {
                ObjectId = Constants.Oids.KEY_USAGE,
                IsCritical = false,
                Value = new KeyUsage(KeyUsage.DigitalSignature).GetEncoded()
            };
            
            var certificateRequestProperties = new CertificateRequestProperties
            {
                Subject = subject,
                KeyUsages = EnrollKeyUsages.Signing,
                KeyStorageProviderName = microsoftPlatformCryptoProvider,
                Exportable = ExportOption.NotExportable,
                KeyProtectionLevel = keyProtectionLevel,
                KeyAlgorithmName = keyAlgorithmName,
                HashAlgorithmName = hashAlgorithmName,
                FriendlyName = CERTIFICATE_FRIENDLY_NAME,
                Extensions = {basicConstraint, extendedKeyUsage, keyUsage},
                UseExistingKey = false
            }; 

这就是我生成 SCR 的方式:

 var csr = await CertificateEnrollmentManager.UserCertificateEnrollmentManager
            .CreateRequestAsync(certificateRequestProperties);

CSR 已成功生成,但生成的扩展名不正确:(为简洁起见,排除了一些属性)

BasicConstraints=ObjectId: 2.5.29.19 Criticality=true
ExtendedKeyUsage=ObjectId: 2.5.29.37 Criticality=false
KeyUsage=ObjectId: 2.5.29.15 Criticality=true
SubjectKeyIdentifier=ObjectId: 2.5.29.14 Criticality=false

生成的 CSR 有什么问题:

  • 它已经自动添加了SubjectKeyIdentifier,但是我只指定了3个扩展。
  • criticality 不是属性中指定的。我指定了 false,但它生成了 true

我的问题是,如何生成我在请求属性中指定的 CSR?

PS:

  • 我们不能只在服务器端修改收到的 CSR。
  • 我需要为 CSR 使用 TPM (MicrosoftPlatformCryptoProvider),因此无法导出私钥。

【问题讨论】:

标签: c# .net cryptography certificate csr


【解决方案1】:

我自己解决了,答案很简单。事实证明,我们可以使用请求中的 SuppressedDefaults 属性来抑制默认值。 Documented here.

所以请求属性将如下所示:

// added new constant
public const string SUBJECT_KEY_IDENTIFIER = "2.5.29.14";

// updated request properties
var certificateRequestProperties = new CertificateRequestProperties
        {
            Subject = subject,
            KeyUsages = EnrollKeyUsages.Signing,
            KeyStorageProviderName = microsoftPlatformCryptoProvider,
            Exportable = ExportOption.NotExportable,
            KeyProtectionLevel = keyProtectionLevel,
            KeyAlgorithmName = keyAlgorithmName,
            HashAlgorithmName = hashAlgorithmName,
            FriendlyName = CERTIFICATE_FRIENDLY_NAME,
            Extensions = {basicConstraint, extendedKeyUsage, keyUsage},
            UseExistingKey = false,
            SuppressedDefaults = { SUBJECT_KEY_IDENTIFIER } 
        }; 

我很确定在我在这里发布问题之前尝试过这个解决方案,但也许我当时只是使用了错误的 OID。

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 2021-04-14
    相关资源
    最近更新 更多