Botan 3.0.0
Crypto and TLS for C&
Classes | Public Member Functions | 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 TLS::Callbacks. More...
 

Public Member Functions

bool shutdown_received () const
 Indicates whether a close_notify alert has been received from the peer.
 
construction
template<typename... Args>
 Stream (std::shared_ptr< Context > context, Args &&... args)
 Construct a new Stream.
 
template<typename Arg >
 Stream (Arg &&arg, std::shared_ptr< Context > context)
 Construct a new Stream.
 
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.
 
void set_verify_callback (Context::Verify_Callback callback, boost::system::error_code &ec)
 Compatibility overload of set_verify_callback.
 
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.
 
void shutdown ()
 Shut down SSL on the stream.
 
template<typename CompletionToken >
auto async_shutdown (CompletionToken &&completion_token)
 Asynchronously shut down SSL on the stream.
 
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.
 
template<typename MutableBufferSequence >
std::size_t read_some (const MutableBufferSequence &buffers)
 Read some data from the stream.
 
template<typename ConstBufferSequence >
std::size_t write_some (const ConstBufferSequence &buffers, boost::system::error_code &ec)
 Write some data to the stream.
 
template<typename ConstBufferSequence >
std::size_t write_some (const ConstBufferSequence &buffers)
 Write some data to the stream.
 
template<typename ConstBufferSequence , typename CompletionToken >
auto async_write_some (const ConstBufferSequence &buffers, CompletionToken &&completion_token)
 Start an asynchronous write. The function call always returns immediately.
 
template<typename MutableBufferSequence , typename CompletionToken >
auto async_read_some (const MutableBufferSequence &buffers, CompletionToken &&completion_token)
 Start an asynchronous read. The function call always returns immediately.
 

Protected Member Functions

void consume_send_buffer (std::size_t bytesConsumed)
 Mark bytes in the send buffer as consumed, removing them from the buffer.
 
template<typename MutableBufferSequence >
std::size_t copy_received_data (MutableBufferSequence buffers)
 Copy decrypted data into the user-provided buffer.
 
bool has_data_to_send () const
 Check if encrypted data is available in the send buffer.
 
bool has_received_data () const
 Check if decrypted data is available in the receive buffer.
 
const boost::asio::mutable_buffer & input_buffer ()
 
void process_encrypted_data (const boost::asio::const_buffer &read_buffer, boost::system::error_code &ec)
 Pass encrypted data to the native handle for processing.
 
boost::asio::const_buffer send_buffer () const
 
size_t send_pending_encrypted_data (boost::system::error_code &ec)
 Synchronously write encrypted data from the send buffer to the next layer.
 
void setup_native_handle (Connection_Side side, boost::system::error_code &ec)
 Create the native handle.
 
template<typename ConstBufferSequence >
void tls_encrypt (const ConstBufferSequence &buffers, boost::system::error_code &ec)
 Pass plaintext data to the native handle for processing.
 
template<typename Fun >
void try_with_error_code (Fun f, boost::system::error_code &ec)
 Catch exceptions and set an error_code.
 

Protected Attributes

std::shared_ptr< Contextm_context
 
std::shared_ptr< StreamCorem_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
 

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
 
const next_layer_typenext_layer () const
 
next_layer_typenext_layer ()
 
lowest_layer_typelowest_layer ()
 
const lowest_layer_typelowest_layer () const
 
executor_type get_executor () noexcept
 
native_handle_type native_handle ()
 

handshake methods

const ConstBufferSequence & buffers
 
const ConstBufferSequence BufferedHandshakeHandler && handler
 
void handshake (Connection_Side side)
 Performs SSL handshaking.
 
void handshake (Connection_Side side, boost::system::error_code &ec)
 Performs SSL handshaking.
 
template<typename CompletionToken >
auto async_handshake (Botan::TLS::Connection_Side side, CompletionToken &&completion_token)
 Starts an asynchronous SSL handshake.
 
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

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

Definition at line 48 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 124 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 118 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 127 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 102 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 ( std::shared_ptr< 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 63 of file asio_stream.h.

64 : m_context(context)
65 , m_nextLayer(std::forward<Args>(args)...)
66 , m_core(std::make_shared<StreamCore>(context))
69 {}
const boost::asio::mutable_buffer m_input_buffer
Definition: asio_stream.h:836
std::shared_ptr< StreamCore > m_core
Definition: asio_stream.h:831
std::vector< uint8_t > m_input_buffer_space
Definition: asio_stream.h:835
std::shared_ptr< Context > m_context
Definition: asio_stream.h:828
StreamLayer m_nextLayer
Definition: asio_stream.h:829
@ MAX_CIPHERTEXT_SIZE
Definition: tls_magic.h:35

◆ Stream() [2/4]

template<class StreamLayer , class ChannelT = Channel>
template<typename Arg >
Botan::TLS::Stream< StreamLayer, ChannelT >::Stream ( Arg &&  arg,
std::shared_ptr< 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 82 of file asio_stream.h.

83 : m_context(context)
84 , m_nextLayer(std::forward<Arg>(arg))
85 , m_core(std::make_shared<StreamCore>(context))
88 {}

◆ ~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 CompletionToken >
auto Botan::TLS::Stream< StreamLayer, ChannelT >::async_handshake ( Botan::TLS::Connection_Side  side,
CompletionToken &&  completion_token 
)
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.
completion_tokenThe completion handler to be called when the handshake operation completes. The completion signature of the handler must be: void(boost::system::error_code).

Definition at line 262 of file asio_stream.h.

263 {
264 return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code)>(
265 [this](auto&& completion_handler, TLS::Connection_Side connection_side)
266 {
267 using completion_handler_t = std::decay_t<decltype(completion_handler)>;
268
269 BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(completion_handler_t, completion_handler) type_check;
270
271 boost::system::error_code ec;
272 setup_native_handle(connection_side, ec);
273
274 detail::AsyncHandshakeOperation<completion_handler_t, Stream> op
275 {
276 std::forward<completion_handler_t>(completion_handler),
277 *this,
278 ec
279 };
280 }, completion_token, side);
281 }
void setup_native_handle(Connection_Side side, boost::system::error_code &ec)
Create the native handle.
Definition: asio_stream.h:707
BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(BufferedHandshakeHandler, handler) type_check

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

◆ async_read_some()

template<class StreamLayer , class ChannelT = Channel>
template<typename MutableBufferSequence , typename CompletionToken >
auto Botan::TLS::Stream< StreamLayer, ChannelT >::async_read_some ( const MutableBufferSequence &  buffers,
CompletionToken &&  completion_token 
)
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.
completion_tokenThe completion handler to be called when the read operation completes. The completion signature of the handler must be: void(boost::system::error_code, std::size_t).

Definition at line 556 of file asio_stream.h.

557 {
558 return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code, std::size_t)>(
559 [this](auto&& completion_handler, const auto& bufs)
560 {
561 using completion_handler_t = std::decay_t<decltype(completion_handler)>;
562
563 BOOST_ASIO_READ_HANDLER_CHECK(completion_handler_t, completion_handler) type_check;
564
565 detail::AsyncReadOperation<completion_handler_t, Stream, MutableBufferSequence> op
566 {
567 std::forward<completion_handler_t>(completion_handler),
568 *this,
569 bufs
570 };
571 }, completion_token, buffers);
572 }
const ConstBufferSequence & buffers
Definition: asio_stream.h:287

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

◆ async_shutdown()

template<class StreamLayer , class ChannelT = Channel>
template<typename CompletionToken >
auto Botan::TLS::Stream< StreamLayer, ChannelT >::async_shutdown ( CompletionToken &&  completion_token)
inline

Asynchronously shut down SSL on the stream.

This function call always returns immediately.

Note that this can be used in reaction of a received shutdown alert from the peer.

Parameters
completion_tokenThe completion handler to be called when the shutdown operation completes. The completion signature of the handler must be: void(boost::system::error_code).

Definition at line 382 of file asio_stream.h.

383 {
384 return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code)>(
385 [this](auto&& completion_handler)
386 {
387 using completion_handler_t = std::decay_t<decltype(completion_handler)>;
388
389 BOOST_ASIO_SHUTDOWN_HANDLER_CHECK(completion_handler_t, completion_handler) type_check;
390
391 boost::system::error_code ec;
392 try_with_error_code([&] { native_handle()->close(); }, ec);
393
394 using write_handler_t = Wrapper<completion_handler_t, typename Stream::executor_type>;
395
396 TLS::detail::AsyncWriteOperation<write_handler_t, Stream> op
397 {
398 write_handler_t{std::forward<completion_handler_t>(completion_handler), get_executor()},
399 *this,
400 boost::asio::buffer_size(send_buffer()),
401 ec
402 };
403 }, completion_token);
404 }
native_handle_type native_handle()
Definition: asio_stream.h:128
void try_with_error_code(Fun f, boost::system::error_code &ec)
Catch exceptions and set an error_code.
Definition: asio_stream.h:808
boost::asio::const_buffer send_buffer() const
Definition: asio_stream.h:674
executor_type get_executor() noexcept
Definition: asio_stream.h:125

References Botan::TLS::Stream< StreamLayer, ChannelT >::get_executor(), Botan::TLS::Stream< StreamLayer, ChannelT >::native_handle(), Botan::TLS::Stream< StreamLayer, ChannelT >::send_buffer(), and Botan::TLS::Stream< StreamLayer, ChannelT >::try_with_error_code().

◆ async_write_some()

template<class StreamLayer , class ChannelT = Channel>
template<typename ConstBufferSequence , typename CompletionToken >
auto Botan::TLS::Stream< StreamLayer, ChannelT >::async_write_some ( const ConstBufferSequence &  buffers,
CompletionToken &&  completion_token 
)
inline

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

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

Definition at line 517 of file asio_stream.h.

518 {
519 return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code, std::size_t)>(
520 [this](auto&& completion_handler, const auto& bufs)
521 {
522 using completion_handler_t = std::decay_t<decltype(completion_handler)>;
523
524 BOOST_ASIO_WRITE_HANDLER_CHECK(completion_handler_t, completion_handler) type_check;
525
526 boost::system::error_code ec;
527 tls_encrypt(bufs, ec);
528
529 if(ec)
530 {
531 // we cannot be sure how many bytes were committed here so clear the send_buffer and let the
532 // AsyncWriteOperation call the handler with the error_code set
533 consume_send_buffer(m_core->send_buffer.size());
534 }
535
536 detail::AsyncWriteOperation<completion_handler_t, Stream> op
537 {
538 std::forward<completion_handler_t>(completion_handler),
539 *this,
540 ec ? 0 : boost::asio::buffer_size(bufs),
541 ec
542 };
543 }, completion_token, buffers);
544 }
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:696
void tls_encrypt(const ConstBufferSequence &buffers, boost::system::error_code &ec)
Pass plaintext data to the native handle for processing.
Definition: asio_stream.h:773

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

◆ 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 681 of file asio_stream.h.

682 {
683 // Note: It would be nice to avoid this buffer copy. This could be achieved by equipping the StreamCore with
684 // the user's desired target buffer once a read is started, and reading directly into that buffer in tls_record
685 // received. However, we need to deal with the case that the receive buffer provided by the caller is smaller
686 // than the decrypted record, so this optimization might not be worth the additional complexity.
687 const auto copiedBytes = boost::asio::buffer_copy(buffers, m_core->receive_buffer.data());
688 m_core->receive_buffer.consume(copiedBytes);
689 return copiedBytes;
690 }

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

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

◆ get_executor()

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

◆ 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

Definition at line 215 of file asio_stream.h.

216 {
217 boost::system::error_code ec;
218 handshake(side, ec);
219 boost::asio::detail::throw_error(ec, "handshake");
220 }
void handshake(Connection_Side side)
Performs SSL handshaking.
Definition: asio_stream.h:215

References Botan::TLS::Stream< StreamLayer, ChannelT >::handshake().

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

◆ 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 230 of file asio_stream.h.

231 {
232 setup_native_handle(side, ec);
233
234 if(side == Connection_Side::Client)
235 {
236 // send client hello, which was written to the send buffer on client instantiation
238 }
239
240 while(!native_handle()->is_active() && !ec)
241 {
242 boost::asio::const_buffer read_buffer{input_buffer().data(), m_nextLayer.read_some(input_buffer(), ec)};
243 if(ec)
244 { return; }
245
246 process_encrypted_data(read_buffer, ec);
247
249 }
250 }
const boost::asio::mutable_buffer & input_buffer()
Definition: asio_stream.h:673
void process_encrypted_data(const boost::asio::const_buffer &read_buffer, boost::system::error_code &ec)
Pass encrypted data to the native handle for processing.
Definition: asio_stream.h:798
size_t send_pending_encrypted_data(boost::system::error_code &ec)
Synchronously write encrypted data from the send buffer to the next layer.
Definition: asio_stream.h:750

References Botan::TLS::Client, 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 >::process_encrypted_data(), Botan::TLS::Stream< StreamLayer, ChannelT >::send_pending_encrypted_data(), and Botan::TLS::Stream< StreamLayer, ChannelT >::setup_native_handle().

◆ has_data_to_send()

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

Check if encrypted data is available in the send buffer.

Definition at line 693 of file asio_stream.h.

693{ return m_core->send_buffer.size() > 0; }

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

Referenced by Botan::TLS::detail::AsyncWriteOperation< Handler, Stream, Allocator >::operator()(), and Botan::TLS::detail::AsyncHandshakeOperation< Handler, Stream, Allocator >::operator()().

◆ 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 677 of file asio_stream.h.

677{ return m_core->receive_buffer.size() > 0; }

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

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

◆ 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 120 of file asio_stream.h.

120{ return m_nextLayer.lowest_layer(); }

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

◆ 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 121 of file asio_stream.h.

121{ return m_nextLayer.lowest_layer(); }

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

◆ 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>
next_layer_type & Botan::TLS::Stream< StreamLayer, ChannelT >::next_layer ( )
inline

Definition at line 105 of file asio_stream.h.

105{ return m_nextLayer; }

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

◆ next_layer() [2/2]

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

◆ 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= ( const Stream< StreamLayer, ChannelT > &  other)
delete

◆ operator=() [2/2]

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

◆ process_encrypted_data()

template<class StreamLayer , class ChannelT = Channel>
void Botan::TLS::Stream< StreamLayer, ChannelT >::process_encrypted_data ( const boost::asio::const_buffer &  read_buffer,
boost::system::error_code &  ec 
)
inlineprotected

Pass encrypted data to the native handle for processing.

If an exception occurs while processing the data, an error code will be set.

Parameters
read_bufferInput buffer containing the encrypted data.
ecSet to indicate what error occurred, if any.

Definition at line 798 of file asio_stream.h.

799 {
801 {
802 native_handle()->received_data({static_cast<const uint8_t*>(read_buffer.data()), read_buffer.size()});
803 }, ec);
804 }

References Botan::TLS::Stream< StreamLayer, ChannelT >::native_handle(), and Botan::TLS::Stream< StreamLayer, ChannelT >::try_with_error_code().

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

◆ 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)
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 462 of file asio_stream.h.

463 {
464 boost::system::error_code ec;
465 auto const n = read_some(buffers, ec);
466 boost::asio::detail::throw_error(ec, "read_some");
467 return n;
468 }
std::size_t read_some(const MutableBufferSequence &buffers, boost::system::error_code &ec)
Read some data from the stream.
Definition: asio_stream.h:422

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

◆ 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,
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 occurred, if any. Specifically, StreamTruncated will be set if the peer has closed the connection but did not properly shut down the SSL connection.
Returns
The number of bytes read. Returns 0 if an error occurred.

Definition at line 422 of file asio_stream.h.

424 {
426 { return copy_received_data(buffers); }
427
428 boost::asio::const_buffer read_buffer{input_buffer().data(), m_nextLayer.read_some(input_buffer(), ec)};
429 if(ec)
430 { return 0; }
431
432 process_encrypted_data(read_buffer, ec);
433
434 if(ec) // something went wrong in process_encrypted_data()
435 { return 0; }
436
438 {
439 // we just received a 'close_notify' from the peer and don't expect any more data
440 ec = boost::asio::error::eof;
441 }
442 else if(ec == boost::asio::error::eof)
443 {
444 // we did not expect this disconnection from the peer
446 }
447
448 return !ec ? copy_received_data(buffers) : 0;
449 }
std::size_t copy_received_data(MutableBufferSequence buffers)
Copy decrypted data into the user-provided buffer.
Definition: asio_stream.h:681
bool has_received_data() const
Check if decrypted data is available in the receive buffer.
Definition: asio_stream.h:677
bool shutdown_received() const
Indicates whether a close_notify alert has been received from the peer.
Definition: asio_stream.h:580
@ StreamTruncated
Definition: asio_error.h:35

References Botan::TLS::Stream< StreamLayer, ChannelT >::buffers, Botan::TLS::Stream< StreamLayer, ChannelT >::copy_received_data(), 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 >::process_encrypted_data(), Botan::TLS::Stream< StreamLayer, ChannelT >::shutdown_received(), and Botan::TLS::StreamTruncated.

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

◆ 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

Synchronously write encrypted data from the send buffer to the next layer.

If this function is called with an error code other than 'Success', it will do nothing and return 0.

Parameters
ecSet to indicate what error occurred, if any. Specifically, StreamTruncated will be set if the peer has closed the connection but did not properly shut down the SSL connection.
Returns
The number of bytes written.

Definition at line 750 of file asio_stream.h.

751 {
752 if(ec)
753 { return 0; }
754
755 auto writtenBytes = boost::asio::write(m_nextLayer, send_buffer(), ec);
756 consume_send_buffer(writtenBytes);
757
758 if(ec == boost::asio::error::eof && !shutdown_received())
759 {
760 // transport layer was closed by peer without receiving 'close_notify'
762 }
763
764 return writtenBytes;
765 }

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

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

◆ 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 148 of file asio_stream.h.

149 {
150 m_context->set_verify_callback(std::move(callback));
151 }

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

◆ 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 159 of file asio_stream.h.

160 {
161 BOTAN_UNUSED(ec);
162 m_context->set_verify_callback(std::move(callback));
163 }
#define BOTAN_UNUSED(...)
Definition: assert.h:141

References BOTAN_UNUSED, and Botan::TLS::Stream< StreamLayer, ChannelT >::m_context.

◆ 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 166 of file asio_stream.h.

167 {
168 BOTAN_UNUSED(depth);
169 throw Not_Implemented("set_verify_depth is not implemented");
170 }
throw Not_Implemented("buffered async handshake is not implemented")

References BOTAN_UNUSED.

◆ 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 177 of file asio_stream.h.

178 {
179 BOTAN_UNUSED(depth);
181 }

References BOTAN_UNUSED, and Botan::NotImplemented.

◆ 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 185 of file asio_stream.h.

186 {
187 BOTAN_UNUSED(v);
188 throw Not_Implemented("set_verify_mode is not implemented");
189 }

References BOTAN_UNUSED.

◆ 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 197 of file asio_stream.h.

198 {
199 BOTAN_UNUSED(v);
201 }

References BOTAN_UNUSED, and Botan::NotImplemented.

◆ setup_native_handle()

template<class StreamLayer , class ChannelT = Channel>
void 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 TLS::Client or a TLS::Server.

Parameters
sideThe desired connection side (client or server)
ecSet to indicate what error occurred, if any.

Definition at line 707 of file asio_stream.h.

708 {
709 BOTAN_UNUSED(side); // workaround: GCC 9 produces a warning claiming side is unused
710
711 // Do not attempt to instantiate the native_handle when a custom (mocked) channel type template parameter has
712 // been specified. This allows mocking the native_handle in test code.
713 if constexpr(std::is_same<ChannelT, Channel>::value)
714 {
716 {
717 if(side == Connection_Side::Client)
718 {
719 m_native_handle = std::unique_ptr<Client>(
720 new Client(m_core,
721 m_context->m_session_manager,
722 m_context->m_credentials_manager,
723 m_context->m_policy,
724 m_context->m_rng,
725 m_context->m_server_info,
726 m_context->m_policy->latest_supported_version(false /* no DTLS */)));
727 }
728 else
729 {
730 m_native_handle = std::unique_ptr<Server>(
731 new Server(m_core,
732 m_context->m_session_manager,
733 m_context->m_credentials_manager,
734 m_context->m_policy,
735 m_context->m_rng,
736 false /* no DTLS */));
737 }
738 }, ec);
739 }
740 }

References BOTAN_UNUSED, Botan::TLS::Client, Botan::TLS::Stream< StreamLayer, ChannelT >::m_context, Botan::TLS::Stream< StreamLayer, ChannelT >::m_core, Botan::TLS::Stream< StreamLayer, ChannelT >::m_native_handle, and Botan::TLS::Stream< StreamLayer, ChannelT >::try_with_error_code().

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

