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