Botan  2.13.0
Crypto and TLS for C++11
Classes | Protected Member Functions | Protected Attributes | Friends | List of all members
Botan::TLS::Stream< StreamLayer, ChannelT > Class Template Reference

boost::asio compatible SSL/TLS stream More...

#include <asio_stream.h>

Classes

class  StreamCore
 Helper class that implements Botan::TLS::Callbacks. More...
 

Public Member Functions

construction
template<typename... Args>
 Stream (Context &context, Args &&... args)
 Construct a new Stream. More...
 
template<typename Arg >
 Stream (Arg &&arg, Context &context)
 Construct a new Stream. More...
 
virtual ~Stream ()=default
 
 Stream (Stream &&other)=default
 
Streamoperator= (Stream &&other)=default
 
 Stream (const Stream &other)=delete
 
Streamoperator= (const Stream &other)=delete
 
configuration and callback setters
void set_verify_callback (Context::Verify_Callback callback)
 Override the tls_verify_cert_chain callback. More...
 
void set_verify_callback (Context::Verify_Callback callback, boost::system::error_code &ec)
 Compatibility overload of set_verify_callback. More...
 
void set_verify_depth (int depth)
 
void set_verify_depth (int depth, boost::system::error_code &ec)
 
template<typename verify_mode >
void set_verify_mode (verify_mode v)
 
template<typename verify_mode >
void set_verify_mode (verify_mode v, boost::system::error_code &ec)
 
shutdown methods
void shutdown (boost::system::error_code &ec)
 Shut down SSL on the stream. More...
 
void shutdown ()
 Shut down SSL on the stream. More...
 
template<typename ShutdownHandler >
void async_shutdown (ShutdownHandler &&handler)
 Asynchronously shut down SSL on the stream. More...
 
I/O methods
template<typename MutableBufferSequence >
std::size_t read_some (const MutableBufferSequence &buffers, boost::system::error_code &ec)
 Read some data from the stream. More...
 
template<typename MutableBufferSequence >
std::size_t read_some (const MutableBufferSequence &buffers)
 Read some data from the stream. More...
 
template<typename ConstBufferSequence >
std::size_t write_some (const ConstBufferSequence &buffers, boost::system::error_code &ec)
 Write some data to the stream. More...
 
template<typename ConstBufferSequence >
std::size_t write_some (const ConstBufferSequence &buffers)
 Write some data to the stream. More...
 
template<typename ConstBufferSequence , typename WriteHandler >
auto async_write_some (const ConstBufferSequence &buffers, WriteHandler &&handler) -> BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void(boost::system::error_code, std::size_t))
 Start an asynchronous write. The function call always returns immediately. More...
 
template<typename MutableBufferSequence , typename ReadHandler >
auto async_read_some (const MutableBufferSequence &buffers, ReadHandler &&handler) -> BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void(boost::system::error_code, std::size_t))
 Start an asynchronous read. The function call always returns immediately. More...
 

Protected Member Functions

void consume_send_buffer (std::size_t bytesConsumed)
 Mark bytes in the send buffer as consumed, removing them from the buffer. More...
 
template<typename MutableBufferSequence >
std::size_t copy_received_data (MutableBufferSequence buffers)
 Copy decrypted data into the user-provided buffer. More...
 
bool has_data_to_send () const
 Check if encrypted data is available in the send buffer. More...
 
bool has_received_data () const
 Check if decrypted data is available in the receive buffer. More...
 
const boost::asio::mutable_buffer & input_buffer ()
 
boost::asio::const_buffer send_buffer () const
 
size_t send_pending_encrypted_data (boost::system::error_code &ec)
 
template<class T = ChannelT>
std::enable_if<!std::is_same< Channel, T >::value >::type setup_native_handle (Connection_Side, boost::system::error_code &)
 
template<class T = ChannelT>
std::enable_if< std::is_same< Channel, T >::value >::type setup_native_handle (Connection_Side side, boost::system::error_code &ec)
 Create the native handle. More...
 
template<typename ConstBufferSequence >
void tls_encrypt (const ConstBufferSequence &buffers, boost::system::error_code &ec)
 

Protected Attributes

Contextm_context
 
StreamCore m_core
 
const boost::asio::mutable_buffer m_input_buffer
 
std::vector< uint8_t > m_input_buffer_space
 
std::unique_ptr< ChannelT > m_native_handle
 
StreamLayer m_nextLayer
 
boost::beast::flat_buffer m_receive_buffer
 
boost::beast::flat_buffer m_send_buffer
 

Friends

template<class H , class S , class A >
class detail::AsyncHandshakeOperation
 
template<class H , class S , class M , class A >
class detail::AsyncReadOperation
 
template<class H , class S , class A >
class detail::AsyncWriteOperation
 

boost::asio accessor methods

using next_layer_type = typename std::remove_reference< StreamLayer >::type
 
using lowest_layer_type = typename next_layer_type::lowest_layer_type
 
using executor_type = typename next_layer_type::executor_type
 
using native_handle_type = typename std::add_pointer< ChannelT >::type
 
executor_type get_executor () noexcept
 
const next_layer_typenext_layer () const
 
next_layer_typenext_layer ()
 
lowest_layer_typelowest_layer ()
 
const lowest_layer_typelowest_layer () const
 
native_handle_type native_handle ()
 

handshake methods

const ConstBufferSequence & buffers
 
const ConstBufferSequence BufferedHandshakeHandler && handler
 
void handshake (Connection_Side side)
 Performs SSL handshaking. More...
 
void handshake (Connection_Side side, boost::system::error_code &ec)
 Performs SSL handshaking. More...
 
template<typename HandshakeHandler >
auto async_handshake (Connection_Side side, HandshakeHandler &&handler) ->
 Starts an asynchronous SSL handshake. More...
 
