Botan 3.0.0
Crypto and TLS for C&
tls_channel_impl_12.h
Go to the documentation of this file.
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
23namespace Botan {
24
25class X509_Certificate;
26
27namespace TLS {
28
29class Connection_Cipher_State;
30class Connection_Sequence_Numbers;
31class Handshake_State;
32class Handshake_Message;
33class Client_Hello_12;
34class Server_Hello_12;
35class Policy;
36
37/**
38* Generic interface for TLSv.12 endpoint
39*/
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(const std::shared_ptr<Callbacks>& callbacks,
64 const std::shared_ptr<Session_Manager>& session_manager,
65 const std::shared_ptr<RandomNumberGenerator>& rng,
66 const std::shared_ptr<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
74
76
77 size_t from_peer(std::span<const uint8_t> data) override;
78 void to_peer(std::span<const uint8_t> data) override;
79
80 /**
81 * Send a TLS alert message. If the alert is fatal, the internal
82 * state (keys, etc) will be reset.
83 * @param alert the Alert to send
84 */
85 void send_alert(const Alert& alert) override;
86
87 /**
88 * @return true iff the connection is active for sending application data
89 */
90 bool is_active() const override;
91
92 /**
93 * @return true iff the connection has been definitely closed
94 */
95 bool is_closed() const override;
96
97 bool is_closed_for_reading() const override { return is_closed(); }
98 bool is_closed_for_writing() const override { return is_closed(); }
99
100 /**
101 * @return certificate chain of the peer (may be empty)
102 */
103 std::vector<X509_Certificate> peer_cert_chain() const override;
104
105 /**
106 * Key material export (RFC 5705)
107 * @param label a disambiguating label string
108 * @param context a per-association context value
109 * @param length the length of the desired key in bytes
110 * @return key of length bytes
111 */
112 SymmetricKey key_material_export(std::string_view label,
113 std::string_view context,
114 size_t length) const override;
115
116 /**
117 * Attempt to renegotiate the session
118 * @param force_full_renegotiation if true, require a full renegotiation,
119 * otherwise allow session resumption
120 */
121 void renegotiate(bool force_full_renegotiation = false) override;
122
123 /**
124 * Attempt to update the session's traffic key material
125 * Note that this is possible with a TLS 1.3 channel, only.
126 *
127 * @param request_peer_update if true, require a reciprocal key update
128 */
129 void update_traffic_keys(bool request_peer_update = false) override;
130
131 /**
132 * @return true iff the counterparty supports the secure
133 * renegotiation extensions.
134 */
135 bool secure_renegotiation_supported() const override;
136
137 /**
138 * Perform a handshake timeout check. This does nothing unless
139 * this is a DTLS channel with a pending handshake state, in
140 * which case we check for timeout and potentially retransmit
141 * handshake packets.
142 */
143 bool timeout_check() override;
144
145 protected:
146 virtual void process_handshake_msg(const Handshake_State* active_state,
147 Handshake_State& pending_state,
148 Handshake_Type type,
149 const std::vector<uint8_t>& contents,
150 bool epoch0_restart) = 0;
151
153 virtual std::unique_ptr<Handshake_State> new_handshake_state(std::unique_ptr<class Handshake_IO> io) = 0;
154
156
157 void activate_session();
158
160
162
163 /* secure renegotiation handling */
164
165 void secure_renegotiation_check(const Client_Hello_12* client_hello);
166 void secure_renegotiation_check(const Server_Hello_12* server_hello);
167
168 std::vector<uint8_t> secure_renegotiation_data_for_client_hello() const;
169 std::vector<uint8_t> secure_renegotiation_data_for_server_hello() const;
170
171 RandomNumberGenerator& rng() { return *m_rng; }
172
173 Session_Manager& session_manager() { return *m_session_manager; }
174
175 const Policy& policy() const { return *m_policy; }
176
177 Callbacks& callbacks() const { return *m_callbacks; }
178
180
181 virtual void initiate_handshake(Handshake_State& state, bool force_full_renegotiation) = 0;
182
183 virtual std::vector<X509_Certificate> get_peer_cert_chain(const Handshake_State& state) const = 0;
184
185 private:
186 void send_record(Record_Type record_type, const std::vector<uint8_t>& record);
187
188 void send_record_under_epoch(uint16_t epoch, Record_Type record_type,
189 const std::vector<uint8_t>& record);
190
191 void send_record_array(uint16_t epoch, Record_Type record_type,
192 const uint8_t input[], size_t length);
193
194 void write_record(Connection_Cipher_State* cipher_state,
195 uint16_t epoch, Record_Type type,
196 const uint8_t input[], size_t length);
197
198 void reset_state();
199
200 Connection_Sequence_Numbers& sequence_numbers() const;
201
202 std::shared_ptr<Connection_Cipher_State> read_cipher_state_epoch(uint16_t epoch) const;
203
204 std::shared_ptr<Connection_Cipher_State> write_cipher_state_epoch(uint16_t epoch) const;
205
206 const Handshake_State* active_state() const { return m_active_state.get(); }
207
208 const Handshake_State* pending_state() const { return m_pending_state.get(); }
209
210 /* methods to handle incoming traffic through Channel_Impl_12::receive_data. */
211 void process_handshake_ccs(const secure_vector<uint8_t>& record,
212 uint64_t record_sequence,
213 Record_Type record_type,
214 Protocol_Version record_version,
215 bool epoch0_restart);
216
217 void process_application_data(uint64_t req_no, const secure_vector<uint8_t>& record);
218
219 void process_alert(const secure_vector<uint8_t>& record);
220
221 const bool m_is_server;
222 const bool m_is_datagram;
223
224 /* callbacks */
225 std::shared_ptr<Callbacks> m_callbacks;
226
227 /* external state */
228 std::shared_ptr<Session_Manager> m_session_manager;
229 std::shared_ptr<const Policy> m_policy;
230 std::shared_ptr<RandomNumberGenerator> m_rng;
231
232 /* sequence number state */
233 std::unique_ptr<Connection_Sequence_Numbers> m_sequence_numbers;
234
235 /* pending and active connection states */
236 std::unique_ptr<Handshake_State> m_active_state;
237 std::unique_ptr<Handshake_State> m_pending_state;
238
239 /* cipher states for each epoch */
240 std::map<uint16_t, std::shared_ptr<Connection_Cipher_State>> m_write_cipher_states;
241 std::map<uint16_t, std::shared_ptr<Connection_Cipher_State>> m_read_cipher_states;
242
243 /* I/O buffers */
244 secure_vector<uint8_t> m_writebuf;
245 secure_vector<uint8_t> m_readbuf;
246 secure_vector<uint8_t> m_record_buf;
247
248 bool m_has_been_closed;
249 };
250
251}
252
253}
254
255#endif
RandomNumberGenerator & rng()
virtual std::vector< X509_Certificate > get_peer_cert_chain(const Handshake_State &state) const =0
void change_cipher_spec_reader(Connection_Side side)
std::function< bool(const Session &)> handshake_cb
Channel_Impl_12 & operator=(const Channel_Impl_12 &)=delete
void inspect_handshake_message(const Handshake_Message &msg)
void update_traffic_keys(bool request_peer_update=false) override
std::function< void(const Handshake_Message &)> handshake_msg_cb
Handshake_State & create_handshake_state(Protocol_Version version)
std::vector< uint8_t > secure_renegotiation_data_for_server_hello() const
std::function< void(const uint8_t[], size_t)> output_fn
size_t from_peer(std::span< const uint8_t > data) override
bool is_closed_for_reading() const override
void secure_renegotiation_check(const Client_Hello_12 *client_hello)
Session_Manager & session_manager()
Channel_Impl_12(const Channel_Impl_12 &)=delete
const Policy & policy() const
std::function< void(Alert, const uint8_t[], size_t)> alert_cb
void send_alert(const Alert &alert) override
virtual void initiate_handshake(Handshake_State &state, bool force_full_renegotiation)=0
std::vector< X509_Certificate > peer_cert_chain() const override
void to_peer(std::span< const uint8_t > data) override
bool is_closed_for_writing() const override
std::function< void(const uint8_t[], size_t)> data_cb
void change_cipher_spec_writer(Connection_Side side)
std::vector< uint8_t > secure_renegotiation_data_for_client_hello() const
virtual std::unique_ptr< Handshake_State > new_handshake_state(std::unique_ptr< class Handshake_IO > io)=0
SymmetricKey key_material_export(std::string_view label, std::string_view context, size_t length) const override
virtual void process_handshake_msg(const Handshake_State *active_state, Handshake_State &pending_state, Handshake_Type type, const std::vector< uint8_t > &contents, bool epoch0_restart)=0
bool secure_renegotiation_supported() const override
void renegotiate(bool force_full_renegotiation=false) override
static constexpr size_t IO_BUF_DEFAULT_SIZE
Definition: tls_channel.h:33
Definition: alg_id.cpp:12
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64