Coverage Report

Created: 2023-01-25 06:35

/src/botan/build/include/botan/internal/tls_channel_impl_12.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* TLS Channel - implementation for TLS 1.2
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_IMPL_12_H_
10
#define BOTAN_TLS_CHANNEL_IMPL_12_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/internal/tls_channel_impl.h>
17
#include <functional>
18
#include <vector>
19
#include <string>
20
#include <map>
21
#include <memory>
22
23
namespace Botan {
24
25
class X509_Certificate;
26
27
namespace TLS {
28
29
class Connection_Cipher_State;
30
class Connection_Sequence_Numbers;
31
class Handshake_State;
32
class Handshake_Message;
33
class Client_Hello_12;
34
class Server_Hello_12;
35
class Policy;
36
37
/**
38
* Generic interface for TLSv.12 endpoint
39
*/
40
class Channel_Impl_12 : public Channel_Impl
41
   {
42
   public:
43
      typedef std::function<void (const uint8_t[], size_t)> output_fn;
44
      typedef std::function<void (const uint8_t[], size_t)> data_cb;
45
      typedef std::function<void (Alert, const uint8_t[], size_t)> alert_cb;
46
      typedef std::function<bool (const Session&)> handshake_cb;
47
      typedef std::function<void (const Handshake_Message&)> handshake_msg_cb;
48
49
      /**
50
      * Set up a new TLS session
51
      *
52
      * @param callbacks contains a set of callback function references
53
      *        required by the TLS endpoint.
54
      * @param session_manager manages session state
55
      * @param rng a random number generator
56
      * @param policy specifies other connection policy information
57
      * @param is_server whether this is a server session or not
58
      * @param is_datagram whether this is a DTLS session
59
      * @param io_buf_sz This many bytes of memory will
60
      *        be preallocated for the read and write buffers. Smaller
61
      *        values just mean reallocations and copies are more likely.
62
      */
63
      explicit Channel_Impl_12(Callbacks& callbacks,
64
                               Session_Manager& session_manager,
65
                               RandomNumberGenerator& rng,
66
                               const Policy& policy,
67
                               bool is_server,
68
                               bool is_datagram,
69
                               size_t io_buf_sz = TLS::Channel::IO_BUF_DEFAULT_SIZE);
70
71
      explicit Channel_Impl_12(const Channel_Impl_12&) = delete;
72
73
      Channel_Impl_12& operator=(const Channel_Impl_12&) = delete;
74
75
      virtual ~Channel_Impl_12();
76
77
      size_t received_data(const uint8_t buf[], size_t buf_size) override;
78
79
      /**
80
      * Inject plaintext intended for counterparty
81
      * Throws an exception if is_active() is false
82
      */
83
      void send(const uint8_t buf[], size_t buf_size) override;
84
85
      /**
86
      * Send a TLS alert message. If the alert is fatal, the internal
87
      * state (keys, etc) will be reset.
88
      * @param alert the Alert to send
89
      */
90
      void send_alert(const Alert& alert) override;
91
92
      /**
93
      * @return true iff the connection is active for sending application data
94
      */
95
      bool is_active() const override;
96
97
      /**
98
      * @return true iff the connection has been definitely closed
99
      */
100
      bool is_closed() const override;
101
102
0
      bool is_closed_for_reading() const override { return is_closed(); }
103
0
      bool is_closed_for_writing() const override { return is_closed(); }
104
105
      /**
106
      * @return certificate chain of the peer (may be empty)
107
      */
108
      std::vector<X509_Certificate> peer_cert_chain() const override;
109
110
      /**
111
      * Key material export (RFC 5705)
112
      * @param label a disambiguating label string
113
      * @param context a per-association context value
114
      * @param length the length of the desired key in bytes
115
      * @return key of length bytes
116
      */
117
      SymmetricKey key_material_export(const std::string& label,
118
                                       const std::string& context,
119
                                       size_t length) const override;
120
121
      /**
122
      * Attempt to renegotiate the session
123
      * @param force_full_renegotiation if true, require a full renegotiation,
124
      * otherwise allow session resumption
125
      */
126
      void renegotiate(bool force_full_renegotiation = false) override;
127
128
      /**
129
      * Attempt to update the session's traffic key material
130
      * Note that this is possible with a TLS 1.3 channel, only.
131
      *
132
      * @param request_peer_update if true, require a reciprocal key update
133
      */
134
      void update_traffic_keys(bool request_peer_update = false) override;
135
136
      /**
137
      * @return true iff the counterparty supports the secure
138
      * renegotiation extensions.
139
      */
140
      bool secure_renegotiation_supported() const override;
141
142
      /**
143
      * Perform a handshake timeout check. This does nothing unless
144
      * this is a DTLS channel with a pending handshake state, in
145
      * which case we check for timeout and potentially retransmit
146
      * handshake packets.
147
      */
148
      bool timeout_check() override;
149
150
   protected:
151
      virtual void process_handshake_msg(const Handshake_State* active_state,
152
                                 Handshake_State& pending_state,
153
                                 Handshake_Type type,
154
                                 const std::vector<uint8_t>& contents,
155
                                 bool epoch0_restart) = 0;
156
157
      Handshake_State& create_handshake_state(Protocol_Version version);
158
      virtual std::unique_ptr<Handshake_State> new_handshake_state(std::unique_ptr<class Handshake_IO> io) = 0;
159
160
      void inspect_handshake_message(const Handshake_Message& msg);
161
162
      void activate_session();
163
164
      void change_cipher_spec_reader(Connection_Side side);
165
166
      void change_cipher_spec_writer(Connection_Side side);
167
168
      /* secure renegotiation handling */
169
170
      void secure_renegotiation_check(const Client_Hello_12* client_hello);
171
      void secure_renegotiation_check(const Server_Hello_12* server_hello);
172
173
      std::vector<uint8_t> secure_renegotiation_data_for_client_hello() const;
174
      std::vector<uint8_t> secure_renegotiation_data_for_server_hello() const;
175
176
80.3k
      RandomNumberGenerator& rng() { return m_rng; }
177
178
23.6k
      Session_Manager& session_manager() { return m_session_manager; }
179
180
192k
      const Policy& policy() const { return m_policy; }
181
182
      bool save_session(const Session& session);
183
184
218k
      Callbacks& callbacks() const { return m_callbacks; }
185
186
      void reset_active_association_state();
187
188
      virtual void initiate_handshake(Handshake_State& state, bool force_full_renegotiation) = 0;
189
190
      virtual std::vector<X509_Certificate> get_peer_cert_chain(const Handshake_State& state) const = 0;
191
192
   private:
193
      void send_record(uint8_t record_type, const std::vector<uint8_t>& record);
194
195
      void send_record_under_epoch(uint16_t epoch, uint8_t record_type,
196
                                   const std::vector<uint8_t>& record);
197
198
      void send_record_array(uint16_t epoch, uint8_t record_type,
199
                             const uint8_t input[], size_t length);
200
201
      void write_record(Connection_Cipher_State* cipher_state,
202
                        uint16_t epoch, uint8_t type, const uint8_t input[], size_t length);
203
204
      void reset_state();
205
206
      Connection_Sequence_Numbers& sequence_numbers() const;
207
208
      std::shared_ptr<Connection_Cipher_State> read_cipher_state_epoch(uint16_t epoch) const;
209
210
      std::shared_ptr<Connection_Cipher_State> write_cipher_state_epoch(uint16_t epoch) const;
211
212
227k
      const Handshake_State* active_state() const { return m_active_state.get(); }
213
214
126k
      const Handshake_State* pending_state() const { return m_pending_state.get(); }
215
216
      /* methods to handle incoming traffic through Channel_Impl_12::receive_data. */
217
      void process_handshake_ccs(const secure_vector<uint8_t>& record,
218
                                 uint64_t record_sequence,
219
                                 Record_Type record_type,
220
                                 Protocol_Version record_version,
221
                                 bool epoch0_restart);
222
223
      void process_application_data(uint64_t req_no, const secure_vector<uint8_t>& record);
224
225
      void process_alert(const secure_vector<uint8_t>& record);
226
227
      const bool m_is_server;
228
      const bool m_is_datagram;
229
230
      /* callbacks */
231
      Callbacks& m_callbacks;
232
233
      /* external state */
234
      Session_Manager& m_session_manager;
235
      const Policy& m_policy;
236
      RandomNumberGenerator& m_rng;
237
238
      /* sequence number state */
239
      std::unique_ptr<Connection_Sequence_Numbers> m_sequence_numbers;
240
241
      /* pending and active connection states */
242
      std::unique_ptr<Handshake_State> m_active_state;
243
      std::unique_ptr<Handshake_State> m_pending_state;
244
245
      /* cipher states for each epoch */
246
      std::map<uint16_t, std::shared_ptr<Connection_Cipher_State>> m_write_cipher_states;
247
      std::map<uint16_t, std::shared_ptr<Connection_Cipher_State>> m_read_cipher_states;
248
249
      /* I/O buffers */
250
      secure_vector<uint8_t> m_writebuf;
251
      secure_vector<uint8_t> m_readbuf;
252
      secure_vector<uint8_t> m_record_buf;
253
254
      bool m_has_been_closed;
255
   };
256
257
}
258
259
}
260
261
#endif