ssl - https 客户端使用客户端证书和密码获取 cpp-netlib

标签 ssl boost https client-certificates cpp-netlib

我正在尝试使用 cppnetlib,甚至是 boost asio 库进行连接,以执行简单的 url 获取并下拉生成的页面。

我已经让它与 http 一起工作,甚至 https 使用 cppnetlib,但我需要提供一个需要密码的客户端证书。不幸的是,我需要使用旧的 v0.10 cppnetlib。

这可以吗?我认为答案是创建我自己的 _io_service 并自定义使用证书和密码为 https 请求配置它,然后将其提供给 boost::network::http:client 构造函数。以下适用于 http,并且适用于没有证书要求的 https。

std::string url = "http://www.boost.org";
std::string certFile = "C:\\cert\\mycert.p12";
std::string password = "MyPassWord";
try {
        http::client client;
        http::client::request request(url);
        http::client::response response = client->get(request);

        std::string resultText = static_cast<std::string>(body(response));
        std::cout << resultText << std::endl;
        delete client;
    }
    catch (std::exception &e) {
        std::cerr << "Caught something connecting " << e.what() << std::endl;
    }

最佳答案

v0.10 cppnetlib 不直接支持客户端认证。由于您使用 cppnetleb,您可以将 boost asio 与 boost 1.49 一起使用

这是一个完成大部分 asio 工作的示例代码 https://github.com/alexandruc/SimpleHttpsClient/blob/master/https_client.cpp

该代码与 http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/example/ssl/client.cpp 非常相似

我把两者都放上以防链接中断。处理 https 连接以进行客户端认证您需要在创建客户端之前将以下行添加到主要功能:

        std::string tempString = "test.pem"; //this is a pem file that contains a private key and a certificate. 

        boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23_client);
        ctx.set_options(boost::asio::ssl::context::default_workarounds
                                    | boost::asio::ssl::context::no_sslv2
                                    | boost::asio::ssl::context::no_sslv3);
        ctx.set_default_verify_paths();

        ctx.use_certificate_file(tempFileStr.c_str(), boost::asio::ssl::context_base::pem);
        ctx.use_private_key_file(tempFileStr.c_str(), boost::asio::ssl::context_base::pem);

但是在本示例中,您没有 PEM 文件,而是 p12 文件(pkcs12 格式)。 openssl 可用于解密和创建 deisred pem 文件。我从 How to load a PKCS#12 file in OpenSSL programmatically? 改编了下面的代码不幸的是,此版本的 boost 不支持内存中的证书,因此必须将其写入并加密到文件中。我把它放在临时目录中,最后它应该会被删除。

std::string _certFile = "C:\\cert\\mycert.p12";
std::string password = "_certPassword";
boost::filesystem::path tempFile = boost::filesystem::temp_directory_path() / "temp.pem";
std::string tempFileStr = tempFile.generic_string();
std::cout<<"Using temp file " << tempFileStr<<std::endl;
try
    {

        //read in the pksc12 file, decode it and write a PEM file
        FILE *fp;
        EVP_PKEY *pkey;
        X509 *cert;
        STACK_OF(X509) *ca = NULL;
        PKCS12 *p12;
        int i;

        OpenSSL_add_all_algorithms();
        ERR_load_crypto_strings();
        if (!(fp = fopen(_certFile.c_str(), "rb"))) {
            fprintf(stderr, "Error opening file %s\n", _certFile);
            return false;       
        }
        p12 = d2i_PKCS12_fp(fp, NULL);
        fclose (fp);
        if (!p12) {
            fprintf(stderr, "Error reading PKCS#12 file\n");
            ERR_print_errors_fp(stderr);
            return false; 
        }
        if (!PKCS12_parse(p12, _certpPassword.c_str(), &pkey, &cert, &ca)) {
            fprintf(stderr, "Error parsing PKCS#12 file\n");
            ERR_print_errors_fp(stderr);
            return false; 

        }
        PKCS12_free(p12);
        if (!(fp = fopen(tempFileStr.c_str(), "w"))) {
            fprintf(stderr, "Error opening file %s\n", tempFileStr.c_str());
            return false; 
        }

        if (pkey) {
            fprintf(fp, "***Private Key***\n");
            PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
        }
        if (cert) {
            fprintf(fp, "***User Certificate***\n");
            PEM_write_X509(fp, cert);
        }
        if (ca && sk_X509_num(ca)) {
            fprintf(fp, "***Other Certificates***\n");

            for (i = 0; i < sk_X509_num(ca); i++) 
            {
                PEM_write_X509(fp, sk_X509_value(ca, i));
            }

        }

        sk_X509_pop_free(ca, X509_free);
        X509_free(cert);
        EVP_PKEY_free(pkey);    
        fclose(fp);

    }
    catch (std::exception &e) {
        retVal = false;
        std::cout <<"Error parsing/decrypting pkcs12 file into PEM or writing temporary pem file" << e.what() << std::endl;        
    }

这是我使用的包含

//for ssl connection
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/asio/ssl/context_base.hpp>

//for parsing key file
#include <openssl/pkcs12.h>

关于ssl - https 客户端使用客户端证书和密码获取 cpp-netlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42984534/

相关文章:

python - 调试斜纹异常错误

apache - .htaccess 非 www 重定向到根文件夹的子文件夹

asp.net - 在另一台服务器上为我们的网站设置 HTTPS/SSL 证书

c++ - Boost ASIO 示例无法构建

PHP4:通过 cURL 通过 HTTPS/POST 发送 XML?

android - 如何以编程方式获取服务器证书并添加到 truestore,并检查证书

django - 如何在 Django 应用程序中使用 SSL(使用 mod_wsgi 和 virtualenv 部署)

c++ - 多个端口的多个线程?

c++ - 创建库以覆盖迭代器的 operator*() - 风险悬空指针

ssl - 无论如何我可以使用 Strict-Transport security