Botan 3.4.0
Crypto and TLS for C&
tls_messages.h
Go to the documentation of this file.
1/*
2* TLS Messages
3* (C) 2004-2011,2015 Jack Lloyd
4* 2016 Matthias Gierlings
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_TLS_MESSAGES_H_
10#define BOTAN_TLS_MESSAGES_H_
11
12#include <chrono>
13#include <memory>
14#include <optional>
15#include <set>
16#include <string>
17#include <variant>
18#include <vector>
19
20#include <botan/pk_keys.h>
21#include <botan/strong_type.h>
22#include <botan/tls_ciphersuite.h>
23#include <botan/tls_extensions.h>
24#include <botan/tls_handshake_msg.h>
25#include <botan/tls_policy.h>
26#include <botan/tls_session.h>
27#include <botan/x509cert.h>
28
29namespace Botan {
30
31class Public_Key;
32class Credentials_Manager;
33
34namespace OCSP {
35class Response;
36}
37
38namespace TLS {
39
40class Session_Manager;
41class Handshake_IO;
42class Handshake_State;
43class Hello_Retry_Request;
44class Callbacks;
45class Cipher_State;
46class Policy;
47
48std::vector<uint8_t> make_hello_random(RandomNumberGenerator& rng, Callbacks& cb, const Policy& policy);
49
50/**
51* DTLS Hello Verify Request
52*/
54 public:
55 std::vector<uint8_t> serialize() const override;
56
57 Handshake_Type type() const override { return Handshake_Type::HelloVerifyRequest; }
58
59 const std::vector<uint8_t>& cookie() const { return m_cookie; }
60
61 explicit Hello_Verify_Request(const std::vector<uint8_t>& buf);
62
63 Hello_Verify_Request(const std::vector<uint8_t>& client_hello_bits,
64 std::string_view client_identity,
65 const SymmetricKey& secret_key);
66
67 private:
68 std::vector<uint8_t> m_cookie;
69};
70
71class Client_Hello_Internal;
72
73/**
74* Client Hello Message
75*/
77 public:
78 Client_Hello(const Client_Hello&) = delete;
81 Client_Hello& operator=(Client_Hello&&) noexcept;
82
83 ~Client_Hello() override;
84
85 Handshake_Type type() const override;
86
87 /**
88 * Return the version indicated in the ClientHello.
89 * This may differ from the version indicated in the supported_versions extension.
90 *
91 * See RFC 8446 4.1.2:
92 * TLS 1.3, the client indicates its version preferences in the
93 * "supported_versions" extension (Section 4.2.1) and the
94 * legacy_version field MUST be set to 0x0303, which is the version
95 * number for TLS 1.2.
96 */
97 Protocol_Version legacy_version() const;
98
99 const std::vector<uint8_t>& random() const;
100
101 const Session_ID& session_id() const;
102
103 const std::vector<uint16_t>& ciphersuites() const;
104
105 bool offered_suite(uint16_t ciphersuite) const;
106
107 std::vector<Signature_Scheme> signature_schemes() const;
108 std::vector<Signature_Scheme> certificate_signature_schemes() const;
109
110 std::vector<Group_Params> supported_ecc_curves() const;
111
112 // This returns any groups in the FFDHE range
113 std::vector<Group_Params> supported_dh_groups() const;
114
115 std::vector<Protocol_Version> supported_versions() const;
116
117 std::string sni_hostname() const;
118
119 bool supports_alpn() const;
120
121 bool sent_signature_algorithms() const;
122
123 std::vector<std::string> next_protocols() const;
124
125 std::vector<uint16_t> srtp_profiles() const;
126
127 std::vector<uint8_t> serialize() const override;
128
129 const std::vector<uint8_t>& cookie() const;
130
131 std::vector<uint8_t> cookie_input_data() const;
132
133 std::set<Extension_Code> extension_types() const;
134
135 const Extensions& extensions() const;
136
137 protected:
138 Client_Hello();
139 explicit Client_Hello(std::unique_ptr<Client_Hello_Internal> data);
140
141 const std::vector<uint8_t>& compression_methods() const;
142
143 protected:
144 std::unique_ptr<Client_Hello_Internal> m_data;
145};
146
148 public:
150 public:
151 Settings(const Protocol_Version version, std::string_view hostname = "") :
152 m_new_session_version(version), m_hostname(hostname) {}
153
154 Protocol_Version protocol_version() const { return m_new_session_version; }
155
156 const std::string& hostname() const { return m_hostname; }
157
158 private:
159 const Protocol_Version m_new_session_version;
160 const std::string m_hostname;
161 };
162
163 public:
164 explicit Client_Hello_12(const std::vector<uint8_t>& buf);
165
167 Handshake_Hash& hash,
168 const Policy& policy,
169 Callbacks& cb,
171 const std::vector<uint8_t>& reneg_info,
172 const Settings& client_settings,
173 const std::vector<std::string>& next_protocols);
174
176 Handshake_Hash& hash,
177 const Policy& policy,
178 Callbacks& cb,
180 const std::vector<uint8_t>& reneg_info,
181 const Session_with_Handle& session_and_handle,
182 const std::vector<std::string>& next_protocols);
183
184 protected:
185 friend class Client_Hello_13; // to allow construction by Client_Hello_13::parse()
186 Client_Hello_12(std::unique_ptr<Client_Hello_Internal> data);
187
188 public:
189 using Client_Hello::compression_methods;
190 using Client_Hello::random;
191
192 bool prefers_compressed_ec_points() const;
193
194 bool secure_renegotiation() const;
195
196 std::vector<uint8_t> renegotiation_info() const;
197
198 bool supports_session_ticket() const;
199
200 Session_Ticket session_ticket() const;
201
202 std::optional<Session_Handle> session_handle() const;
203
204 bool supports_extended_master_secret() const;
205
206 bool supports_cert_status_message() const;
207
208 bool supports_encrypt_then_mac() const;
209
210 void update_hello_cookie(const Hello_Verify_Request& hello_verify);
211
212 private:
213 void add_tls12_supported_groups_extensions(const Policy& policy);
214};
215
216#if defined(BOTAN_HAS_TLS_13)
217
219 public:
220 /**
221 * Creates a client hello which might optionally use the passed-in
222 * @p session for resumption. In that case, this will "extract" the
223 * master secret from the passed-in @p session.
224 */
225 Client_Hello_13(const Policy& policy,
226 Callbacks& cb,
228 std::string_view hostname,
229 const std::vector<std::string>& next_protocols,
230 std::optional<Session_with_Handle>& session,
231 std::vector<ExternalPSK> psks);
232
233 static std::variant<Client_Hello_13, Client_Hello_12> parse(const std::vector<uint8_t>& buf);
234
235 void retry(const Hello_Retry_Request& hrr,
236 const Transcript_Hash_State& transcript_hash_state,
237 Callbacks& cb,
239
240 /**
241 * Select the highest protocol version from the list of versions
242 * supported by the client. If no such version can be determind this
243 * returns std::nullopt.
244 */
245 std::optional<Protocol_Version> highest_supported_version(const Policy& policy) const;
246
247 /**
248 * This validates that a Client Hello received after sending a Hello
249 * Retry Request was updated in accordance with RFC 8446 4.1.2. If issues
250 * are found, this method throws accordingly.
251 */
252 void validate_updates(const Client_Hello_13& new_ch);
253
254 private:
255 Client_Hello_13(std::unique_ptr<Client_Hello_Internal> data);
256
257 /**
258 * If the Client Hello contains a PSK extensions with identities this will
259 * generate the PSK binders as described in RFC 8446 4.2.11.2.
260 * Note that the passed in \p transcript_hash_state might be virgin for
261 * the initial Client Hello and should be primed with ClientHello1 and
262 * HelloRetryRequest for an updated Client Hello.
263 */
264 void calculate_psk_binders(Transcript_Hash_State transcript_hash_state);
265};
266
267#endif // BOTAN_HAS_TLS_13
268
269class Server_Hello_Internal;
270
271/**
272* Server Hello Message
273*/
275 public:
276 Server_Hello(const Server_Hello&) = delete;
279 Server_Hello& operator=(Server_Hello&&) noexcept;
280
281 ~Server_Hello() override;
282
283 std::vector<uint8_t> serialize() const override;
284
285 Handshake_Type type() const override;
286
287 // methods available in both subclasses' interface
288 uint16_t ciphersuite() const;
289 const Extensions& extensions() const;
290 const Session_ID& session_id() const;
291
292 virtual Protocol_Version selected_version() const = 0;
293
294 protected:
295 explicit Server_Hello(std::unique_ptr<Server_Hello_Internal> data);
296
297 // methods used internally and potentially exposed by one of the subclasses
298 std::set<Extension_Code> extension_types() const;
299 const std::vector<uint8_t>& random() const;
300 uint8_t compression_method() const;
301 Protocol_Version legacy_version() const;
302
303 protected:
304 std::unique_ptr<Server_Hello_Internal> m_data;
305};
306
308 public:
310 public:
311 Settings(Session_ID new_session_id,
312 Protocol_Version new_session_version,
313 uint16_t ciphersuite,
314 bool offer_session_ticket) :
315 m_new_session_id(std::move(new_session_id)),
316 m_new_session_version(new_session_version),
317 m_ciphersuite(ciphersuite),
318 m_offer_session_ticket(offer_session_ticket) {}
319
320 const Session_ID& session_id() const { return m_new_session_id; }
321
322 Protocol_Version protocol_version() const { return m_new_session_version; }
323
324 uint16_t ciphersuite() const { return m_ciphersuite; }
325
326 bool offer_session_ticket() const { return m_offer_session_ticket; }
327
328 private:
329 const Session_ID m_new_session_id;
330 Protocol_Version m_new_session_version;
331 uint16_t m_ciphersuite;
332 bool m_offer_session_ticket;
333 };
334
336 Handshake_Hash& hash,
337 const Policy& policy,
338 Callbacks& cb,
340 const std::vector<uint8_t>& secure_reneg_info,
341 const Client_Hello_12& client_hello,
342 const Settings& settings,
343 std::string_view next_protocol);
344
346 Handshake_Hash& hash,
347 const Policy& policy,
348 Callbacks& cb,
350 const std::vector<uint8_t>& secure_reneg_info,
351 const Client_Hello_12& client_hello,
352 const Session& resumed_session,
353 bool offer_session_ticket,
354 std::string_view next_protocol);
355
356 explicit Server_Hello_12(const std::vector<uint8_t>& buf);
357
358 protected:
359 friend class Server_Hello_13; // to allow construction by Server_Hello_13::parse()
360 explicit Server_Hello_12(std::unique_ptr<Server_Hello_Internal> data);
361
362 public:
363 using Server_Hello::compression_method;
364 using Server_Hello::extension_types;
365 using Server_Hello::legacy_version;
366 using Server_Hello::random;
367
368 /**
369 * @returns the selected version as indicated in the legacy_version field
370 */
371 Protocol_Version selected_version() const override;
372
373 bool secure_renegotiation() const;
374
375 std::vector<uint8_t> renegotiation_info() const;
376
377 std::string next_protocol() const;
378
379 bool supports_extended_master_secret() const;
380
381 bool supports_encrypt_then_mac() const;
382
383 bool supports_certificate_status_message() const;
384
385 bool supports_session_ticket() const;
386
387 uint16_t srtp_profile() const;
388 bool prefers_compressed_ec_points() const;
389
390 /**
391 * Return desired downgrade version indicated by hello random, if any.
392 */
393 std::optional<Protocol_Version> random_signals_downgrade() const;
394};
395
396#if defined(BOTAN_HAS_TLS_13)
397
399
401 protected:
402 static const struct Server_Hello_Tag {
403 } as_server_hello;
404
405 static const struct Hello_Retry_Request_Tag {
406 } as_hello_retry_request;
407
408 static const struct Hello_Retry_Request_Creation_Tag {
409 } as_new_hello_retry_request;
410
411 // These constructors are meant for instantiating Server Hellos
412 // after parsing a peer's message. They perform basic validation
413 // and are therefore not suitable for constructing a message to
414 // be sent to a client.
415 explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Server_Hello_Tag tag = as_server_hello);
416 explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Hello_Retry_Request_Tag tag);
417 void basic_validation() const;
418
419 // Instantiate a Server Hello as response to a client's Client Hello
420 // (called from Server_Hello_13::create())
422 std::optional<Named_Group> key_exchange_group,
423 Session_Manager& session_mgr,
424 Credentials_Manager& credentials_mgr,
426 Callbacks& cb,
427 const Policy& policy);
428
429 explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Hello_Retry_Request_Creation_Tag tag);
430
431 public:
432 static std::variant<Hello_Retry_Request, Server_Hello_13> create(const Client_Hello_13& ch,
433 bool hello_retry_request_allowed,
434 Session_Manager& session_mgr,
435 Credentials_Manager& credentials_mgr,
437 const Policy& policy,
438 Callbacks& cb);
439
440 static std::variant<Hello_Retry_Request, Server_Hello_13, Server_Hello_12> parse(const std::vector<uint8_t>& buf);
441
442 /**
443 * Return desired downgrade version indicated by hello random, if any.
444 */
445 std::optional<Protocol_Version> random_signals_downgrade() const;
446
447 /**
448 * @returns the selected version as indicated by the supported_versions extension
449 */
450 Protocol_Version selected_version() const final;
451};
452
454 protected:
455 friend class Server_Hello_13; // to allow construction by Server_Hello_13::parse() and ::create()
456 explicit Hello_Retry_Request(std::unique_ptr<Server_Hello_Internal> data);
457 Hello_Retry_Request(const Client_Hello_13& ch, Named_Group selected_group, const Policy& policy, Callbacks& cb);
458
459 public:
460 Handshake_Type type() const override { return Handshake_Type::HelloRetryRequest; }
461
462 Handshake_Type wire_type() const override { return Handshake_Type::ServerHello; }
463};
464
466 public:
467 explicit Encrypted_Extensions(const std::vector<uint8_t>& buf);
468 Encrypted_Extensions(const Client_Hello_13& client_hello, const Policy& policy, Callbacks& cb);
469
470 Handshake_Type type() const override { return Handshake_Type::EncryptedExtensions; }
471
472 const Extensions& extensions() const { return m_extensions; }
473
474 std::vector<uint8_t> serialize() const override;
475
476 private:
477 Extensions m_extensions;
478};
479
480#endif // BOTAN_HAS_TLS_13
481
482/**
483* Client Key Exchange Message
484*/
486 public:
487 Handshake_Type type() const override { return Handshake_Type::ClientKeyExchange; }
488
489 const secure_vector<uint8_t>& pre_master_secret() const { return m_pre_master; }
490
491 /**
492 * @returns the agreed upon PSK identity or std::nullopt if not applicable
493 */
494 const std::optional<std::string>& psk_identity() const { return m_psk_identity; }
495
497 Handshake_State& state,
498 const Policy& policy,
499 Credentials_Manager& creds,
500 const Public_Key* server_public_key,
501 std::string_view hostname,
503
504 Client_Key_Exchange(const std::vector<uint8_t>& buf,
505 const Handshake_State& state,
506 const Private_Key* server_rsa_kex_key,
507 Credentials_Manager& creds,
508 const Policy& policy,
510
511 private:
512 std::vector<uint8_t> serialize() const override { return m_key_material; }
513
514 std::vector<uint8_t> m_key_material;
515 secure_vector<uint8_t> m_pre_master;
516 std::optional<std::string> m_psk_identity;
517};
518
519/**
520* Certificate Message of TLS 1.2
521*/
523 public:
524 Handshake_Type type() const override { return Handshake_Type::Certificate; }
525
526 const std::vector<X509_Certificate>& cert_chain() const { return m_certs; }
527
528 size_t count() const { return m_certs.size(); }
529
530 bool empty() const { return m_certs.empty(); }
531
532 Certificate_12(Handshake_IO& io, Handshake_Hash& hash, const std::vector<X509_Certificate>& certs);
533
534 Certificate_12(const std::vector<uint8_t>& buf, const Policy& policy);
535
536 std::vector<uint8_t> serialize() const override;
537
538 private:
539 std::vector<X509_Certificate> m_certs;
540};
541
542#if defined(BOTAN_HAS_TLS_13)
543
544class Certificate_Request_13;
545
546/**
547* Certificate Message of TLS 1.3
548*/
550 public:
552 public:
553 Certificate_Entry(TLS_Data_Reader& reader, const Connection_Side side, const Certificate_Type cert_type);
555 Certificate_Entry(std::shared_ptr<Public_Key> raw_public_key);
556
557 bool has_certificate() const { return m_certificate.has_value(); }
558
559 const X509_Certificate& certificate() const;
560 std::shared_ptr<const Public_Key> public_key() const;
561
562 std::vector<uint8_t> serialize() const;
563
564 Extensions& extensions() { return m_extensions; }
565
566 const Extensions& extensions() const { return m_extensions; }
567
568 private:
569 std::optional<X509_Certificate> m_certificate;
570 std::shared_ptr<Public_Key> m_raw_public_key;
571 Extensions m_extensions;
572 };
573
574 public:
575 Handshake_Type type() const override { return Handshake_Type::Certificate; }
576
577 std::vector<X509_Certificate> cert_chain() const;
578
579 bool has_certificate_chain() const;
580 bool is_raw_public_key() const;
581
582 size_t count() const { return m_entries.size(); }
583
584 bool empty() const { return m_entries.empty(); }
585
586 std::shared_ptr<const Public_Key> public_key() const;
587 const X509_Certificate& leaf() const;
588
589 const std::vector<uint8_t>& request_context() const { return m_request_context; }
590
591 /**
592 * Create a Client Certificate message
593 * ... in response to a Certificate Request message.
594 */
595 Certificate_13(const Certificate_Request_13& cert_request,
596 std::string_view hostname,
597 Credentials_Manager& credentials_manager,
598 Callbacks& callbacks,
599 Certificate_Type cert_type);
600
601 /**
602 * Create a Server Certificate message
603 * ... in response to a Client Hello indicating the need to authenticate
604 * with a server certificate.
605 */
606 Certificate_13(const Client_Hello_13& client_hello,
607 Credentials_Manager& credentials_manager,
608 Callbacks& callbacks,
609 Certificate_Type cert_type);
610
611 /**
612 * Deserialize a Certificate message
613 * @param buf the serialized message
614 * @param policy the TLS policy
615 * @param side is this a Connection_Side::Server or Connection_Side::Client certificate message
616 * @param cert_type is the certificate type that was negotiated during the handshake
617 */
618 Certificate_13(const std::vector<uint8_t>& buf,
619 const Policy& policy,
620 Connection_Side side,
621 Certificate_Type cert_type);
622
623 /**
624 * Validate a Certificate message regarding what extensions are expected based on
625 * previous handshake messages. Also call the tls_examine_extenions() callback
626 * for each entry.
627 *
628 * @param requested_extensions Extensions of Client_Hello or Certificate_Request messages
629 * @param cb Callback that will be called for each extension.
630 */
631 void validate_extensions(const std::set<Extension_Code>& requested_extensions, Callbacks& cb) const;
632
633 /**
634 * Verify the certificate chain
635 *
636 * @throws if verification fails.
637 */
638 void verify(Callbacks& callbacks,
639 const Policy& policy,
640 Credentials_Manager& creds,
641 std::string_view hostname,
642 bool use_ocsp) const;
643
644 std::vector<uint8_t> serialize() const override;
645
646 private:
647 void setup_entries(std::vector<X509_Certificate> cert_chain,
649 Callbacks& callbacks);
650 void setup_entry(std::shared_ptr<Public_Key> raw_public_key, Callbacks& callbacks);
651
652 void verify_certificate_chain(Callbacks& callbacks,
653 const Policy& policy,
654 Credentials_Manager& creds,
655 std::string_view hostname,
656 bool use_ocsp,
657 Usage_Type usage_type) const;
658
659 private:
660 std::vector<uint8_t> m_request_context;
661 std::vector<Certificate_Entry> m_entries;
662 Connection_Side m_side;
663};
664
665#endif // BOTAN_HAS_TLS_13
666
667/**
668* Certificate Status (RFC 6066)
669*/
671 public:
672 Handshake_Type type() const override { return Handshake_Type::CertificateStatus; }
673
674 //std::shared_ptr<const OCSP::Response> response() const { return m_response; }
675
676 const std::vector<uint8_t>& response() const { return m_response; }
677
678 explicit Certificate_Status(const std::vector<uint8_t>& buf, Connection_Side from);
679
681
682 /*
683 * Create a Certificate_Status message using an already DER encoded OCSP response.
684 */
685 Certificate_Status(Handshake_IO& io, Handshake_Hash& hash, std::vector<uint8_t> raw_response_bytes);
686
687 Certificate_Status(std::vector<uint8_t> raw_response_bytes);
688
689 std::vector<uint8_t> serialize() const override;
690
691 private:
692 std::vector<uint8_t> m_response;
693};
694
695/**
696* Certificate Request Message (TLS 1.2)
697*/
699 public:
700 Handshake_Type type() const override;
701
702 const std::vector<std::string>& acceptable_cert_types() const;
703
704 const std::vector<X509_DN>& acceptable_CAs() const;
705
706 const std::vector<Signature_Scheme>& signature_schemes() const;
707
709 Handshake_Hash& hash,
710 const Policy& policy,
711 const std::vector<X509_DN>& allowed_cas);
712
713 explicit Certificate_Request_12(const std::vector<uint8_t>& buf);
714
715 std::vector<uint8_t> serialize() const override;
716
717 private:
718 std::vector<X509_DN> m_names;
719 std::vector<std::string> m_cert_key_types;
720 std::vector<Signature_Scheme> m_schemes;
721};
722
723#if defined(BOTAN_HAS_TLS_13)
724
726 public:
727 Handshake_Type type() const override;
728
729 Certificate_Request_13(const std::vector<uint8_t>& buf, Connection_Side side);
730
731 //! Creates a Certificate_Request message if it is required by the configuration
732 //! @return std::nullopt if configuration does not require client authentication
733 static std::optional<Certificate_Request_13> maybe_create(const Client_Hello_13& sni_hostname,
734 Credentials_Manager& cred_mgr,
735 Callbacks& callbacks,
736 const Policy& policy);
737
738 std::vector<X509_DN> acceptable_CAs() const;
739 const std::vector<Signature_Scheme>& signature_schemes() const;
740 const std::vector<Signature_Scheme>& certificate_signature_schemes() const;
741
742 const Extensions& extensions() const { return m_extensions; }
743
744 std::vector<uint8_t> serialize() const override;
745
746 const std::vector<uint8_t>& context() const { return m_context; }
747
748 private:
749 Certificate_Request_13(std::vector<X509_DN> acceptable_CAs, const Policy& policy, Callbacks& callbacks);
750
751 private:
752 std::vector<uint8_t> m_context;
753 Extensions m_extensions;
754};
755
756#endif
757
759 public:
760 Handshake_Type type() const override { return Handshake_Type::CertificateVerify; }
761
762 Signature_Scheme signature_scheme() const { return m_scheme; }
763
764 Certificate_Verify(const std::vector<uint8_t>& buf);
766
767 std::vector<uint8_t> serialize() const override;
768
769 protected:
770 std::vector<uint8_t> m_signature;
772};
773
774/**
775* Certificate Verify Message
776*/
778 public:
779 using Certificate_Verify::Certificate_Verify;
780
782 Handshake_State& state,
783 const Policy& policy,
785 const Private_Key* key);
786
787 /**
788 * Check the signature on a certificate verify message
789 * @param cert the purported certificate
790 * @param state the handshake state
791 * @param policy the TLS policy
792 */
793 bool verify(const X509_Certificate& cert, const Handshake_State& state, const Policy& policy) const;
794};
795
796#if defined(BOTAN_HAS_TLS_13)
797
798/**
799* Certificate Verify Message
800*/
802 public:
803 /**
804 * Deserialize a Certificate message
805 * @param buf the serialized message
806 * @param side is this a Connection_Side::Server or Connection_Side::Client certificate message
807 */
808 Certificate_Verify_13(const std::vector<uint8_t>& buf, Connection_Side side);
809
810 Certificate_Verify_13(const Certificate_13& certificate_message,
811 const std::vector<Signature_Scheme>& peer_allowed_schemes,
812 std::string_view hostname,
813 const Transcript_Hash& hash,
814 Connection_Side whoami,
815 Credentials_Manager& creds_mgr,
816 const Policy& policy,
817 Callbacks& callbacks,
819
820 bool verify(const Public_Key& public_key, Callbacks& callbacks, const Transcript_Hash& transcript_hash) const;
821
822 private:
823 Connection_Side m_side;
824};
825
826#endif
827
828/**
829* Finished Message
830*/
832 public:
833 explicit Finished(const std::vector<uint8_t>& buf);
834
835 Handshake_Type type() const override { return Handshake_Type::Finished; }
836
837 std::vector<uint8_t> verify_data() const;
838
839 std::vector<uint8_t> serialize() const override;
840
841 protected:
842 using Handshake_Message::Handshake_Message;
843 std::vector<uint8_t> m_verification_data;
844};
845
847 public:
848 using Finished::Finished;
850
851 bool verify(const Handshake_State& state, Connection_Side side) const;
852};
853
854#if defined(BOTAN_HAS_TLS_13)
856 public:
857 using Finished::Finished;
858 Finished_13(Cipher_State* cipher_state, const Transcript_Hash& transcript_hash);
859
860 bool verify(Cipher_State* cipher_state, const Transcript_Hash& transcript_hash) const;
861};
862#endif
863
864/**
865* Hello Request Message
866*/
868 public:
869 Handshake_Type type() const override { return Handshake_Type::HelloRequest; }
870
871 explicit Hello_Request(Handshake_IO& io);
872 explicit Hello_Request(const std::vector<uint8_t>& buf);
873
874 private:
875 std::vector<uint8_t> serialize() const override;
876};
877
878/**
879* Server Key Exchange Message
880*/
882 public:
883 Handshake_Type type() const override { return Handshake_Type::ServerKeyExchange; }
884
885 const std::vector<uint8_t>& params() const { return m_params; }
886
887 bool verify(const Public_Key& server_key, const Handshake_State& state, const Policy& policy) const;
888
889 // Only valid for certain kex types
890 const PK_Key_Agreement_Key& server_kex_key() const;
891
892 /**
893 * @returns the agreed upon KEX group or std::nullopt if the KEX type does
894 * not depend on a group
895 */
896 const std::optional<Group_Params>& shared_group() const { return m_shared_group; }
897
899 Handshake_State& state,
900 const Policy& policy,
901 Credentials_Manager& creds,
903 const Private_Key* signing_key = nullptr);
904
905 Server_Key_Exchange(const std::vector<uint8_t>& buf,
906 Kex_Algo kex_alg,
907 Auth_Method sig_alg,
908 Protocol_Version version);
909
910 private:
911 std::vector<uint8_t> serialize() const override;
912
913 std::unique_ptr<PK_Key_Agreement_Key> m_kex_key;
914 std::optional<Group_Params> m_shared_group;
915
916 std::vector<uint8_t> m_params;
917
918 std::vector<uint8_t> m_signature;
919 Signature_Scheme m_scheme;
920};
921
922/**
923* Server Hello Done Message
924*/
926 public:
927 Handshake_Type type() const override { return Handshake_Type::ServerHelloDone; }
928
929 explicit Server_Hello_Done(Handshake_IO& io, Handshake_Hash& hash);
930 explicit Server_Hello_Done(const std::vector<uint8_t>& buf);
931
932 private:
933 std::vector<uint8_t> serialize() const override;
934};
935
936/**
937* New Session Ticket Message
938*/
940 public:
941 Handshake_Type type() const override { return Handshake_Type::NewSessionTicket; }
942
943 std::chrono::seconds ticket_lifetime_hint() const { return m_ticket_lifetime_hint; }
944
945 const Session_Ticket& ticket() const { return m_ticket; }
946
948 Handshake_Hash& hash,
949 Session_Ticket ticket,
950 std::chrono::seconds lifetime);
951
953
954 explicit New_Session_Ticket_12(const std::vector<uint8_t>& buf);
955
956 std::vector<uint8_t> serialize() const override;
957
958 private:
959 std::chrono::seconds m_ticket_lifetime_hint;
960 Session_Ticket m_ticket;
961};
962
963#if defined(BOTAN_HAS_TLS_13)
964
965/// @brief Used to derive the ticket's PSK from the resumption_master_secret
966using Ticket_Nonce = Strong<std::vector<uint8_t>, struct Ticket_Nonce_>;
967
969 public:
970 Handshake_Type type() const override { return Handshake_Type::NewSessionTicket; }
971
973 const Session& session,
974 const Session_Handle& handle,
975 Callbacks& callbacks);
976
977 New_Session_Ticket_13(const std::vector<uint8_t>& buf, Connection_Side from);
978
979 std::vector<uint8_t> serialize() const override;
980
981 const Extensions& extensions() const { return m_extensions; }
982
983 const Opaque_Session_Handle& handle() const { return m_handle; }
984
985 const Ticket_Nonce& nonce() const { return m_ticket_nonce; }
986
987 uint32_t ticket_age_add() const { return m_ticket_age_add; }
988
989 std::chrono::seconds lifetime_hint() const { return m_ticket_lifetime_hint; }
990
991 /**
992 * @return the number of bytes allowed for early data or std::nullopt
993 * when early data is not allowed at all
994 */
995 std::optional<uint32_t> early_data_byte_limit() const;
996
997 private:
998 // RFC 8446 4.6.1
999 // Clients MUST NOT cache tickets for longer than 7 days, regardless of
1000 // the ticket_lifetime, and MAY delete tickets earlier based on local
1001 // policy. A server MAY treat a ticket as valid for a shorter period
1002 // of time than what is stated in the ticket_lifetime.
1003 //
1004 // ... hence we call it 'lifetime hint'.
1005 std::chrono::seconds m_ticket_lifetime_hint;
1006 uint32_t m_ticket_age_add;
1007 Ticket_Nonce m_ticket_nonce;
1008 Opaque_Session_Handle m_handle;
1009 Extensions m_extensions;
1010};
1011
1012#endif
1013
1014/**
1015* Change Cipher Spec
1016*/
1018 public:
1019 Handshake_Type type() const override { return Handshake_Type::HandshakeCCS; }
1020
1021 std::vector<uint8_t> serialize() const override { return std::vector<uint8_t>(1, 1); }
1022};
1023
1024#if defined(BOTAN_HAS_TLS_13)
1025
1027 public:
1028 Handshake_Type type() const override { return Handshake_Type::KeyUpdate; }
1029
1030 explicit Key_Update(bool request_peer_update);
1031 explicit Key_Update(const std::vector<uint8_t>& buf);
1032
1033 std::vector<uint8_t> serialize() const override;
1034
1035 bool expects_reciprocation() const { return m_update_requested; }
1036
1037 private:
1038 bool m_update_requested;
1039};
1040
1041namespace {
1042template <typename T>
1043struct as_wrapped_references {};
1044
1045template <typename... AlternativeTs>
1046struct as_wrapped_references<std::variant<AlternativeTs...>> {
1047 using type = std::variant<std::reference_wrapper<AlternativeTs>...>;
1048};
1049
1050template <typename T>
1051using as_wrapped_references_t = typename as_wrapped_references<T>::type;
1052} // namespace
1053
1054// Handshake message types from RFC 8446 4.
1060 // End_Of_Early_Data,
1065 Finished_13>;
1066using Handshake_Message_13_Ref = as_wrapped_references_t<Handshake_Message_13>;
1067
1068using Post_Handshake_Message_13 = std::variant<New_Session_Ticket_13, Key_Update>;
1069
1070// Key_Update is handled generically by the Channel. The messages assigned
1071// to those variants are the ones that need to be handled by the specific
1072// client and/or server implementations.
1073using Server_Post_Handshake_13_Message = std::variant<New_Session_Ticket_13, Key_Update>;
1074using Client_Post_Handshake_13_Message = std::variant<Key_Update>;
1075
1077 Server_Hello_12, // indicates a TLS version downgrade
1083 Finished_13>;
1084using Server_Handshake_13_Message_Ref = as_wrapped_references_t<Server_Handshake_13_Message>;
1085
1087 Client_Hello_12, // indicates a TLS peer that does not offer TLS 1.3
1090 Finished_13>;
1091using Client_Handshake_13_Message_Ref = as_wrapped_references_t<Client_Handshake_13_Message>;
1092
1093#endif // BOTAN_HAS_TLS_13
1094
1095} // namespace TLS
1096
1097} // namespace Botan
1098
1099#endif
Handshake_Type type() const override
const std::vector< X509_Certificate > & cert_chain() const
const Extensions & extensions() const
Extensions & extensions()
bool has_certificate() const
Handshake_Type type() const override
const std::vector< uint8_t > & request_context() const
const Extensions & extensions() const
const std::vector< uint8_t > & context() const
const std::vector< uint8_t > & response() const
Handshake_Type type() const override
Handshake_Type type() const override
Signature_Scheme signature_scheme() const
std::vector< uint8_t > m_signature
std::vector< uint8_t > serialize() const override
Handshake_Type type() const override
const std::string & hostname() const
Settings(const Protocol_Version version, std::string_view hostname="")
Protocol_Version protocol_version() const
Client_Hello(Client_Hello &&) noexcept
Client_Hello & operator=(const Client_Hello &)=delete
Client_Hello(const Client_Hello &)=delete
Handshake_Type type() const override
const std::optional< std::string > & psk_identity() const
const secure_vector< uint8_t > & pre_master_secret() const
const Extensions & extensions() const
Handshake_Type type() const override
std::vector< uint8_t > m_verification_data
Handshake_Type type() const override
Handshake_Type type() const override
Handshake_Type wire_type() const override
Handshake_Type type() const override
Handshake_Type type() const override
const std::vector< uint8_t > & cookie() const
bool expects_reciprocation() const
Handshake_Type type() const override
std::chrono::seconds ticket_lifetime_hint() const
Handshake_Type type() const override
const Session_Ticket & ticket() const
Handshake_Type type() const override
std::chrono::seconds lifetime_hint() const
const Ticket_Nonce & nonce() const
const Opaque_Session_Handle & handle() const
const Extensions & extensions() const
Protocol_Version protocol_version() const
Settings(Session_ID new_session_id, Protocol_Version new_session_version, uint16_t ciphersuite, bool offer_session_ticket)
const Session_ID & session_id() const
Handshake_Type type() const override
Server_Hello(const Server_Hello &)=delete
Server_Hello(Server_Hello &&) noexcept
Server_Hello & operator=(const Server_Hello &)=delete
Handshake_Type type() const override
const std::vector< uint8_t > & params() const
const std::optional< Group_Params > & shared_group() const
Helper class to embody a session handle in all protocol versions.
Definition tls_session.h:64
int(* final)(unsigned char *, CTX *)
#define BOTAN_UNSTABLE_API
Definition compiler.h:44
as_wrapped_references_t< Server_Handshake_13_Message > Server_Handshake_13_Message_Ref
as_wrapped_references_t< Client_Handshake_13_Message > Client_Handshake_13_Message_Ref
std::vector< uint8_t > Transcript_Hash
Definition tls_magic.h:81
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, Certificate_13, Certificate_Verify_13, Finished_13 > Client_Handshake_13_Message
std::variant< Server_Hello_13, Server_Hello_12, Hello_Retry_Request, Encrypted_Extensions, Certificate_13, Certificate_Request_13, Certificate_Verify_13, Finished_13 > Server_Handshake_13_Message
std::vector< uint8_t > make_hello_random(RandomNumberGenerator &rng, Callbacks &cb, const Policy &policy)
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
std::variant< Client_Hello_13, Client_Hello_12, Server_Hello_13, Server_Hello_12, Hello_Retry_Request, Encrypted_Extensions, Certificate_13, Certificate_Request_13, Certificate_Verify_13, Finished_13 > Handshake_Message_13
Usage_Type
Definition x509cert.h:22
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61