Botan 3.13.0
Crypto and TLS for C&
tls_messages_13.h
Go to the documentation of this file.
1/*
2* TLS Messages
3* (C) 2021-2022 Jack Lloyd
4* 2021 Elektrobit Automotive GmbH
5* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#ifndef BOTAN_TLS_MESSAGES_13_H_
11#define BOTAN_TLS_MESSAGES_13_H_
12
13#include <botan/tls_extensions.h>
14#include <botan/tls_external_psk.h>
15#include <botan/tls_messages.h>
16#include <chrono>
17
18namespace Botan {
19
20enum class Usage_Type : uint8_t;
22
23} // namespace Botan
24
25namespace Botan::TLS {
26
28class Client_Hello_12;
29
31 public:
32 /**
33 * Creates a client hello which might optionally use the passed-in
34 * @p session for resumption. In that case, this will "extract" the
35 * master secret from the passed-in @p session.
36 */
37 Client_Hello_13(const Policy& policy,
38 Callbacks& cb,
40 std::string_view hostname,
41 std::vector<std::string> next_protocols,
42 std::optional<Session_with_Handle>& session,
43 std::vector<ExternalPSK> psks);
44
45 static std::variant<Client_Hello_13, Client_Hello_12_Shim> parse(std::span<const uint8_t> buf);
46
47 void retry(const Hello_Retry_Request& hrr,
48 const Transcript_Hash_State& transcript_hash_state,
49 Callbacks& cb,
51
52 /**
53 * Select the highest protocol version from the list of versions
54 * supported by the client. If no such version can be determined this
55 * returns std::nullopt.
56 */
57 std::optional<Protocol_Version> highest_supported_version(const Policy& policy) const;
58
59 /**
60 * This validates that a Client Hello received after sending a Hello
61 * Retry Request was updated in accordance with RFC 8446 4.1.2. If issues
62 * are found, this method throws accordingly.
63 */
64 void validate_updates(const Client_Hello_13& new_ch);
65
66 private:
67 explicit Client_Hello_13(std::unique_ptr<Client_Hello_Internal> data);
68
69#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
70 // This lets the TLS 1.2 implementation extract the internal data
71 // from this Client Hello object for downgrade purposes.
72 friend class Client_Hello_12;
73#endif
74
75 /**
76 * If the Client Hello contains a PSK extensions with identities this will
77 * generate the PSK binders as described in RFC 8446 4.2.11.2.
78 * Note that the passed in \p transcript_hash_state might be virgin for
79 * the initial Client Hello and should be primed with ClientHello1 and
80 * HelloRetryRequest for an updated Client Hello.
81 */
82 void calculate_psk_binders(Transcript_Hash_State transcript_hash_state);
83};
84
86
88 protected:
89 static const struct Server_Hello_Tag {
91
92 static const struct Hello_Retry_Request_Tag {
94
95 static const struct Hello_Retry_Request_Creation_Tag {
97
98 // These constructors are meant for instantiating Server Hellos
99 // after parsing a peer's message. They perform basic validation
100 // and are therefore not suitable for constructing a message to
101 // be sent to a client.
102 explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Server_Hello_Tag tag = as_server_hello);
103 explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Hello_Retry_Request_Tag tag);
104 void basic_validation() const;
105
106 // Instantiate a Server Hello as response to a client's Client Hello
107 // (called from Server_Hello_13::create())
109 std::optional<Named_Group> key_exchange_group,
110 Session_Manager& session_mgr,
111 Credentials_Manager& credentials_mgr,
113 Callbacks& cb,
114 const Policy& policy);
115
116 explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Hello_Retry_Request_Creation_Tag tag);
117
118 public:
119 static std::variant<Hello_Retry_Request, Server_Hello_13> create(const Client_Hello_13& ch,
120 bool hello_retry_request_allowed,
121 Session_Manager& session_mgr,
122 Credentials_Manager& credentials_mgr,
124 const Policy& policy,
125 Callbacks& cb);
126
127 static std::variant<Hello_Retry_Request, Server_Hello_13, Server_Hello_12_Shim> parse(
128 std::span<const uint8_t> buf);
129
130 /**
131 * Return desired downgrade version indicated by hello random, if any.
132 */
133 std::optional<Protocol_Version> random_signals_downgrade() const;
134
135 /**
136 * @returns the selected version as indicated by the supported_versions extension
137 */
139};
140
142 protected:
143 friend class Server_Hello_13; // to allow construction by Server_Hello_13::parse() and ::create()
144 explicit Hello_Retry_Request(std::unique_ptr<Server_Hello_Internal> data);
145 Hello_Retry_Request(const Client_Hello_13& ch, Named_Group selected_group, const Policy& policy, Callbacks& cb);
146
147 public:
149
151};
152
154 public:
155 explicit Encrypted_Extensions(std::span<const uint8_t> buf);
156 Encrypted_Extensions(const Client_Hello_13& client_hello,
157 const Policy& policy,
158 Callbacks& cb,
159 bool is_resumption,
160 bool requesting_client_auth);
161
163
164 const Extensions& extensions() const { return m_extensions; }
165
166 std::vector<uint8_t> serialize() const override;
167
168 private:
169 Extensions m_extensions;
170};
171
172class Certificate_Request_13;
173
174/**
175* Certificate Message of TLS 1.3
176*/
178 public:
180 public:
182 explicit Certificate_Entry(const X509_Certificate& cert);
183 explicit Certificate_Entry(std::shared_ptr<Public_Key> raw_public_key);
184
185 bool has_certificate() const { return m_certificate != nullptr; }
186
187 const X509_Certificate& certificate() const;
188 std::shared_ptr<const Public_Key> public_key() const;
189
190 std::vector<uint8_t> serialize() const;
191
192 Extensions& extensions() { return m_extensions; }
193
194 const Extensions& extensions() const { return m_extensions; }
195
196 Certificate_Entry(const Certificate_Entry& other) = delete;
198
201
203
204 private:
205 std::unique_ptr<X509_Certificate> m_certificate; // possibly null if raw public key in use
206 std::shared_ptr<Public_Key> m_raw_public_key;
207 Extensions m_extensions;
208 };
209
210 public:
212
213 std::vector<X509_Certificate> cert_chain() const;
214
215 bool has_certificate_chain() const;
216 bool is_raw_public_key() const;
217
218 size_t count() const { return m_entries.size(); }
219
220 bool empty() const { return m_entries.empty(); }
221
222 std::shared_ptr<const Public_Key> public_key() const;
223 const X509_Certificate& leaf() const;
224
225 const std::vector<uint8_t>& request_context() const { return m_request_context; }
226
227 /**
228 * Create a Client Certificate message
229 * ... in response to a Certificate Request message.
230 */
231 Certificate_13(const Certificate_Request_13& cert_request,
232 std::string_view hostname,
233 Credentials_Manager& credentials_manager,
234 Callbacks& callbacks,
235 Certificate_Type cert_type);
236
237 /**
238 * Create a Server Certificate message
239 * ... in response to a Client Hello indicating the need to authenticate
240 * with a server certificate.
241 */
242 Certificate_13(const Client_Hello_13& client_hello,
243 Credentials_Manager& credentials_manager,
244 Callbacks& callbacks,
245 Certificate_Type cert_type);
246
247 /**
248 * Deserialize a Certificate message
249 * @param buf the serialized message
250 * @param policy the TLS policy
251 * @param side is this a Connection_Side::Server or Connection_Side::Client certificate message
252 * @param cert_type is the certificate type that was negotiated during the handshake
253 */
254 Certificate_13(std::span<const uint8_t> buf,
255 const Policy& policy,
256 Connection_Side side,
257 Certificate_Type cert_type);
258
259 /**
260 * Validate a Certificate message regarding what extensions are expected based on
261 * previous handshake messages. Also call the tls_examine_extensions() callback
262 * for each entry.
263 *
264 * @param requested_extensions Extensions of Client_Hello or Certificate_Request messages
265 * @param cb Callback that will be called for each extension.
266 */
267 void validate_extensions(const std::set<Extension_Code>& requested_extensions, Callbacks& cb) const;
268
269 /**
270 * Verify the certificate chain
271 *
272 * @throws if verification fails.
273 */
274 void verify(Callbacks& callbacks,
275 const Policy& policy,
276 Credentials_Manager& creds,
277 std::string_view hostname,
278 bool use_ocsp) const;
279
280 std::vector<uint8_t> serialize() const override;
281
282 private:
283 void setup_entries(std::vector<X509_Certificate> cert_chain,
285 Callbacks& callbacks);
286 void setup_entry(std::shared_ptr<Public_Key> raw_public_key, Callbacks& callbacks);
287
288 void verify_certificate_chain(Callbacks& callbacks,
289 const Policy& policy,
290 Credentials_Manager& creds,
291 std::string_view hostname,
292 bool use_ocsp,
293 Usage_Type usage_type) const;
294
295 private:
296 std::vector<uint8_t> m_request_context;
297 std::vector<Certificate_Entry> m_entries;
298 Connection_Side m_side;
299};
300
302 public:
303 Handshake_Type type() const override;
304
305 Certificate_Request_13(std::span<const uint8_t> buf, Connection_Side side);
306
307 //! Creates a Certificate_Request message if it is required by the configuration
308 //! @return std::nullopt if configuration does not require client authentication
309 static std::optional<Certificate_Request_13> maybe_create(const Client_Hello_13& sni_hostname,
310 Credentials_Manager& cred_mgr,
311 Callbacks& callbacks,
312 const Policy& policy);
313
314 std::vector<X509_DN> acceptable_CAs() const;
315 const std::vector<Signature_Scheme>& signature_schemes() const;
316 const std::vector<Signature_Scheme>& certificate_signature_schemes() const;
317
318 const Extensions& extensions() const { return m_extensions; }
319
320 std::vector<uint8_t> serialize() const override;
321
322 const std::vector<uint8_t>& context() const { return m_context; }
323
324 private:
325 Certificate_Request_13(std::vector<X509_DN> acceptable_CAs, const Policy& policy, Callbacks& callbacks);
326
327 private:
328 std::vector<uint8_t> m_context;
329 Extensions m_extensions;
330};
331
332/**
333* Certificate Verify Message
334*/
336 public:
337 /**
338 * Deserialize a Certificate message
339 * @param buf the serialized message
340 * @param side is this a Connection_Side::Server or Connection_Side::Client certificate message
341 */
342 Certificate_Verify_13(std::span<const uint8_t> buf, Connection_Side side);
343
344 Certificate_Verify_13(const Certificate_13& certificate_message,
345 const std::vector<Signature_Scheme>& peer_allowed_schemes,
346 std::string_view hostname,
347 const Transcript_Hash& hash,
348 Connection_Side whoami,
349 Credentials_Manager& creds_mgr,
350 const Policy& policy,
351 Callbacks& callbacks,
353
354 bool verify(const Public_Key& public_key, Callbacks& callbacks, const Transcript_Hash& transcript_hash) const;
355
356 private:
357 Connection_Side m_side;
358};
359
361 public:
362 using Finished::Finished;
363 Finished_13(Cipher_State* cipher_state, const Transcript_Hash& transcript_hash);
364
365 bool verify(Cipher_State* cipher_state, const Transcript_Hash& transcript_hash) const;
366};
367
369 public:
371
373 const Session& session,
374 const Session_Handle& handle,
375 Callbacks& callbacks);
376
377 New_Session_Ticket_13(std::span<const uint8_t> buf, Connection_Side from);
378
379 std::vector<uint8_t> serialize() const override;
380
381 const Extensions& extensions() const { return m_extensions; }
382
383 const Opaque_Session_Handle& handle() const { return m_handle; }
384
385 const Ticket_Nonce& nonce() const { return m_ticket_nonce; }
386
387 uint32_t ticket_age_add() const { return m_ticket_age_add; }
388
389 std::chrono::seconds lifetime_hint() const { return m_ticket_lifetime_hint; }
390
391 /**
392 * @return the number of bytes allowed for early data or std::nullopt
393 * when early data is not allowed at all
394 */
395 std::optional<uint32_t> early_data_byte_limit() const;
396
397 private:
398 // RFC 8446 4.6.1
399 // Clients MUST NOT cache tickets for longer than 7 days, regardless of
400 // the ticket_lifetime, and MAY delete tickets earlier based on local
401 // policy. A server MAY treat a ticket as valid for a shorter period
402 // of time than what is stated in the ticket_lifetime.
403 //
404 // ... hence we call it 'lifetime hint'.
405 std::chrono::seconds m_ticket_lifetime_hint{};
406 uint32_t m_ticket_age_add;
407 Ticket_Nonce m_ticket_nonce;
408 Opaque_Session_Handle m_handle;
409 Extensions m_extensions;
410};
411
413 public:
414 Handshake_Type type() const override { return Handshake_Type::KeyUpdate; }
415
416 explicit Key_Update(bool request_peer_update);
417 explicit Key_Update(std::span<const uint8_t> buf);
418
419 std::vector<uint8_t> serialize() const override;
420
421 bool expects_reciprocation() const { return m_update_requested; }
422
423 private:
424 bool m_update_requested;
425};
426
427namespace detail {
428template <typename T>
430
431template <typename... AlternativeTs>
432struct as_wrapped_references<std::variant<AlternativeTs...>> {
433 using type = std::variant<std::reference_wrapper<AlternativeTs>...>;
434};
435
436template <typename T>
438} // namespace detail
439
440// Handshake message types from RFC 8446 4.
446 // End_Of_Early_Data,
453
454using Post_Handshake_Message_13 = std::variant<New_Session_Ticket_13, Key_Update>;
455
456// Key_Update is handled generically by the Channel. The messages assigned
457// to those variants are the ones that need to be handled by the specific
458// client and/or server implementations.
459using Server_Post_Handshake_13_Message = std::variant<New_Session_Ticket_13, Key_Update>;
460using Client_Post_Handshake_13_Message = std::variant<Key_Update>;
461
463 Server_Hello_12_Shim, // indicates a TLS version downgrade
471
473 std::variant<Client_Hello_13,
474 Client_Hello_12_Shim, // indicates a TLS peer that does not offer TLS 1.3
479
480} // namespace Botan::TLS
481
482#endif
#define BOTAN_UNSTABLE_API
Definition api.h:34
Certificate_Entry & operator=(const Certificate_Entry &other)=delete
const Extensions & extensions() const
Extensions & extensions()
Certificate_Entry(TLS_Data_Reader &reader, Connection_Side side, Certificate_Type cert_type)
Certificate_Entry & operator=(Certificate_Entry &&other) noexcept
Certificate_Entry(const Certificate_Entry &other)=delete
~Certificate_Entry()
bool has_certificate() const
Certificate_Entry(Certificate_Entry &&other) noexcept
std::shared_ptr< const Public_Key > public_key() const
Handshake_Type type() const override
const std::vector< uint8_t > & request_context() const
std::vector< uint8_t > serialize() const override
Certificate_13(const Certificate_Request_13 &cert_request, std::string_view hostname, Credentials_Manager &credentials_manager, Callbacks &callbacks, Certificate_Type cert_type)
const std::vector< Signature_Scheme > & signature_schemes() const
const Extensions & extensions() const
const std::vector< uint8_t > & context() const
Handshake_Type type() const override
const std::vector< Signature_Scheme > & certificate_signature_schemes() const
Certificate_Request_13(std::span< const uint8_t > buf, Connection_Side side)
static std::optional< Certificate_Request_13 > maybe_create(const Client_Hello_13 &sni_hostname, Credentials_Manager &cred_mgr, Callbacks &callbacks, const Policy &policy)
std::vector< X509_DN > acceptable_CAs() const
bool verify(const Public_Key &public_key, Callbacks &callbacks, const Transcript_Hash &transcript_hash) const
Certificate_Verify_13(std::span< const uint8_t > buf, Connection_Side side)
Certificate_Verify(std::span< const uint8_t > buf)
void validate_updates(const Client_Hello_13 &new_ch)
std::optional< Protocol_Version > highest_supported_version(const Policy &policy) const
Client_Hello_13(const Policy &policy, Callbacks &cb, RandomNumberGenerator &rng, std::string_view hostname, std::vector< std::string > next_protocols, std::optional< Session_with_Handle > &session, std::vector< ExternalPSK > psks)
static std::variant< Client_Hello_13, Client_Hello_12_Shim > parse(std::span< const uint8_t > buf)
void retry(const Hello_Retry_Request &hrr, const Transcript_Hash_State &transcript_hash_state, Callbacks &cb, RandomNumberGenerator &rng)
std::vector< std::string > next_protocols() const
Client_Hello(const Client_Hello &)=delete
const Extensions & extensions() const
Encrypted_Extensions(std::span< const uint8_t > buf)
Handshake_Type type() const override
bool verify(Cipher_State *cipher_state, const Transcript_Hash &transcript_hash) const
Finished_13(Cipher_State *cipher_state, const Transcript_Hash &transcript_hash)
Finished(std::vector< uint8_t > buf)
Finished(std::vector< uint8_t > buf)
Hello_Retry_Request(std::unique_ptr< Server_Hello_Internal > data)
Handshake_Type wire_type() const override
Handshake_Type type() const override
bool expects_reciprocation() const
Key_Update(bool request_peer_update)
Handshake_Type type() const override
Handshake_Type type() const override
std::chrono::seconds lifetime_hint() const
const Ticket_Nonce & nonce() const
New_Session_Ticket_13(Ticket_Nonce nonce, const Session &session, const Session_Handle &handle, Callbacks &callbacks)
const Opaque_Session_Handle & handle() const
const Extensions & extensions() const
static std::variant< Hello_Retry_Request, Server_Hello_13, Server_Hello_12_Shim > parse(std::span< const uint8_t > buf)
static const struct Botan::TLS::Server_Hello_13::Hello_Retry_Request_Tag as_hello_retry_request
static const struct Botan::TLS::Server_Hello_13::Hello_Retry_Request_Creation_Tag as_new_hello_retry_request
Server_Hello_13(std::unique_ptr< Server_Hello_Internal > data, Server_Hello_Tag tag=as_server_hello)
std::optional< Protocol_Version > random_signals_downgrade() const
static std::variant< Hello_Retry_Request, Server_Hello_13 > create(const Client_Hello_13 &ch, bool hello_retry_request_allowed, Session_Manager &session_mgr, Credentials_Manager &credentials_mgr, RandomNumberGenerator &rng, const Policy &policy, Callbacks &cb)
Protocol_Version selected_version() const final
static const struct Botan::TLS::Server_Hello_13::Server_Hello_Tag as_server_hello
Server_Hello(const Server_Hello &)=delete
Helper class to embody a session handle in all protocol versions.
typename as_wrapped_references< T >::type as_wrapped_references_t
detail::as_wrapped_references_t< Server_Handshake_13_Message > Server_Handshake_13_Message_Ref
std::variant< Server_Hello_13, Server_Hello_12_Shim, Hello_Retry_Request, Encrypted_Extensions, Certificate_13, Certificate_Request_13, Certificate_Verify_13, Finished_13 > Server_Handshake_13_Message
std::vector< uint8_t > Transcript_Hash
Definition tls_magic.h:97
detail::as_wrapped_references_t< Handshake_Message_13 > Handshake_Message_13_Ref
std::variant< Key_Update > Client_Post_Handshake_13_Message
std::variant< Client_Hello_13, Client_Hello_12_Shim, Certificate_13, Certificate_Verify_13, Finished_13 > Client_Handshake_13_Message
std::variant< Client_Hello_13, Client_Hello_12_Shim, Server_Hello_13, Server_Hello_12_Shim, Hello_Retry_Request, Encrypted_Extensions, Certificate_13, Certificate_Request_13, Certificate_Verify_13, Finished_13 > Handshake_Message_13
Strong< std::vector< uint8_t >, struct Ticket_Nonce_ > Ticket_Nonce
Used to derive the ticket's PSK from the resumption_master_secret.
Definition tls_magic.h:100
Group_Params Named_Group
detail::as_wrapped_references_t< Client_Handshake_13_Message > Client_Handshake_13_Message_Ref
std::variant< New_Session_Ticket_13, Key_Update > Post_Handshake_Message_13
std::variant< New_Session_Ticket_13, Key_Update > Server_Post_Handshake_13_Message
Strong< std::vector< uint8_t >, struct Opaque_Session_Handle_ > Opaque_Session_Handle
holds an opaque session handle as used in TLS 1.3 that could be either a ticket for stateless resumpt...
std::variant< std::reference_wrapper< AlternativeTs >... > type