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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
/*
* Copyright (C) 2000-2012 Free Software Foundation, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/* This file contains functions which are wrappers for the key exchange
* part of TLS. They are called by the handshake functions (gnutls_handshake)
*/
#include "gnutls_int.h"
#include "handshake.h"
#include "kx.h"
#include "dh.h"
#include "errors.h"
#include "algorithms.h"
#include "debug.h"
#include "locks.h"
#include "mpi.h"
#include <state.h>
#include <datum.h>
#include <mbuffers.h>
/* This file contains important thing for the TLS handshake procedure.
*/
#define MASTER_SECRET "master secret"
#define MASTER_SECRET_SIZE (sizeof(MASTER_SECRET)-1)
#define EXT_MASTER_SECRET "extended master secret"
#define EXT_MASTER_SECRET_SIZE (sizeof(EXT_MASTER_SECRET)-1)
GNUTLS_STATIC_MUTEX(keylog_mutex);
static FILE *keylog;
static int generate_normal_master(gnutls_session_t session,
gnutls_datum_t *, int);
int _gnutls_generate_master(gnutls_session_t session, int keep_premaster)
{
if (session->internals.resumed == RESUME_FALSE)
return generate_normal_master(session, &session->key.key,
keep_premaster);
else if (session->internals.premaster_set) {
gnutls_datum_t premaster;
premaster.size =
sizeof(session->internals.resumed_security_parameters.
master_secret);
premaster.data =
session->internals.resumed_security_parameters.
master_secret;
return generate_normal_master(session, &premaster, 1);
}
return 0;
}
void _gnutls_nss_keylog_write(gnutls_session_t session,
const char *label,
const uint8_t *secret, size_t secret_size)
{
static const char *keylogfile = NULL;
static unsigned checked_env = 0;
if (!checked_env) {
checked_env = 1;
keylogfile = secure_getenv("SSLKEYLOGFILE");
if (keylogfile != NULL)
keylog = fopen(keylogfile, "a");
}
if (keylog) {
char client_random_hex[2*GNUTLS_RANDOM_SIZE+1];
char secret_hex[2*MAX_HASH_SIZE+1];
GNUTLS_STATIC_MUTEX_LOCK(keylog_mutex);
fprintf(keylog, "%s %s %s\n",
label,
_gnutls_bin2hex(session->security_parameters.
client_random, GNUTLS_RANDOM_SIZE,
client_random_hex,
sizeof(client_random_hex), NULL),
_gnutls_bin2hex(secret, secret_size,
secret_hex, sizeof(secret_hex), NULL));
fflush(keylog);
GNUTLS_STATIC_MUTEX_UNLOCK(keylog_mutex);
}
}
void _gnutls_nss_keylog_deinit(void)
{
if (keylog) {
fclose(keylog);
keylog = NULL;
}
}
/* here we generate the TLS Master secret.
*/
static int
generate_normal_master(gnutls_session_t session,
gnutls_datum_t * premaster, int keep_premaster)
{
int ret = 0;
char buf[512];
_gnutls_hard_log("INT: PREMASTER SECRET[%d]: %s\n",
premaster->size, _gnutls_bin2hex(premaster->data,
premaster->size,
buf, sizeof(buf),
NULL));
_gnutls_hard_log("INT: CLIENT RANDOM[%d]: %s\n", 32,
_gnutls_bin2hex(session->security_parameters.
client_random, 32, buf,
sizeof(buf), NULL));
_gnutls_hard_log("INT: SERVER RANDOM[%d]: %s\n", 32,
_gnutls_bin2hex(session->security_parameters.
server_random, 32, buf,
sizeof(buf), NULL));
if (session->security_parameters.ext_master_secret == 0) {
uint8_t rnd[2 * GNUTLS_RANDOM_SIZE + 1];
memcpy(rnd, session->security_parameters.client_random,
GNUTLS_RANDOM_SIZE);
memcpy(&rnd[GNUTLS_RANDOM_SIZE],
session->security_parameters.server_random,
GNUTLS_RANDOM_SIZE);
#ifdef ENABLE_SSL3
if (get_num_version(session) == GNUTLS_SSL3) {
ret =
_gnutls_ssl3_generate_random(premaster->data,
premaster->size, rnd,
2 * GNUTLS_RANDOM_SIZE,
GNUTLS_MASTER_SIZE,
session->security_parameters.
master_secret);
} else
#endif
ret =
_gnutls_PRF(session, premaster->data, premaster->size,
MASTER_SECRET, MASTER_SECRET_SIZE,
rnd, 2 * GNUTLS_RANDOM_SIZE,
GNUTLS_MASTER_SIZE,
session->security_parameters.
master_secret);
} else {
gnutls_datum_t shash = {NULL, 0};
/* draft-ietf-tls-session-hash-02 */
ret = _gnutls_handshake_get_session_hash(session, &shash);
if (ret < 0)
return gnutls_assert_val(ret);
#ifdef ENABLE_SSL3
if (get_num_version(session) == GNUTLS_SSL3)
return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
#endif
ret =
_gnutls_PRF(session, premaster->data, premaster->size,
EXT_MASTER_SECRET, EXT_MASTER_SECRET_SIZE,
shash.data, shash.size,
GNUTLS_MASTER_SIZE,
session->security_parameters.
master_secret);
gnutls_free(shash.data);
}
_gnutls_nss_keylog_write(session, "CLIENT_RANDOM",
session->security_parameters.master_secret,
GNUTLS_MASTER_SIZE);
if (!keep_premaster)
_gnutls_free_temp_key_datum(premaster);
if (ret < 0)
return ret;
_gnutls_hard_log("INT: MASTER SECRET: %s\n",
_gnutls_bin2hex(session->security_parameters.
master_secret, GNUTLS_MASTER_SIZE,
buf, sizeof(buf), NULL));
return ret;
}
/* This is called when we want to receive the key exchange message of the
* server. It does nothing if this type of message is not required
* by the selected ciphersuite.
*/
int _gnutls_send_server_kx_message(gnutls_session_t session, int again)
{
gnutls_buffer_st buf;
int ret = 0;
mbuffer_st *bufel = NULL;
if (session->internals.auth_struct->gnutls_generate_server_kx ==
NULL)
return 0;
if (again == 0) {
ret = _gnutls_buffer_init_handshake_mbuffer(&buf);
if (ret < 0)
return gnutls_assert_val(ret);
ret =
session->internals.auth_struct->
gnutls_generate_server_kx(session, &buf);
if (ret == GNUTLS_E_INT_RET_0) {
gnutls_assert();
ret = 0;
goto cleanup;
}
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
bufel = _gnutls_buffer_to_mbuffer(&buf);
}
return _gnutls_send_handshake(session, bufel, GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE);
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
/* This function sends a certificate request message to the
* client.
*/
int _gnutls_send_server_crt_request(gnutls_session_t session, int again)
{
gnutls_buffer_st buf;
int ret = 0;
mbuffer_st *bufel = NULL;
if (session->internals.auth_struct->
gnutls_generate_server_crt_request == NULL)
return 0;
if (session->internals.send_cert_req <= 0)
return 0;
if (again == 0) {
ret = _gnutls_buffer_init_handshake_mbuffer(&buf);
if (ret < 0)
return gnutls_assert_val(ret);
ret =
session->internals.auth_struct->
gnutls_generate_server_crt_request(session, &buf);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
bufel = _gnutls_buffer_to_mbuffer(&buf);
}
return _gnutls_send_handshake(session, bufel, GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST);
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
/* This is the function for the client to send the key
* exchange message
*/
int _gnutls_send_client_kx_message(gnutls_session_t session, int again)
{
gnutls_buffer_st buf;
int ret = 0;
mbuffer_st *bufel = NULL;
if (session->internals.auth_struct->gnutls_generate_client_kx ==
NULL)
return 0;
if (again == 0) {
ret = _gnutls_buffer_init_handshake_mbuffer(&buf);
if (ret < 0)
return gnutls_assert_val(ret);
ret =
session->internals.auth_struct->
gnutls_generate_client_kx(session, &buf);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
bufel = _gnutls_buffer_to_mbuffer(&buf);
}
return _gnutls_send_handshake(session, bufel, GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE);
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
/* This is the function for the client to send the certificate
* verify message
*/
int
_gnutls_send_client_certificate_verify(gnutls_session_t session, int again)
{
gnutls_buffer_st buf;
int ret = 0;
mbuffer_st *bufel = NULL;
/* This is a packet that is only sent by the client
*/
if (session->security_parameters.entity == GNUTLS_SERVER)
return 0;
/* if certificate verify is not needed just exit
*/
if (!(session->internals.hsk_flags & HSK_CRT_ASKED))
return 0;
if (session->internals.auth_struct->
gnutls_generate_client_crt_vrfy == NULL) {
gnutls_assert();
return 0; /* this algorithm does not support cli_crt_vrfy
*/
}
if (again == 0) {
ret = _gnutls_buffer_init_handshake_mbuffer(&buf);
if (ret < 0)
return gnutls_assert_val(ret);
ret =
session->internals.auth_struct->
gnutls_generate_client_crt_vrfy(session, &buf);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
if (ret == 0)
goto cleanup;
bufel = _gnutls_buffer_to_mbuffer(&buf);
}
return _gnutls_send_handshake(session, bufel, GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY);
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
/* This is called when we want send our certificate
*/
int _gnutls_send_client_certificate(gnutls_session_t session, int again)
{
gnutls_buffer_st buf;
int ret = 0;
mbuffer_st *bufel = NULL;
if (!(session->internals.hsk_flags & HSK_CRT_ASKED))
return 0;
if (session->internals.auth_struct->
gnutls_generate_client_certificate == NULL)
return 0;
if (again == 0) {
ret = _gnutls_buffer_init_handshake_mbuffer(&buf);
if (ret < 0)
return gnutls_assert_val(ret);
#ifdef ENABLE_SSL3
if (get_num_version(session) != GNUTLS_SSL3 ||
session->internals.selected_cert_list_length > 0)
#endif
{
/* TLS 1.x or SSL 3.0 with a valid certificate
*/
ret =
session->internals.auth_struct->
gnutls_generate_client_certificate(session,
&buf);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
}
bufel = _gnutls_buffer_to_mbuffer(&buf);
}
#ifdef ENABLE_SSL3
/* In the SSL 3.0 protocol we need to send a
* no certificate alert instead of an
* empty certificate.
*/
if (get_num_version(session) == GNUTLS_SSL3 &&
session->internals.selected_cert_list_length == 0) {
_mbuffer_xfree(&bufel);
return
gnutls_alert_send(session, GNUTLS_AL_WARNING,
GNUTLS_A_SSL3_NO_CERTIFICATE);
} else /* TLS 1.0 or SSL 3.0 with a valid certificate
*/
#endif
return _gnutls_send_handshake(session, bufel, GNUTLS_HANDSHAKE_CERTIFICATE_PKT);
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
/* This is called when we want send our certificate
*/
int _gnutls_send_server_certificate(gnutls_session_t session, int again)
{
gnutls_buffer_st buf;
int ret = 0;
mbuffer_st *bufel = NULL;
if (session->internals.auth_struct->
gnutls_generate_server_certificate == NULL)
return 0;
if (again == 0) {
ret = _gnutls_buffer_init_handshake_mbuffer(&buf);
if (ret < 0)
return gnutls_assert_val(ret);
ret =
session->internals.auth_struct->
gnutls_generate_server_certificate(session, &buf);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
bufel = _gnutls_buffer_to_mbuffer(&buf);
}
return _gnutls_send_handshake(session, bufel, GNUTLS_HANDSHAKE_CERTIFICATE_PKT);
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
int _gnutls_recv_server_kx_message(gnutls_session_t session)
{
gnutls_buffer_st buf;
int ret = 0;
unsigned int optflag = 0;
if (session->internals.auth_struct->gnutls_process_server_kx !=
NULL) {
/* Server key exchange packet is optional for PSK. */
if (_gnutls_session_is_psk(session))
optflag = 1;
ret =
_gnutls_recv_handshake(session,
GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE,
optflag, &buf);
if (ret < 0) {
gnutls_assert();
return ret;
}
ret =
session->internals.auth_struct->
gnutls_process_server_kx(session, buf.data,
buf.length);
_gnutls_buffer_clear(&buf);
if (ret < 0) {
gnutls_assert();
return ret;
}
}
return ret;
}
int _gnutls_recv_server_crt_request(gnutls_session_t session)
{
gnutls_buffer_st buf;
int ret = 0;
if (session->internals.auth_struct->
gnutls_process_server_crt_request != NULL) {
ret =
_gnutls_recv_handshake(session,
GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST,
1, &buf);
if (ret < 0)
return ret;
if (ret == 0 && buf.length == 0) {
_gnutls_buffer_clear(&buf);
return 0; /* ignored */
}
ret =
session->internals.auth_struct->
gnutls_process_server_crt_request(session, buf.data,
buf.length);
_gnutls_buffer_clear(&buf);
if (ret < 0)
return ret;
}
return ret;
}
int _gnutls_recv_client_kx_message(gnutls_session_t session)
{
gnutls_buffer_st buf;
int ret = 0;
/* Do key exchange only if the algorithm permits it */
if (session->internals.auth_struct->gnutls_process_client_kx !=
NULL) {
ret =
_gnutls_recv_handshake(session,
GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE,
0, &buf);
if (ret < 0)
return ret;
ret =
session->internals.auth_struct->
gnutls_process_client_kx(session, buf.data,
buf.length);
_gnutls_buffer_clear(&buf);
if (ret < 0)
return ret;
}
return ret;
}
int _gnutls_recv_client_certificate(gnutls_session_t session)
{
gnutls_buffer_st buf;
int ret = 0;
int optional;
if (session->internals.auth_struct->
gnutls_process_client_certificate == NULL)
return 0;
/* if we have not requested a certificate then just return
*/
if (session->internals.send_cert_req == 0) {
return 0;
}
if (session->internals.send_cert_req == GNUTLS_CERT_REQUIRE)
optional = 0;
else
optional = 1;
ret =
_gnutls_recv_handshake(session,
GNUTLS_HANDSHAKE_CERTIFICATE_PKT,
optional, &buf);
if (ret < 0) {
/* Handle the case of old SSL3 clients who send
* a warning alert instead of an empty certificate to indicate
* no certificate.
*/
#ifdef ENABLE_SSL3
if (optional != 0 &&
ret == GNUTLS_E_WARNING_ALERT_RECEIVED &&
get_num_version(session) == GNUTLS_SSL3 &&
gnutls_alert_get(session) ==
GNUTLS_A_SSL3_NO_CERTIFICATE) {
/* SSL3 does not send an empty certificate,
* but this alert. So we just ignore it.
*/
gnutls_assert();
return 0;
}
#endif
/* certificate was required
*/
if ((ret == GNUTLS_E_WARNING_ALERT_RECEIVED
|| ret == GNUTLS_E_FATAL_ALERT_RECEIVED)
&& optional == 0) {
gnutls_assert();
return GNUTLS_E_NO_CERTIFICATE_FOUND;
}
return ret;
}
if (ret == 0 && buf.length == 0 && optional != 0) {
/* Client has not sent the certificate message.
* well I'm not sure we should accept this
* behaviour.
*/
gnutls_assert();
ret = 0;
goto cleanup;
}
ret =
session->internals.auth_struct->
gnutls_process_client_certificate(session, buf.data,
buf.length);
if (ret < 0 && ret != GNUTLS_E_NO_CERTIFICATE_FOUND) {
gnutls_assert();
goto cleanup;
}
/* ok we should expect a certificate verify message now
*/
if (ret == GNUTLS_E_NO_CERTIFICATE_FOUND && optional != 0)
ret = 0;
else
session->internals.hsk_flags |= HSK_CRT_VRFY_EXPECTED;
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
int _gnutls_recv_server_certificate(gnutls_session_t session)
{
gnutls_buffer_st buf;
int ret = 0;
if (session->internals.auth_struct->
gnutls_process_server_certificate != NULL) {
ret =
_gnutls_recv_handshake(session,
GNUTLS_HANDSHAKE_CERTIFICATE_PKT,
0, &buf);
if (ret < 0) {
gnutls_assert();
return ret;
}
ret =
session->internals.auth_struct->
gnutls_process_server_certificate(session, buf.data,
buf.length);
_gnutls_buffer_clear(&buf);
if (ret < 0) {
gnutls_assert();
return ret;
}
}
return ret;
}
/* Recv the client certificate verify. This packet may not
* arrive if the peer did not send us a certificate.
*/
int
_gnutls_recv_client_certificate_verify_message(gnutls_session_t session)
{
gnutls_buffer_st buf;
int ret = 0;
if (session->internals.auth_struct->
gnutls_process_client_crt_vrfy == NULL)
return 0;
if (session->internals.send_cert_req == 0 ||
(!(session->internals.hsk_flags & HSK_CRT_VRFY_EXPECTED))) {
return 0;
}
ret =
_gnutls_recv_handshake(session,
GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY,
1, &buf);
if (ret < 0)
return ret;
if (ret == 0 && buf.length == 0
&& session->internals.send_cert_req == GNUTLS_CERT_REQUIRE) {
/* certificate was required */
gnutls_assert();
ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
goto cleanup;
}
ret =
session->internals.auth_struct->
gnutls_process_client_crt_vrfy(session, buf.data, buf.length);
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
|