summaryrefslogtreecommitdiff
path: root/lib/gnutls_kx.c
blob: ee545243e58245f326c292ccedb516013e5ad3e2 (plain)
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
/*
 * Copyright (C) 2000, 2001, 2004, 2005  Free Software Foundation
 *
 * Author: Nikos Mavroyanopoulos
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */

/* 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 "gnutls_handshake.h"
#include "gnutls_kx.h"
#include "gnutls_dh.h"
#include "gnutls_errors.h"
#include "gnutls_algorithms.h"
#include "debug.h"
#include "gnutls_mpi.h"
#include <gnutls_state.h>
#include <gnutls_datum.h>
#include <gnutls_rsa_export.h>

/* This file contains important thing for the TLS handshake procedure.
 */

#define MASTER_SECRET "master secret"
static int generate_normal_master(gnutls_session_t session, int);

int _gnutls_generate_master(gnutls_session_t session, int keep_premaster)
{
    if (session->internals.resumed == RESUME_FALSE)
	return generate_normal_master(session, keep_premaster);
    return 0;
}

/* here we generate the TLS Master secret.
 */
#define PREMASTER session->key->key
static int generate_normal_master(gnutls_session_t session,
				  int keep_premaster)
{
    int ret = 0;
    opaque rnd[2 * TLS_RANDOM_SIZE + 1];
    char buf[64];

    memcpy(rnd, session->security_parameters.client_random,
	   TLS_RANDOM_SIZE);
    memcpy(&rnd[TLS_RANDOM_SIZE],
	   session->security_parameters.server_random, TLS_RANDOM_SIZE);

    _gnutls_hard_log("INT: PREMASTER SECRET[%d]: %s\n", PREMASTER.size,
		     _gnutls_bin2hex(PREMASTER.data, PREMASTER.size, buf,
				     sizeof(buf)));
    _gnutls_hard_log("INT: CLIENT RANDOM[%d]: %s\n", 32,
		     _gnutls_bin2hex(session->security_parameters.
				     client_random, 32, buf, sizeof(buf)));
    _gnutls_hard_log("INT: SERVER RANDOM[%d]: %s\n", 32,
		     _gnutls_bin2hex(session->security_parameters.
				     server_random, 32, buf, sizeof(buf)));

    if (gnutls_protocol_get_version(session) == GNUTLS_SSL3) {
	ret =
	    _gnutls_ssl3_generate_random(PREMASTER.data, PREMASTER.size,
					 rnd, 2 * TLS_RANDOM_SIZE,
					 TLS_MASTER_SIZE,
					 session->security_parameters.
					 master_secret);

    } else {
	ret =
	    _gnutls_PRF(PREMASTER.data, PREMASTER.size,
			MASTER_SECRET, strlen(MASTER_SECRET),
			rnd, 2 * TLS_RANDOM_SIZE, TLS_MASTER_SIZE,
			session->security_parameters.master_secret);
    }

    if (!keep_premaster)
	_gnutls_free_datum(&PREMASTER);

    if (ret < 0)
	return ret;

    _gnutls_hard_log("INT: MASTER SECRET: %s\n",
		     _gnutls_bin2hex(session->security_parameters.
				     master_secret, TLS_MASTER_SIZE, buf,
				     sizeof(buf)));

    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)
{
    uint8 *data = NULL;
    int data_size = 0;
    int ret = 0;

    if (session->internals.auth_struct->gnutls_generate_server_kx == NULL)
	return 0;

    data = NULL;
    data_size = 0;

    if (again == 0) {
	data_size =
	    session->internals.auth_struct->
	    gnutls_generate_server_kx(session, &data);

	if (data_size == GNUTLS_E_INT_RET_0) {
	    gnutls_assert();
	    return 0;
	}

	if (data_size < 0) {
	    gnutls_assert();
	    return data_size;
	}
    }

    ret =
	_gnutls_send_handshake(session, data, data_size,
			       GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE);
    gnutls_free(data);

    if (ret < 0) {
	gnutls_assert();
	return ret;
    }
    return data_size;
}

/* This function sends a certificate request message to the
 * client.
 */
int _gnutls_send_server_certificate_request(gnutls_session_t session,
					    int again)
{
    uint8 *data = NULL;
    int data_size = 0;
    int ret = 0;

    if (session->internals.auth_struct->
	gnutls_generate_server_certificate_request == NULL)
	return 0;

    if (session->internals.send_cert_req <= 0)
	return 0;

    data = NULL;
    data_size = 0;

    if (again == 0) {
	data_size =
	    session->internals.auth_struct->
	    gnutls_generate_server_certificate_request(session, &data);

	if (data_size < 0) {
	    gnutls_assert();
	    return data_size;
	}
    }
    ret =
	_gnutls_send_handshake(session, data, data_size,
			       GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST);
    gnutls_free(data);

    if (ret < 0) {
	gnutls_assert();
	return ret;
    }
    return data_size;
}


/* 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)
{
    uint8 *data;
    int data_size;
    int ret = 0;

    if (session->internals.auth_struct->gnutls_generate_client_kx == NULL)
	return 0;


    data = NULL;
    data_size = 0;

    if (again == 0) {
	data_size =
	    session->internals.auth_struct->
	    gnutls_generate_client_kx(session, &data);
	if (data_size < 0) {
	    gnutls_assert();
	    return data_size;
	}
    }
    ret =
	_gnutls_send_handshake(session, data, data_size,
			       GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE);
    gnutls_free(data);

    if (ret < 0) {
	gnutls_assert();
	return ret;
    }

    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)
{
    uint8 *data;
    int ret = 0;
    int data_size;

    /* 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->key->certificate_requested == 0)
	return 0;

    if (session->internals.auth_struct->gnutls_generate_client_cert_vrfy ==
	NULL) {
	gnutls_assert();
	return 0;		/* this algorithm does not support cli_cert_vrfy 
				 */
    }

    data = NULL;
    data_size = 0;

    if (again == 0) {
	data_size =
	    session->internals.auth_struct->
	    gnutls_generate_client_cert_vrfy(session, &data);
	if (data_size < 0) {
	    gnutls_assert();
	    return data_size;
	}
	if (data_size == 0)
	    return 0;

    }
    ret =
	_gnutls_send_handshake(session, data,
			       data_size, GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY);
    gnutls_free(data);

    return ret;
}


int _gnutls_recv_server_kx_message(gnutls_session_t session)
{
    uint8 *data = NULL;
    int datasize;
    int ret = 0;

    if (session->internals.auth_struct->gnutls_process_server_kx != NULL) {

	/* EXCEPTION FOR RSA_EXPORT cipher suite 
	 */
	if (_gnutls_session_is_export(session) != 0 &&
	    _gnutls_peers_cert_less_512(session) != 0) {
	    gnutls_assert();
	    return 0;
	}

	ret =
	    _gnutls_recv_handshake(session, &data,
				   &datasize,
				   GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE,
				   MANDATORY_PACKET);
	if (ret < 0) {
	    gnutls_assert();
	    return ret;
	}

	ret =
	    session->internals.auth_struct->
	    gnutls_process_server_kx(session, data, datasize);
	gnutls_free(data);

	if (ret < 0) {
	    gnutls_assert();
	    return ret;
	}

    }
    return ret;
}

int _gnutls_recv_server_certificate_request(gnutls_session_t session)
{
    uint8 *data;
    int datasize;
    int ret = 0;

    if (session->internals.auth_struct->
	gnutls_process_server_certificate_request != NULL) {

	ret =
	    _gnutls_recv_handshake(session, &data,
				   &datasize,
				   GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST,
				   OPTIONAL_PACKET);
	if (ret < 0)
	    return ret;

	if (ret == 0 && datasize == 0)
	    return 0;		/* ignored */

	ret =
	    session->internals.auth_struct->
	    gnutls_process_server_certificate_request(session, data,
						      datasize);
	gnutls_free(data);
	if (ret < 0)
	    return ret;

    }
    return ret;
}

int _gnutls_recv_client_kx_message(gnutls_session_t session)
{
    uint8 *data;
    int datasize;
    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, &data,
				   &datasize,
				   GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE,
				   MANDATORY_PACKET);
	if (ret < 0)
	    return ret;

	ret =
	    session->internals.auth_struct->
	    gnutls_process_client_kx(session, data, datasize);
	gnutls_free(data);
	if (ret < 0)
	    return ret;

    }

    return ret;
}


/* This is called when we want send our certificate
 */
int _gnutls_send_client_certificate(gnutls_session_t session, int again)
{
    uint8 *data = NULL;
    int data_size = 0;
    int ret = 0;


    if (session->key->certificate_requested == 0)
	return 0;

    if (session->internals.auth_struct->
	gnutls_generate_client_certificate == NULL)
	return 0;

    data = NULL;
    data_size = 0;

    if (again == 0) {
	if (gnutls_protocol_get_version(session) != GNUTLS_SSL3 ||
	    session->internals.selected_cert_list_length > 0) {
	    /* TLS 1.0 or SSL 3.0 with a valid certificate 
	     */
	    data_size =
		session->internals.auth_struct->
		gnutls_generate_client_certificate(session, &data);

	    if (data_size < 0) {
		gnutls_assert();
		return data_size;
	    }
	}
    }

    /* In the SSL 3.0 protocol we need to send a
     * no certificate alert instead of an
     * empty certificate.
     */
    if (gnutls_protocol_get_version(session) == GNUTLS_SSL3 &&
	session->internals.selected_cert_list_length == 0) {
	ret =
	    gnutls_alert_send(session, GNUTLS_AL_WARNING,
			      GNUTLS_A_SSL3_NO_CERTIFICATE);

    } else {			/* TLS 1.0 or SSL 3.0 with a valid certificate 
				 */
	ret =
	    _gnutls_send_handshake(session, data, data_size,
				   GNUTLS_HANDSHAKE_CERTIFICATE_PKT);
	gnutls_free(data);
    }

    if (ret < 0) {
	gnutls_assert();
	return ret;
    }

    return data_size;
}


