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