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