Botan 3.14.1
Crypto and TLS for C&
tls_handshake_transitions.cpp
Go to the documentation of this file.
1/*
2* TLS Handshake State Transitions
3* (C) 2004-2006,2011,2012 Jack Lloyd
4* 2017 Harry Reimann, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/tls_handshake_transitions.h>
10
11#include <botan/tls_exceptn.h>
12
13#include <sstream>
14
15namespace Botan::TLS {
16
17namespace {
18
19uint32_t bitmask_for_handshake_type(Handshake_Type type) {
20 switch(type) {
22 return (1 << 0);
23
25 return (1 << 1);
26
28 return (1 << 2);
29
31 return (1 << 3);
32
34 return (1 << 4);
35
37 return (1 << 5);
38
40 return (1 << 6);
41
43 return (1 << 7);
44
46 return (1 << 8);
47
49 return (1 << 9);
50
52 return (1 << 10);
53
55 return (1 << 11);
56
58 return (1 << 12);
59
61 return (1 << 13);
62
64 return (1 << 14);
65
66 case Handshake_Type::EndOfEarlyData: // RFC 8446
67 return (1 << 15);
68
70 return (1 << 16);
71
72 case Handshake_Type::KeyUpdate: // RFC 8446
73 return (1 << 17);
74
75 case Handshake_Type::HelloRetryRequest: // RFC 8446
76 return (1 << 18);
77
78 case Handshake_Type::MessageHash: // RFC 9846
79 return (1 << 19);
80
81 // allow explicitly disabling new handshakes
83 return 0;
84 }
85
86 throw TLS_Exception(Alert::UnexpectedMessage,
87 "Unknown TLS handshake message type " + std::to_string(static_cast<size_t>(type)));
88}
89
90std::string handshake_mask_to_string(uint32_t mask, char combiner) {
91 // clang-format off
92 const Handshake_Type types[] = {
113 };
114 // clang-format on
115
116 std::ostringstream o;
117 bool empty = true;
118
119 for(auto&& t : types) {
120 if((mask & bitmask_for_handshake_type(t)) != 0) {
121 if(!empty) {
122 o << combiner;
123 }
125 empty = false;
126 }
127 }
128
129 return o.str();
130}
131
132} // namespace
133
135 const uint32_t mask = bitmask_for_handshake_type(msg_type);
136
137 return (m_hand_received_mask & mask) != 0;
138}
139
141 const uint32_t mask = bitmask_for_handshake_type(msg_type);
142
143 const bool ok = (m_hand_expecting_mask & mask) != 0; // overlap?
144
145 if(!ok) {
146 const uint32_t seen_so_far = m_hand_received_mask & ~mask;
147
148 std::ostringstream msg;
149
150 msg << "Unexpected state transition in handshake got a " << handshake_type_to_string(msg_type);
151
152 if(m_hand_expecting_mask == 0) {
153 msg << " not expecting messages";
154 } else {
155 msg << " expected " << handshake_mask_to_string(m_hand_expecting_mask, '|');
156 }
157
158 if(seen_so_far != 0) {
159 msg << " seen " << handshake_mask_to_string(seen_so_far, '+');
160 }
161
162 throw Unexpected_Message(msg.str());
163 }
164
165 // Record the message as received only once the transition is accepted
166 m_hand_received_mask |= mask;
167
168 /* We don't know what to expect next, so force a call to
169 set_expected_next; if it doesn't happen, the next transition
170 check will always fail which is what we want.
171 */
172 m_hand_expecting_mask = 0;
173}
174
176 m_hand_expecting_mask |= bitmask_for_handshake_type(msg_type);
177}
178
179void Handshake_Transitions::set_expected_next(const std::vector<Handshake_Type>& msg_types) {
180 for(const auto type : msg_types) {
181 set_expected_next(type);
182 }
183}
184
186 return (bitmask_for_handshake_type(Handshake_Type::HandshakeCCS) & m_hand_expecting_mask) != 0;
187}
188
189} // namespace Botan::TLS
void confirm_transition_to(Handshake_Type msg_type)
bool received_handshake_msg(Handshake_Type msg_type) const
void set_expected_next(Handshake_Type msg_type)
const char * handshake_type_to_string(Handshake_Type type)
Definition tls_magic.cpp:15