template<typename ConstBufferSequence , typename BufferedHandshakeHandler >
 BOOST_ASIO_INITFN_RESULT_TYPE (BufferedHandshakeHandler, void(boost::system::error_code, std::size_t)) async_handshake(Connection_Side side
 
 BOOST_ASIO_HANDSHAKE_HANDLER_CHECK (BufferedHandshakeHandler, handler) type_check
 
throw Not_Implemented ("buffered async handshake is not implemented")
 

Detailed Description

template<class StreamLayer, class ChannelT = Channel>
class Botan::TLS::Stream< StreamLayer, ChannelT >

boost::asio compatible SSL/TLS stream

Currently only the TLS::Client specialization is implemented.

Template Parameters
StreamLayertype of the next layer, usually a network socket
ChannelTtype of the native_handle, defaults to Botan::TLS::Channel, only needed for testing purposes

Definition at line 49 of file asio_stream.h.

Member Typedef Documentation

◆ executor_type

template<class StreamLayer , class ChannelT = Channel>
using Botan::TLS::Stream< StreamLayer, ChannelT >::executor_type = typename next_layer_type::executor_type

Definition at line 105 of file asio_stream.h.

◆ lowest_layer_type

template<class StreamLayer , class ChannelT = Channel>
using Botan::TLS::Stream< StreamLayer, ChannelT >::lowest_layer_type = typename next_layer_type::lowest_layer_type

Definition at line 104 of file asio_stream.h.

◆ native_handle_type

template<class StreamLayer , class ChannelT = Channel>
using Botan::TLS::Stream< StreamLayer, ChannelT >::native_handle_type = typename std::add_pointer<ChannelT>::type

Definition at line 106 of file asio_stream.h.

◆ next_layer_type

template<class StreamLayer , class ChannelT = Channel>
using Botan::TLS::Stream< StreamLayer, ChannelT >::next_layer_type = typename std::remove_reference<StreamLayer>::type

Definition at line 103 of file asio_stream.h.

Constructor & Destructor Documentation

◆ Stream() [1/4]

template<class StreamLayer , class ChannelT = Channel>
template<typename... Args>
Botan::TLS::Stream< StreamLayer, ChannelT >::Stream ( Context context,
Args &&...  args 
)
inlineexplicit

Construct a new Stream.

Parameters
contextThe context parameter is used to set up the underlying native handle. Using code is responsible for lifetime management of the context and must ensure that it is available for the lifetime of the stream.
argsArguments to be forwarded to the construction of the next layer.

Definition at line 64 of file asio_stream.h.

Referenced by Botan::TLS::Stream< StreamLayer, ChannelT >::async_read_some().

65  : m_context(context)
66  , m_nextLayer(std::forward<Args>(args)...)
70  {}
const boost::asio::mutable_buffer m_input_buffer
Definition: asio_stream.h:709
StreamLayer m_nextLayer
Definition: asio_stream.h:699
StreamCore m_core
Definition: asio_stream.h:704
Context & m_context
Definition: asio_stream.h:698
boost::beast::flat_buffer m_send_buffer
Definition: asio_stream.h:702
std::vector< uint8_t > m_input_buffer_space
Definition: asio_stream.h:708
boost::beast::flat_buffer m_receive_buffer
Definition: asio_stream.h:701

◆ Stream() [2/4]

template<class StreamLayer , class ChannelT = Channel>
template<typename Arg >
Botan::TLS::Stream< StreamLayer, ChannelT >::Stream ( Arg &&  arg,
Context context 
)
inlineexplicit

Construct a new Stream.

Convenience overload for boost::asio::ssl::stream compatibility.

Parameters
argThis argument is forwarded to the construction of the next layer.
contextThe context parameter is used to set up the underlying native handle. Using code is responsible for lifetime management of the context and must ensure that is available for the lifetime of the stream.

Definition at line 83 of file asio_stream.h.

84  : m_context(context)
85  , m_nextLayer(std::forward<Arg>(arg))
89  {}
const boost::asio::mutable_buffer m_input_buffer
Definition: asio_stream.h:709
StreamLayer m_nextLayer
Definition: asio_stream.h:699
StreamCore m_core
Definition: asio_stream.h:704
Context & m_context
Definition: asio_stream.h:698
boost::beast::flat_buffer m_send_buffer
Definition: asio_stream.h:702
std::vector< uint8_t > m_input_buffer_space
Definition: asio_stream.h:708
boost::beast::flat_buffer m_receive_buffer
Definition: asio_stream.h:701

◆ ~Stream()

template<class StreamLayer , class ChannelT = Channel>
virtual Botan::TLS::Stream< StreamLayer, ChannelT >::~Stream ( )
virtualdefault

◆ Stream() [3/4]

template<class StreamLayer , class ChannelT = Channel>
Botan::TLS::Stream< StreamLayer, ChannelT >::Stream ( Stream< StreamLayer, ChannelT > &&  other)
default

◆ Stream() [4/4]

template<class StreamLayer , class ChannelT = Channel>
Botan::TLS::Stream< StreamLayer, ChannelT >::Stream ( const Stream< StreamLayer, ChannelT > &  other)
delete

Member Function Documentation

◆ async_handshake()

template<class StreamLayer , class ChannelT = Channel>
template<typename HandshakeHandler >
auto Botan::TLS::Stream< StreamLayer, ChannelT >::async_handshake ( Connection_Side  side,
HandshakeHandler &&  handler 
) ->
inline

Starts an asynchronous SSL handshake.

This function call always returns immediately.

Parameters
sideThe type of handshaking to be performed, i.e. as a client or as a server.
handlerThe handler to be called when the handshake operation completes. The equivalent function signature of the handler must be: void(boost::system::error_code)
Exceptions
NotImplementedif Connection_Side is not CLIENT

Definition at line 258 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(), Botan::TLS::Stream< StreamLayer, ChannelT >::handler, init, and Botan::TLS::Stream< StreamLayer, ChannelT >::setup_native_handle().

260  {
261  BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(HandshakeHandler, handler) type_check;
262 
263  boost::system::error_code ec;
264  setup_native_handle(side, ec);
265  // If ec is set by setup_native_handle, the AsyncHandshakeOperation created below will do nothing but call the
266  // handler with the error_code set appropriately - no need to early return here.
267 
268  boost::asio::async_completion<HandshakeHandler, void(boost::system::error_code)> init(handler);
269 
271  op{std::move(init.completion_handler), *this, ec};
272 
273  return init.result.get();
274  }
std::enable_if<!std::is_same< Channel, T >::value >::type setup_native_handle(Connection_Side, boost::system::error_code &)
Definition: asio_stream.h:625
BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(BufferedHandshakeHandler, handler) type_check
MechanismType type
const ConstBufferSequence BufferedHandshakeHandler && handler
Definition: asio_stream.h:282
int(* init)(CTX *)
Stream(Context &context, Args &&... args)
Construct a new Stream.
Definition: asio_stream.h:64

◆ async_read_some()

template<class StreamLayer , class ChannelT = Channel>
template<typename MutableBufferSequence , typename ReadHandler >
auto Botan::TLS::Stream< StreamLayer, ChannelT >::async_read_some ( const MutableBufferSequence &  buffers,
ReadHandler &&  handler 
) -> BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void(boost::system::error_code, std::size_t))
inline

Start an asynchronous read. The function call always returns immediately.

Parameters
buffersThe buffers into which the data will be read. Although the buffers object may be copied as necessary, ownership of the underlying buffers is retained by the caller, which must guarantee that they remain valid until the handler is called.
handlerThe handler to be called when the read operation completes. The equivalent function signature of the handler must be: void(boost::system::error_code, std::size_t)

Definition at line 504 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::buffers, Botan::TLS::Stream< StreamLayer, ChannelT >::handler, init, and Botan::TLS::Stream< StreamLayer, ChannelT >::Stream().

507  {
508  BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
509 
510  boost::asio::async_completion<ReadHandler, void(boost::system::error_code, std::size_t)> init(handler);
511 
513  op{std::move(init.completion_handler), *this, buffers};
514  return init.result.get();
515  }
const ConstBufferSequence & buffers
Definition: asio_stream.h:280
MechanismType type
const ConstBufferSequence BufferedHandshakeHandler && handler
Definition: asio_stream.h:282
int(* init)(CTX *)
Stream(Context &context, Args &&... args)
Construct a new Stream.
Definition: asio_stream.h:64

◆ async_shutdown()

template<class StreamLayer , class ChannelT = Channel>
template<typename ShutdownHandler >
void Botan::TLS::Stream< StreamLayer, ChannelT >::async_shutdown ( ShutdownHandler &&  handler)
inline

Asynchronously shut down SSL on the stream.

This function call always returns immediately.

Parameters
handlerThe handler to be called when the handshake operation completes. The equivalent function signature of the handler must be: void(boost::system::error_code)

Definition at line 347 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(), BOTAN_UNUSED, Botan::TLS::Stream< StreamLayer, ChannelT >::handler, and Botan::TLS::Stream< StreamLayer, ChannelT >::Not_Implemented().

348  {
349  BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(ShutdownHandler, handler) type_check;
351  throw Not_Implemented("async shutdown is not implemented");
352  // TODO: Implement a subclass of AsyncBase that calls native_handle()->close() and writes pending data from
353  // the core to the network, e.g. using AsyncWriteOperation.
354  }
BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(BufferedHandshakeHandler, handler) type_check
throw Not_Implemented("buffered async handshake is not implemented")
const ConstBufferSequence BufferedHandshakeHandler && handler
Definition: asio_stream.h:282
#define BOTAN_UNUSED(...)
Definition: assert.h:142

◆ async_write_some()

template<class StreamLayer , class ChannelT = Channel>
template<typename ConstBufferSequence , typename WriteHandler >
auto Botan::TLS::Stream< StreamLayer, ChannelT >::async_write_some ( const ConstBufferSequence &  buffers,
WriteHandler &&  handler 
) -> BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void(boost::system::error_code, std::size_t))
inline

Start an asynchronous write. The function call always returns immediately.

Parameters
buffersThe data to be written.
handlerThe handler to be called when the write operation completes. Copies will be made of the handler as required. The equivalent function signature of the handler must be: void(boost::system::error_code, std::size_t)

Definition at line 467 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::buffers, Botan::TLS::Stream< StreamLayer, ChannelT >::consume_send_buffer(), Botan::TLS::Stream< StreamLayer, ChannelT >::handler, init, Botan::TLS::Stream< StreamLayer, ChannelT >::m_send_buffer, and Botan::TLS::Stream< StreamLayer, ChannelT >::tls_encrypt().

470  {
471  BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
472 
473  boost::asio::async_completion<WriteHandler, void(boost::system::error_code, std::size_t)> init(handler);
474 
475  boost::system::error_code ec;
476  tls_encrypt(buffers, ec);
477  if(ec)
478  {
479  // we cannot be sure how many bytes were committed here so clear the send_buffer and let the
480  // AsyncWriteOperation call the handler with the error_code set
483  op{std::move(init.completion_handler), *this, std::size_t(0), ec};
484  return init.result.get();
485  }
486 
488  op{std::move(init.completion_handler), *this, boost::asio::buffer_size(buffers)};
489 
490  return init.result.get();
491  }
const ConstBufferSequence & buffers
Definition: asio_stream.h:280
MechanismType type
const ConstBufferSequence BufferedHandshakeHandler && handler
Definition: asio_stream.h:282
boost::beast::flat_buffer m_send_buffer
Definition: asio_stream.h:702
void tls_encrypt(const ConstBufferSequence &buffers, boost::system::error_code &ec)
Definition: asio_stream.h:669
int(* init)(CTX *)
Stream(Context &context, Args &&... args)
Construct a new Stream.
Definition: asio_stream.h:64
void consume_send_buffer(std::size_t bytesConsumed)
Mark bytes in the send buffer as consumed, removing them from the buffer.
Definition: asio_stream.h:617

◆ BOOST_ASIO_HANDSHAKE_HANDLER_CHECK()

template<class StreamLayer , class ChannelT = Channel>
Botan::TLS::Stream< StreamLayer, ChannelT >::BOOST_ASIO_HANDSHAKE_HANDLER_CHECK ( BufferedHandshakeHandler  ,
handler   
)

◆ BOOST_ASIO_INITFN_RESULT_TYPE()

template<class StreamLayer , class ChannelT = Channel>
template<typename ConstBufferSequence , typename BufferedHandshakeHandler >
Botan::TLS::Stream< StreamLayer, ChannelT >::BOOST_ASIO_INITFN_RESULT_TYPE ( BufferedHandshakeHandler  ,
void(boost::system::error_code, std::size_t)   
)
Exceptions
Not_Implemented

◆ consume_send_buffer()

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::consume_send_buffer ( std::size_t  bytesConsumed)
inlineprotected

◆ copy_received_data()

template<class StreamLayer , class ChannelT = Channel>
template<typename MutableBufferSequence >
std::size_t Botan::TLS::Stream< StreamLayer, ChannelT >::copy_received_data ( MutableBufferSequence  buffers)
inlineprotected

Copy decrypted data into the user-provided buffer.

Definition at line 602 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::buffers, and Botan::TLS::Stream< StreamLayer, ChannelT >::m_receive_buffer.

Referenced by Botan::TLS::detail::AsyncReadOperation< Handler, Stream, MutableBufferSequence, Allocator >::operator()(), and Botan::TLS::Stream< StreamLayer, ChannelT >::read_some().

603  {
604  // Note: It would be nice to avoid this buffer copy. This could be achieved by equipping the StreamCore with
605  // the user's desired target buffer once a read is started, and reading directly into that buffer in tls_record
606  // received. However, we need to deal with the case that the receive buffer provided by the caller is smaller
607  // than the decrypted record, so this optimization might not be worth the additional complexity.
608  const auto copiedBytes = boost::asio::buffer_copy(buffers, m_receive_buffer.data());
609  m_receive_buffer.consume(copiedBytes);
610  return copiedBytes;
611  }
const ConstBufferSequence & buffers
Definition: asio_stream.h:280
boost::beast::flat_buffer m_receive_buffer
Definition: asio_stream.h:701

◆ get_executor()

template<class StreamLayer , class ChannelT = Channel>
executor_type Botan::TLS::Stream< StreamLayer, ChannelT >::get_executor ( )
inlinenoexcept

Definition at line 108 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::m_nextLayer.

108 { return m_nextLayer.get_executor(); }
StreamLayer m_nextLayer
Definition: asio_stream.h:699

◆ handshake() [1/2]

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::handshake ( Connection_Side  side)
inline

Performs SSL handshaking.

The function call will block until handshaking is complete or an error occurs.

Parameters
sideThe type of handshaking to be performed, i.e. as a client or as a server.
Exceptions
boost::system::system_errorif error occured, or if the chosen Connection_Side is not available

Definition at line 198 of file asio_stream.h.

199  {
200  boost::system::error_code ec;
201  handshake(side, ec);
202  boost::asio::detail::throw_error(ec, "handshake");
203  }
void handshake(Connection_Side side)
Performs SSL handshaking.
Definition: asio_stream.h:198

◆ handshake() [2/2]

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::handshake ( Connection_Side  side,
boost::system::error_code &  ec 
)
inline

Performs SSL handshaking.

The function call will block until handshaking is complete or an error occurs.

Parameters
sideThe type of handshaking to be performed, i.e. as a client or as a server.
ecSet to indicate what error occurred, if any.

Definition at line 213 of file asio_stream.h.

References Botan::Exception::error_type(), Botan::TLS::Stream< StreamLayer, ChannelT >::input_buffer(), Botan::TLS::Stream< StreamLayer, ChannelT >::m_nextLayer, Botan::TLS::Stream< StreamLayer, ChannelT >::native_handle(), Botan::TLS::Stream< StreamLayer, ChannelT >::send_pending_encrypted_data(), Botan::TLS::Stream< StreamLayer, ChannelT >::setup_native_handle(), Botan::TLS::TLS_Exception::type(), and Botan::Unknown.

214  {
215  setup_native_handle(side, ec);
216 
217  // send client hello, which was written to the send buffer on client instantiation
219 
220  while(!native_handle()->is_active() && !ec)
221  {
222  boost::asio::const_buffer read_buffer{input_buffer().data(), m_nextLayer.read_some(input_buffer(), ec)};
223  if(ec)
224  { return; }
225 
226  try
227  {
228  native_handle()->received_data(static_cast<const uint8_t*>(read_buffer.data()), read_buffer.size());
229  }
230  catch(const TLS_Exception& e)
231  {
232  ec = e.type();
233  }
234  catch(const Botan::Exception& e)
235  {
236  ec = e.error_type();
237  }
238  catch(const std::exception&)
239  {
241  }
242 
244  }
245  }
size_t send_pending_encrypted_data(boost::system::error_code &ec)
Definition: asio_stream.h:658
std::enable_if<!std::is_same< Channel, T >::value >::type setup_native_handle(Connection_Side, boost::system::error_code &)
Definition: asio_stream.h:625
virtual ErrorType error_type() const noexcept
Definition: exceptn.h:101
StreamLayer m_nextLayer
Definition: asio_stream.h:699
const boost::asio::mutable_buffer & input_buffer()
Definition: asio_stream.h:594
native_handle_type native_handle()
Definition: asio_stream.h:116

◆ has_data_to_send()