◆ shutdown() [1/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.

Note that this can be used in reaction of a received shutdown alert from the peer.

Exceptions
boost::system::system_errorif error occured

Definition at line 329 of file asio_stream.h.

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

References Botan::TLS::Stream< StreamLayer, ChannelT >::shutdown().

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

◆ shutdown() [2/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.

Note that this can be used in reaction of a received shutdown alert from the peer.

Parameters
ecSet to indicate what error occured, if any.

Definition at line 309 of file asio_stream.h.

310 {
312 {
313 native_handle()->close();
314 }, ec);
315
317 }

References Botan::TLS::Stream< StreamLayer, ChannelT >::native_handle(), Botan::TLS::Stream< StreamLayer, ChannelT >::send_pending_encrypted_data(), and Botan::TLS::Stream< StreamLayer, ChannelT >::try_with_error_code().

◆ shutdown_received()

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

Indicates whether a close_notify alert has been received from the peer.

Note that we cannot m_core.is_closed_for_reading() because this wants to explicitly check that the peer sent close_notify.

Definition at line 580 of file asio_stream.h.

581 {
582 return m_core->shutdown_received;
583 }

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

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

◆ 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

Pass plaintext data to the native handle for processing.

The native handle will then create TLS records and hand them back to the Stream via the tls_emit_data callback.

Definition at line 773 of file asio_stream.h.

774 {
775 // NOTE: This is not asynchronous: it encrypts the data synchronously.
776 // The data encrypted by native_handle()->send() is synchronously stored in the send_buffer of m_core,
777 // but is not actually written to the wire, yet.
778 for(auto it = boost::asio::buffer_sequence_begin(buffers);
779 !ec && it != boost::asio::buffer_sequence_end(buffers);
780 it++)
781 {
782 const boost::asio::const_buffer buffer = *it;
784 {
785 native_handle()->send({static_cast<const uint8_t*>(buffer.data()), buffer.size()});
786 }, ec);
787 }
788 }

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

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

◆ try_with_error_code()

template<class StreamLayer , class ChannelT = Channel>
template<typename Fun >
void Botan::TLS::Stream< StreamLayer, ChannelT >::try_with_error_code ( Fun  f,
boost::system::error_code &  ec 
)
inlineprotected

Catch exceptions and set an error_code.

Definition at line 808 of file asio_stream.h.

809 {
810 try
811 {
812 f();
813 }
814 catch(const TLS_Exception& e)
815 {
816 ec = e.type();
817 }
818 catch(const Exception& e)
819 {
820 ec = e.error_type();
821 }
822 catch(const std::exception&)
823 {
825 }
826 }

References Botan::TLS::TLS_Exception::error_type(), Botan::TLS::TLS_Exception::type(), and Botan::Unknown.

Referenced by Botan::TLS::Stream< StreamLayer, ChannelT >::async_shutdown(), Botan::TLS::Stream< StreamLayer, ChannelT >::process_encrypted_data(), Botan::TLS::Stream< StreamLayer, ChannelT >::setup_native_handle(), Botan::TLS::Stream< StreamLayer, ChannelT >::shutdown(), and Botan::TLS::Stream< StreamLayer, ChannelT >::tls_encrypt().

◆ 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)
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 500 of file asio_stream.h.

501 {
502 boost::system::error_code ec;
503 auto const n = write_some(buffers, ec);
504 boost::asio::detail::throw_error(ec, "write_some");
505 return n;
506 }
std::size_t write_some(const ConstBufferSequence &buffers, boost::system::error_code &ec)
Write some data to the stream.
Definition: asio_stream.h:481

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

◆ 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,
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 481 of file asio_stream.h.

483 {
484 tls_encrypt(buffers, ec);
486 return !ec ? boost::asio::buffer_size(buffers) : 0;
487 }

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().

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 588 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 586 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 587 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
Initial value:
{
const ConstBufferSequence BufferedHandshakeHandler && handler
Definition: asio_stream.h:289

Definition at line 288 of file asio_stream.h.

◆ m_context

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

◆ m_core

template<class StreamLayer , class ChannelT = Channel>
std::shared_ptr<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 835 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

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