gpt4 book ai didi

c++ - 如何将 CNG key 转换为 OpenSSL EVP_PKEY(反之亦然)?

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:14 33 4
gpt4 key购买 nike

我正在使用 Windows CNG API 编写自定义 OpenSSL 引擎。在实现 EVP_PKEY_meths 以生成和使用 ECDH key 时,我遇到了将 key 从 OpenSSL EVP_PKEY 转换为 CNG BCRYPT_KEY 的问题,反之亦然。我在实现 Keygen 和 Derive 函数时遇到了这种情况。有什么简单的方法可以执行这些转换吗?

最佳答案

我只使用 RSA 私钥完成了此操作,但我假设其他类型(例如 ECC)将遵循导出和导入 key 参数的相同原则。

我使用 BCryptExportKey导出私钥数据和BCryptImportKeyPair在 win32 端导入数据。在 openssl 端,我使用“set”类型调用来设置 key 数据,如“RSA_set0_crt_params”以设置 RSA key 。

这是我将 RSA 私钥的 BCRYPT_KEY_HANDLE 转换为 EVP_PKEY 的示例。反之亦然。它没有回答您的特定 ECDH 要求,但我认为它与预期您处理的是 ECC 私钥/公钥详细信息而不是 RSA key 详细信息大致相同。

EVP_PKEY* extract_private_key(const BCRYPT_KEY_HANDLE key_handle)
{
EVP_PKEY* pkey = nullptr;
DWORD length = 0;
if(SUCCEEDED(BCryptExportKey(key_handle, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, nullptr, 0, &length, 0)))
{
auto data = std::make_unique<BYTE[]>(length);

if(SUCCEEDED(BCryptExportKey(key_handle, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, data.get(), length, &length, 0)))
{
// https://learn.microsoft.com/en-us/windows/desktop/api/bcrypt/ns-bcrypt-_bcrypt_rsakey_blob
auto const blob = reinterpret_cast<BCRYPT_RSAKEY_BLOB*>(data.get());

if(blob->Magic == BCRYPT_RSAFULLPRIVATE_MAGIC)
{
auto rsa = RSA_new();

// n is the modulus common to both public and private key
auto const n = BN_bin2bn(data.get() + sizeof(BCRYPT_RSAKEY_BLOB) + blob->cbPublicExp, blob->cbModulus, nullptr);
// e is the public exponent
auto const e = BN_bin2bn(data.get() + sizeof(BCRYPT_RSAKEY_BLOB), blob->cbPublicExp, nullptr);
// d is the private exponent
auto const d = BN_bin2bn(data.get() + sizeof(BCRYPT_RSAKEY_BLOB) + blob->cbPublicExp + blob->cbModulus + blob->cbPrime1 + blob->cbPrime2 + blob->cbPrime1 + blob->cbPrime2 + blob->cbPrime1, blob->cbModulus, nullptr);

RSA_set0_key(rsa, n, e, d);

// p and q are the first and second factor of n
auto const p = BN_bin2bn(data.get() + sizeof(BCRYPT_RSAKEY_BLOB) + blob->cbPublicExp + blob->cbModulus, blob->cbPrime1, nullptr);
auto const q = BN_bin2bn(data.get() + sizeof(BCRYPT_RSAKEY_BLOB) + blob->cbPublicExp + blob->cbModulus + blob->cbPrime1, blob->cbPrime2, nullptr);

RSA_set0_factors(rsa, p, q);

// dmp1, dmq1 and iqmp are the exponents and coefficient for CRT calculations
auto const dmp1 = BN_bin2bn(data.get() + sizeof(BCRYPT_RSAKEY_BLOB) + blob->cbPublicExp + blob->cbModulus + blob->cbPrime1 + blob->cbPrime2, blob->cbPrime1, nullptr);
auto const dmq1 = BN_bin2bn(data.get() + sizeof(BCRYPT_RSAKEY_BLOB) + blob->cbPublicExp + blob->cbModulus + blob->cbPrime1 + blob->cbPrime2 + blob->cbPrime1, blob->cbPrime2, nullptr);
auto const iqmp = BN_bin2bn(data.get() + sizeof(BCRYPT_RSAKEY_BLOB) + blob->cbPublicExp + blob->cbModulus + blob->cbPrime1 + blob->cbPrime2 + blob->cbPrime1 + blob->cbPrime2, blob->cbPrime1, nullptr);

RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);

pkey = EVP_PKEY_new();

// ownership of rsa transferred to pkey
EVP_PKEY_assign_RSA(pkey, rsa);
}
}
}

return pkey;
}

关于c++ - 如何将 CNG key 转换为 OpenSSL EVP_PKEY(反之亦然)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59313462/

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