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