【问题标题】:How to convert CNG key to OpenSSL EVP_PKEY (and vice versa)?如何将 CNG 密钥转换为 OpenSSL EVP_PKEY(反之亦然)?
【发布时间】:2019-12-12 22:14:24
【问题描述】:

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

【问题讨论】:

    标签: c++ windows openssl cryptography cng


    【解决方案1】:

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

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

    这是我将 RSA 私钥的 BCRYPT_KEY_HANDLE 转换为 EVP_PKEY 的示例。反过来也是类似的。它不能满足您的特定 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;
    }
    

    【讨论】:

    • 非常感谢,ECC 的方式非常接近您的答案。一旦我有时间,我会发布我的解决方案以帮助其他人。
    • @themadking 你能发布你的解决方案吗?
    • 我不想将密钥导出到文件,因为它会构成安全性。我们要转换句柄。
    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 2011-07-27
    • 2021-06-10
    • 2012-03-24
    • 2018-06-28
    • 2010-12-04
    • 2012-01-15
    相关资源
    最近更新 更多