ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

asio 使用 openssl 示例

2022-05-28 00:34:12  阅读:213  来源: 互联网

标签:asio std return 示例 openssl ec ssl include


#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#define OPENSSL_NO_DEPRECATED
#include <openssl/ssl.h>
#include <wincrypt.h>
#pragma comment(lib, "Crypt32.lib")

#include <iostream>
#include <fstream>
#include <string_view>

void fail(const char* what, const boost::system::error_code& ec) {
    std::cout << what << ": " << ec.message() << std::endl;
}

namespace asio = boost::asio;
using tcp = asio::ip::tcp;

int add_ca(asio::ssl::context& ssl_ctx, boost::system::error_code& ec)
{
    BIO* bio = BIO_new_fp(stdout, BIO_NOCLOSE);
    if (!bio) {
        return -1;
    }

    auto cert_store = CertOpenSystemStore(NULL, L"ROOT");
    if (!cert_store) {
        return -1;
    }

    const unsigned char* encoded_cert = nullptr;
    int i = 0;
    for (PCCERT_CONTEXT cert = nullptr; cert = CertEnumCertificatesInStore(cert_store, cert);) {
        X509* x = d2i_X509(NULL, (const unsigned char**)&cert->pbCertEncoded, cert->cbCertEncoded);
        if (x) {
            BIO* bio_mem = BIO_new(BIO_s_mem());
            PEM_write_bio_X509(bio_mem, x);
            const char* data = nullptr;
            auto len = BIO_get_mem_data(bio_mem, &data);

            ssl_ctx.add_certificate_authority(asio::buffer(data, len), ec);

            BIO_free(bio_mem);

            if (ec) {
                return -1;
            }
        }
        ++i;
    }

    BIO_printf(bio, "cert sum: %d\n", i);

    CertCloseStore(cert_store, CERT_CLOSE_STORE_FORCE_FLAG);

    return 0;
}

int main()
{
    boost::system::error_code ec;

    asio::io_context io_ctx;

    tcp::resolver resolver(io_ctx);
    auto results = resolver.resolve("www.baidu.com", "https", ec);
    if (ec) {
        fail("resolver.resolve", ec);
        return -1;
    }

    asio::ssl::context ssl_ctx(asio::ssl::context::method::sslv23_client);
    add_ca(ssl_ctx, ec);
    if (ec) {
        fail("add_ca", ec);
        return -1;
    }

    asio::ssl::stream<tcp::socket> s(io_ctx, ssl_ctx);
    s.set_verify_mode(asio::ssl::verify_peer | asio::ssl::verify_client_once);
    asio::connect(s.lowest_layer(), results, ec);
    if (ec) {
        fail("asio::connect", ec);
        return -1;
    }

    s.handshake(s.client, ec);
    if (ec) {
        fail("s.handshake", ec);
        return -1;
    }

    const char* req = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nAccept: text/html\r\n\r\n";
    asio::write(s, asio::buffer(req, std::strlen(req)), ec);
    if (ec) {
        fail("asio::write", ec);
        return -1;
    }

    // Header
    asio::streambuf streambuf;
    std::size_t content_length = 0;
    constexpr std::string_view delimiter = "\r\n";
    constexpr std::string_view header_name = "Content-Length";
    while (true) {
        std::size_t len = asio::read_until(s, streambuf, delimiter, ec);
        if (ec) {
            fail("asio::read_until", ec);
            return -1;
        }

        std::string_view header(asio::buffer_cast<const char*>(streambuf.data()), len - delimiter.size());

        std::cout << header << std::endl;

        if (!header.empty()) {
            if (boost::algorithm::istarts_with(header, header_name)) {
                bool has_optional_space = header[header_name.size() + 1] == ' ';
                std::string_view header_value(header.cbegin() + header_name.size() + 1 + has_optional_space, header.cend());
                content_length = boost::lexical_cast<std::size_t>(header_value);
            }
        }

        streambuf.consume(len);

        if (header.empty()) {
            break;
        }
    }

    // Body
    std::cout << "content_length: " << content_length << std::endl;
    
    std::size_t len = asio::read(s, streambuf.prepare(content_length - streambuf.size()), ec);
    if (ec) {
        fail("asio::read", ec);
        return -1;
    }
    streambuf.commit(len);

    std::string_view body(asio::buffer_cast<const char*>(streambuf.data()), streambuf.size());

    std::system("chcp 65001");
    std::cout << body;

    streambuf.consume(streambuf.size());

    s.shutdown(ec);
    if (ec) {
        fail("s.shutdown", ec);
        return -1;
    }

    s.lowest_layer().shutdown(tcp::socket::shutdown_both, ec);
    if (ec) {
        fail("s.lowest_layer().shutdown", ec);
        return -1;
    }

    return 0;
}

标签:asio,std,return,示例,openssl,ec,ssl,include
来源: https://www.cnblogs.com/mkckr0/p/16319533.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有