1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*
* TLS Handshaking
* (C) 2004-2006,2011 Jack Lloyd
*
* Released under the terms of the Botan license
*/
#include <botan/internal/tls_handshake_state.h>
#include <botan/internal/tls_messages.h>
#include <botan/internal/assert.h>
#include <botan/lookup.h>
namespace Botan {
namespace TLS {
namespace {
u32bit bitmask_for_handshake_type(Handshake_Type type)
{
switch(type)
{
case HELLO_VERIFY_REQUEST:
return (1 << 0);
case HELLO_REQUEST:
return (1 << 1);
/*
* Same code point for both client hello styles
*/
case CLIENT_HELLO:
case CLIENT_HELLO_SSLV2:
return (1 << 2);
case SERVER_HELLO:
return (1 << 3);
case CERTIFICATE:
return (1 << 4);
case CERTIFICATE_URL:
return (1 << 5);
case CERTIFICATE_STATUS:
return (1 << 6);
case SERVER_KEX:
return (1 << 7);
case CERTIFICATE_REQUEST:
return (1 << 8);
case SERVER_HELLO_DONE:
return (1 << 9);
case CERTIFICATE_VERIFY:
return (1 << 10);
case CLIENT_KEX:
return (1 << 11);
case NEXT_PROTOCOL:
return (1 << 12);
case NEW_SESSION_TICKET:
return (1 << 13);
case HANDSHAKE_CCS:
return (1 << 14);
case FINISHED:
return (1 << 15);
// allow explicitly disabling new handshakes
case HANDSHAKE_NONE:
return 0;
}
throw Internal_Error("Unknown handshake type " + std::to_string(type));
}
}
/*
* Initialize the SSL/TLS Handshake State
*/
Handshake_State::Handshake_State(Handshake_IO* io) :
m_handshake_io(io),
m_version(Protocol_Version::SSL_V3)
{
}
void Handshake_State::set_version(const Protocol_Version& version)
{
m_version = version;
}
void Handshake_State::confirm_transition_to(Handshake_Type handshake_msg)
{
const u32bit mask = bitmask_for_handshake_type(handshake_msg);
m_hand_received_mask |= mask;
const bool ok = (m_hand_expecting_mask & mask); // overlap?
if(!ok)
throw Unexpected_Message("Unexpected state transition in handshake, got " +
std::to_string(handshake_msg) +
" expected " + std::to_string(m_hand_expecting_mask) +
" received " + std::to_string(m_hand_received_mask));
/* We don't know what to expect next, so force a call to
set_expected_next; if it doesn't happen, the next transition
check will always fail which is what we want.
*/
m_hand_expecting_mask = 0;
}
void Handshake_State::set_expected_next(Handshake_Type handshake_msg)
{
m_hand_expecting_mask |= bitmask_for_handshake_type(handshake_msg);
}
bool Handshake_State::received_handshake_msg(Handshake_Type handshake_msg) const
{
const u32bit mask = bitmask_for_handshake_type(handshake_msg);
return (m_hand_received_mask & mask);
}
std::string Handshake_State::srp_identifier() const
{
if(suite.valid() && suite.kex_algo() == "SRP_SHA")
return client_hello->srp_identifier();
return "";
}
const std::vector<byte>& Handshake_State::session_ticket() const
{
if(new_session_ticket && !new_session_ticket->ticket().empty())
return new_session_ticket->ticket();
return client_hello->session_ticket();
}
KDF* Handshake_State::protocol_specific_prf()
{
if(version() == Protocol_Version::SSL_V3)
{
return get_kdf("SSL3-PRF");
}
else if(version().supports_ciphersuite_specific_prf())
{
if(suite.mac_algo() == "MD5" || suite.mac_algo() == "SHA-1")
return get_kdf("TLS-12-PRF(SHA-256)");
return get_kdf("TLS-12-PRF(" + suite.mac_algo() + ")");
}
else
{
// TLS v1.0, v1.1 and DTLS v1.0
return get_kdf("TLS-PRF");
}
throw Internal_Error("Unknown version code " + version().to_string());
}
namespace {
std::string choose_hash(const std::string& sig_algo,
Protocol_Version negotiated_version,
const Policy& policy,
bool for_client_auth,
Client_Hello* client_hello,
Certificate_Req* cert_req)
{
if(!negotiated_version.supports_negotiable_signature_algorithms())
{
if(for_client_auth && negotiated_version == Protocol_Version::SSL_V3)
return "Raw";
if(sig_algo == "RSA")
return "TLS.Digest.0";
if(sig_algo == "DSA")
return "SHA-1";
if(sig_algo == "ECDSA")
return "SHA-1";
throw Internal_Error("Unknown TLS signature algo " + sig_algo);
}
const auto supported_algos = for_client_auth ?
cert_req->supported_algos() :
client_hello->supported_algos();
if(!supported_algos.empty())
{
const auto hashes = policy.allowed_signature_hashes();
/*
* Choose our most preferred hash that the counterparty supports
* in pairing with the signature algorithm we want to use.
*/
for(auto hash : hashes)
{
for(auto algo : supported_algos)
{
if(algo.first == hash && algo.second == sig_algo)
return hash;
}
}
}
// TLS v1.2 default hash if the counterparty sent nothing
return "SHA-1";
}
}
std::pair<std::string, Signature_Format>
Handshake_State::choose_sig_format(const Private_Key* key,
std::string& hash_algo_out,
std::string& sig_algo_out,
bool for_client_auth,
const Policy& policy)
{
const std::string sig_algo = key->algo_name();
const std::string hash_algo =
choose_hash(sig_algo,
this->version(),
policy,
for_client_auth,
client_hello,
cert_req);
if(this->version().supports_negotiable_signature_algorithms())
{
hash_algo_out = hash_algo;
sig_algo_out = sig_algo;
}
if(sig_algo == "RSA")
{
const std::string padding = "EMSA3(" + hash_algo + ")";
return std::make_pair(padding, IEEE_1363);
}
else if(sig_algo == "DSA" || sig_algo == "ECDSA")
{
const std::string padding = "EMSA1(" + hash_algo + ")";
return std::make_pair(padding, DER_SEQUENCE);
}
throw Invalid_Argument(sig_algo + " is invalid/unknown for TLS signatures");
}
std::pair<std::string, Signature_Format>
Handshake_State::understand_sig_format(const Public_Key* key,
std::string hash_algo,
std::string sig_algo,
bool for_client_auth)
{
const std::string algo_name = key->algo_name();
/*
FIXME: This should check what was sent against the client hello
preferences, or the certificate request, to ensure it was allowed
by those restrictions.
Or not?
*/
if(this->version().supports_negotiable_signature_algorithms())
{
if(hash_algo == "")
throw Decoding_Error("Counterparty did not send hash/sig IDS");
if(sig_algo != algo_name)
throw Decoding_Error("Counterparty sent inconsistent key and sig types");
}
else
{
if(hash_algo != "" || sig_algo != "")
throw Decoding_Error("Counterparty sent hash/sig IDs with old version");
}
if(algo_name == "RSA")
{
if(for_client_auth && this->version() == Protocol_Version::SSL_V3)
{
hash_algo = "Raw";
}
else if(!this->version().supports_negotiable_signature_algorithms())
{
hash_algo = "TLS.Digest.0";
}
const std::string padding = "EMSA3(" + hash_algo + ")";
return std::make_pair(padding, IEEE_1363);
}
else if(algo_name == "DSA" || algo_name == "ECDSA")
{
if(algo_name == "DSA" && for_client_auth && this->version() == Protocol_Version::SSL_V3)
{
hash_algo = "Raw";
}
else if(!this->version().supports_negotiable_signature_algorithms())
{
hash_algo = "SHA-1";
}
const std::string padding = "EMSA1(" + hash_algo + ")";
return std::make_pair(padding, DER_SEQUENCE);
}
throw Invalid_Argument(algo_name + " is invalid/unknown for TLS signatures");
}
/*
* Destroy the SSL/TLS Handshake State
*/
Handshake_State::~Handshake_State()
{
delete client_hello;
delete server_hello;
delete server_certs;
delete server_kex;
delete cert_req;
delete server_hello_done;
delete next_protocol;
delete new_session_ticket;
delete client_certs;
delete client_kex;
delete client_verify;
delete client_finished;
delete server_finished;
delete m_handshake_io;
}
}
}
|