Botan  2.6.0
Crypto and TLS for C++11
tls_channel.h
Go to the documentation of this file.
1 /*
2 * TLS Channel
3 * (C) 2011,2012,2014,2015 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_CHANNEL_H_
10 #define BOTAN_TLS_CHANNEL_H_
11 
12 #include <botan/tls_session.h>
13 #include <botan/tls_alert.h>
14 #include <botan/tls_session_manager.h>
15 #include <botan/tls_callbacks.h>
16 #include <botan/x509cert.h>
17 #include <functional>
18 #include <vector>
19 #include <string>
20 #include <map>
21 
22 namespace Botan {
23 
24 namespace TLS {
25 
26 class Connection_Cipher_State;
27 class Connection_Sequence_Numbers;
28 class Handshake_State;
29 class Handshake_Message;
30 class Client_Hello;
31 class Server_Hello;
32 class Policy;
33 
34 /**
35 * Generic interface for TLS endpoint
36 */
38  {
39  public:
40  typedef std::function<void (const uint8_t[], size_t)> output_fn;
41  typedef std::function<void (const uint8_t[], size_t)> data_cb;
42  typedef std::function<void (Alert, const uint8_t[], size_t)> alert_cb;
43  typedef std::function<bool (const Session&)> handshake_cb;
44  typedef std::function<void (const Handshake_Message&)> handshake_msg_cb;
45  static size_t IO_BUF_DEFAULT_SIZE;
46 
47  /**
48  * Set up a new TLS session
49  *
50  * @param callbacks contains a set of callback function references
51  * required by the TLS endpoint.
52  *
53  * @param session_manager manages session state
54  *
55  * @param rng a random number generator
56  *
57  * @param policy specifies other connection policy information
58  *
59  * @param is_datagram whether this is a DTLS session
60  *
61  * @param io_buf_sz This many bytes of memory will
62  * be preallocated for the read and write buffers. Smaller
63  * values just mean reallocations and copies are more likely.
64  */
65  Channel(Callbacks& callbacks,
66  Session_Manager& session_manager,
68  const Policy& policy,
69  bool is_datagram,
70  size_t io_buf_sz = IO_BUF_DEFAULT_SIZE);
71 
72  /**
73  * DEPRECATED. This constructor is only provided for backward
74  * compatibility and should not be used in new implementations.
75  * (Not marked deprecated since it is only called internally, by
76  * other deprecated constructors)
77  */
78  Channel(output_fn out,
79  data_cb app_data_cb,
81  handshake_cb hs_cb,
82  handshake_msg_cb hs_msg_cb,
83  Session_Manager& session_manager,
85  const Policy& policy,
86  bool is_datagram,
87  size_t io_buf_sz = IO_BUF_DEFAULT_SIZE);
88 
89  Channel(const Channel&) = delete;
90 
91  Channel& operator=(const Channel&) = delete;
92 
93  virtual ~Channel();
94 
95  /**
96  * Inject TLS traffic received from counterparty
97  * @return a hint as the how many more bytes we need to process the
98  * current record (this may be 0 if on a record boundary)
99  */
100  size_t received_data(const uint8_t buf[], size_t buf_size);
101 
102  /**
103  * Inject TLS traffic received from counterparty
104  * @return a hint as the how many more bytes we need to process the
105  * current record (this may be 0 if on a record boundary)
106  */
107  size_t received_data(const std::vector<uint8_t>& buf);
108 
109  /**
110  * Inject plaintext intended for counterparty
111  * Throws an exception if is_active() is false
112  */
113  void send(const uint8_t buf[], size_t buf_size);
114 
115  /**
116  * Inject plaintext intended for counterparty
117  * Throws an exception if is_active() is false
118  */
119  void send(const std::string& val);
120 
121  /**
122  * Inject plaintext intended for counterparty
123  * Throws an exception if is_active() is false
124  */
125  template<typename Alloc>
126  void send(const std::vector<unsigned char, Alloc>& val)
127  {
128  send(val.data(), val.size());
129  }
130 
131  /**
132  * Send a TLS alert message. If the alert is fatal, the internal
133  * state (keys, etc) will be reset.
134  * @param alert the Alert to send
135  */
136  void send_alert(const Alert& alert);
137 
138  /**
139  * Send a warning alert
140  */
141  void send_warning_alert(Alert::Type type) { send_alert(Alert(type, false)); }
142 
143  /**
144  * Send a fatal alert
145  */
146  void send_fatal_alert(Alert::Type type) { send_alert(Alert(type, true)); }
147 
148  /**
149  * Send a close notification alert
150  */
151  void close() { send_warning_alert(Alert::CLOSE_NOTIFY); }
152 
153  /**
154  * @return true iff the connection is active for sending application data
155  */
156  bool is_active() const;
157 
158  /**
159  * @return true iff the connection has been definitely closed
160  */
161  bool is_closed() const;
162 
163 
164  /**
165  * @return certificate chain of the peer (may be empty)
166  */
167  std::vector<X509_Certificate> peer_cert_chain() const;
168 
169  /**
170  * Key material export (RFC 5705)
171  * @param label a disambiguating label string
172  * @param context a per-association context value
173  * @param length the length of the desired key in bytes
174  * @return key of length bytes
175  */
176  SymmetricKey key_material_export(const std::string& label,
177  const std::string& context,
178  size_t length) const;
179 
180  /**
181  * Attempt to renegotiate the session
182  * @param force_full_renegotiation if true, require a full renegotiation,
183  * otherwise allow session resumption
184  */
185  void renegotiate(bool force_full_renegotiation = false);
186 
187  /**
188  * @return true iff the counterparty supports the secure
189  * renegotiation extensions.
190  */
191  bool secure_renegotiation_supported() const;
192 
193  /**
194  * Perform a handshake timeout check. This does nothing unless
195  * this is a DTLS channel with a pending handshake state, in
196  * which case we check for timeout and potentially retransmit
197  * handshake packets.
198  */
199  bool timeout_check();
200 
201  protected:
202 
203  virtual void process_handshake_msg(const Handshake_State* active_state,
204  Handshake_State& pending_state,
206  const std::vector<uint8_t>& contents) = 0;
207 
208  virtual void initiate_handshake(Handshake_State& state,
209  bool force_full_renegotiation) = 0;
210 
211  virtual std::vector<X509_Certificate>
212  get_peer_cert_chain(const Handshake_State& state) const = 0;
213 
214  virtual Handshake_State* new_handshake_state(class Handshake_IO* io) = 0;
215 
216  Handshake_State& create_handshake_state(Protocol_Version version);
217 
218  void inspect_handshake_message(const Handshake_Message& msg);
219 
220  void activate_session();
221 
222  void change_cipher_spec_reader(Connection_Side side);
223 
224  void change_cipher_spec_writer(Connection_Side side);
225 
226  /* secure renegotiation handling */
227 
228  void secure_renegotiation_check(const Client_Hello* client_hello);
229  void secure_renegotiation_check(const Server_Hello* server_hello);
230 
231  std::vector<uint8_t> secure_renegotiation_data_for_client_hello() const;
232  std::vector<uint8_t> secure_renegotiation_data_for_server_hello() const;
233 
234  RandomNumberGenerator& rng() { return m_rng; }
235 
236  Session_Manager& session_manager() { return m_session_manager; }
237 
238  const Policy& policy() const { return m_policy; }
239 
240  bool save_session(const Session& session);
241 
242  Callbacks& callbacks() const { return m_callbacks; }
243  private:
244  void init(size_t io_buf_sze);
245 
246  void send_record(uint8_t record_type, const std::vector<uint8_t>& record);
247 
248  void send_record_under_epoch(uint16_t epoch, uint8_t record_type,
249  const std::vector<uint8_t>& record);
250 
251  void send_record_array(uint16_t epoch, uint8_t record_type,
252  const uint8_t input[], size_t length);
253 
254  void write_record(Connection_Cipher_State* cipher_state,
255  uint16_t epoch, uint8_t type, const uint8_t input[], size_t length);
256 
257  Connection_Sequence_Numbers& sequence_numbers() const;
258 
259  std::shared_ptr<Connection_Cipher_State> read_cipher_state_epoch(uint16_t epoch) const;
260 
261  std::shared_ptr<Connection_Cipher_State> write_cipher_state_epoch(uint16_t epoch) const;
262 
263  void reset_state();
264 
265  const Handshake_State* active_state() const { return m_active_state.get(); }
266 
267  const Handshake_State* pending_state() const { return m_pending_state.get(); }
268 
269  /* methods to handle incoming traffic through Channel::receive_data. */
270  void process_handshake_ccs(const secure_vector<uint8_t>& record,
271  uint64_t record_sequence,
272  Record_Type record_type,
273  Protocol_Version record_version);
274 
275  void process_application_data(uint64_t req_no, const secure_vector<uint8_t>& record);
276 
277  void process_alert(const secure_vector<uint8_t>& record);
278 
279  bool m_is_datagram;
280 
281  /* callbacks */
282  std::unique_ptr<Compat_Callbacks> m_compat_callbacks;
283  Callbacks& m_callbacks;
284 
285  /* external state */
286  Session_Manager& m_session_manager;
287  const Policy& m_policy;
288  RandomNumberGenerator& m_rng;
289 
290  /* sequence number state */
291  std::unique_ptr<Connection_Sequence_Numbers> m_sequence_numbers;
292 
293  /* pending and active connection states */
294  std::unique_ptr<Handshake_State> m_active_state;
295  std::unique_ptr<Handshake_State> m_pending_state;
296 
297  /* cipher states for each epoch */
298  std::map<uint16_t, std::shared_ptr<Connection_Cipher_State>> m_write_cipher_states;
299  std::map<uint16_t, std::shared_ptr<Connection_Cipher_State>> m_read_cipher_states;
300 
301  /* I/O buffers */
302  secure_vector<uint8_t> m_writebuf;
303  secure_vector<uint8_t> m_readbuf;
304  };
305 
306 }
307 
308 }
309 
310 #endif
std::function< void(Alert, const uint8_t[], size_t)> alert_cb
Definition: tls_channel.h:42
void send(const std::vector< unsigned char, Alloc > &val)
Definition: tls_channel.h:126
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
void send_warning_alert(Alert::Type type)
Definition: tls_channel.h:141
std::function< void(const uint8_t[], size_t)> output_fn
Definition: tls_channel.h:40
RandomNumberGenerator & rng()
Definition: tls_channel.h:234
MechanismType type
std::function< bool(const Session &)> handshake_cb
Definition: tls_channel.h:43
static size_t IO_BUF_DEFAULT_SIZE
Definition: tls_channel.h:45
Definition: alg_id.cpp:13
void send_fatal_alert(Alert::Type type)
Definition: tls_channel.h:146
std::function< void(const uint8_t[], size_t)> data_cb
Definition: tls_channel.h:41
Callbacks & callbacks() const
Definition: tls_channel.h:242
const Policy & policy() const
Definition: tls_channel.h:238
void write_record(secure_vector< uint8_t > &output, Record_Message msg, Protocol_Version version, uint64_t seq, Connection_Cipher_State *cs, RandomNumberGenerator &rng)
Definition: tls_record.cpp:210
Session_Manager & session_manager()
Definition: tls_channel.h:236
std::function< void(const Handshake_Message &)> handshake_msg_cb
Definition: tls_channel.h:44