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

标签 c++ windows openssl cryptography cng

我正在使用 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/

相关文章:

node.js - express ssl 地址

c++ - 实现一个类似于 Qt 的高性能互斥锁

c++ - 使用 C++ 解析/proc/stat

windows - 是否有用于捕获和播放修改后的 UDP 数据包的 Windows 工具?

c - 如何获取链中的下一个证书

java - Android AES/CBC 加密

c++ - OpenGL 中的视觉糖果

c++ - 如何避免插入函数中的指针失效?

c# - 如何使用密码控件禁用 Caps Lock 警告?

c++ - 使用 node-gyp 编译时 CFLAGS 在 Windows 上不起作用