当 Azure 密钥保管库显示 "one or more x.509 properties are invalid" 时,我该如何调试我的证书有什么问题?

When an Azure key vault says "one or more x.509 properties are invalid", how can I debug what is wrong with my certificate?

我正在尝试在 C# 代码中生成 X509 证书(使用 class X509Certificate2)并将这些证书上传到 Azure Key Vault。当我尝试上传它们时(以编程方式或通过浏览器中的 Azure 门户),我收到以下错误消息:

The specified X.509 certificate content is invalid. Error: one or more x.509 properties are invalid.

如何调试我的证书生成有什么问题?是否记录了可接受的 X509 属性组合?如果是这样,我在哪里可以阅读它?

来自certutil -dump foo.pfx的输出:

> certutil -dump E:\Raven\Certificates\test2.pfx
Enter PFX password:
================ Certificate 0 ================
================ Begin Nesting Level 1 ================
Element 0:
Serial Number: 86ae932f199f419115e8087f3f0cb6df747bd2adf966d46aab194f4283849635
Issuer: CN=intermediate CA
NotBefore: 24-01-2022 11:22
NotAfter: 25-01-2022 11:22
Subject: O=foo, CN=bar
Non-root Certificate
Cert Hash(sha1): a700464f1708cec627eabcd007ef574d1f0fc140
----------------  End Nesting Level 1  ----------------
Provider = Microsoft Software Key Storage Provider
Private key is NOT plain text exportable
Signature test passed
CertUtil: -dump command completed successfully.

在我的例子中,结果是两件事:

  1. 序号。我正在使用 RandomNumberGenerator.GetBytes(32) 生成序列号,这显然很糟糕。如果我这样做,金库会更快乐 Guid.NewGuid().ToByteArray()
  2. 标志(枚举X509KeyUsageFlags)。我测试了几种标志组合,结果是每当我使用标志 X509KeyUsageFlags.KeyEncipherment 时,保险库都会拒绝证书。

我不知道为什么。我假设如果您使用 KeyEncipherment 标志,则证书需要遵守其他一些不变量。

无论如何,当我修复序列号并删除此标志时,保管库似乎接受了我的证书。

如果我只修复序列号但保留 KeyEncipherment 标志,我会收到此错误消息:

Unsupported key operation(s): "wrapKey", "unwrapKey". Supported values are "sign", "verify".