简体   繁体   English

如何将 CNG 密钥转换为 OpenSSL EVP_PKEY(反之亦然)?

[英]How to convert CNG key to OpenSSL EVP_PKEY (and vice versa)?

I am writing a custom OpenSSL engine using Windows CNG API.我正在使用 Windows CNG API 编写自定义 OpenSSL 引擎。 While implementing the EVP_PKEY_meths to generate and use ECDH keys, I came across the issue of converting keys from OpenSSL EVP_PKEY to CNG BCRYPT_KEY and vice versa.在实施EVP_PKEY_meths以生成和使用 ECDH 密钥时,我遇到了将密钥从 OpenSSL EVP_PKEY转换为 CNG BCRYPT_KEY ,反之亦然。 I am facing this scenario while implementing the Keygen and Derive functions.我在实现 Keygen 和 Derive 功能时面临这种情况。 Is there any easy way to perform these conversions?有没有简单的方法来执行这些转换?

I've only done this with RSA private keys, but I assume that other types (eg ECC) would follow the same principal of exporting the key parameters and importing them.我只使用 RSA 私钥完成了此操作,但我假设其他类型(例如 ECC)将遵循导出和导入密钥参数的相同原则。

I use BCryptExportKey to export the private key data and BCryptImportKeyPair to import the data on the win32 side.我使用BCryptExportKey导出私钥数据,使用BCryptImportKeyPair在 win32 端导入数据。 On the openssl side I use the "set" type calls to set the key data like " RSA_set0_crt_params " to setup a RSA key.在 openssl 端,我使用“set”类型调用来设置密钥数据,如“ RSA_set0_crt_params ”来设置 RSA 密钥。

Here is my example of converting a BCRYPT_KEY_HANDLE for a RSA private key to EVP_PKEY.这是我将 RSA 私钥的 BCRYPT_KEY_HANDLE 转换为 EVP_PKEY 的示例。 The reverse is similar.反过来也是类似的。 It doesn't answer your specific ECDH requirement but I would assume that it's roughly the same expect you are dealing with the ECC private / public key details instead of the RSA key details.它不满足您的特定 ECDH 要求,但我认为它与您处理 ECC 私钥/公钥详细信息而不是 RSA 密钥详细信息的期望大致相同。

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://docs.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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM