如何将CNG密钥转换为OpenSSL EVP_PKEY(反之亦然)? [英] How to convert CNG key to OpenSSL EVP_PKEY (and vice versa)?

查看:435
本文介绍了如何将CNG密钥转换为OpenSSL EVP_PKEY(反之亦然)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows CNG API编写自定义OpenSSL引擎.在实现EVP_PKEY_meths以生成和使用ECDH密钥时,我遇到了将密钥从OpenSSL EVP_PKEY转换为CNG BCRYPT_KEY的问题,反之亦然.在实现Keygen和Derive功能时,我面临着这种情况.有没有简单的方法可以执行这些转换?

I am writing a custom OpenSSL engine using Windows CNG API. 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. I am facing this scenario while implementing the Keygen and Derive functions. Is there any easy way to perform these conversions?

推荐答案

我只使用RSA私钥来做到这一点,但是我假设其他类型(例如ECC)将遵循导出密钥参数和导入的相同原理.他们.

I've only done this with RSA private keys, but I assume that other types (e.g. ECC) would follow the same principal of exporting the key parameters and importing them.

我使用 BCryptExportKey 导出私钥数据和 BCryptImportKeyPair 在win32端导入数据.在openssl方面,我使用"set"类型调用来设置密钥数据,例如"

I use BCryptExportKey to export the private key data and BCryptImportKeyPair to import the data on the win32 side. 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.

这是我将RSA私钥的BCRYPT_KEY_HANDLE转换为EVP_PKEY的示例.反之类似.它不能满足您的特定ECDH要求,但我认为与您处理ECC私钥/公钥详细信息而不是RSA密钥详细信息大致相同.

Here is my example of converting a BCRYPT_KEY_HANDLE for a RSA private key to 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.

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;
}

这篇关于如何将CNG密钥转换为OpenSSL EVP_PKEY(反之亦然)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