Botan 3.12.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
28
30 public:
31 /**
32 * Creates a client hello which might optionally use the passed-in
33 * @p session for resumption. In that case, this will "extract" the
34 * master secret from the passed-in @p session.
35 */
36 Client_Hello_13(const Policy& policy,
37 Callbacks& cb,
39 std::string_view hostname,
40 const std::vector<std::string>& next_protocols,
41 std::optional<Session_with_Handle>& session,
42 std::vector<ExternalPSK> psks);
43
44 static std::variant<Client_Hello_13, Client_Hello_12_Shim> parse(const std::vector<uint8_t>& buf);
45
46 void retry(const Hello_Retry_Request& hrr,
47 const Transcript_Hash_State& transcript_hash_state,
48 Callbacks& cb,
50
51 /**
52 * Select the highest protocol version from the list of versions
53 * supported by the client. If no such version can be determined this
54 * returns std::nullopt.
55 */
56 std::optional<Protocol_Version> highest_supported_version(const Policy& policy) const;
57
58 /**
59 * This validates that a Client Hello received after sending a Hello
60 * Retry Request was updated in accordance with RFC 8446 4.1.2. If issues
61 * are found, this method throws accordingly.
62 */
63 void validate_updates(const Client_Hello_13& new_ch);
64
65 private:
66 explicit Client_Hello_13(std::unique_ptr<Client_Hello_Internal> data);
67
68 /**
69 * If the Client Hello contains a PSK extensions with identities this will
70 * generate the PSK binders as described in RFC 8446 4.2.11.2.
71 * Note that the passed in \p transcript_hash_state might be virgin for
72 * the initial Client Hello and should be primed with ClientHello1 and
73 * HelloRetryRequest for an updated Client Hello.
74 */
75 void calculate_psk_binders(Transcript_Hash_State transcript_hash_state);
76};
77
79
81 protected:
82 static const struct Server_Hello_Tag {
84
85 static const struct Hello_Retry_Request_Tag {
87
88 static const struct Hello_Retry_Request_Creation_Tag {
90
91 // These constructors are meant for instantiating Server Hellos
92 // after parsing a peer's message. They perform basic validation
93 // and are therefore not suitable for constructing a message to
94 // be sent to a client.
95 explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Server_Hello_Tag tag = as_server_hello);
96 explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Hello_Retry_Request_Tag tag);
97 void basic_validation() const;
98
99 // Instantiate a Server Hello as response to a client's Client Hello
100 // (called from Server_Hello_13::create())
102 std::optional<Named_Group> key_exchange_group,
103 Session_Manager& session_mgr,
104 Credentials_Manager& credentials_mgr,
106 Callbacks& cb,
107 const Policy& policy);
108
109 explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Hello_Retry_Request_Creation_Tag tag);
110
111 public:
112 static std::variant<Hello_Retry_Request, Server_Hello_13> create(const Client_Hello_13& ch,
113 bool hello_retry_request_allowed,
114 Session_Manager& session_mgr,
115 Credentials_Manager& credentials_mgr,
117 const Policy& policy,
118 Callbacks& cb);
119
120 static std::variant<Hello_Retry_Request, Server_Hello_13, Server_Hello_12_Shim> parse(
121 const std::vector<uint8_t>& buf);
122
123 /**
124 * Return desired downgrade version indicated by hello random, if any.
125 */
126 std::optional<Protocol_Version> random_signals_downgrade() const;
127
128 /**
129 * @returns the selected version as indicated by the supported_versions extension
130 */
132};
133
135 protected:
136 friend class Server_Hello_13; // to allow construction by Server_Hello_13::parse() and ::create()
137 explicit Hello_Retry_Request(std::unique_ptr<Server_Hello_Internal> data);
138 Hello_Retry_Request(const Client_Hello_13& ch, Named_Group selected_group, const Policy& policy, Callbacks& cb);
139
140 public:
142
144};
145
147 public:
148 explicit Encrypted_Extensions(const std::vector<uint8_t>& buf);
149 Encrypted_Extensions(const Client_Hello_13& client_hello,
150 const Policy& policy,
151 Callbacks& cb,
152 bool is_resumption,
153 bool requesting_client_auth);
154
156
157 const Extensions& extensions() const { return m_extensions; }
158
159 std::vector<uint8_t> serialize() const override;
160
161 private:
162 Extensions m_extensions;
163};
164
165class Certificate_Request_13;
166
167/**
168* Certificate Message of TLS 1.3
169*/
171 public:
173 public:
175 explicit Certificate_Entry(const X509_Certificate& cert);
176 explicit Certificate_Entry(std::shared_ptr<Public_Key> raw_public_key);
177
178 bool has_certificate() const { return m_certificate != nullptr; }
179
180 const X509_Certificate& certificate() const;
181 std::shared_ptr<const Public_Key> public_key() const;
182
183 std::vector<uint8_t> serialize() const;
184
185 Extensions& extensions() { return m_extensions; }
186
187 const Extensions& extensions() const { return m_extensions; }
188
189 Certificate_Entry(const Certificate_Entry& other) = delete;
191
194
196
197 private:
198 std::unique_ptr<X509_Certificate> m_certificate; // possibly null if raw public key in use
199 std::shared_ptr<Public_Key> m_raw_public_key;
200 Extensions m_extensions;
201 };
202
203 public:
205
206 std::vector<X509_Certificate> cert_chain() const;
207
208 bool has_certificate_chain() const;
209 bool is_raw_public_key() const;
210
211 size_t count() const { return m_entries.size(); }
212
213 bool empty() const { return m_entries.empty(); }
214
215 std::shared_ptr<const Public_Key> public_key() const;
216 const X509_Certificate& leaf() const;
217
218 const std::vector<uint8_t>& request_context() const { return m_request_context; }
219
220 /**
221 * Create a Client Certificate message
222 * ... in response to a Certificate Request message.
223 */
224 Certificate_13(const Certificate_Request_13& cert_request,
225 std::string_view hostname,
226 Credentials_Manager& credentials_manager,
227 Callbacks& callbacks,
228 Certificate_Type cert_type);
229
230 /**
231 * Create a Server Certificate message
232 * ... in response to a Client Hello indicating the need to authenticate
233 * with a server certificate.
234 */
235 Certificate_13(const Client_Hello_13& client_hello,
236 Credentials_Manager& credentials_manager,
237 Callbacks& callbacks,
238 Certificate_Type cert_type);
239
240 /**
241 * Deserialize a Certificate message
242 * @param buf the serialized message
243 * @param policy the TLS policy
244 * @param side is this a Connection_Side::Server or Connection_Side::Client certificate message
245 * @param cert_type is the certificate type that was negotiated during the handshake
246 */
247 Certificate_13(const std::vector<uint8_t>& buf,
248 const Policy& policy,
249 Connection_Side side,
250 Certificate_Type cert_type);
251
252 /**
253 * Validate a Certificate message regarding what extensions are expected based on
254 * previous handshake messages. Also call the tls_examine_extensions() callback
255 * for each entry.
256 *
257 * @param requested_extensions Extensions of Client_Hello or Certificate_Request messages
258 * @param cb Callback that will be called for each extension.
259 */
260 void validate_extensions(const std::set<Extension_Code>& requested_extensions, Callbacks& cb) const;
261
262 /**
263 * Verify the certificate chain
264 *
265 * @throws if verification fails.
266 */
267 void verify(Callbacks& callbacks,
268 const Policy& policy,
269 Credentials_Manager& creds,
270 std::string_view hostname,
271 bool use_ocsp) const;
272
273 std::vector<uint8_t> serialize() const override;
274
275 private:
276 void setup_entries(std::vector<X509_Certificate> cert_chain,
278 Callbacks& callbacks);
279 void setup_entry(std::shared_ptr<Public_Key> raw_public_key, Callbacks& callbacks);
280
281 void verify_certificate_chain(Callbacks& callbacks,
282 const Policy& policy,
283 Credentials_Manager& creds,
284 std::string_view hostname,
285 bool use_ocsp,
286 Usage_Type usage_type) const;
287
288 private:
289 std::vector<uint8_t> m_request_context;
290 std::vector<Certificate_Entry> m_entries;
291 Connection_Side m_side;
292};
293
295 public:
296 Handshake_Type type() const override;
297
298 Certificate_Request_13(const std::vector<uint8_t>& buf, Connection_Side side);
299
300 //! Creates a Certificate_Request message if it is required by the configuration
301 //! @return std::nullopt if configuration does not require client authentication
302 static std::optional<Certificate_Request_13> maybe_create(const Client_Hello_13& sni_hostname,
303 Credentials_Manager& cred_mgr,
304 Callbacks& callbacks,
305 const Policy& policy);
306
307 std::vector<X509_DN> acceptable_CAs() const;
308 const std::vector<Signature_Scheme>& signature_schemes() const;
309 const std::vector<Signature_Scheme>& certificate_signature_schemes() const;
310
311 const Extensions& extensions() const { return m_extensions; }
312
313 std::vector<uint8_t> serialize() const override;
314
315 const std::vector<uint8_t>& context() const { return m_context; }
316
317 private:
318 Certificate_Request_13(const std::vector<X509_DN>& acceptable_CAs, const Policy& policy, Callbacks& callbacks);
319
320 private:
321 std::vector<uint8_t> m_context;
322 Extensions m_extensions;
323};
324
325/**
326* Certificate Verify Message
327*/
329 public:
330 /**
331 * Deserialize a Certificate message
332 * @param buf the serialized message
333 * @param side is this a Connection_Side::Server or Connection_Side::Client certificate message
334 */
335 Certificate_Verify_13(const std::vector<uint8_t>& buf, Connection_Side side);
336
337 Certificate_Verify_13(const Certificate_13& certificate_message,
338 const std::vector<Signature_Scheme>& peer_allowed_schemes,
339 std::string_view hostname,
340 const Transcript_Hash& hash,
341 Connection_Side whoami,
342 Credentials_Manager& creds_mgr,
343 const Policy& policy,
344 Callbacks& callbacks,
346
347 bool verify(const Public_Key& public_key, Callbacks& callbacks, const Transcript_Hash& transcript_hash) const;
348
349 private:
350 Connection_Side m_side;
351};
352
354 public:
355 using Finished::Finished;
356 Finished_13(Cipher_State* cipher_state, const Transcript_Hash& transcript_hash);
357
358 bool verify(Cipher_State* cipher_state, const Transcript_Hash& transcript_hash) const;
359};
360
362 public:
364
366 const Session& session,
367 const Session_Handle& handle,
368 Callbacks& callbacks);
369
370 New_Session_Ticket_13(const std::vector<uint8_t>& buf, Connection_Side from);
371
372 std::vector<uint8_t> serialize() const override;
373
374 const Extensions& extensions() const { return m_extensions; }
375
376 const Opaque_Session_Handle& handle() const { return m_handle; }
377
378 const Ticket_Nonce& nonce() const { return m_ticket_nonce; }
379
380 uint32_t ticket_age_add() const { return m_ticket_age_add; }
381
382 std::chrono::seconds lifetime_hint() const { return m_ticket_lifetime_hint; }
383
384 /**
385 * @return the number of bytes allowed for early data or std::nullopt
386 * when early data is not allowed at all
387 */
388 std::optional<uint32_t> early_data_byte_limit() const;
389
390 private:
391 // RFC 8446 4.6.1
392 // Clients MUST NOT cache tickets for longer than 7 days, regardless of
393 // the ticket_lifetime, and MAY delete tickets earlier based on local
394 // policy. A server MAY treat a ticket as valid for a shorter period
395 // of time than what is stated in the ticket_lifetime.
396 //
397 // ... hence we call it 'lifetime hint'.
398 std::chrono::seconds m_ticket_lifetime_hint{};
399 uint32_t m_ticket_age_add;
400 Ticket_Nonce m_ticket_nonce;
401 Opaque_Session_Handle m_handle;
402 Extensions m_extensions;
403};
404
406 public:
407 Handshake_Type type() const override { return Handshake_Type::KeyUpdate; }
408
409 explicit Key_Update(bool request_peer_update);
410 explicit Key_Update(const std::vector<uint8_t>& buf);
411
412 std::vector<uint8_t> serialize() const override;
413
414 bool expects_reciprocation() const { return m_update_requested; }
415
416 private:
417 bool m_update_requested;
418};
419
420namespace detail {
421template <typename T>
423
424template <typename... AlternativeTs>
425struct as_wrapped_references<std::variant<AlternativeTs...>> {
426 using type = std::variant<std::reference_wrapper<AlternativeTs>...>;
427};
428
429template <typename T>
431} // namespace detail
432
433// Handshake message types from RFC 8446 4.
439 // End_Of_Early_Data,
446
447using Post_Handshake_Message_13 = std::variant<New_Session_Ticket_13, Key_Update>;
448
449// Key_Update is handled generically by the Channel. The messages assigned
450// to those variants are the ones that need to be handled by the specific
451// client and/or server implementations.
452using Server_Post_Handshake_13_Message = std::variant<New_Session_Ticket_13, Key_Update>;
453using Client_Post_Handshake_13_Message = std::variant<Key_Update>;
454
456 Server_Hello_12_Shim, // indicates a TLS version downgrade
464
466 std::variant<Client_Hello_13,
467 Client_Hello_12_Shim, // indicates a TLS peer that does not offer TLS 1.3
472
473} // namespace Botan::TLS
474
475#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
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
Certificate_Request_13(const std::vector< uint8_t > &buf, Connection_Side side)
bool verify(const Public_Key &public_key, Callbacks &callbacks, const Transcript_Hash &transcript_hash) const
Certificate_Verify_13(const std::vector< uint8_t > &buf, Connection_Side side)
Certificate_Verify(const std::vector< uint8_t > &buf)
void validate_updates(const Client_Hello_13 &new_ch)
static std::variant< Client_Hello_13, Client_Hello_12_Shim > parse(const std::vector< uint8_t > &buf)
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, const std::vector< std::string > &next_protocols, std::optional< Session_with_Handle > &session, std::vector< ExternalPSK > psks)
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
Handshake_Type type() const override
Encrypted_Extensions(const std::vector< uint8_t > &buf)
Finished(const std::vector< uint8_t > &buf)
bool verify(Cipher_State *cipher_state, const Transcript_Hash &transcript_hash) const
Finished_13(Cipher_State *cipher_state, const Transcript_Hash &transcript_hash)
Finished(const 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(const std::vector< 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:93
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:96
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