Botan 3.11.0
Crypto and TLS for C&
tls_handshake_state_13.h
Go to the documentation of this file.
1/*
2* TLS handshake state (machine) implementation for TLS 1.3
3* (C) 2022 Jack Lloyd
4* 2022 Hannes Rantzsch, René Meusel - neXenio GmbH
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_TLS_HANDSHAKE_STATE_13_H_
10#define BOTAN_TLS_HANDSHAKE_STATE_13_H_
11
12#include <botan/tls_exceptn.h>
13#include <botan/tls_magic.h>
14#include <botan/tls_messages_13.h>
15#include <botan/internal/stl_util.h>
16#include <optional>
17#include <variant>
18
19namespace Botan::TLS {
20
21namespace Internal {
23 public:
24 bool has_client_hello() const { return m_client_hello.has_value(); }
25
26 bool has_server_hello() const { return m_server_hello.has_value(); }
27
28 bool has_server_certificate_msg() const { return m_server_certificate.has_value(); }
29
30 bool has_client_certificate_msg() const { return m_client_certificate.has_value(); }
31
32 bool has_hello_retry_request() const { return m_hello_retry_request.has_value(); }
33
34 bool has_certificate_request() const { return m_certificate_request.has_value(); }
35
36 bool has_server_finished() const { return m_server_finished.has_value(); }
37
38 bool has_client_finished() const { return m_client_finished.has_value(); }
39
41
42 // Client_Hello_13 cannot be const because it might need modification due to a Hello_Retry_Request
43 Client_Hello_13& client_hello() { return get(m_client_hello); }
44
45 const Client_Hello_13& client_hello() const { return get(m_client_hello); }
46
47 const Server_Hello_13& server_hello() const { return get(m_server_hello); }
48
49 const Hello_Retry_Request& hello_retry_request() const { return get(m_hello_retry_request); }
50
51 const Encrypted_Extensions& encrypted_extensions() const { return get(m_encrypted_extensions); }
52
53 const Certificate_Request_13& certificate_request() const { return get(m_certificate_request); }
54
55 const Certificate_13& server_certificate() const { return get(m_server_certificate); }
56
57 const Certificate_13& client_certificate() const { return get(m_client_certificate); }
58
59 const Certificate_Verify_13& server_certificate_verify() const { return get(m_server_certificate_verify); }
60
61 const Certificate_Verify_13& client_certificate_verify() const { return get(m_client_certificate_verify); }
62
63 const Finished_13& server_finished() const { return get(m_server_finished); }
64
65 const Finished_13& client_finished() const { return get(m_client_finished); }
66
67 protected:
68 explicit Handshake_State_13_Base(Connection_Side whoami) : m_side(whoami) {}
69
70 Client_Hello_13& store(Client_Hello_13 client_hello, bool from_peer);
71 Client_Hello_12_Shim& store(Client_Hello_12_Shim client_hello, bool from_peer);
72 Server_Hello_13& store(Server_Hello_13 server_hello, bool from_peer);
73 Server_Hello_12_Shim& store(Server_Hello_12_Shim server_hello, bool from_peer);
74 Hello_Retry_Request& store(Hello_Retry_Request hello_retry_request, bool from_peer);
75 Encrypted_Extensions& store(Encrypted_Extensions encrypted_extensions, bool from_peer);
76 Certificate_Request_13& store(Certificate_Request_13 certificate_request, bool from_peer);
77 Certificate_13& store(Certificate_13 certificate, bool from_peer);
78 Certificate_Verify_13& store(Certificate_Verify_13 certificate_verify, bool from_peer);
79 Finished_13& store(Finished_13 finished, bool from_peer);
80
81 private:
82 template <typename MessageT>
83 const MessageT& get(const std::optional<MessageT>& opt) const {
84 if(!opt.has_value()) {
85 throw Invalid_State("TLS handshake message not set");
86 }
87 return opt.value();
88 }
89
90 template <typename MessageT>
91 MessageT& get(std::optional<MessageT>& opt) {
92 if(!opt.has_value()) {
93 throw Invalid_State("TLS handshake message not set");
94 }
95 return opt.value();
96 }
97
98 Connection_Side m_side;
99
100 std::optional<Client_Hello_13> m_client_hello;
101 std::optional<Client_Hello_12_Shim> m_client_hello_12;
102 std::optional<Server_Hello_13> m_server_hello;
103 std::optional<Server_Hello_12_Shim> m_server_hello_12;
104 std::optional<Hello_Retry_Request> m_hello_retry_request;
105 std::optional<Encrypted_Extensions> m_encrypted_extensions;
106 std::optional<Certificate_Request_13> m_certificate_request;
107 std::optional<Certificate_13> m_server_certificate;
108 std::optional<Certificate_13> m_client_certificate;
109 std::optional<Certificate_Verify_13> m_server_certificate_verify;
110 std::optional<Certificate_Verify_13> m_client_certificate_verify;
111 std::optional<Finished_13> m_server_finished;
112 std::optional<Finished_13> m_client_finished;
113};
114} // namespace Internal
115
116/**
117 * Place to store TLS handshake messages
118 *
119 * This class is used to keep all handshake messages that have been received from and sent to
120 * the peer as part of the TLS 1.3 handshake. Getters are provided for all message types.
121 * Specializations for the client and server side provide specific setters in the form of
122 * `sent` and `received` that only allow those types of handshake messages that are sensible
123 * for the respective connection side.
124 *
125 * The handshake state machine as described in RFC 8446 Appendix A is NOT validated here.
126 */
127template <Connection_Side whoami,
128 typename Outbound_Message_T,
129 typename Inbound_Message_T,
130 typename Inbound_Post_Handshake_Message_T>
132 public:
134
135 template <typename MsgT>
136 std::reference_wrapper<MsgT> sending(MsgT msg)
137 requires(std::is_constructible_v<Outbound_Message_T, MsgT>)
138 {
139 return std::reference_wrapper<decltype(msg)>(store(std::move(msg), false));
140 }
141
142 template <typename... MsgTs>
143 decltype(auto) sending(std::variant<MsgTs...> message)
145 {
146 return std::visit(
147 [&](auto msg) -> detail::as_wrapped_references_t<std::variant<MsgTs...>> {
148 return sending(std::move(msg));
149 },
150 std::move(message));
151 }
152
153 decltype(auto) received(Handshake_Message_13 message) {
154 return std::visit(
156 if constexpr(std::is_constructible_v<Inbound_Message_T, decltype(msg)>) {
157 return std::reference_wrapper<decltype(msg)>(store(std::move(msg), true));
158 } else {
159 throw TLS_Exception(AlertType::UnexpectedMessage, "received an illegal handshake message");
160 }
161 },
162 std::move(message));
163 }
164
165 decltype(auto) received(Post_Handshake_Message_13 message) {
166 return std::visit(
167 [](auto msg) -> Inbound_Post_Handshake_Message_T {
168 if constexpr(std::is_constructible_v<Inbound_Post_Handshake_Message_T, decltype(msg)>) {
169 return msg;
170 } else {
171 throw TLS_Exception(AlertType::UnexpectedMessage, "received an unexpected post-handshake message");
172 }
173 },
174 std::move(message));
175 }
176};
177
182
187} // namespace Botan::TLS
188
189#endif
#define BOTAN_TEST_API
Definition api.h:41
std::reference_wrapper< MsgT > sending(MsgT msg)
decltype(auto) received(Handshake_Message_13 message)
decltype(auto) sending(std::variant< MsgTs... > message)
decltype(auto) received(Post_Handshake_Message_13 message)
const Hello_Retry_Request & hello_retry_request() const
const Certificate_Verify_13 & client_certificate_verify() const
const Certificate_Request_13 & certificate_request() const
const Encrypted_Extensions & encrypted_extensions() const
const Certificate_Verify_13 & server_certificate_verify() const
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
Handshake_State_13< Connection_Side::Client, Client_Handshake_13_Message, Server_Handshake_13_Message, Server_Post_Handshake_13_Message > Client_Handshake_State_13
std::variant< Key_Update > Client_Post_Handshake_13_Message
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::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
Handshake_State_13< Connection_Side::Server, Server_Handshake_13_Message, Client_Handshake_13_Message, Client_Post_Handshake_13_Message > Server_Handshake_State_13
constexpr bool is_generalizable_to(const SpecialT &) noexcept
Definition stl_util.h:70