aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorHannes Rantzsch <[email protected]>2019-02-20 11:29:22 +0100
committerHannes Rantzsch <[email protected]>2019-04-16 10:47:50 +0200
commit1735bd0941187a96ec5c5a797d2db624c259d69b (patch)
tree405c3d9796fe801abb24e534f2b42451b35eb215 /src/lib
parent235471bd84f902db5c44ca4f29388287f0cbb189 (diff)
cleanup asio::ssl::stream compatibility interfaces
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/tls/asio/asio_stream.h111
-rw-r--r--src/lib/tls/asio/asio_stream_base.h47
2 files changed, 81 insertions, 77 deletions
diff --git a/src/lib/tls/asio/asio_stream.h b/src/lib/tls/asio/asio_stream.h
index 72f363a88..1cd2af87b 100644
--- a/src/lib/tls/asio/asio_stream.h
+++ b/src/lib/tls/asio/asio_stream.h
@@ -45,31 +45,7 @@ class Stream final : public StreamBase<Channel>
using executor_type = typename next_layer_type::executor_type;
using native_handle_type = typename std::add_pointer<Channel>::type;
- enum handshake_type
- {
- client,
- server
- };
-
- private:
- void validate_handshake_type(handshake_type type)
- {
- if(type != handshake_type::client)
- {
- throw Not_Implemented("server-side TLS stream is not implemented");
- }
- }
-
- bool validate_handshake_type(handshake_type type, boost::system::error_code& ec)
- {
- if(type != handshake_type::client)
- {
- ec = make_error_code(Botan::TLS::error::not_implemented);
- return false;
- }
-
- return true;
- }
+ using StreamBase<Channel>::validate_handshake_type;
public:
template <typename... Args>
@@ -93,7 +69,6 @@ class Stream final : public StreamBase<Channel>
throw Not_Implemented("cannot handle an asio::ssl::context");
}
-
Stream(Stream&& other) = default;
Stream& operator=(Stream&& other) = default;
@@ -167,22 +142,15 @@ class Stream final : public StreamBase<Channel>
// -- -- handshake methods
//
- void handshake(handshake_type type)
+ void handshake()
{
- validate_handshake_type(type);
-
boost::system::error_code ec;
- handshake(type, ec);
+ handshake(ec);
boost::asio::detail::throw_error(ec, "handshake");
}
- void handshake(handshake_type type, boost::system::error_code& ec)
+ void handshake(boost::system::error_code& ec)
{
- if(!validate_handshake_type(type, ec))
- {
- return;
- }
-
while(!native_handle()->is_active())
{
writePendingTlsData(ec);
@@ -214,11 +182,47 @@ class Stream final : public StreamBase<Channel>
}
}
+ template <typename HandshakeHandler>
+ BOOST_ASIO_INITFN_RESULT_TYPE(HandshakeHandler,
+ void(boost::system::error_code))
+ async_handshake(HandshakeHandler&& handler)
+ {
+ BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(HandshakeHandler, handler) type_check;
+
+ boost::asio::async_completion<HandshakeHandler,
+ void(boost::system::error_code)>
+ init(handler);
+
+ auto op = create_async_handshake_op(std::move(init.completion_handler));
+ op(boost::system::error_code{}, 0, 1);
+
+ return init.result.get();
+ }
+
+ //
+ // -- -- asio::ssl::stream compatibility methods
+ //
+
+ void handshake(handshake_type type)
+ {
+ validate_handshake_type(type);
+ handshake();
+ }
+
+ void handshake(handshake_type type, boost::system::error_code& ec)
+ {
+ if(validate_handshake_type(type, ec))
+ {
+ handshake(ec);
+ }
+ }
+
template<typename ConstBufferSequence>
void handshake(handshake_type type, const ConstBufferSequence& buffers)
{
- BOTAN_UNUSED(type, buffers);
- throw Not_Implemented("server-side TLS stream is not implemented");
+ BOTAN_UNUSED(buffers);
+ validate_handshake_type(type);
+ throw Not_Implemented("buffered handshake is not implemented");
}
template<typename ConstBufferSequence>
@@ -226,8 +230,11 @@ class Stream final : public StreamBase<Channel>
const ConstBufferSequence& buffers,
boost::system::error_code& ec)
{
- BOTAN_UNUSED(type, buffers);
- ec = make_error_code(Botan::TLS::error::not_implemented);
+ BOTAN_UNUSED(buffers);
+ if(validate_handshake_type(type, ec))
+ {
+ ec = make_error_code(Botan::TLS::error::not_implemented);
+ }
}
template <typename HandshakeHandler>
@@ -235,33 +242,19 @@ class Stream final : public StreamBase<Channel>
void(boost::system::error_code))
async_handshake(handshake_type type, HandshakeHandler&& handler)
{
- // If you get an error on the following line it means that your handler does
- // not meet the documented type requirements for a HandshakeHandler.
- BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(HandshakeHandler, handler) type_check;
-
validate_handshake_type(type);
-
- boost::asio::async_completion<HandshakeHandler,
- void(boost::system::error_code)>
- init(handler);
-
- auto op = create_async_handshake_op(std::move(init.completion_handler));
- op(boost::system::error_code{}, 0, 1);
-
- return init.result.get();
+ return async_handshake(handler);
}
template <typename ConstBufferSequence, typename BufferedHandshakeHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(BufferedHandshakeHandler,
void(boost::system::error_code, std::size_t))
- async_handshake(handshake_type type,
- const ConstBufferSequence& buffers,
+ async_handshake(handshake_type type, const ConstBufferSequence& buffers,
BufferedHandshakeHandler&& handler)
{
- // If you get an error on the following line it means that your handler does
- // not meet the documented type requirements for a BufferedHandshakeHandler.
+ BOTAN_UNUSED(buffers, handler);
BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(BufferedHandshakeHandler, handler) type_check;
- BOTAN_UNUSED(type, buffers, handler);
+ validate_handshake_type(type);
throw Not_Implemented("buffered async handshake is not implemented");
}
@@ -293,8 +286,6 @@ class Stream final : public StreamBase<Channel>
template <typename ShutdownHandler>
void async_shutdown(ShutdownHandler&& handler)
{
- // If you get an error on the following line it means that your handler does
- // not meet the documented type requirements for a ShutdownHandler.
BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(ShutdownHandler, handler) type_check;
BOTAN_UNUSED(handler);
throw Not_Implemented("async shutdown is not implemented");
diff --git a/src/lib/tls/asio/asio_stream_base.h b/src/lib/tls/asio/asio_stream_base.h
index 0fb5353ed..161392ad8 100644
--- a/src/lib/tls/asio/asio_stream_base.h
+++ b/src/lib/tls/asio/asio_stream_base.h
@@ -12,11 +12,23 @@
#include <botan/auto_rng.h>
#include <botan/tls_client.h>
#include <botan/tls_server.h>
+#include <botan/asio_error.h>
namespace Botan {
namespace TLS {
+enum handshake_type
+ {
+ client,
+ server
+ };
+
+
+/* Base class for all Botan::TLS::Stream implementations.
+ *
+ *
+ */
template <class Channel>
class StreamBase
{
@@ -43,30 +55,31 @@ class StreamBase<Botan::TLS::Client>
StreamBase(const StreamBase&) = delete;
StreamBase& operator=(const StreamBase&) = delete;
- protected:
- Botan::TLS::StreamCore m_core;
- Botan::AutoSeeded_RNG m_rng;
- Botan::TLS::Client m_channel;
- };
+ using handshake_type = Botan::TLS::handshake_type;
-template <>
-class StreamBase<Botan::TLS::Server>
- {
- public:
- StreamBase(Botan::TLS::Session_Manager& sessionManager,
- Botan::Credentials_Manager& credentialsManager,
- const Botan::TLS::Policy& policy = Botan::TLS::Strict_Policy{})
- : m_channel(m_core, sessionManager, credentialsManager, policy, m_rng)
+ protected:
+ void validate_handshake_type(handshake_type type)
{
+ if(type != handshake_type::client)
+ {
+ throw Invalid_Argument("wrong handshake_type");
+ }
}
- StreamBase(const StreamBase&) = delete;
- StreamBase& operator=(const StreamBase&) = delete;
+ bool validate_handshake_type(handshake_type type, boost::system::error_code& ec)
+ {
+ if(type != handshake_type::client)
+ {
+ ec = make_error_code(Botan::TLS::error::invalid_argument);
+ return false;
+ }
+
+ return true;
+ }
- protected:
Botan::TLS::StreamCore m_core;
Botan::AutoSeeded_RNG m_rng;
- Botan::TLS::Server m_channel;
+ Botan::TLS::Client m_channel;
};
} // namespace TLS