1

我从 ldap 搜索请求中获取 DER 编码证书(在 base64 解码后),需要从中解析公钥。我很确定它可以使用 openssl 库。不幸的是,API 文档保存得不是很好。是否有任何示例或其他库来提取信息?

4

2 回答 2

2

使用 d2i_X509 获取 X509 * 结构中的证书。之后使用 X509_get_pubkey 获取公钥。X509_get_pubkey 将为您提供 EVP_PKEY * 结构中的公钥。我希望这必须解决您的目的。

如果您的证书是 PEM 格式(Base64 编码由 "-----BEGIN CERTIFICATE-----" 包装),那么您也可以使用 PEM_read_X509 直接获取 X509 * 对象。

例子:

//Get the X509 object.
//Say certificate is encoded in a file
X509 * xcert = PEM_read_X509(fp, NULL, NULL, NULL);

//or assuming DER encoded certificate in buf with length of buffer is buflen.
X509 * xcert = d2i_X509(NULL, buf, buflen);

//Get the public key.
EVP_PKEY * pubkey = X509_get_pubkey(xcert);


//later free this pubkey object when no longer required.
EVP_PKEY_free(pubkey);
于 2013-02-25T06:25:57.387 回答
1

您可以尝试使用d2i_X509 API 来解码 DER 编码的证书。它为您提供了一个 X509 结构,您应该能够从中获取公钥。

于 2013-02-25T06:17:54.430 回答