Skip to content

Commit 8d9538f

Browse files
committed
Fixes cpp-netlib#346 - add an option to always verify peer
1 parent 947269e commit 8d9538f

16 files changed

+524
-542
lines changed

boost/network/protocol/http/client/async_impl.hpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ struct async_client
3636

3737
typedef function<bool(string_type&)> body_generator_function_type;
3838

39-
async_client(bool cache_resolved,
40-
bool follow_redirect,
39+
async_client(bool cache_resolved, bool follow_redirect,
40+
bool always_verify_peer,
4141
boost::shared_ptr<boost::asio::io_service> service,
4242
optional<string_type> const& certificate_filename,
4343
optional<string_type> const& verify_path)
@@ -49,7 +49,8 @@ struct async_client
4949
resolver_(service_),
5050
sentinel_(new boost::asio::io_service::work(service_)),
5151
certificate_filename_(certificate_filename),
52-
verify_path_(verify_path) {
52+
verify_path_(verify_path),
53+
always_verify_peer_(always_verify_peer) {
5354
connection_base::resolver_strand_.reset(
5455
new boost::asio::io_service::strand(service_));
5556
lifetime_thread_.reset(new boost::thread(
@@ -65,16 +66,15 @@ struct async_client
6566
}
6667

6768
basic_response<Tag> const request_skeleton(
68-
basic_request<Tag> const& request_,
69-
string_type const& method,
70-
bool get_body,
71-
body_callback_function_type callback,
69+
basic_request<Tag> const& request_, string_type const& method,
70+
bool get_body, body_callback_function_type callback,
7271
body_generator_function_type generator) {
7372
typename connection_base::connection_ptr connection_;
7473
connection_ = connection_base::get_connection(
75-
resolver_, request_, certificate_filename_, verify_path_);
76-
return connection_->send_request(
77-
method, request_, get_body, callback, generator);
74+
resolver_, request_, always_verify_peer_, certificate_filename_,
75+
verify_path_);
76+
return connection_->send_request(method, request_, get_body, callback,
77+
generator);
7878
}
7979

8080
boost::shared_ptr<boost::asio::io_service> service_ptr;
@@ -83,6 +83,7 @@ struct async_client
8383
boost::shared_ptr<boost::asio::io_service::work> sentinel_;
8484
boost::shared_ptr<boost::thread> lifetime_thread_;
8585
optional<string_type> certificate_filename_, verify_path_;
86+
bool always_verify_peer_;
8687
};
8788
} // namespace impl
8889
} // namespace http

boost/network/protocol/http/client/connection/async_base.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace boost { namespace network { namespace http { namespace impl {
3737
resolve_function resolve,
3838
resolver_type & resolver,
3939
bool follow_redirect,
40+
bool always_verify_peer,
4041
bool https,
4142
optional<string_type> certificate_filename=optional<string_type>(),
4243
optional<string_type> const & verify_path=optional<string_type>()) {
@@ -52,6 +53,7 @@ namespace boost { namespace network { namespace http { namespace impl {
5253
delegate_factory_type::new_connection_delegate(
5354
resolver.get_io_service(),
5455
https,
56+
always_verify_peer,
5557
certificate_filename,
5658
verify_path)));
5759
BOOST_ASSERT(temp.get() != 0);

boost/network/protocol/http/client/connection/async_normal.hpp

+111-182
Large diffs are not rendered by default.

boost/network/protocol/http/client/connection/connection_delegate_factory.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ struct connection_delegate_factory {
3030
static connection_delegate_ptr new_connection_delegate(
3131
asio::io_service & service,
3232
bool https,
33+
bool always_verify_peer,
3334
optional<string_type> certificate_filename,
3435
optional<string_type> verify_path) {
3536
connection_delegate_ptr delegate;
3637
if (https) {
3738
#ifdef BOOST_NETWORK_ENABLE_HTTPS
3839
delegate.reset(new ssl_delegate(service,
40+
always_verify_peer,
3941
certificate_filename,
4042
verify_path));
4143
#else

boost/network/protocol/http/client/connection/ssl_delegate.hpp

+23-15
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,38 @@
1515
#include <boost/network/support/is_default_string.hpp>
1616
#include <boost/network/support/is_default_wstring.hpp>
1717

18-
namespace boost { namespace network { namespace http { namespace impl {
18+
namespace boost {
19+
namespace network {
20+
namespace http {
21+
namespace impl {
1922

20-
struct ssl_delegate : connection_delegate, enable_shared_from_this<ssl_delegate> {
21-
ssl_delegate(asio::io_service & service,
22-
optional<std::string> certificate_filename,
23-
optional<std::string> verify_path);
23+
struct ssl_delegate : connection_delegate,
24+
enable_shared_from_this<ssl_delegate> {
25+
ssl_delegate(asio::io_service &service, bool always_verify_peer,
26+
optional<std::string> certificate_filename,
27+
optional<std::string> verify_path);
2428

25-
virtual void connect(asio::ip::tcp::endpoint & endpoint,
29+
virtual void connect(asio::ip::tcp::endpoint &endpoint,
2630
function<void(system::error_code const &)> handler);
27-
virtual void write(asio::streambuf & command_streambuf,
28-
function<void(system::error_code const &, size_t)> handler);
29-
virtual void read_some(asio::mutable_buffers_1 const & read_buffer,
30-
function<void(system::error_code const &, size_t)> handler);
31+
virtual void write(
32+
asio::streambuf &command_streambuf,
33+
function<void(system::error_code const &, size_t)> handler);
34+
virtual void read_some(
35+
asio::mutable_buffers_1 const &read_buffer,
36+
function<void(system::error_code const &, size_t)> handler);
3137
~ssl_delegate();
3238

3339
private:
34-
asio::io_service & service_;
40+
asio::io_service &service_;
3541
optional<std::string> certificate_filename_, verify_path_;
3642
scoped_ptr<asio::ssl::context> context_;
3743
scoped_ptr<asio::ssl::stream<asio::ip::tcp::socket> > socket_;
44+
bool always_verify_peer_;
3845

39-
ssl_delegate(ssl_delegate const &); // = delete
40-
ssl_delegate& operator=(ssl_delegate); // = delete
46+
ssl_delegate(ssl_delegate const &); // = delete
47+
ssl_delegate &operator=(ssl_delegate); // = delete
4148

42-
void handle_connected(system::error_code const & ec,
49+
void handle_connected(system::error_code const &ec,
4350
function<void(system::error_code const &)> handler);
4451
};
4552

@@ -55,4 +62,5 @@ struct ssl_delegate : connection_delegate, enable_shared_from_this<ssl_delegate>
5562
#include <boost/network/protocol/http/client/connection/ssl_delegate.ipp>
5663
#endif /* BOOST_NETWORK_NO_LIB */
5764

58-
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_CONNECTION_SSL_DELEGATE_20110819 */
65+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_CONNECTION_SSL_DELEGATE_20110819 \
66+
*/

boost/network/protocol/http/client/connection/ssl_delegate.ipp

+31-22
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,47 @@
88
// http://www.boost.org/LICENSE_1_0.txt)
99

1010
#include <boost/network/protocol/http/client/connection/ssl_delegate.hpp>
11+
#include <boost/asio/ssl.hpp>
1112
#include <boost/bind.hpp>
1213

13-
boost::network::http::impl::ssl_delegate::ssl_delegate(asio::io_service & service,
14-
optional<std::string> certificate_filename,
15-
optional<std::string> verify_path) :
16-
service_(service),
17-
certificate_filename_(certificate_filename),
18-
verify_path_(verify_path) {}
14+
boost::network::http::impl::ssl_delegate::ssl_delegate(
15+
asio::io_service &service, bool always_verify_peer,
16+
optional<std::string> certificate_filename,
17+
optional<std::string> verify_path)
18+
: service_(service),
19+
certificate_filename_(certificate_filename),
20+
verify_path_(verify_path),
21+
always_verify_peer_(always_verify_peer) {}
1922

2023
void boost::network::http::impl::ssl_delegate::connect(
21-
asio::ip::tcp::endpoint & endpoint,
24+
asio::ip::tcp::endpoint &endpoint,
2225
function<void(system::error_code const &)> handler) {
23-
context_.reset(new asio::ssl::context(
24-
service_,
25-
asio::ssl::context::sslv23_client));
26+
context_.reset(
27+
new asio::ssl::context(service_, asio::ssl::context::sslv23_client));
2628
if (certificate_filename_ || verify_path_) {
2729
context_->set_verify_mode(asio::ssl::context::verify_peer);
28-
if (certificate_filename_) context_->load_verify_file(*certificate_filename_);
30+
if (certificate_filename_)
31+
context_->load_verify_file(*certificate_filename_);
2932
if (verify_path_) context_->add_verify_path(*verify_path_);
3033
} else {
31-
context_->set_verify_mode(asio::ssl::context::verify_none);
34+
if (always_verify_peer_)
35+
context_->set_verify_mode(asio::ssl::context::verify_peer);
36+
else
37+
context_->set_verify_mode(asio::ssl::context::verify_none);
3238
}
33-
socket_.reset(new asio::ssl::stream<asio::ip::tcp::socket>(service_, *context_));
39+
socket_.reset(
40+
new asio::ssl::stream<asio::ip::tcp::socket>(service_, *context_));
3441
socket_->lowest_layer().async_connect(
3542
endpoint,
36-
::boost::bind(&boost::network::http::impl::ssl_delegate::handle_connected,
37-
boost::network::http::impl::ssl_delegate::shared_from_this(),
38-
asio::placeholders::error,
39-
handler));
43+
::boost::bind(
44+
&boost::network::http::impl::ssl_delegate::handle_connected,
45+
boost::network::http::impl::ssl_delegate::shared_from_this(),
46+
asio::placeholders::error, handler));
4047
}
4148

42-
void boost::network::http::impl::ssl_delegate::handle_connected(system::error_code const & ec,
43-
function<void(system::error_code const &)> handler) {
49+
void boost::network::http::impl::ssl_delegate::handle_connected(
50+
system::error_code const &ec,
51+
function<void(system::error_code const &)> handler) {
4452
if (!ec) {
4553
socket_->async_handshake(asio::ssl::stream_base::client, handler);
4654
} else {
@@ -49,17 +57,18 @@ void boost::network::http::impl::ssl_delegate::handle_connected(system::error_co
4957
}
5058

5159
void boost::network::http::impl::ssl_delegate::write(
52-
asio::streambuf & command_streambuf,
60+
asio::streambuf &command_streambuf,
5361
function<void(system::error_code const &, size_t)> handler) {
5462
asio::async_write(*socket_, command_streambuf, handler);
5563
}
5664

5765
void boost::network::http::impl::ssl_delegate::read_some(
58-
asio::mutable_buffers_1 const & read_buffer,
66+
asio::mutable_buffers_1 const &read_buffer,
5967
function<void(system::error_code const &, size_t)> handler) {
6068
socket_->async_read_some(read_buffer, handler);
6169
}
6270

6371
boost::network::http::impl::ssl_delegate::~ssl_delegate() {}
6472

65-
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_CONNECTION_SSL_DELEGATE_IPP_20110819 */
73+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_CONNECTION_SSL_DELEGATE_IPP_20110819 \
74+
*/

0 commit comments

Comments
 (0)