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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
/*
* TLS Client
* (C) 2004-2011 Jack Lloyd
*
* Released under the terms of the Botan license
*/
#include <botan/tls_client.h>
#include <botan/internal/tls_handshake_state.h>
#include <botan/internal/tls_messages.h>
#include <botan/internal/stl_util.h>
#include <memory>
namespace Botan {
namespace TLS {
/*
* TLS Client Constructor
*/
Client::Client(std::function<void (const byte[], size_t)> output_fn,
std::function<void (const byte[], size_t, Alert)> proc_fn,
std::function<bool (const Session&)> handshake_fn,
Session_Manager& session_manager,
Credentials_Manager& creds,
const Policy& policy,
RandomNumberGenerator& rng,
const std::string& hostname,
std::function<std::string (std::vector<std::string>)> next_protocol) :
Channel(output_fn, proc_fn, handshake_fn, session_manager),
m_policy(policy),
m_rng(rng),
m_creds(creds),
m_hostname(hostname)
{
m_writer.set_version(Protocol_Version::SSL_V3);
m_state = new Handshake_State(new Stream_Handshake_Reader);
m_state->set_expected_next(SERVER_HELLO);
m_state->client_npn_cb = next_protocol;
const std::string srp_identifier = m_creds.srp_identifier("tls-client", hostname);
const bool send_npn_request = static_cast<bool>(next_protocol);
if(hostname != "")
{
Session session_info;
if(m_session_manager.load_from_host_info(m_hostname, 0, session_info))
{
if(session_info.srp_identifier() == srp_identifier)
{
m_state->client_hello = new Client_Hello(
m_writer,
m_state->hash,
m_policy,
m_rng,
m_secure_renegotiation.for_client_hello(),
session_info,
send_npn_request);
m_state->resume_master_secret = session_info.master_secret();
}
}
}
if(!m_state->client_hello) // not resuming
{
m_state->client_hello = new Client_Hello(
m_writer,
m_state->hash,
m_policy.pref_version(),
m_policy,
m_rng,
m_secure_renegotiation.for_client_hello(),
send_npn_request,
m_hostname,
srp_identifier);
}
m_secure_renegotiation.update(m_state->client_hello);
}
/*
* Send a new client hello to renegotiate
*/
void Client::renegotiate(bool force_full_renegotiation)
{
if(m_state && m_state->client_hello)
return; // currently in active handshake
delete m_state;
m_state = new Handshake_State(new Stream_Handshake_Reader);
m_state->set_expected_next(SERVER_HELLO);
if(!force_full_renegotiation)
{
Session session_info;
if(m_session_manager.load_from_host_info(m_hostname, 0, session_info))
{
m_state->client_hello = new Client_Hello(
m_writer,
m_state->hash,
m_policy,
m_rng,
m_secure_renegotiation.for_client_hello(),
session_info);
m_state->resume_master_secret = session_info.master_secret();
}
}
if(!m_state->client_hello)
{
m_state->client_hello = new Client_Hello(
m_writer,
m_state->hash,
m_reader.get_version(),
m_policy,
m_rng,
m_secure_renegotiation.for_client_hello());
}
m_secure_renegotiation.update(m_state->client_hello);
}
void Client::alert_notify(const Alert& alert)
{
if(alert.type() == Alert::NO_RENEGOTIATION)
{
if(m_handshake_completed && m_state)
{
delete m_state;
m_state = nullptr;
}
}
}
/*
* Process a handshake message
*/
void Client::process_handshake_msg(Handshake_Type type,
const std::vector<byte>& contents)
{
if(!m_state)
throw Unexpected_Message("Unexpected handshake message from server");
if(type == HELLO_REQUEST)
{
Hello_Request hello_request(contents);
// Ignore request entirely if we are currently negotiating a handshake
if(m_state->client_hello)
return;
if(!m_secure_renegotiation.supported() && !m_policy.allow_insecure_renegotiation())
{
delete m_state;
m_state = nullptr;
// RFC 5746 section 4.2
send_alert(Alert(Alert::NO_RENEGOTIATION));
return;
}
renegotiate(false);
return;
}
m_state->confirm_transition_to(type);
if(type != HANDSHAKE_CCS && type != FINISHED)
m_state->hash.update(type, contents);
if(type == SERVER_HELLO)
{
m_state->server_hello = new Server_Hello(contents);
if(!m_state->client_hello->offered_suite(m_state->server_hello->ciphersuite()))
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server replied with ciphersuite we didn't send");
}
if(!value_exists(m_state->client_hello->compression_methods(),
m_state->server_hello->compression_method()))
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server replied with compression method we didn't send");
}
if(!m_state->client_hello->next_protocol_notification() &&
m_state->server_hello->next_protocol_notification())
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server sent next protocol but we didn't request it");
}
if(m_state->server_hello->supports_session_ticket())
{
if(!m_state->client_hello->supports_session_ticket())
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server sent session ticket extension but we did not");
}
m_state->set_version(m_state->server_hello->version());
m_writer.set_version(m_state->version());
m_reader.set_version(m_state->version());
m_secure_renegotiation.update(m_state->server_hello);
m_peer_supports_heartbeats = m_state->server_hello->supports_heartbeats();
m_heartbeat_sending_allowed = m_state->server_hello->peer_can_send_heartbeats();
m_state->suite = Ciphersuite::by_id(m_state->server_hello->ciphersuite());
const bool server_returned_same_session_id =
!m_state->server_hello->session_id().empty() &&
(m_state->server_hello->session_id() == m_state->client_hello->session_id());
if(server_returned_same_session_id)
{
// successful resumption
/*
* In this case, we offered the version used in the original
* session, and the server must resume with the same version.
*/
if(m_state->server_hello->version() != m_state->client_hello->version())
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server resumed session but with wrong version");
m_state->keys = Session_Keys(m_state,
m_state->resume_master_secret,
true);
// The server is not strictly required to send us a new ticket
if(m_state->server_hello->supports_session_ticket())
m_state->set_expected_next(NEW_SESSION_TICKET);
m_state->set_expected_next(HANDSHAKE_CCS);
}
else
{
// new session
if(m_state->version() > m_state->client_hello->version())
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Client: Server replied with bad version");
}
if(m_state->version() < m_policy.min_version())
{
throw TLS_Exception(Alert::PROTOCOL_VERSION,
"Client: Server is too old for specified policy");
}
if(m_state->suite.sig_algo() != "")
{
m_state->set_expected_next(CERTIFICATE);
}
else if(m_state->suite.kex_algo() == "PSK")
{
/* PSK is anonymous so no certificate/cert req message is
ever sent. The server may or may not send a server kex,
depending on if it has an identity hint for us.
(EC)DHE_PSK always sends a server key exchange for the
DH exchange portion.
*/
m_state->set_expected_next(SERVER_KEX);
m_state->set_expected_next(SERVER_HELLO_DONE);
}
else if(m_state->suite.kex_algo() != "RSA")
{
m_state->set_expected_next(SERVER_KEX);
}
else
{
m_state->set_expected_next(CERTIFICATE_REQUEST); // optional
m_state->set_expected_next(SERVER_HELLO_DONE);
}
}
}
else if(type == CERTIFICATE)
{
if(m_state->suite.kex_algo() != "RSA")
{
m_state->set_expected_next(SERVER_KEX);
}
else
{
m_state->set_expected_next(CERTIFICATE_REQUEST); // optional
m_state->set_expected_next(SERVER_HELLO_DONE);
}
m_state->server_certs = new Certificate(contents);
m_peer_certs = m_state->server_certs->cert_chain();
if(m_peer_certs.empty())
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Client: No certificates sent by server");
try
{
m_creds.verify_certificate_chain("tls-client", m_hostname, m_peer_certs);
}
catch(std::exception& e)
{
throw TLS_Exception(Alert::BAD_CERTIFICATE, e.what());
}
std::unique_ptr<Public_Key> peer_key(m_peer_certs[0].subject_public_key());
if(peer_key->algo_name() != m_state->suite.sig_algo())
throw TLS_Exception(Alert::ILLEGAL_PARAMETER,
"Certificate key type did not match ciphersuite");
}
else if(type == SERVER_KEX)
{
m_state->set_expected_next(CERTIFICATE_REQUEST); // optional
m_state->set_expected_next(SERVER_HELLO_DONE);
m_state->server_kex = new Server_Key_Exchange(contents,
m_state->suite.kex_algo(),
m_state->suite.sig_algo(),
m_state->version());
if(m_state->suite.sig_algo() != "")
{
if(!m_state->server_kex->verify(m_peer_certs[0], m_state))
{
throw TLS_Exception(Alert::DECRYPT_ERROR,
"Bad signature on server key exchange");
}
}
}
else if(type == CERTIFICATE_REQUEST)
{
m_state->set_expected_next(SERVER_HELLO_DONE);
m_state->cert_req = new Certificate_Req(contents, m_state->version());
}
else if(type == SERVER_HELLO_DONE)
{
m_state->server_hello_done = new Server_Hello_Done(contents);
if(m_state->received_handshake_msg(CERTIFICATE_REQUEST))
{
const std::vector<std::string>& types =
m_state->cert_req->acceptable_cert_types();
std::vector<X509_Certificate> client_certs =
m_creds.cert_chain(types,
"tls-client",
m_hostname);
m_state->client_certs = new Certificate(m_writer,
m_state->hash,
client_certs);
}
m_state->client_kex =
new Client_Key_Exchange(m_writer,
m_state,
m_creds,
m_peer_certs,
m_hostname,
m_rng);
m_state->keys = Session_Keys(m_state,
m_state->client_kex->pre_master_secret(),
false);
if(m_state->received_handshake_msg(CERTIFICATE_REQUEST) &&
!m_state->client_certs->empty())
{
Private_Key* private_key =
m_creds.private_key_for(m_state->client_certs->cert_chain()[0],
"tls-client",
m_hostname);
m_state->client_verify = new Certificate_Verify(m_writer,
m_state,
m_rng,
private_key);
}
m_writer.send(CHANGE_CIPHER_SPEC, 1);
m_writer.activate(CLIENT, m_state->suite, m_state->keys,
m_state->server_hello->compression_method());
if(m_state->server_hello->next_protocol_notification())
{
const std::string protocol =
m_state->client_npn_cb(m_state->server_hello->next_protocols());
m_state->next_protocol = new Next_Protocol(m_writer, m_state->hash, protocol);
}
m_state->client_finished = new Finished(m_writer, m_state, CLIENT);
if(m_state->server_hello->supports_session_ticket())
m_state->set_expected_next(NEW_SESSION_TICKET);
else
m_state->set_expected_next(HANDSHAKE_CCS);
}
else if(type == NEW_SESSION_TICKET)
{
m_state->new_session_ticket = new New_Session_Ticket(contents);
m_state->set_expected_next(HANDSHAKE_CCS);
}
else if(type == HANDSHAKE_CCS)
{
m_state->set_expected_next(FINISHED);
m_reader.activate(CLIENT, m_state->suite, m_state->keys,
m_state->server_hello->compression_method());
}
else if(type == FINISHED)
{
m_state->set_expected_next(HELLO_REQUEST);
m_state->server_finished = new Finished(contents);
if(!m_state->server_finished->verify(m_state, SERVER))
throw TLS_Exception(Alert::DECRYPT_ERROR,
"Finished message didn't verify");
m_state->hash.update(type, contents);
if(!m_state->client_finished) // session resume case
{
m_writer.send(CHANGE_CIPHER_SPEC, 1);
m_writer.activate(CLIENT, m_state->suite, m_state->keys,
m_state->server_hello->compression_method());
m_state->client_finished = new Finished(m_writer, m_state, CLIENT);
}
m_secure_renegotiation.update(m_state->client_finished, m_state->server_finished);
std::vector<byte> session_id = m_state->server_hello->session_id();
const std::vector<byte>& session_ticket = m_state->session_ticket();
if(session_id.empty() && !session_ticket.empty())
session_id = make_hello_random(m_rng);
Session session_info(
session_id,
m_state->keys.master_secret(),
m_state->server_hello->version(),
m_state->server_hello->ciphersuite(),
m_state->server_hello->compression_method(),
CLIENT,
m_secure_renegotiation.supported(),
m_state->server_hello->fragment_size(),
m_peer_certs,
session_ticket,
m_hostname,
""
);
const bool should_save = m_handshake_fn(session_info);
if(!session_id.empty())
{
if(should_save)
m_session_manager.save(session_info);
else
m_session_manager.remove_entry(session_info.session_id());
}
delete m_state;
m_state = nullptr;
m_handshake_completed = true;
m_active_session = session_info.session_id();
}
else
throw Unexpected_Message("Unknown handshake message received");
}
}
}
|