Windows上的OpenSSL可以使用系统证书存储吗?

dic*_*oce 31 c++ windows openssl

我正在从Linux移植到Windows的一些正在运行的C++代码在Windows上失败,因为它SSL_get_verify_result()正在返回X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY.

代码SSL_CTX_set_default_verify_paths()在Linux上使用,告诉SSL只查看证书存储的标准默认位置.

是否可以让OpenSSL使用系统证书存储?

Tus*_*ake 41

我早点完成了.希望这有帮助,如果这正是您正在寻找的.

  1. PCCERT_CONTEXT使用Crypto API从Windows Cert Store 加载证书(结构中).
  2. 以二进制格式获取加密内容.[ PCCERT_CONTEXT->pbCertEncoded].
  3. 使用OpenSSL d2i_X509()方法将此二进制缓冲区解析为X509证书对象.
  4. 使用SSL_CTX_get_cert_store()方法获取OpenSSL的信任库的句柄.
  5. 使用X509_STORE_add_cert()方法将解析的X509证书加载到此信任库中.
  6. 你完成了!


Pet*_*ter 17

对于那些仍在努力解决这个问题的人来说,这里有一个示例代码可以帮助您入门:

#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#include <cryptuiapi.h>
#include <iostream>
#include <tchar.h>

#include "openssl\x509.h"

#pragma comment (lib, "crypt32.lib")
#pragma comment (lib, "cryptui.lib")

#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

int main(void)
{
    HCERTSTORE hStore;
    PCCERT_CONTEXT pContext = NULL;
    X509 *x509;
    X509_STORE *store = X509_STORE_new();

    hStore = CertOpenSystemStore(NULL, L"ROOT");

    if (!hStore)
        return 1;

    while (pContext = CertEnumCertificatesInStore(hStore, pContext))
    {
        //uncomment the line below if you want to see the certificates as pop ups
        //CryptUIDlgViewContext(CERT_STORE_CERTIFICATE_CONTEXT, pContext,   NULL, NULL, 0, NULL);

        x509 = NULL;
        x509 = d2i_X509(NULL, (const unsigned char **)&pContext->pbCertEncoded, pContext->cbCertEncoded);
        if (x509)
        {
            int i = X509_STORE_add_cert(store, x509);

            if (i == 1)
                std::cout << "certificate added" << std::endl;

            X509_free(x509);
        }
    }

CertFreeCertificateContext(pContext);
CertCloseStore(hStore, 0);
system("pause");
return 0;

}
Run Code Online (Sandbox Code Playgroud)

  • 你对`d2i_X509`的使用是无效的,因为`d2i_X509`会增加`*in`参数(你使用强制转换来避免编译错误).您必须使用临时变量来避免内存问题,例如:`const unsigned char*encoded_cert = win_cert_context-> pbCertEncoded; d2i_X509(nullptr,&encoded_cert,...` (3认同)