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