template<class StreamLayer , class ChannelT = Channel>
bool Botan::TLS::Stream< StreamLayer, ChannelT >::has_data_to_send ( ) const
inlineprotected

◆ has_received_data()

template<class StreamLayer , class ChannelT = Channel>
bool Botan::TLS::Stream< StreamLayer, ChannelT >::has_received_data ( ) const
inlineprotected

Check if decrypted data is available in the receive buffer.

Definition at line 598 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::m_receive_buffer.

Referenced by Botan::TLS::detail::AsyncReadOperation< Handler, Stream, MutableBufferSequence, Allocator >::operator()(), and Botan::TLS::Stream< StreamLayer, ChannelT >::read_some().

598 { return m_receive_buffer.size() > 0; }
boost::beast::flat_buffer m_receive_buffer
Definition: asio_stream.h:701

◆ input_buffer()

template<class StreamLayer , class ChannelT = Channel>
const boost::asio::mutable_buffer& Botan::TLS::Stream< StreamLayer, ChannelT >::input_buffer ( )
inlineprotected

◆ lowest_layer() [1/2]

template<class StreamLayer , class ChannelT = Channel>
lowest_layer_type& Botan::TLS::Stream< StreamLayer, ChannelT >::lowest_layer ( )
inline

Definition at line 113 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::m_nextLayer.

113 { return m_nextLayer.lowest_layer(); }
StreamLayer m_nextLayer
Definition: asio_stream.h:699

◆ lowest_layer() [2/2]

template<class StreamLayer , class ChannelT = Channel>
const lowest_layer_type& Botan::TLS::Stream< StreamLayer, ChannelT >::lowest_layer ( ) const
inline

Definition at line 114 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::m_nextLayer.

114 { return m_nextLayer.lowest_layer(); }
StreamLayer m_nextLayer
Definition: asio_stream.h:699

◆ native_handle()

template<class StreamLayer , class ChannelT = Channel>
native_handle_type Botan::TLS::Stream< StreamLayer, ChannelT >::native_handle ( )
inline

◆ next_layer() [1/2]

template<class StreamLayer , class ChannelT = Channel>
const next_layer_type& Botan::TLS::Stream< StreamLayer, ChannelT >::next_layer ( ) const
inline

◆ next_layer() [2/2]

template<class StreamLayer , class ChannelT = Channel>
next_layer_type& Botan::TLS::Stream< StreamLayer, ChannelT >::next_layer ( )
inline

Definition at line 111 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::m_nextLayer.

111 { return m_nextLayer; }
StreamLayer m_nextLayer
Definition: asio_stream.h:699

◆ Not_Implemented()

template<class StreamLayer , class ChannelT = Channel>
throw Botan::TLS::Stream< StreamLayer, ChannelT >::Not_Implemented ( "buffered async handshake is not implemented"  )

◆ operator=() [1/2]

template<class StreamLayer , class ChannelT = Channel>
Stream& Botan::TLS::Stream< StreamLayer, ChannelT >::operator= ( Stream< StreamLayer, ChannelT > &&  other)
default

◆ operator=() [2/2]

template<class StreamLayer , class ChannelT = Channel>
Stream& Botan::TLS::Stream< StreamLayer, ChannelT >::operator= ( const Stream< StreamLayer, ChannelT > &  other)
delete

◆ read_some() [1/2]

template<class StreamLayer , class ChannelT = Channel>
template<typename MutableBufferSequence >
std::size_t Botan::TLS::Stream< StreamLayer, ChannelT >::read_some ( const MutableBufferSequence &  buffers,
boost::system::error_code &  ec 
)
inline

Read some data from the stream.

The function call will block until one or more bytes of data has been read successfully, or until an error occurs.

Parameters
buffersThe buffers into which the data will be read.
ecSet to indicate what error occured, if any.
Returns
The number of bytes read. Returns 0 if an error occurred.

Definition at line 371 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::buffers, Botan::TLS::Stream< StreamLayer, ChannelT >::copy_received_data(), Botan::Exception::error_type(), Botan::TLS::Stream< StreamLayer, ChannelT >::has_received_data(), Botan::TLS::Stream< StreamLayer, ChannelT >::input_buffer(), Botan::TLS::Stream< StreamLayer, ChannelT >::m_nextLayer, Botan::TLS::Stream< StreamLayer, ChannelT >::native_handle(), Botan::TLS::TLS_Exception::type(), and Botan::Unknown.

Referenced by Botan::TLS::Stream< StreamLayer, ChannelT >::read_some().

373  {
374  if(has_received_data())
375  { return copy_received_data(buffers); }
376 
377  boost::asio::const_buffer read_buffer{input_buffer().data(), m_nextLayer.read_some(input_buffer(), ec)};
378  if(ec)
379  { return 0; }
380 
381  try
382  {
383  native_handle()->received_data(static_cast<const uint8_t*>(read_buffer.data()), read_buffer.size());
384  }
385  catch(const TLS_Exception& e)
386  {
387  ec = e.type();
388  }
389  catch(const Botan::Exception& e)
390  {
391  ec = e.error_type();
392  }
393  catch(const std::exception&)
394  {
396  }
397 
398  return !ec ? copy_received_data(buffers) : 0;
399  }
const ConstBufferSequence & buffers
Definition: asio_stream.h:280
virtual ErrorType error_type() const noexcept
Definition: exceptn.h:101
StreamLayer m_nextLayer
Definition: asio_stream.h:699
const boost::asio::mutable_buffer & input_buffer()
Definition: asio_stream.h:594
std::size_t copy_received_data(MutableBufferSequence buffers)
Copy decrypted data into the user-provided buffer.
Definition: asio_stream.h:602
native_handle_type native_handle()
Definition: asio_stream.h:116
bool has_received_data() const
Check if decrypted data is available in the receive buffer.
Definition: asio_stream.h:598

◆ read_some() [2/2]

