Botan 3.11.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, const Policy& policy, Callbacks& cb);
150
152
153 const Extensions& extensions() const { return m_extensions; }
154
155 std::vector<uint8_t> serialize() const override;
156
157 private:
158 Extensions m_extensions;
159};
160
161class Certificate_Request_13;
162
163/**
164* Certificate Message of TLS 1.3
165*/
167 public:
169 public:
171 explicit Certificate_Entry(const X509_Certificate& cert);
172 explicit Certificate_Entry(std::shared_ptr<Public_Key> raw_public_key);
173
174 bool has_certificate() const { return m_certificate != nullptr; }
175
176 const X509_Certificate& certificate() const;
177 std::shared_ptr<const Public_Key> public_key() const;
178
179 std::vector<uint8_t> serialize() const;
180
181 Extensions& extensions() { return m_extensions; }
182
183 const Extensions& extensions() const { return m_extensions; }
184
185 Certificate_Entry(const Certificate_Entry& other) = delete;
187
190
192
193 private:
194 std::unique_ptr<X509_Certificate> m_certificate; // possibly null if raw public key in use
195 std::shared_ptr<Public_Key> m_raw_public_key;
196 Extensions m_extensions;
197 };
198
199 public:
201
202 std::vector<X509_Certificate> cert_chain() const;
203
204 bool has_certificate_chain() const;
205 bool is_raw_public_key() const;
206
207 size_t count() const { return m_entries.size(); }
208
209 bool empty() const { return m_entries.empty(); }
210
211 std::shared_ptr<const Public_Key> public_key() const;
212 const X509_Certificate& leaf() const;
213
214 const std::vector<uint8_t>& request_context() const { return m_request_context; }
215
216 /**
217 * Create a Client Certificate message
218 * ... in response to a Certificate Request message.
219 */
220 Certificate_13(const Certificate_Request_13& cert_request,
221 std::string_view hostname,
222 Credentials_Manager& credentials_manager,
223 Callbacks& callbacks,
224 Certificate_Type cert_type);
225
226 /**
227 * Create a Server Certificate message
228 * ... in response to a Client Hello indicating the need to authenticate
229 * with a server certificate.
230 */
231 Certificate_13(const Client_Hello_13& client_hello,
232 Credentials_Manager& credentials_manager,
233 Callbacks& callbacks,
234 Certificate_Type cert_type);
235
236 /**
237 * Deserialize a Certificate message
238 * @param buf the serialized message
239 * @param policy the TLS policy
240 * @param side is this a Connection_Side::Server or Connection_Side::Client certificate message
241 * @param cert_type is the certificate type that was negotiated during the handshake
242 */
243 Certificate_13(const std::vector<uint8_t>& buf,
244 const Policy& policy,
245 Connection_Side side,
246 Certificate_Type cert_type);
247
248 /**
249 * Validate a Certificate message regarding what extensions are expected based on
250 * previous handshake messages. Also call the tls_examine_extensions() callback
251 * for each entry.
252 *
253 * @param requested_extensions Extensions of Client_Hello or Certificate_Request messages
254 * @param cb Callback that will be called for each extension.
255 */
256 void validate_extensions(const std::set<Extension_Code>& requested_extensions, Callbacks& cb) const;
257
258 /**
259 * Verify the certificate chain
260 *
261 * @throws if verification fails.
262 */
263 void verify(Callbacks& callbacks,
264 const Policy& policy,
265 Credentials_Manager& creds,
266 std::string_view hostname,
267 bool use_ocsp) const;
268
269 std::vector<uint8_t> serialize() const override;
270
271 private:
272 void setup_entries(std::vector<X509_Certificate> cert_chain,
274 Callbacks& callbacks);
275 void setup_entry(std::shared_ptr<Public_Key> raw_public_key, Callbacks& callbacks);
276
277 void verify_certificate_chain(Callbacks& callbacks,
278 const Policy& policy,
279 Credentials_Manager& creds,
280 std::string_view hostname,
281 bool use_ocsp,
282 Usage_Type usage_type) const;
283
284 private:
285 std::vector<uint8_t> m_request_context;
286 std::vector<Certificate_Entry> m_entries;
287 Connection_Side m_side;
288};
289
291 public:
292 Handshake_Type type() const override;
293
294 Certificate_Request_13(const std::vector<uint8_t>& buf, Connection_Side side);
295
296 //! Creates a Certificate_Request message if it is required by the configuration
297 //! @return std::nullopt if configuration does not require client authentication
298 static std::optional<Certificate_Request_13> maybe_create(const Client_Hello_13& sni_hostname,
299 Credentials_Manager& cred_mgr,
300 Callbacks& callbacks,
301 const Policy& policy);
302
303 std::vector<X509_DN> acceptable_CAs() const;
304 const std::vector<Signature_Scheme>& signature_schemes() const;
305 const std::vector<Signature_Scheme>& certificate_signature_schemes() const;
306
307 const Extensions& extensions() const { return m_extensions; }
308
309 std::vector<uint8_t> serialize() const override;
310
311 const std::vector<uint8_t>& context() const { return m_context; }
312
313 private:
314 Certificate_Request_13(const std::vector<X509_DN>& acceptable_CAs, const Policy& policy, Callbacks& callbacks);
315
316 private:
317 std::vector<uint8_t> m_context;
318 Extensions m_extensions;
319};
320
321/**
322* Certificate Verify Message
323*/
325 public:
326 /**
327 * Deserialize a Certificate message
328 * @param buf the serialized message
329 * @param side is this a Connection_Side::Server or Connection_Side::Client certificate message
330 */
331 Certificate_Verify_13(const std::vector<uint8_t>& buf, Connection_Side side);
332
333 Certificate_Verify_13(const Certificate_13& certificate_message,
334 const std::vector<Signature_Scheme>& peer_allowed_schemes,
335 std::string_view hostname,
336 const Transcript_Hash& hash,
337 Connection_Side whoami,
338 Credentials_Manager& creds_mgr,
339 const Policy& policy,
340 Callbacks& callbacks,
342
343 bool verify(const Public_Key& public_key, Callbacks& callbacks, const Transcript_Hash& transcript_hash) const;
344
345 private:
346 Connection_Side m_side;
347};
348
350 public:
351 using Finished::Finished;
352 Finished_13(Cipher_State* cipher_state, const Transcript_Hash& transcript_hash);
353
354 bool verify(Cipher_State* cipher_state, const Transcript_Hash& transcript_hash) const;
355};
356
358 public:
360
362 const Session& session,
363 const Session_Handle& handle,
364 Callbacks& callbacks);
365
366 New_Session_Ticket_13(const std::vector<uint8_t>& buf, Connection_Side from);
367
368 std::vector<uint8_t> serialize() const override;
369
370 const Extensions& extensions() const { return m_extensions; }
371
372 const Opaque_Session_Handle& handle() const { return m_handle; }
373
374 const Ticket_Nonce& nonce() const { return m_ticket_nonce; }
375
376 uint32_t ticket_age_add() const { return m_ticket_age_add; }
377
378 std::chrono::seconds lifetime_hint() const { return m_ticket_lifetime_hint; }
379
380 /**
381 * @return the number of bytes allowed for early data or std::nullopt
382 * when early data is not allowed at all
383 */
384 std::optional<uint32_t> early_data_byte_limit() const;
385
386 private:
387 // RFC 8446 4.6.1
388 // Clients MUST NOT cache tickets for longer than 7 days, regardless of
389 // the ticket_lifetime, and MAY delete tickets earlier based on local
390 // policy. A server MAY treat a ticket as valid for a shorter period
391 // of time than what is stated in the ticket_lifetime.
392 //
393 // ... hence we call it 'lifetime hint'.
394 std::chrono::seconds m_ticket_lifetime_hint{};
395 uint32_t m_ticket_age_add;
396 Ticket_Nonce m_ticket_nonce;
397 Opaque_Session_Handle m_handle;
398 Extensions m_extensions;
399};
400
402 public:
403 Handshake_Type type() const override { return Handshake_Type::KeyUpdate; }
404
405 explicit Key_Update(bool request_peer_update);
406 explicit Key_Update(const std::vector<uint8_t>& buf);
407
408 std::vector<uint8_t> serialize() const override;
409
410 bool expects_reciprocation() const { return m_update_requested; }
411
412 private:
413 bool m_update_requested;
414};
415
416namespace detail {
417template <typename T>
419
420template <typename... AlternativeTs>
421struct as_wrapped_references<std::variant<AlternativeTs...>> {
422 using type = std::variant<std::reference_wrapper<AlternativeTs>...>;
423};
424
425template <typename T>
427} // namespace detail
428
429// Handshake message types from RFC 8446 4.
435 // End_Of_Early_Data,
442
443using Post_Handshake_Message_13 = std::variant<New_Session_Ticket_13, Key_Update>;
444
445// Key_Update is handled generically by the Channel. The messages assigned
446// to those variants are the ones that need to be handled by the specific
447// client and/or server implementations.
448using Server_Post_Handshake_13_Message = std::variant<New_Session_Ticket_13, Key_Update>;
449using Client_Post_Handshake_13_Message = std::variant<Key_Update>;
450
452 Server_Hello_12_Shim, // indicates a TLS version downgrade
460
462 std::variant<Client_Hello_13,
463 Client_Hello_12_Shim, // indicates a TLS peer that does not offer TLS 1.3
468
469} // namespace Botan::TLS
470
471#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
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
detail::as_wrapped_references_t< Server_Handshake_13_Message > Server_Handshake_13_Message_Ref
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
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
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
detail::as_wrapped_references_t< Client_Handshake_13_Message > Client_Handshake_13_Message_Ref
std::variant< Client_Hello_13, Client_Hello_12_Shim, Certificate_13, Certificate_Verify_13, Finished_13 > Client_Handshake_13_Message
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