Botan 3.0.0
Crypto and TLS for C&
tls_channel_impl.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* 2021 Elektrobit Automotive GmbH
6* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#ifndef BOTAN_TLS_CHANNEL_IMPL_H_
12#define BOTAN_TLS_CHANNEL_IMPL_H_
13
14#include <botan/tls_channel.h>
15#include <botan/tls_version.h>
16#include <botan/tls_magic.h>
17
18#include <vector>
19#include <memory>
20#include <utility>
21
22namespace Botan {
23
24class Credentials_Manager;
25class X509_Certificate;
26
27namespace TLS {
28
29class Client;
30class Server;
31
32enum class Record_Type: uint8_t {
33 Invalid = 0, // RFC 8446 (TLS 1.3)
34
36 Alert = 21,
37 Handshake = 22,
38 ApplicationData = 23,
39
40 Heartbeat = 24, // RFC 6520 (TLS 1.3)
41};
42
44 {
45 public:
46 virtual ~Channel_Impl() = default;
47
48 /**
49 * Inject TLS traffic received from counterparty
50 * @return a hint as the how many more bytes we need to q the
51 * current record (this may be 0 if on a record boundary)
52 */
53 virtual size_t from_peer(std::span<const uint8_t> data) = 0;
54
55 /**
56 * Inject plaintext intended for counterparty
57 * Throws an exception if is_active() is false
58 */
59 virtual void to_peer(std::span<const uint8_t> data) = 0;
60
61 /**
62 * Send a TLS alert message. If the alert is fatal, the internal
63 * state (keys, etc) will be reset.
64 * @param alert the Alert to send
65 */
66 virtual void send_alert(const Alert& alert) = 0;
67
68 /**
69 * Send a warning alert
70 */
71 void send_warning_alert(Alert::Type type) { send_alert(Alert(type, false)); }
72
73 /**
74 * Send a fatal alert
75 */
76 void send_fatal_alert(Alert::Type type) { send_alert(Alert(type, true)); }
77
78 /**
79 * Send a close notification alert
80 */
81 void close() { send_warning_alert(Alert::CloseNotify); }
82
83 /**
84 * @return true iff the connection is active for sending application data
85 */
86 virtual bool is_active() const = 0;
87
88 /**
89 * @return true iff the connection has been definitely closed
90 */
91 virtual bool is_closed() const = 0;
92
93 /**
94 * @return true iff the connection is active for sending application data
95 */
96 virtual bool is_closed_for_reading() const = 0;
97
98 /**
99 * @return true iff the connection has been definitely closed
100 */
101 virtual bool is_closed_for_writing() const = 0;
102
103 /**
104 * @return certificate chain of the peer (may be empty)
105 */
106 virtual std::vector<X509_Certificate> peer_cert_chain() const = 0;
107
108 /**
109 * Key material export (RFC 5705)
110 * @param label a disambiguating label string
111 * @param context a per-association context value
112 * @param length the length of the desired key in bytes
113 * @return key of length bytes
114 */
115 virtual SymmetricKey key_material_export(std::string_view label,
116 std::string_view context,
117 size_t length) const = 0;
118
119 /**
120 * Attempt to renegotiate the session
121 * @param force_full_renegotiation if true, require a full renegotiation,
122 * otherwise allow session resumption
123 */
124 virtual void renegotiate(bool force_full_renegotiation = false) = 0;
125
126 /**
127 * @return true if this channel can issue TLS 1.3 style session tickets.
128 */
129 virtual bool new_session_ticket_supported() const { return false; }
130
131 /**
132 * Send @p tickets new session tickets to the peer. This is only supported
133 * on TLS 1.3 servers.
134 *
135 * If the server's Session_Manager does not accept the generated Session
136 * objects, the server implementation won't be able to send new tickets.
137 * Additionally, anything but TLS 1.3 servers will return 0 (because they
138 * don't support sending such session tickets).
139 *
140 * @returns the number of session tickets successfully sent to the client
141 */
142 virtual size_t send_new_session_tickets(const size_t /* tickets */) { return 0; }
143
144 /**
145 * Attempt to update the session's traffic key material
146 * Note that this is possible with a TLS 1.3 channel, only.
147 *
148 * @param request_peer_update if true, require a reciprocal key update
149 */
150 virtual void update_traffic_keys(bool request_peer_update = false) = 0;
151
152 /**
153 * @return true iff the counterparty supports the secure
154 * renegotiation extensions.
155 */
156 virtual bool secure_renegotiation_supported() const = 0;
157
158 /**
159 * Perform a handshake timeout check. This does nothing unless
160 * this is a DTLS channel with a pending handshake state, in
161 * which case we check for timeout and potentially retransmit
162 * handshake packets.
163 */
164 virtual bool timeout_check() = 0;
165
166 /**
167 * Return the protocol notification set for this connection, if any (ALPN).
168 * This value is not tied to the session and a later renegotiation of the
169 * same session can choose a new protocol.
170 */
171 virtual std::string application_protocol() const = 0;
172
173 protected:
174 /**
175 * This struct collect all information required to perform a downgrade from TLS 1.3 to TLS 1.2.
176 *
177 * The downgrade process is (currently) triggered when a TLS 1.3 client receives a downgrade request
178 * in the server hello message (@sa `Client_Impl_13::handle(Server_Hello_12)`). As a result,
179 * `Client::received_data` should detect this condition and replace its `Channel_Impl_13` member by a
180 * `Channel_Impl_12`.
181 *
182 * Note that the downgrade process for the server implementation will likely differ.
183 */
185 {
186 /// The client hello message including the handshake header bytes as transferred to the peer.
187 std::vector<uint8_t> client_hello_message;
188
189 /// The full data transcript received from the peer. This will contain the server hello message that forced us to downgrade.
190 std::vector<uint8_t> peer_transcript;
191
192 /// The TLS 1.2 session information found by a TLS 1.3 client that
193 /// caused it to initiate a downgrade before even sending a client hello.
194 std::optional<Session_with_Handle> tls12_session;
195
197 std::vector<std::string> next_protocols;
199
200 std::shared_ptr<Callbacks> callbacks;
201 std::shared_ptr<Session_Manager> session_manager;
202 std::shared_ptr<Credentials_Manager> creds;
203 std::shared_ptr<RandomNumberGenerator> rng;
204 std::shared_ptr<const Policy> policy;
205
208 };
209
210 std::unique_ptr<Downgrade_Information> m_downgrade_info;
211
212 void preserve_peer_transcript(std::span<const uint8_t> input)
213 {
215 m_downgrade_info->peer_transcript.insert(m_downgrade_info->peer_transcript.end(),
216 input.begin(), input.end());
217 }
218
219 void preserve_client_hello(std::span<const uint8_t> msg)
220 {
222 m_downgrade_info->client_hello_message.assign(msg.begin(), msg.end());
223 }
224
225 friend class Client;
226 friend class Server;
227 void set_io_buffer_size(size_t io_buf_sz)
228 {
230 m_downgrade_info->io_buffer_size = io_buf_sz;
231 }
232
233 /**
234 * Implementations use this to signal that the peer indicated a protocol
235 * version downgrade. After calling `request_downgrade()` no further
236 * state changes must be perfomed by the implementation. Particularly, no
237 * further handshake messages must be emitted. Instead, they must yield
238 * control flow back to the underlying Channel implementation to perform
239 * the protocol version downgrade.
240 */
242 {
244 m_downgrade_info->will_downgrade = true;
245 }
246
248 {
250 m_downgrade_info->client_hello_message.empty() &&
251 m_downgrade_info->peer_transcript.empty() &&
252 !m_downgrade_info->tls12_session.has_value());
254 m_downgrade_info->tls12_session = std::move(session);
256 }
257
258 public:
259 /**
260 * Indicates whether a downgrade to TLS 1.2 or lower is in progress
261 *
262 * @sa Downgrade_Information
263 */
264 bool is_downgrading() const { return m_downgrade_info && m_downgrade_info->will_downgrade; }
265
266 /**
267 * @sa Downgrade_Information
268 */
269 std::unique_ptr<Downgrade_Information> extract_downgrade_info() { return std::exchange(m_downgrade_info, {}); }
270
271 bool expects_downgrade() const { return m_downgrade_info != nullptr; }
272 };
273
274}
275
276}
277
278#endif
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:67
#define BOTAN_STATE_CHECK(expr)
Definition: assert.h:48
virtual SymmetricKey key_material_export(std::string_view label, std::string_view context, size_t length) const =0
virtual bool is_active() const =0
void preserve_peer_transcript(std::span< const uint8_t > input)
virtual std::string application_protocol() const =0
virtual bool new_session_ticket_supported() const
virtual ~Channel_Impl()=default
void request_downgrade_for_resumption(Session_with_Handle session)
void preserve_client_hello(std::span< const uint8_t > msg)
virtual size_t from_peer(std::span< const uint8_t > data)=0
virtual bool timeout_check()=0
virtual bool is_closed_for_reading() const =0
virtual void update_traffic_keys(bool request_peer_update=false)=0
virtual void send_alert(const Alert &alert)=0
virtual void to_peer(std::span< const uint8_t > data)=0
virtual bool is_closed() const =0
void send_warning_alert(Alert::Type type)
virtual bool secure_renegotiation_supported() const =0
std::unique_ptr< Downgrade_Information > extract_downgrade_info()
virtual bool is_closed_for_writing() const =0
void send_fatal_alert(Alert::Type type)
void set_io_buffer_size(size_t io_buf_sz)
virtual void renegotiate(bool force_full_renegotiation=false)=0
std::unique_ptr< Downgrade_Information > m_downgrade_info
virtual std::vector< X509_Certificate > peer_cert_chain() const =0
virtual size_t send_new_session_tickets(const size_t)
Protocol_Version version() const
Definition: tls_session.h:177
Definition: alg_id.cpp:12
std::vector< uint8_t > peer_transcript
The full data transcript received from the peer. This will contain the server hello message that forc...
std::vector< uint8_t > client_hello_message
The client hello message including the handshake header bytes as transferred to the peer.
std::shared_ptr< Session_Manager > session_manager
std::shared_ptr< Credentials_Manager > creds
std::optional< Session_with_Handle > tls12_session
std::shared_ptr< RandomNumberGenerator > rng