Botan  2.1.0
Crypto and TLS for C++11
tls_blocking.h
Go to the documentation of this file.
1 /*
2 * TLS Blocking API
3 * (C) 2013 Jack Lloyd
4 * 2016 Matthias Gierlings
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #ifndef BOTAN_TLS_BLOCKING_CHANNELS_H__
10 #define BOTAN_TLS_BLOCKING_CHANNELS_H__
11 
12 #include <botan/tls_client.h>
13 #include <botan/tls_server.h>
14 #include <deque>
15 
16 namespace Botan {
17 
18 //template<typename T> using secure_deque = std::vector<T, secure_allocator<T>>;
19 
20 namespace TLS {
21 
22 /**
23 * Blocking TLS Client
24 * Can be used directly, or subclass to get handshake and alert notifications
25 */
26 class BOTAN_DLL Blocking_Client
27  {
28  public:
29  /*
30  * These functions are expected to block until completing entirely, or
31  * fail by throwing an exception.
32  */
33  typedef std::function<size_t (uint8_t[], size_t)> read_fn;
34  typedef std::function<void (const uint8_t[], size_t)> write_fn;
35 
36  BOTAN_DEPRECATED("Use the regular TLS::Client interface")
37  Blocking_Client(read_fn reader,
38  write_fn writer,
39  Session_Manager& session_manager,
40  Credentials_Manager& creds,
41  const Policy& policy,
43  const Server_Information& server_info = Server_Information(),
44  const Protocol_Version& offer_version = Protocol_Version::latest_tls_version(),
45  const std::vector<std::string>& next_protos = {});
46 
47  /**
48  * Completes full handshake then returns
49  */
50  void do_handshake();
51 
52  /**
53  * Number of bytes pending read in the plaintext buffer (bytes
54  * readable without blocking)
55  */
56  size_t pending() const { return m_plaintext.size(); }
57 
58  /**
59  * Blocking read, will return at least 1 byte (eventually) or else 0 if the connection
60  * is closed.
61  */
62  size_t read(uint8_t buf[], size_t buf_len);
63 
64  void write(const uint8_t buf[], size_t buf_len) { m_channel.send(buf, buf_len); }
65 
66  const TLS::Channel& underlying_channel() const { return m_channel; }
67  TLS::Channel& underlying_channel() { return m_channel; }
68 
69  void close() { m_channel.close(); }
70 
71  bool is_closed() const { return m_channel.is_closed(); }
72 
73  std::vector<X509_Certificate> peer_cert_chain() const
74  { return m_channel.peer_cert_chain(); }
75 
76  virtual ~Blocking_Client() = default;
77 
78  protected:
79  /**
80  * Application can override to get the handshake complete notification
81  */
82  virtual bool handshake_complete(const Session&) { return true; }
83 
84  /**
85  * Application can override to get notification of alerts
86  */
87  virtual void alert_notification(const Alert&) {}
88 
89  private:
90 
91  bool handshake_cb(const Session&);
92 
93  void data_cb(const uint8_t data[], size_t data_len);
94 
95  void alert_cb(const Alert& alert);
96 
97  read_fn m_read;
98  std::unique_ptr<Compat_Callbacks> m_callbacks;
99  TLS::Client m_channel;
100  secure_vector<uint8_t> m_plaintext;
101  };
102 
103 }
104 
105 }
106 
107 #endif
Definition: bigint.h:619
std::function< size_t(uint8_t[], size_t)> read_fn
Definition: tls_blocking.h:33
virtual void alert_notification(const Alert &)
Definition: tls_blocking.h:87
void write(const uint8_t buf[], size_t buf_len)
Definition: tls_blocking.h:64
TLS::Channel & underlying_channel()
Definition: tls_blocking.h:67
std::function< void(const uint8_t[], size_t)> write_fn
Definition: tls_blocking.h:34
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
class BOTAN_DLL BOTAN_DEPRECATED("LibraryInitializer is no longer required") LibraryInitializer
Definition: init.h:22
const TLS::Channel & underlying_channel() const
Definition: tls_blocking.h:66
Definition: alg_id.cpp:13
virtual bool handshake_complete(const Session &)
Definition: tls_blocking.h:82
std::vector< X509_Certificate > peer_cert_chain() const
Definition: tls_blocking.h:73