diff --git a/include/opendht/http.h b/include/opendht/http.h index 724768272..8c9b7d74e 100644 --- a/include/opendht/http.h +++ b/include/opendht/http.h @@ -63,6 +63,7 @@ using HandlerCb = std::function; using BytesHandlerCb = std::function; using ConnectHandlerCb = std::function; +using SSLVerifyCb = std::function; using ssl_socket_t = restinio::impl::tls_socket_t; using socket_t = asio::ip::tcp::socket; @@ -91,11 +92,10 @@ class OPENDHT_PUBLIC Connection unsigned int id(); bool is_open(); - bool is_v6(); bool is_ssl(); - void set_endpoint(const asio::ip::tcp::endpoint& endpoint, - const asio::ssl::verify_mode verify_mode = asio::ssl::verify_none); + void set_ssl_verification(const asio::ip::tcp::endpoint& endpoint, const asio::ssl::verify_mode verify_mode); + void set_ssl_verification(SSLVerifyCb verify_cb, const asio::ssl::verify_mode verify_mode); asio::streambuf& input(); asio::streambuf& data(); @@ -122,8 +122,6 @@ class OPENDHT_PUBLIC Connection std::unique_ptr ssl_socket_; std::unique_ptr certificate_; - asio::ip::tcp::endpoint endpoint_; - asio::streambuf write_buf_; asio::streambuf read_buf_; @@ -248,6 +246,7 @@ class OPENDHT_PUBLIC Request void add_on_status_callback(OnStatusCb cb); void add_on_body_callback(OnDataCb cb); + void add_on_ssl_verify_callback(SSLVerifyCb cb); void add_on_state_change_callback(OnStateChangeCb cb); void send(); @@ -270,6 +269,7 @@ class OPENDHT_PUBLIC Request OnCompleteCb on_headers_complete; OnCompleteCb on_message_complete; + SSLVerifyCb ssl_verify; OnStateChangeCb on_state_change; }; diff --git a/src/http.cpp b/src/http.cpp index eedd9016f..6ac2cc901 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -152,12 +152,6 @@ Connection::is_open() return socket_->is_open(); } -bool -Connection::is_v6() -{ - return endpoint_.address().is_v6(); -} - bool Connection::is_ssl() { @@ -165,11 +159,10 @@ Connection::is_ssl() } void -Connection::set_endpoint(const asio::ip::tcp::endpoint& endpoint, const asio::ssl::verify_mode verify_mode) +Connection::set_ssl_verification(const asio::ip::tcp::endpoint& endpoint, const asio::ssl::verify_mode verify_mode) { - endpoint_ = endpoint; if (ssl_ctx_ and verify_mode != asio::ssl::verify_none){ - auto hostname = endpoint_.address().to_string(); + auto hostname = endpoint.address().to_string(); ssl_socket_->asio_ssl_stream().set_verify_mode(verify_mode); ssl_socket_->asio_ssl_stream().set_verify_callback( [this, hostname](bool preverified, asio::ssl::verify_context& ctx) -> bool { @@ -187,6 +180,17 @@ Connection::set_endpoint(const asio::ip::tcp::endpoint& endpoint, const asio::ss } } +void +Connection::set_ssl_verification(SSLVerifyCb verify_cb, const asio::ssl::verify_mode verify_mode) +{ + if (ssl_ctx_ and verify_mode != asio::ssl::verify_none){ + ssl_socket_->asio_ssl_stream().set_verify_mode(verify_mode); + ssl_socket_->asio_ssl_stream().set_verify_callback(verify_cb); + if (logger_) + logger_->d("[http:client] [connection:%i] ssl verify callback set", id_); + } +} + asio::streambuf& Connection::input() { @@ -606,6 +610,13 @@ Request::add_on_body_callback(OnDataCb cb) cbs_->on_body = std::move(cb); } +void +Request::add_on_ssl_verify_callback(SSLVerifyCb cb) +{ + std::lock_guard lock(cbs_mutex_); + cbs_->ssl_verify = std::move(cb); +} + void Request::add_on_state_change_callback(OnStateChangeCb cb) { @@ -748,9 +759,11 @@ Request::connect(std::vector&& endpoints, HandlerCb cb) logger_->d("[http:client] [request:%i] connect success", id_); if (get_url().protocol == "https"){ - if (certificate_) - conn_->set_endpoint(endpoint, asio::ssl::verify_peer - | asio::ssl::verify_fail_if_no_peer_cert); + auto verify_mode = asio::ssl::verify_peer | asio::ssl::verify_fail_if_no_peer_cert; + if (cbs_->ssl_verify) + conn_->set_ssl_verification(cbs_->ssl_verify, verify_mode); + else if (certificate_) + conn_->set_ssl_verification(endpoint, verify_mode); if (conn_ and conn_->is_open() and conn_->is_ssl()){ conn_->async_handshake([this, cb](const asio::error_code& ec){ @@ -768,8 +781,6 @@ Request::connect(std::vector&& endpoints, HandlerCb cb) cb(asio::error::operation_aborted); return; } - else - conn_->set_endpoint(endpoint, asio::ssl::verify_none); } if (cb) cb(ec);