/* This is called when we want send our certificate
 */
int _gnutls_send_server_certificate(gnutls_session_t session, int again)
{
    uint8 *data = NULL;
    int data_size = 0;
    int ret = 0;


    if (session->internals.auth_struct->
	gnutls_generate_server_certificate == NULL)
	return 0;

    data = NULL;
    data_size = 0;

    if (again == 0) {
	data_size =
	    session->internals.auth_struct->
	    gnutls_generate_server_certificate(session, &data);

	if (data_size < 0) {
	    gnutls_assert();
	    return data_size;
	}
    }
    ret =
	_gnutls_send_handshake(session, data, data_size,
			       GNUTLS_HANDSHAKE_CERTIFICATE_PKT);
    gnutls_free(data);

    if (ret < 0) {
	gnutls_assert();
	return ret;
    }

    return data_size;
}


int _gnutls_recv_client_certificate(gnutls_session_t session)
{
    int datasize;
    opaque *data;
    int ret = 0;
    int optional;

    if (session->internals.auth_struct->
	gnutls_process_client_certificate != NULL) {

	/* 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 = MANDATORY_PACKET;
	else
	    optional = OPTIONAL_PACKET;

	ret =
	    _gnutls_recv_handshake(session, &data,
				   &datasize,
				   GNUTLS_HANDSHAKE_CERTIFICATE_PKT, optional);
	if (ret < 0) {
	    if (optional == OPTIONAL_PACKET &&
		ret == GNUTLS_E_WARNING_ALERT_RECEIVED &&
		gnutls_protocol_get_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;
	    }
	    /* certificate was required */
	    if (optional == MANDATORY_PACKET) {
		gnutls_assert();
		return GNUTLS_E_NO_CERTIFICATE_FOUND;
	    }

	    return ret;
	}

	if (ret == 0 && datasize == 0 && optional == OPTIONAL_PACKET) {
	    /* Client has not sent the certificate message.
	     * well I'm not sure we should accept this
	     * behaviour.
	     */
	    gnutls_assert();
	    return 0;
	}


	ret =
	    session->internals.auth_struct->
	    gnutls_process_client_certificate(session, data, datasize);
	gnutls_free(data);
	if (ret < 0 && ret != GNUTLS_E_NO_CERTIFICATE_FOUND) {
	    gnutls_assert();
	    return ret;
	}

	/* ok we should expect a certificate verify message now 
	 */
	if (ret == GNUTLS_E_NO_CERTIFICATE_FOUND
	    && optional == OPTIONAL_PACKET)
	    ret = 0;
	else
	    session->key->certificate_requested = 1;

    }

    return ret;
}

int _gnutls_recv_server_certificate(gnutls_session_t session)
{
    int datasize;
    opaque *data;
    int ret = 0;

    if (session->internals.auth_struct->
	gnutls_process_server_certificate != NULL) {

	ret =
	    _gnutls_recv_handshake(session, &data,
				   &datasize,
				   GNUTLS_HANDSHAKE_CERTIFICATE_PKT,
				   MANDATORY_PACKET);
	if (ret < 0) {
	    gnutls_assert();
	    return ret;
	}

	ret =
	    session->internals.auth_struct->
	    gnutls_process_server_certificate(session, data, datasize);
	gnutls_free(data);
	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)
{
    uint8 *data;
    int datasize;
    int ret = 0;


    if (session->internals.auth_struct->gnutls_process_client_cert_vrfy !=
	NULL) {

	if (session->internals.send_cert_req == 0 ||
	    session->key->certificate_requested == 0) {
	    return 0;
	}

	ret =
	    _gnutls_recv_handshake(session, &data,
				   &datasize,
				   GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY,
				   OPTIONAL_PACKET);
	if (ret < 0)
	    return ret;

	if (ret == 0 && datasize == 0
	    && session->internals.send_cert_req == GNUTLS_CERT_REQUIRE) {
	    /* certificate was required */
	    gnutls_assert();
	    return GNUTLS_E_NO_CERTIFICATE_FOUND;
	}

	ret =
	    session->internals.auth_struct->
	    gnutls_process_client_cert_vrfy(session, data, datasize);
	gnutls_free(data);
	if (ret < 0)
	    return ret;

    }

    return ret;
}