template<class StreamLayer , class ChannelT = Channel>
template<typename MutableBufferSequence >
std::size_t Botan::TLS::Stream< StreamLayer, ChannelT >::read_some ( const MutableBufferSequence &  buffers)
inline

Read some data from the stream.

The function call will block until one or more bytes of data has been read successfully, or until an error occurs.

Parameters
buffersThe buffers into which the data will be read.
Returns
The number of bytes read. Returns 0 if an error occurred.
Exceptions
boost::system::system_errorif error occured

Definition at line 412 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::buffers, and Botan::TLS::Stream< StreamLayer, ChannelT >::read_some().

413  {
414  boost::system::error_code ec;
415  auto const n = read_some(buffers, ec);
416  boost::asio::detail::throw_error(ec, "read_some");
417  return n;
418  }
const ConstBufferSequence & buffers
Definition: asio_stream.h:280
std::size_t read_some(const MutableBufferSequence &buffers, boost::system::error_code &ec)
Read some data from the stream.
Definition: asio_stream.h:371

◆ send_buffer()

template<class StreamLayer , class ChannelT = Channel>
boost::asio::const_buffer Botan::TLS::Stream< StreamLayer, ChannelT >::send_buffer ( ) const
inlineprotected

◆ send_pending_encrypted_data()

template<class StreamLayer , class ChannelT = Channel>
size_t Botan::TLS::Stream< StreamLayer, ChannelT >::send_pending_encrypted_data ( boost::system::error_code &  ec)
inlineprotected

Definition at line 658 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::consume_send_buffer(), Botan::TLS::Stream< StreamLayer, ChannelT >::m_nextLayer, and Botan::TLS::Stream< StreamLayer, ChannelT >::send_buffer().

Referenced by Botan::TLS::Stream< StreamLayer, ChannelT >::handshake(), Botan::TLS::Stream< StreamLayer, ChannelT >::shutdown(), and Botan::TLS::Stream< StreamLayer, ChannelT >::write_some().

659  {
660  if(ec)
661  { return 0; }
662 
663  auto writtenBytes = boost::asio::write(m_nextLayer, send_buffer(), ec);
664  consume_send_buffer(writtenBytes);
665  return writtenBytes;
666  }
StreamLayer m_nextLayer
Definition: asio_stream.h:699
boost::asio::const_buffer send_buffer() const
Definition: asio_stream.h:595
void consume_send_buffer(std::size_t bytesConsumed)
Mark bytes in the send buffer as consumed, removing them from the buffer.
Definition: asio_stream.h:617

◆ set_verify_callback() [1/2]

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::set_verify_callback ( Context::Verify_Callback  callback)
inline

Override the tls_verify_cert_chain callback.

This changes the verify_callback in the stream's TLS::Context, and hence the tls_verify_cert_chain callback used in the handshake. Using this function is equivalent to setting the callback via

See also
Botan::TLS::Context::set_verify_callback
Note
This function should only be called before initiating the TLS handshake

Definition at line 131 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::m_context, and Botan::TLS::Context::set_verify_callback().

132  {
133  m_context.set_verify_callback(std::move(callback));
134  }
void set_verify_callback(Verify_Callback callback)
Override the tls_verify_cert_chain callback.
Definition: asio_context.h:84
Context & m_context
Definition: asio_stream.h:698

◆ set_verify_callback() [2/2]

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::set_verify_callback ( Context::Verify_Callback  callback,
boost::system::error_code &  ec 
)
inline

Compatibility overload of set_verify_callback.

Parameters
callbackthe callback implementation
ecThis parameter is unused.

Definition at line 142 of file asio_stream.h.

References BOTAN_UNUSED, Botan::TLS::Stream< StreamLayer, ChannelT >::m_context, and Botan::TLS::Context::set_verify_callback().

143  {
144  BOTAN_UNUSED(ec);
145  m_context.set_verify_callback(std::move(callback));
146  }
void set_verify_callback(Verify_Callback callback)
Override the tls_verify_cert_chain callback.
Definition: asio_context.h:84
Context & m_context
Definition: asio_stream.h:698
#define BOTAN_UNUSED(...)
Definition: assert.h:142

◆ set_verify_depth() [1/2]

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::set_verify_depth ( int  depth)
inline
Exceptions
Not_Implemented

Definition at line 149 of file asio_stream.h.

References BOTAN_UNUSED, and Botan::TLS::Stream< StreamLayer, ChannelT >::Not_Implemented().

150  {
151  BOTAN_UNUSED(depth);
152  throw Not_Implemented("set_verify_depth is not implemented");
153  }
throw Not_Implemented("buffered async handshake is not implemented")
#define BOTAN_UNUSED(...)
Definition: assert.h:142

◆ set_verify_depth() [2/2]

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::set_verify_depth ( int  depth,
boost::system::error_code &  ec 
)
inline

Not Implemented.

Parameters
depththe desired verification depth
ecWill be set to Botan::ErrorType::NotImplemented

Definition at line 160 of file asio_stream.h.

References BOTAN_UNUSED, and Botan::NotImplemented.

161  {
162  BOTAN_UNUSED(depth);
164  }
#define BOTAN_UNUSED(...)
Definition: assert.h:142

◆ set_verify_mode() [1/2]

template<class StreamLayer , class ChannelT = Channel>
template<typename verify_mode >
void Botan::TLS::Stream< StreamLayer, ChannelT >::set_verify_mode ( verify_mode  v)
inline
Exceptions
Not_Implemented

Definition at line 168 of file asio_stream.h.

References BOTAN_UNUSED, and Botan::TLS::Stream< StreamLayer, ChannelT >::Not_Implemented().

169  {
170  BOTAN_UNUSED(v);
171  throw Not_Implemented("set_verify_mode is not implemented");
172  }
throw Not_Implemented("buffered async handshake is not implemented")
#define BOTAN_UNUSED(...)
Definition: assert.h:142

