Can OpenSSL on Windows use the system certificate store?

前端 未结 4 864
深忆病人
深忆病人 2020-12-02 09:17

Some working C++ code that I\'m porting from Linux to Windows is failing on windows because SSL_get_verify_result() is returning X509_V_ERR_UNABLE_TO_GET_

4条回答
  •  攒了一身酷
    2020-12-02 09:50

    For those of you still struggling with this as I have been, here is a sample code to get you started:

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

提交回复
热议问题