【发布时间】: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),因此无法导出私钥。
【问题讨论】:
-
System.Security.Cryptography.X509Certificates.CertificateRequest 类不会添加您不需要的任何内容。它可能会做更多你想做的事(尽管它无法将 CSR 发送到任何地方,这是留给调用者的问题)。
标签: c# .net cryptography certificate csr