◆ set_verify_mode() [2/2]

template<class StreamLayer , class ChannelT = Channel>
template<typename verify_mode >
void Botan::TLS::Stream< StreamLayer, ChannelT >::set_verify_mode ( verify_mode  v,
boost::system::error_code &  ec 
)
inline

Not Implemented.

Parameters
vthe desired verify mode
ecWill be set to Botan::ErrorType::NotImplemented

Definition at line 180 of file asio_stream.h.

References BOTAN_UNUSED, and Botan::NotImplemented.

181  {
182  BOTAN_UNUSED(v);
184  }
#define BOTAN_UNUSED(...)
Definition: assert.h:142

◆ setup_native_handle() [1/2]

template<class StreamLayer , class ChannelT = Channel>
template<class T = ChannelT>
std::enable_if<!std::is_same<Channel, T>::value>::type Botan::TLS::Stream< StreamLayer, ChannelT >::setup_native_handle ( Connection_Side  ,
boost::system::error_code &   
)
inlineprotected

◆ setup_native_handle() [2/2]

template<class StreamLayer , class ChannelT = Channel>
template<class T = ChannelT>
std::enable_if<std::is_same<Channel, T>::value>::type Botan::TLS::Stream< StreamLayer, ChannelT >::setup_native_handle ( Connection_Side  side,
boost::system::error_code &  ec 
)
inlineprotected

Create the native handle.

Depending on the desired connection side, this function will create a Botan::TLS::Client or a Botan::TLS::Server.

Parameters
sideThe desired connection side (client or server)
ecSet to NotImplemented when side is SERVER - currently only CLIENT is implemented

Definition at line 638 of file asio_stream.h.

References Botan::TLS::CLIENT, Botan::TLS::Stream< StreamLayer, ChannelT >::m_context, Botan::TLS::Stream< StreamLayer, ChannelT >::m_core, Botan::TLS::Context::m_credentials_manager, Botan::TLS::Stream< StreamLayer, ChannelT >::m_native_handle, Botan::TLS::Context::m_policy, Botan::TLS::Context::m_rng, Botan::TLS::Context::m_server_info, Botan::TLS::Context::m_session_manager, and Botan::NotImplemented.

