Botan  2.7.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 17 of file tls_blocking.cpp.

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

◆ ~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 64 of file tls_blocking.cpp.

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

65  {
66  std::vector<uint8_t> readbuf(4096);
67 
68  while(!m_channel.is_closed() && !m_channel.is_active())
69  {
70  const size_t from_socket = m_read(readbuf.data(), readbuf.size());
71  m_channel.received_data(readbuf.data(), from_socket);
72  }
73  }
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 75 of file tls_blocking.cpp.

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

76  {
77  std::vector<uint8_t> readbuf(4096);
78 
79  while(m_plaintext.empty() && !m_channel.is_closed())
80  {
81  const size_t from_socket = m_read(readbuf.data(), readbuf.size());
82  m_channel.received_data(readbuf.data(), from_socket);
83  }
84 
85  const size_t returned = std::min(buf_len, m_plaintext.size());
86 
87  for(size_t i = 0; i != returned; ++i)
88  buf[i] = m_plaintext[i];
89  m_plaintext.erase(m_plaintext.begin(), m_plaintext.begin() + returned);
90 
91  BOTAN_ASSERT_IMPLICATION(returned == 0, m_channel.is_closed(),
92  "Only return zero if channel is closed");
93 
94  return returned;
95  }
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:82

◆ 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: