]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/ssl/client.cpp
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 #include <boost/bind/bind.hpp>
14 #include <boost/asio.hpp>
15 #include <boost/asio/ssl.hpp>
17 enum { max_length
= 1024 };
22 client(boost::asio::io_context
& io_context
,
23 boost::asio::ssl::context
& context
,
24 boost::asio::ip::tcp::resolver::results_type endpoints
)
25 : socket_(io_context
, context
)
27 socket_
.set_verify_mode(boost::asio::ssl::verify_peer
);
28 socket_
.set_verify_callback(
29 boost::bind(&client::verify_certificate
, this,
30 boost::placeholders::_1
, boost::placeholders::_2
));
32 boost::asio::async_connect(socket_
.lowest_layer(), endpoints
,
33 boost::bind(&client::handle_connect
, this,
34 boost::asio::placeholders::error
));
37 bool verify_certificate(bool preverified
,
38 boost::asio::ssl::verify_context
& ctx
)
40 // The verify callback can be used to check whether the certificate that is
41 // being presented is valid for the peer. For example, RFC 2818 describes
42 // the steps involved in doing this for HTTPS. Consult the OpenSSL
43 // documentation for more details. Note that the callback is called once
44 // for each certificate in the certificate chain, starting from the root
45 // certificate authority.
47 // In this example we will simply print the certificate's subject name.
48 char subject_name
[256];
49 X509
* cert
= X509_STORE_CTX_get_current_cert(ctx
.native_handle());
50 X509_NAME_oneline(X509_get_subject_name(cert
), subject_name
, 256);
51 std::cout
<< "Verifying " << subject_name
<< "\n";
56 void handle_connect(const boost::system::error_code
& error
)
60 socket_
.async_handshake(boost::asio::ssl::stream_base::client
,
61 boost::bind(&client::handle_handshake
, this,
62 boost::asio::placeholders::error
));
66 std::cout
<< "Connect failed: " << error
.message() << "\n";
70 void handle_handshake(const boost::system::error_code
& error
)
74 std::cout
<< "Enter message: ";
75 std::cin
.getline(request_
, max_length
);
76 size_t request_length
= strlen(request_
);
78 boost::asio::async_write(socket_
,
79 boost::asio::buffer(request_
, request_length
),
80 boost::bind(&client::handle_write
, this,
81 boost::asio::placeholders::error
,
82 boost::asio::placeholders::bytes_transferred
));
86 std::cout
<< "Handshake failed: " << error
.message() << "\n";
90 void handle_write(const boost::system::error_code
& error
,
91 size_t bytes_transferred
)
95 boost::asio::async_read(socket_
,
96 boost::asio::buffer(reply_
, bytes_transferred
),
97 boost::bind(&client::handle_read
, this,
98 boost::asio::placeholders::error
,
99 boost::asio::placeholders::bytes_transferred
));
103 std::cout
<< "Write failed: " << error
.message() << "\n";
107 void handle_read(const boost::system::error_code
& error
,
108 size_t bytes_transferred
)
112 std::cout
<< "Reply: ";
113 std::cout
.write(reply_
, bytes_transferred
);
118 std::cout
<< "Read failed: " << error
.message() << "\n";
123 boost::asio::ssl::stream
<boost::asio::ip::tcp::socket
> socket_
;
124 char request_
[max_length
];
125 char reply_
[max_length
];
128 int main(int argc
, char* argv
[])
134 std::cerr
<< "Usage: client <host> <port>\n";
138 boost::asio::io_context io_context
;
140 boost::asio::ip::tcp::resolver
resolver(io_context
);
141 boost::asio::ip::tcp::resolver::results_type endpoints
=
142 resolver
.resolve(argv
[1], argv
[2]);
144 boost::asio::ssl::context
ctx(boost::asio::ssl::context::sslv23
);
145 ctx
.load_verify_file("ca.pem");
147 client
c(io_context
, ctx
, endpoints
);
151 catch (std::exception
& e
)
153 std::cerr
<< "Exception: " << e
.what() << "\n";