639  {
640  if(side == CLIENT)
641  {
642  m_native_handle = std::unique_ptr<Client>(
643  new Client(m_core,
649  }
650  else
651  {
652  // TODO: First steps in order to support the server side of this stream would be to instantiate a
653  // Botan::TLS::Server instance as the stream's native_handle and implement the handshake appropriately.
655  }
656  }
RandomNumberGenerator & m_rng
Definition: asio_context.h:108
Credentials_Manager & m_credentials_manager
Definition: asio_context.h:107
StreamCore m_core
Definition: asio_stream.h:704
Context & m_context
Definition: asio_stream.h:698
Server_Information m_server_info
Definition: asio_context.h:112
Session_Manager & m_session_manager
Definition: asio_context.h:109
std::unique_ptr< ChannelT > m_native_handle
Definition: asio_stream.h:705

◆ shutdown() [1/2]

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::shutdown ( boost::system::error_code &  ec)
inline

Shut down SSL on the stream.

This function is used to shut down SSL on the stream. The function call will block until SSL has been shut down or an error occurs. Note that this will not close the lowest layer.

Parameters
ecSet to indicate what error occured, if any.

Definition at line 300 of file asio_stream.h.

References Botan::Exception::error_type(), Botan::TLS::Stream< StreamLayer, ChannelT >::native_handle(), Botan::TLS::Stream< StreamLayer, ChannelT >::send_pending_encrypted_data(), Botan::TLS::TLS_Exception::type(), and Botan::Unknown.

301  {
302  try
303  {
304  native_handle()->close();
305  }
306  catch(const TLS_Exception& e)
307  {
308  ec = e.type();
309  }
310  catch(const Botan::Exception& e)
311  {
312  ec = e.error_type();
313  }
314  catch(const std::exception&)
315  {
317  }
318 
319  if(!ec)
321  }
size_t send_pending_encrypted_data(boost::system::error_code &ec)
Definition: asio_stream.h:658
virtual ErrorType error_type() const noexcept
Definition: exceptn.h:101
native_handle_type native_handle()
Definition: asio_stream.h:116

◆ shutdown() [2/2]

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::shutdown ( )
inline

Shut down SSL on the stream.

This function is used to shut down SSL on the stream. The function call will block until SSL has been shut down or an error occurs. Note that this will not close the lowest layer.

Exceptions
boost::system::system_errorif error occured

Definition at line 331 of file asio_stream.h.

332  {
333  boost::system::error_code ec;
334  shutdown(ec);
335  boost::asio::detail::throw_error(ec, "shutdown");
336  }
void shutdown()
Shut down SSL on the stream.
Definition: asio_stream.h:331

◆ tls_encrypt()

template<class StreamLayer , class ChannelT = Channel>
template<typename ConstBufferSequence >
void Botan::TLS::Stream< StreamLayer, ChannelT >::tls_encrypt ( const ConstBufferSequence &  buffers,
boost::system::error_code &  ec 
)
inlineprotected

Definition at line 669 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::buffers, Botan::Exception::error_type(), Botan::TLS::Stream< StreamLayer, ChannelT >::native_handle(), Botan::TLS::TLS_Exception::type(), and Botan::Unknown.

Referenced by Botan::TLS::Stream< StreamLayer, ChannelT >::async_write_some(), and Botan::TLS::Stream< StreamLayer, ChannelT >::write_some().

670  {
671  // NOTE: This is not asynchronous: it encrypts the data synchronously.
672  // The data encrypted by native_handle()->send() is synchronously stored in the send_buffer of m_core,
673  // but is not actually written to the wire, yet.
674  for(auto it = boost::asio::buffer_sequence_begin(buffers);
675  !ec && it != boost::asio::buffer_sequence_end(buffers);
676  it++)
677  {
678  const boost::asio::const_buffer buffer = *it;
679  try
680  {
681  native_handle()->send(static_cast<const uint8_t*>(buffer.data()), buffer.size());
682  }
683  catch(const TLS_Exception& e)
684  {
685  ec = e.type();
686  }
687  catch(const Botan::Exception& e)
688  {
689  ec = e.error_type();
690  }
691  catch(const std::exception&)
692  {
694  }
695  }
696  }
const ConstBufferSequence & buffers
Definition: asio_stream.h:280
virtual ErrorType error_type() const noexcept
Definition: exceptn.h:101
native_handle_type native_handle()
Definition: asio_stream.h:116

◆ write_some() [1/2]

template<class StreamLayer , class ChannelT = Channel>
template<typename ConstBufferSequence >
std::size_t Botan::TLS::Stream< StreamLayer, ChannelT >::write_some ( const ConstBufferSequence &  buffers,
boost::system::error_code &  ec 
)
inline

Write some data to the stream.

The function call will block until one or more bytes of data has been written successfully, or until an error occurs.

Parameters
buffersThe data to be written.
ecSet to indicate what error occurred, if any.
Returns
The number of bytes processed from the input buffers.

Definition at line 431 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::buffers, Botan::TLS::Stream< StreamLayer, ChannelT >::send_pending_encrypted_data(), and Botan::TLS::Stream< StreamLayer, ChannelT >::tls_encrypt().

Referenced by Botan::TLS::Stream< StreamLayer, ChannelT >::write_some().

433  {
434  tls_encrypt(buffers, ec);
436  return !ec ? boost::asio::buffer_size(buffers) : 0;
437  }
size_t send_pending_encrypted_data(boost::system::error_code &ec)
Definition: asio_stream.h:658
const ConstBufferSequence & buffers
Definition: asio_stream.h:280
void tls_encrypt(const ConstBufferSequence &buffers, boost::system::error_code &ec)
Definition: asio_stream.h:669

◆ write_some() [2/2]

template<class StreamLayer , class ChannelT = Channel>
template<typename ConstBufferSequence >
std::size_t Botan::TLS::Stream< StreamLayer, ChannelT >::write_some ( const ConstBufferSequence &  buffers)
inline

Write some data to the stream.

The function call will block until one or more bytes of data has been written successfully, or until an error occurs.

Parameters
buffersThe data to be written.
Returns
The number of bytes written.
Exceptions
boost::system::system_errorif error occured

Definition at line 450 of file asio_stream.h.

References Botan::TLS::Stream< StreamLayer, ChannelT >::buffers, and Botan::TLS::Stream< StreamLayer, ChannelT >::write_some().

451  {
452  boost::system::error_code ec;
453  auto const n = write_some(buffers, ec);
454  boost::asio::detail::throw_error(ec, "write_some");
455  return n;
456  }
const ConstBufferSequence & buffers
Definition: asio_stream.h:280
std::size_t write_some(const ConstBufferSequence &buffers, boost::system::error_code &ec)
Write some data to the stream.
Definition: asio_stream.h:431

Friends And Related Function Documentation

◆ detail::AsyncHandshakeOperation

template<class StreamLayer , class ChannelT = Channel>
template<class H , class S , class A >
friend class detail::AsyncHandshakeOperation
friend

Definition at line 522 of file asio_stream.h.

◆ detail::AsyncReadOperation

template<class StreamLayer , class ChannelT = Channel>
template<class H , class S , class M , class A >
friend class detail::AsyncReadOperation
friend

Definition at line 520 of file asio_stream.h.

◆ detail::AsyncWriteOperation

template<class StreamLayer , class ChannelT = Channel>
template<class H , class S , class A >
friend class detail::AsyncWriteOperation
friend

Definition at line 521 of file asio_stream.h.

Member Data Documentation

◆ buffers

template<class StreamLayer , class ChannelT = Channel>
const ConstBufferSequence& Botan::TLS::Stream< StreamLayer, ChannelT >::buffers

◆ handler

template<class StreamLayer , class ChannelT = Channel>
const ConstBufferSequence BufferedHandshakeHandler&& Botan::TLS::Stream< StreamLayer, ChannelT >::handler

◆ m_context

template<class StreamLayer , class ChannelT = Channel>
Context& Botan::TLS::Stream< StreamLayer, ChannelT >::m_context
protected

◆ m_core

template<class StreamLayer , class ChannelT = Channel>
StreamCore Botan::TLS::Stream< StreamLayer, ChannelT >::m_core
protected

◆ m_input_buffer

template<class StreamLayer , class ChannelT = Channel>
const boost::asio::mutable_buffer Botan::TLS::Stream< StreamLayer, ChannelT >::m_input_buffer
protected

◆ m_input_buffer_space

template<class StreamLayer , class ChannelT = Channel>
std::vector<uint8_t> Botan::TLS::Stream< StreamLayer, ChannelT >::m_input_buffer_space
protected

Definition at line 708 of file asio_stream.h.

◆ m_native_handle

template<class StreamLayer , class ChannelT = Channel>
std::unique_ptr<ChannelT> Botan::TLS::Stream< StreamLayer, ChannelT >::m_native_handle
protected

◆ m_nextLayer

template<class StreamLayer , class ChannelT = Channel>
StreamLayer Botan::TLS::Stream< StreamLayer, ChannelT >::m_nextLayer
protected

◆ m_receive_buffer

template<class StreamLayer , class ChannelT = Channel>
boost::beast::flat_buffer Botan::TLS::Stream< StreamLayer, ChannelT >::m_receive_buffer
protected

◆ m_send_buffer

template<class StreamLayer , class ChannelT = Channel>
boost::beast::flat_buffer Botan::TLS::Stream< StreamLayer, ChannelT >::m_send_buffer
protected

The documentation for this class was generated from the following file: