使用客户端证书和密码的cpp-netlib实现https客户端get请求

3
我正在尝试使用cppnetlib或者boost asio库连接到URL并获取结果页面。已经成功使用http协议,甚至使用cppnetlib也可以使用https协议,但我需要提供一个需要密码的客户端证书。不幸的是,我必须使用旧版本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;
    }

我也可以直接使用asio进行https get请求,并使用p12文件和密码作为客户端证书。 - Jim
事实证明,无法使用cppnetlib v0.10完成此操作。我可以接受使用来自boost 1.49的asio和标准PEM文件的解决方案。 - Jim
1个回答

0

v0.10 cppnetlib不直接支持客户端证书。由于您正在使用cppnetleb,因此可以使用带有boost 1.49的boost asio。

这里是一个示例代码,它完成了大部分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来解密并创建所需的pem文件。我从如何以编程方式加载PKCS#12文件?中适应了下面的代码。不幸的是,这个版本的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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接