如何从证书中获取AuthorityKeyIdentifier

4
我正在尝试使用Bouncy Castle生成证书,但我发现无法获取签发证书的 AuthorityKeyIdentifier。虽然我一直在尝试找出问题所在,但目前为止我还没有头绪。
我在存储中检查的证书具有 Authority Key Identifier。
KeyID=64 c1 59 db eb e7 2b f0 d7 e5 e3 81 77 d2 be b0
Certificate Issuer:
     CN=Test Certification Authority
Certificate SerialNumber=5c 27 00 3b 0f 0a a2 83 4a 8d 2b d5 45 d2 9c 3f

然而,每当我在Bouncy Castle中使用以下代码来获取密钥时,它会给我一个完全不同的AKI:

var password = "p@ssw0rd1";
var file = File.ReadAllBytes(@"C:\somefilepath\TESTCA.pfx");

Pkcs12Store st = new Pkcs12Store(new MemoryStream(file), password.ToCharArray());
var alias = st.Aliases.Cast<string>().Where (s => st.IsKeyEntry(s)).Single();
var cert = (X509Certificate)st.GetCertificate(alias).Certificate;

var subjectPKI = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo( cert.GetPublicKey());
var aki = new AuthorityKeyIdentifier(subjectPKI);
BitConverter.ToString(aki.GetKeyIdentifier()).Replace("-"," ").Dump();

使用这个方法,我得到了一个授权密钥标识符:
68 22 23 ED 45 82 A6 0E D6 A4 87 74 F2 E0 22 C4 4B F7 7D DF

然而,我在证书中找不到任何与此相匹配的信息。有什么想法吗?

1个回答

5
请查看[RFC 5280][1]中关于Authority Key Identifier的规范:

The value of the keyIdentifier field SHOULD be derived from the public key used to verify the certificate's signature or a method that generates unique values. Two common methods for generating key identifiers from the public key are described in Section 4.2.1.2. Where a key identifier has not been previously established, this specification RECOMMENDS use of one of these methods for generating keyIdentifiers or use of a similar method that uses a different hash algorithm. Where a key identifier has been previously established, the CA SHOULD use the previously established identifier.

...

KeyIdentifier ::= OCTET STRING
因此,密钥标识符的值只是一些八位字节字符串,没有明显包含证书信息。如果您查看通常所指的常见方法,生成密钥标识符的两种常见方法如下:
(1)keyIdentifier由BIT STRING subjectPublicKey的160位SHA-1哈希值组成(不包括标签,长度和未使用位数)。
(2)keyIdentifier由值0100的四位类型字段和BIT STRING subjectPublicKey的最低有效60位SHA-1哈希值组成(不包括标签,长度和未使用位数)。
因此,密钥标识符本质上是某个哈希值。
因此,CA证书的Authority Key Identifier组件并不能立即给出有关某个证书的明确信息。Authority Key Identifier可能具有附加字段以包含该信息,但它们是可选的。因此,根据规范的CA证书中,subject key identifier的值必须是在此证书主题颁发的证书的authority key identifier扩展字段(第4.2.1.1节)中放置的键标识符字段的值。因此,通过将这些值作为抽象字节数组进行比较,可以找到匹配的颁发者/颁发证书对,没有其他附加信息意味着。

哇,感谢您提供如此详细的答案!我最终自己得出了这个结论,没有看到RFC。从某种意义上说,它似乎是任意的。我做这件事的主要原因是因为我正在尝试编写一个流畅的接口来创建证书。我最终通过 brute force 和纯运气找到了从父级颁发证书所需做的事情。再次感谢! - tostringtheory

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