asio 使用 openssl 示例

2022-05-28 00:12 CST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#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 使用 openssl 示例 by mkckr0 is licensed under CC BY-NC-ND 4.0