如何使用 CryptoAPI 从 p7b 证书中提取公钥
我有一个 p7b 证书存储。我用我做的证书链验证打开它
$HCERTSTORE cert_store_handle = CertOpenStore(
CERT_STORE_PROV_PKCS7,
PKCS_7_ASN_ENCODING,
NULL,
CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG,
&opm_data_blob
);
,一切正常,直到我必须从叶证书中提取公钥。 我调用
CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, RSA_CSP_PUBLICKEYBLOB, (BYTE*) pubkey + 46, pubkey_len - 46, CRYPT_DECODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG, NULL, &pubkey_decoded_size);
但它返回 ASN1 bad tag 错误。
所以我尝试以下代码:
{
BOOL crypt_res = FALSE;
HCRYPTPROV crypt_prov_hndl = NULL;
crypt_res = CryptAcquireContext(&crypt_prov_hndl, NULL, NULL, PROV_RSA_FULL, 0/*CRYPT_NEWKEYSET*/);
if (!crypt_res) {
HRESULT decode_hr = __HRESULT_FROM_WIN32(GetLastError());
return decode_hr;
}
HCRYPTKEY crypt_key_hndl = NULL;
crypt_res = CryptImportPublicKeyInfoEx(crypt_prov_hndl, X509_ASN_ENCODING, signer_public_key, CALG_RSA_SIGN, 0, NULL, &crypt_key_hndl);
if (!crypt_res) {
HRESULT decode_hr = __HRESULT_FROM_WIN32(GetLastError());
return decode_hr;
}
crypt_res = CryptReleaseContext(crypt_prov_hndl, 0);
}
它工作正常,但我仍然不知道如何提取公钥。
有什么建议吗?
I have a p7b certificate store. I open it with
$HCERTSTORE cert_store_handle = CertOpenStore(
CERT_STORE_PROV_PKCS7,
PKCS_7_ASN_ENCODING,
NULL,
CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG,
&opm_data_blob
);
I do cert chain verification, and it ok, till I have to extract public key from the leaf certificate.
I call
CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, RSA_CSP_PUBLICKEYBLOB, (BYTE*) pubkey + 46, pubkey_len - 46, CRYPT_DECODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG, NULL, &pubkey_decoded_size);
but it returns ASN1 bad tag error.
So I try the following code:
{
BOOL crypt_res = FALSE;
HCRYPTPROV crypt_prov_hndl = NULL;
crypt_res = CryptAcquireContext(&crypt_prov_hndl, NULL, NULL, PROV_RSA_FULL, 0/*CRYPT_NEWKEYSET*/);
if (!crypt_res) {
HRESULT decode_hr = __HRESULT_FROM_WIN32(GetLastError());
return decode_hr;
}
HCRYPTKEY crypt_key_hndl = NULL;
crypt_res = CryptImportPublicKeyInfoEx(crypt_prov_hndl, X509_ASN_ENCODING, signer_public_key, CALG_RSA_SIGN, 0, NULL, &crypt_key_hndl);
if (!crypt_res) {
HRESULT decode_hr = __HRESULT_FROM_WIN32(GetLastError());
return decode_hr;
}
crypt_res = CryptReleaseContext(crypt_prov_hndl, 0);
}
and it works fine, but still I don't know how to extract public key.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
打开证书存储区后,
枚举证书存储区中的所有证书,
CertEnumCertificatesInStore
,一旦获得感兴趣的证书的上下文,就可以在
CERT_INFO
中访问您的公钥> 结构。After opening the certificate store,
Enumerate all the certificates in the certificate store,
CertEnumCertificatesInStore
,Once you get the context of the certificate of interest, you can access your public key in the
CERT_INFO
structure.