gpt4 book ai didi

c++ - 将 PEM 编码的 X.509 证书加载到 Windows CryptoAPI

转载 作者:IT老高 更新时间:2023-10-28 22:33:43 26 4
gpt4 key购买 nike

我需要将 PEM 编码的 X.509 证书加载到 Windows Crypto API 上下文中以用于 C++。他们是那些有 -----BEGIN RSA XXX KEY----------END RSA XXX KEY -----。我找到了 Python 和 .NET 的示例,但它们使用了与普通 Windows Crypto API 无关的特定功能。

我了解如何在获得 HCRYPTKEY 后进行加密/解密。但是,我只是不知道如何在 .PEM 文件中导入 Base64 blob 并获得一个我可以使用的 HCRYPTKEY

我有一种奇怪的感觉,它不仅仅是调用 CryptDecodeObject()

任何可以让我走上正轨的指针?我已经浪费了两天时间进行“试错”编程,但一无所获。

最佳答案

KJKHyperion在他的answer中说:

I discovered the "magic" sequence of calls to import a RSA public key in PEM format. Here you go:

  1. decode the key into a binary blob with CryptStringToBinary; pass CRYPT_STRING_BASE64HEADER in dwFlags
  2. decode the binary key blob into a CERT_PUBLIC_KEY_INFO with CryptDecodeObjectEx; pass X509_ASN_ENCODING in dwCertEncodingType and X509_PUBLIC_KEY_INFO in lpszStructType
  3. decode the PublicKey blob from the CERT_PUBLIC_KEY_INFO into a RSA key blob with CryptDecodeObjectEx; pass X509_ASN_ENCODING in dwCertEncodingType and RSA_CSP_PUBLICKEYBLOB in lpszStructType
  4. import the RSA key blob with CryptImportKey

这个序列确实帮助我理解了正在发生的事情,但它并没有按原样工作。第二次调用 CryptDecodeObjectEx 给了我一个错误:“符合 ASN.1 错误标签值”。经过多次尝试理解微软文档,我终于意识到第一次解码的输出不能再解码为ASN,实际上已经准备好导入了。有了这种理解,我在以下链接中找到了答案:

http://www.ms-news.net/f2748/problem-importing-public-key-4052577.html

以下是我自己的程序,它将公钥从 .pem 文件导入到 CryptApi 上下文:

int main()
{
char pemPubKey[2048];
int readLen;
char derPubKey[2048];
size_t derPubKeyLen = 2048;
CERT_PUBLIC_KEY_INFO *publicKeyInfo;
int publicKeyInfoLen;
HANDLE hFile;
HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;

/*
* Read the public key cert from the file
*/
hFile = CreateFileA( "c:\\pub.pem", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if ( hFile == INVALID_HANDLE_VALUE )
{
fprintf( stderr, "Failed to open file. error: %d\n", GetLastError() );
}

if ( !ReadFile( hFile, pemPubKey, 2048, &readLen, NULL ) )
{
fprintf( stderr, "Failed to read file. error: %d\n", GetLastError() );
}

/*
* Convert from PEM format to DER format - removes header and footer and decodes from base64
*/
if ( !CryptStringToBinaryA( pemPubKey, 0, CRYPT_STRING_BASE64HEADER, derPubKey, &derPubKeyLen, NULL, NULL ) )
{
fprintf( stderr, "CryptStringToBinary failed. Err: %d\n", GetLastError() );
}

/*
* Decode from DER format to CERT_PUBLIC_KEY_INFO
*/
if ( !CryptDecodeObjectEx( X509_ASN_ENCODING, X509_PUBLIC_KEY_INFO, derPubKey, derPubKeyLen,
CRYPT_ENCODE_ALLOC_FLAG, NULL, &publicKeyInfo, &publicKeyInfoLen ) )
{
fprintf( stderr, "CryptDecodeObjectEx 1 failed. Err: %p\n", GetLastError() );
return -1;
}

/*
* Acquire context
*/
if( !CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) )
{
{
printf( "CryptAcquireContext failed - err=0x%x.\n", GetLastError() );
return -1;
}
}

/*
* Import the public key using the context
*/
if ( !CryptImportPublicKeyInfo( hProv, X509_ASN_ENCODING, publicKeyInfo, &hKey ) )
{
fprintf( stderr, "CryptImportPublicKeyInfo failed. error: %d\n", GetLastError() );
return -1;
}
LocalFree( publicKeyInfo );

/*
* Now use hKey to encrypt whatever you need.
*/

return 0;
}

关于c++ - 将 PEM 编码的 X.509 证书加载到 Windows CryptoAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1231178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com