Botan  2.1.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 26 of file tls_blocking.h.

Member Typedef Documentation

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

Definition at line 33 of file tls_blocking.h.

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

Definition at line 34 of file tls_blocking.h.

Constructor & Destructor Documentation

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  writer,
29  std::bind(&Blocking_Client::data_cb, this, _1, _2),
30  std::function<void (Alert)>(std::bind(&Blocking_Client::alert_cb, this, _1)),
31  std::bind(&Blocking_Client::handshake_cb, this, _1)
32  )),
33  m_channel(*m_callbacks.get(),
34  session_manager,
35  creds,
36  policy,
37  rng,
38  server_info,
39  offer_version,
40  next)
41  {
42  }
virtual Botan::TLS::Blocking_Client::~Blocking_Client ( )
virtualdefault

Member Function Documentation

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

Application can override to get notification of alerts

Definition at line 87 of file tls_blocking.h.

87 {}
void Botan::TLS::Blocking_Client::close ( )
inline

Definition at line 69 of file tls_blocking.h.

69 { m_channel.close(); }
void Botan::TLS::Blocking_Client::do_handshake ( )

Completes full handshake then returns

Definition at line 59 of file tls_blocking.cpp.

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

60  {
61  std::vector<uint8_t> readbuf(4096);
62 
63  while(!m_channel.is_closed() && !m_channel.is_active())
64  {
65  const size_t from_socket = m_read(readbuf.data(), readbuf.size());
66  m_channel.received_data(readbuf.data(), from_socket);
67  }
68  }
bool is_closed() const
size_t received_data(const uint8_t buf[], size_t buf_size)
bool is_active() const
virtual bool Botan::TLS::Blocking_Client::handshake_complete ( const Session )
inlineprotectedvirtual

Application can override to get the handshake complete notification

Definition at line 82 of file tls_blocking.h.

82 { return true; }
bool Botan::TLS::Blocking_Client::is_closed ( ) const
inline

Definition at line 71 of file tls_blocking.h.

71 { return m_channel.is_closed(); }
bool is_closed() const
std::vector<X509_Certificate> Botan::TLS::Blocking_Client::peer_cert_chain ( ) const
inline

Definition at line 73 of file tls_blocking.h.

74  { return m_channel.peer_cert_chain(); }
std::vector< X509_Certificate > peer_cert_chain() const
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 56 of file tls_blocking.h.

56 { return m_plaintext.size(); }
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 70 of file tls_blocking.cpp.

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

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

Definition at line 66 of file tls_blocking.h.

66 { return m_channel; }
TLS::Channel& Botan::TLS::Blocking_Client::underlying_channel ( )
inline

Definition at line 67 of file tls_blocking.h.

67 { return m_channel; }
void Botan::TLS::Blocking_Client::write ( const uint8_t  buf[],
size_t  buf_len 
)
inline

Definition at line 64 of file tls_blocking.h.

64 { 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: