【问题标题】:Use private key with Windows certficate store in gSoap在 gSoap 中将私钥与 Windows 证书存储一起使用
【发布时间】:2019-03-15 00:31:22
【问题描述】:

我正在编写一个带有 OpenSSL 的 gSoap 客户端,该客户端使用 Windows 证书。我有一个 PEM 证书和一个 PEM 私钥。当我将它们组合成一个文件并将其提供给 gSoap 时,它可以正常工作:

soap_ssl_client_context( &soap,
                         SOAP_SSL_DEFAULT,
                         "certkey.pem", /* required only when client must authenticate to server         */
                         NULL, /* password to read the key file (not used with GNUTLS)                 */
                         NULL, /* cacert file to store trusted certificates                            */
                         NULL, /* capath to directory with trusted certificates                        */
                         NULL  /* if randfile!=NULL: use a file with random data to seed randomness    */
                                 )

但是当我将证书安装到 Windows 存储中并通过 X509_STORE_add_cert 从那里加载它时,它不起作用。我的猜测是我必须以某种方式使用私钥,但我不知道以什么方式。我该怎么办?

【问题讨论】:

    标签: openssl certificate gsoap


    【解决方案1】:

    您需要加载私钥是正确的,而且 X509_STORE_add_cert 也不正确。如果您想为服务器或客户端使用证书,您需要使用 SSL_CTX_use_xxx 或 SSL_use_xxx 将证书设置为 ssl 上下文,用于证书和证书的私钥。

    例如

    SSL_CTX_use_certificate_chain_file(ctx, "cert.pem");
    SSL_CTX_use_PrivateKey_file(ctx, "cert.pem", SSL_FILETYPE_PEM);
    

    以上假设“cert.pem”同时拥有证书链和私钥。

    更新:

    我假设“Windows 存储”是指“Windows 证书存储”。 在 Windows 证书存储中使用证书的主要问题是私钥的使用。如果私钥被标记为“不可导出”,那么您只能使用Windows Crypto API“使用”私钥。因此,如果您希望将证书与存储在 Windows 证书存储中的私钥一起使用,您需要将证书(足够简单)和私钥“导出”到 openssl x509 和 rsa 对象中,以便在 SSL_CTX_xxx 函数中使用。我发现导出私钥的最佳方法是使用NCryptExportKey,使用BCRYPT_RSAFULLPRIVATE_BLOB blob 类型,然后使用RSA_setxxx 函数将BCRYPT_RSAKEY_BLOB 手动分解为openssl RSA 结构。

    RSA* extract_private_key(const PCCERT_CONTEXT context)
    {
        HCRYPTPROV_OR_NCRYPT_KEY_HANDLE key_handle;
        DWORD key_spec = 0;
        BOOL free_key;
        if (!CryptAcquireCertificatePrivateKey(context, CRYPT_ACQUIRE_ONLY_NCRYPT_KEY_FLAG | CRYPT_ACQUIRE_SILENT_FLAG, nullptr, &key_handle, &key_spec, &free_key))
        {
            return nullptr;
        }
    
        RSA* rsa = nullptr;
        DWORD length = 0;
        if(SUCCEEDED(NCryptExportKey(key_handle, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, nullptr, nullptr, 0, &length, 0)))
        {
            auto data = std::make_unique<BYTE[]>(length);
    
            if(SUCCEEDED(NCryptExportKey(key_handle, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, nullptr, 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)
                {
                    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);
                }
            }
        }
    
        if(free_key)
        {
            NCryptFreeObject(key_handle);
        }
    
        return rsa;
    }
    
    bool set_ctx_certificate_and_private_key(SSL_CTX* ctx, const PCCERT_CONTEXT context)
    {
        auto const x509 = d2i_X509(nullptr, const_cast<const unsigned char **>(&context->pbCertEncoded), context->cbCertEncoded);
        if (!x509)
        {
            return false;
        }
    
        if(!SSL_CTX_use_certificate(ctx, x509))
        {
            X509_free(x509);
            return false;
        }
        X509_free(x509);
    
        auto const rsa = extract_private_key(context);
        if (!rsa)
        {
            return false;
        }
    
        auto const success = SSL_CTX_use_RSAPrivateKey(ctx, rsa) == 1;
        RSA_free(rsa);
        return success;
    }
    

    【讨论】:

    • 如果我的证书仅在 Windows 存储中具有私钥,我该怎么办?如何将它们导出到文件?
    • 您的意思是“windows 存储”中的“windows 证书存储”吗?
    • 感谢您的详细解答!
    • 不幸的是,现在我必须在 Windows XP 中做同样的事情,并且那里不支持 NCrypt。我尝试使用 CryptExportKey() 但它因无效参数错误而失败。你知道这可能是什么问题吗?谢谢!
    • 不确定我是否可以帮助使用该 API。这个问题的答案可能对stackoverflow.com/questions/37717931/… 有所帮助,或者尝试问另一个问题。
    猜你喜欢
    • 2011-12-16
    • 2011-11-09
    • 2013-05-30
    • 2016-02-17
    • 2012-10-25
    • 2018-11-10
    • 2013-05-27
    • 2015-07-19
    • 2022-12-18
    相关资源
    最近更新 更多