Botan  2.15.0
Crypto and TLS for C++11
Public Types | Public Member Functions | Protected Member Functions | List of all members
Botan::TLS::Blocking_Client Class Reference

#include <tls_blocking.h>

Public Types

typedef std::function< size_t(uint8_t[], size_t)> read_fn
 
typedef std::function< void(const uint8_t[], size_t)> write_fn
 

Public Member Functions

 Blocking_Client (read_fn reader, write_fn writer, Session_Manager &session_manager, Credentials_Manager &creds, const Policy &policy, RandomNumberGenerator &rng, const Server_Information &server_info=Server_Information(), const Protocol_Version &offer_version=Protocol_Version::latest_tls_version(), const std::vector< std::string > &next_protos={})
 
void close ()
 
void do_handshake ()
 
bool is_closed () const
 
std::vector< X509_Certificatepeer_cert_chain () const
 
size_t pending () const
 
size_t read (uint8_t buf[], size_t buf_len)
 
const TLS::Channelunderlying_channel () const
 
TLS::Channelunderlying_channel ()
 
void write (const uint8_t buf[], size_t buf_len)
 
virtual ~Blocking_Client ()=default
 

Protected Member Functions

virtual void alert_notification (const Alert &)
 
virtual bool handshake_complete (const Session &)
 

Detailed Description

Blocking TLS Client Can be used directly, or subclass to get handshake and alert notifications

Definition at line 22 of file tls_blocking.h.

Member Typedef Documentation

◆ read_fn

typedef std::function<size_t (uint8_t[], size_t)> Botan::TLS::Blocking_Client::read_fn

Definition at line 29 of file tls_blocking.h.

◆ write_fn

typedef std::function<void (const uint8_t[], size_t)> Botan::TLS::Blocking_Client::write_fn

Definition at line 30 of file tls_blocking.h.

Constructor & Destructor Documentation

◆ Blocking_Client()

Botan::TLS::Blocking_Client::Blocking_Client ( read_fn  reader,
write_fn  writer,
Session_Manager session_manager,
Credentials_Manager creds,
const Policy policy,
RandomNumberGenerator rng,
const Server_Information server_info = Server_Information(),
const Protocol_Version offer_version = Protocol_Version::latest_tls_version(),
const std::vector< std::string > &  next_protos = {} 
)

Definition at line 15 of file tls_blocking.cpp.

23  :
24  m_read(reader),
25  m_callbacks(new TLS::Compat_Callbacks(
26  /*
27  we are ok using deprecated features here because the whole Blocking_Client class
28  is also deprecated, so just silence the warning.
29  */
31  writer,
32  std::bind(&Blocking_Client::data_cb, this, std::placeholders::_1, std::placeholders::_2),
33  std::function<void (Alert)>(std::bind(&Blocking_Client::alert_cb, this, std::placeholders::_1)),
34  std::bind(&Blocking_Client::handshake_cb, this, std::placeholders::_1)
35  )),
36  m_channel(*m_callbacks.get(),
37  session_manager,
38  creds,
39  policy,
40  rng,
41  server_info,
42  offer_version,
43  next)
44  {
45  }

◆ ~Blocking_Client()

virtual Botan::TLS::Blocking_Client::~Blocking_Client ( )
virtualdefault

Member Function Documentation

◆ alert_notification()

virtual void Botan::TLS::Blocking_Client::alert_notification ( const Alert )
inlineprotectedvirtual

Application can override to get notification of alerts

Definition at line 83 of file tls_blocking.h.

83 {}

◆ close()

void Botan::TLS::Blocking_Client::close ( )
inline

Definition at line 65 of file tls_blocking.h.

65 { m_channel.close(); }

◆ do_handshake()

void Botan::TLS::Blocking_Client::do_handshake ( )

Completes full handshake then returns

Definition at line 62 of file tls_blocking.cpp.

References Botan::TLS::Channel::is_active(), Botan::TLS::Channel::is_closed(), and Botan::TLS::Channel::received_data().

63  {
64  std::vector<uint8_t> readbuf(4096);
65 
66  while(!m_channel.is_closed() && !m_channel.is_active())
67  {
68  const size_t from_socket = m_read(readbuf.data(), readbuf.size());
69  m_channel.received_data(readbuf.data(), from_socket);
70  }
71  }
bool is_closed() const
size_t received_data(const uint8_t buf[], size_t buf_size)
bool is_active() const

◆ handshake_complete()

virtual bool Botan::TLS::Blocking_Client::handshake_complete ( const Session )
inlineprotectedvirtual

Application can override to get the handshake complete notification

Definition at line 78 of file tls_blocking.h.

78 { return true; }

◆ is_closed()

bool Botan::TLS::Blocking_Client::is_closed ( ) const
inline

Definition at line 67 of file tls_blocking.h.

67 { return m_channel.is_closed(); }
bool is_closed() const

◆ peer_cert_chain()

std::vector<X509_Certificate> Botan::TLS::Blocking_Client::peer_cert_chain ( ) const
inline

Definition at line 69 of file tls_blocking.h.

70  { return m_channel.peer_cert_chain(); }
std::vector< X509_Certificate > peer_cert_chain() const

◆ pending()

size_t Botan::TLS::Blocking_Client::pending ( ) const
inline

Number of bytes pending read in the plaintext buffer (bytes readable without blocking)

Definition at line 52 of file tls_blocking.h.

52 { return m_plaintext.size(); }

◆ read()

size_t Botan::TLS::Blocking_Client::read ( uint8_t  buf[],
size_t  buf_len 
)

Blocking read, will return at least 1 byte (eventually) or else 0 if the connection is closed.

Definition at line 73 of file tls_blocking.cpp.

References BOTAN_ASSERT_IMPLICATION, Botan::TLS::Channel::is_closed(), and Botan::TLS::Channel::received_data().

74  {
75  std::vector<uint8_t> readbuf(4096);
76 
77  while(m_plaintext.empty() && !m_channel.is_closed())
78  {
79  const size_t from_socket = m_read(readbuf.data(), readbuf.size());
80  m_channel.received_data(readbuf.data(), from_socket);
81  }
82 
83  const size_t returned = std::min(buf_len, m_plaintext.size());
84 
85  for(size_t i = 0; i != returned; ++i)
86  buf[i] = m_plaintext[i];
87  m_plaintext.erase(m_plaintext.begin(), m_plaintext.begin() + returned);
88 
89  BOTAN_ASSERT_IMPLICATION(returned == 0, m_channel.is_closed(),
90  "Only return zero if channel is closed");
91 
92  return returned;
93  }
bool is_closed() const
size_t received_data(const uint8_t buf[], size_t buf_size)
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
Definition: assert.h:94

◆ underlying_channel() [1/2]

const TLS::Channel& Botan::TLS::Blocking_Client::underlying_channel ( ) const
inline

Definition at line 62 of file tls_blocking.h.

62 { return m_channel; }

◆ underlying_channel() [2/2]

TLS::Channel& Botan::TLS::Blocking_Client::underlying_channel ( )
inline

Definition at line 63 of file tls_blocking.h.

63 { return m_channel; }

◆ write()

void Botan::TLS::Blocking_Client::write ( const uint8_t  buf[],
size_t  buf_len 
)
inline

Definition at line 60 of file tls_blocking.h.

60 { m_channel.send(buf, buf_len); }
void send(const uint8_t buf[], size_t buf_size)

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