Add.
[gnutls.git] / lib / gnutls_kx.c
blob950366d52fd1cc77f79fdc929b1495d0e4dd31cf
1 /*
2 * Copyright (C) 2000, 2001, 2004, 2005, 2006, 2008 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 /* This file contains functions which are wrappers for the key exchange
26 * part of TLS. They are called by the handshake functions (gnutls_handshake)
29 #include "gnutls_int.h"
30 #include "gnutls_handshake.h"
31 #include "gnutls_kx.h"
32 #include "gnutls_dh.h"
33 #include "gnutls_errors.h"
34 #include "gnutls_algorithms.h"
35 #include "debug.h"
36 #include "gnutls_mpi.h"
37 #include <gnutls_state.h>
38 #include <gnutls_datum.h>
39 #include <gnutls_rsa_export.h>
41 /* This file contains important thing for the TLS handshake procedure.
44 #define MASTER_SECRET "master secret"
45 static int generate_normal_master (gnutls_session_t session, int);
47 int
48 _gnutls_generate_master (gnutls_session_t session, int keep_premaster)
50 if (session->internals.resumed == RESUME_FALSE)
51 return generate_normal_master (session, keep_premaster);
52 return 0;
55 /* here we generate the TLS Master secret.
57 #define PREMASTER session->key->key
58 static int
59 generate_normal_master (gnutls_session_t session, int keep_premaster)
61 int ret = 0;
62 char buf[512];
64 _gnutls_hard_log ("INT: PREMASTER SECRET[%d]: %s\n", PREMASTER.size,
65 _gnutls_bin2hex (PREMASTER.data, PREMASTER.size, buf,
66 sizeof (buf)));
67 _gnutls_hard_log ("INT: CLIENT RANDOM[%d]: %s\n", 32,
68 _gnutls_bin2hex (session->security_parameters.
69 client_random, 32, buf, sizeof (buf)));
70 _gnutls_hard_log ("INT: SERVER RANDOM[%d]: %s\n", 32,
71 _gnutls_bin2hex (session->security_parameters.
72 server_random, 32, buf, sizeof (buf)));
74 if (gnutls_protocol_get_version (session) == GNUTLS_SSL3)
76 opaque rnd[2 * TLS_RANDOM_SIZE + 1];
78 memcpy (rnd, session->security_parameters.client_random, TLS_RANDOM_SIZE);
79 memcpy (&rnd[TLS_RANDOM_SIZE],
80 session->security_parameters.server_random, TLS_RANDOM_SIZE);
82 ret =
83 _gnutls_ssl3_generate_random (PREMASTER.data, PREMASTER.size,
84 rnd, 2 * TLS_RANDOM_SIZE,
85 TLS_MASTER_SIZE,
86 session->security_parameters.
87 master_secret);
90 else if (session->security_parameters.extensions.oprfi_client_len > 0 &&
91 session->security_parameters.extensions.oprfi_server_len > 0)
93 opaque *rnd;
94 size_t rndlen = 2 * TLS_RANDOM_SIZE;
96 rndlen += session->security_parameters.extensions.oprfi_client_len;
97 rndlen += session->security_parameters.extensions.oprfi_server_len;
99 rnd = gnutls_malloc (rndlen + 1);
100 if (!rnd)
102 gnutls_assert ();
103 return GNUTLS_E_MEMORY_ERROR;
106 _gnutls_hard_log ("INT: CLIENT OPRFI[%d]: %s\n",
107 session->security_parameters.
108 extensions.oprfi_server_len,
109 _gnutls_bin2hex (session->security_parameters.
110 extensions.oprfi_client,
111 session->security_parameters.
112 extensions.oprfi_client_len,
113 buf, sizeof (buf)));
114 _gnutls_hard_log ("INT: SERVER OPRFI[%d]: %s\n",
115 session->security_parameters.
116 extensions.oprfi_server_len,
117 _gnutls_bin2hex (session->security_parameters.
118 extensions.oprfi_server,
119 session->security_parameters.
120 extensions.oprfi_server_len,
121 buf, sizeof (buf)));
123 memcpy (rnd, session->security_parameters.client_random,
124 TLS_RANDOM_SIZE);
125 memcpy (rnd + TLS_RANDOM_SIZE,
126 session->security_parameters.extensions.oprfi_client,
127 session->security_parameters.extensions.oprfi_client_len);
128 memcpy (rnd + TLS_RANDOM_SIZE +
129 session->security_parameters.extensions.oprfi_client_len,
130 session->security_parameters.server_random,
131 TLS_RANDOM_SIZE);
132 memcpy (rnd + TLS_RANDOM_SIZE +
133 session->security_parameters.extensions.oprfi_client_len +
134 TLS_RANDOM_SIZE,
135 session->security_parameters.extensions.oprfi_server,
136 session->security_parameters.extensions.oprfi_server_len);
138 ret = _gnutls_PRF (session, PREMASTER.data, PREMASTER.size,
139 MASTER_SECRET, strlen (MASTER_SECRET),
140 rnd, rndlen, TLS_MASTER_SIZE,
141 session->security_parameters.master_secret);
143 gnutls_free (rnd);
145 else
147 opaque rnd[2 * TLS_RANDOM_SIZE + 1];
149 memcpy (rnd, session->security_parameters.client_random, TLS_RANDOM_SIZE);
150 memcpy (&rnd[TLS_RANDOM_SIZE],
151 session->security_parameters.server_random, TLS_RANDOM_SIZE);
153 ret =
154 _gnutls_PRF (session, PREMASTER.data, PREMASTER.size,
155 MASTER_SECRET, strlen (MASTER_SECRET),
156 rnd, 2 * TLS_RANDOM_SIZE, TLS_MASTER_SIZE,
157 session->security_parameters.master_secret);
160 /* TLS/IA inner secret is derived from the master secret. */
161 memcpy (session->security_parameters.inner_secret,
162 session->security_parameters.master_secret, TLS_MASTER_SIZE);
164 if (!keep_premaster)
165 _gnutls_free_datum (&PREMASTER);
167 if (ret < 0)
168 return ret;
170 _gnutls_hard_log ("INT: MASTER SECRET: %s\n",
171 _gnutls_bin2hex (session->security_parameters.
172 master_secret, TLS_MASTER_SIZE, buf,
173 sizeof (buf)));
175 return ret;
179 /* This is called when we want to receive the key exchange message of the
180 * server. It does nothing if this type of message is not required
181 * by the selected ciphersuite.
184 _gnutls_send_server_kx_message (gnutls_session_t session, int again)
186 uint8_t *data = NULL;
187 int data_size = 0;
188 int ret = 0;
190 if (session->internals.auth_struct->gnutls_generate_server_kx == NULL)
191 return 0;
193 data = NULL;
194 data_size = 0;
196 if (again == 0)
198 data_size =
199 session->internals.auth_struct->
200 gnutls_generate_server_kx (session, &data);
202 if (data_size == GNUTLS_E_INT_RET_0)
204 gnutls_assert ();
205 return 0;
208 if (data_size < 0)
210 gnutls_assert ();
211 return data_size;
215 ret =
216 _gnutls_send_handshake (session, data, data_size,
217 GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE);
218 gnutls_free (data);
220 if (ret < 0)
222 gnutls_assert ();
223 return ret;
225 return data_size;
228 /* This function sends a certificate request message to the
229 * client.
232 _gnutls_send_server_certificate_request (gnutls_session_t session, int again)
234 uint8_t *data = NULL;
235 int data_size = 0;
236 int ret = 0;
238 if (session->internals.auth_struct->
239 gnutls_generate_server_certificate_request == NULL)
240 return 0;
242 if (session->internals.send_cert_req <= 0)
243 return 0;
245 data = NULL;
246 data_size = 0;
248 if (again == 0)
250 data_size =
251 session->internals.auth_struct->
252 gnutls_generate_server_certificate_request (session, &data);
254 if (data_size < 0)
256 gnutls_assert ();
257 return data_size;
260 ret =
261 _gnutls_send_handshake (session, data, data_size,
262 GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST);
263 gnutls_free (data);
265 if (ret < 0)
267 gnutls_assert ();
268 return ret;
270 return data_size;
274 /* This is the function for the client to send the key
275 * exchange message
278 _gnutls_send_client_kx_message (gnutls_session_t session, int again)
280 uint8_t *data;
281 int data_size;
282 int ret = 0;
284 if (session->internals.auth_struct->gnutls_generate_client_kx == NULL)
285 return 0;
288 data = NULL;
289 data_size = 0;
291 if (again == 0)
293 data_size =
294 session->internals.auth_struct->
295 gnutls_generate_client_kx (session, &data);
296 if (data_size < 0)
298 gnutls_assert ();
299 return data_size;
302 ret =
303 _gnutls_send_handshake (session, data, data_size,
304 GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE);
305 gnutls_free (data);
307 if (ret < 0)
309 gnutls_assert ();
310 return ret;
313 return ret;
317 /* This is the function for the client to send the certificate
318 * verify message
321 _gnutls_send_client_certificate_verify (gnutls_session_t session, int again)
323 uint8_t *data;
324 int ret = 0;
325 int data_size;
327 /* This is a packet that is only sent by the client
329 if (session->security_parameters.entity == GNUTLS_SERVER)
330 return 0;
332 /* if certificate verify is not needed just exit
334 if (session->key->certificate_requested == 0)
335 return 0;
337 if (session->internals.auth_struct->gnutls_generate_client_cert_vrfy ==
338 NULL)
340 gnutls_assert ();
341 return 0; /* this algorithm does not support cli_cert_vrfy
345 data = NULL;
346 data_size = 0;
348 if (again == 0)
350 data_size =
351 session->internals.auth_struct->
352 gnutls_generate_client_cert_vrfy (session, &data);
353 if (data_size < 0)
355 gnutls_assert ();
356 return data_size;
358 if (data_size == 0)
359 return 0;
362 ret =
363 _gnutls_send_handshake (session, data,
364 data_size, GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY);
365 gnutls_free (data);
367 return ret;
372 _gnutls_recv_server_kx_message (gnutls_session_t session)
374 uint8_t *data = NULL;
375 int datasize;
376 int ret = 0;
377 Optional optflag = MANDATORY_PACKET;
379 if (session->internals.auth_struct->gnutls_process_server_kx != NULL)
382 /* EXCEPTION FOR RSA_EXPORT cipher suite
384 if (_gnutls_session_is_export (session) != 0 &&
385 _gnutls_peers_cert_less_512 (session) != 0)
387 gnutls_assert ();
388 return 0;
391 /* Server key exchange packet is optional for PSK. */
392 if (_gnutls_session_is_psk (session))
393 optflag = OPTIONAL_PACKET;
395 ret =
396 _gnutls_recv_handshake (session, &data,
397 &datasize,
398 GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE,
399 optflag);
400 if (ret < 0)
402 gnutls_assert ();
403 return ret;
406 ret =
407 session->internals.auth_struct->
408 gnutls_process_server_kx (session, data, datasize);
409 gnutls_free (data);
411 if (ret < 0)
413 gnutls_assert ();
414 return ret;
418 return ret;
422 _gnutls_recv_server_certificate_request (gnutls_session_t session)
424 uint8_t *data;
425 int datasize;
426 int ret = 0;
428 if (session->internals.auth_struct->
429 gnutls_process_server_certificate_request != NULL)
432 ret =
433 _gnutls_recv_handshake (session, &data,
434 &datasize,
435 GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST,
436 OPTIONAL_PACKET);
437 if (ret < 0)
438 return ret;
440 if (ret == 0 && datasize == 0)
441 return 0; /* ignored */
443 ret =
444 session->internals.auth_struct->
445 gnutls_process_server_certificate_request (session, data, datasize);
446 gnutls_free (data);
447 if (ret < 0)
448 return ret;
451 return ret;
455 _gnutls_recv_client_kx_message (gnutls_session_t session)
457 uint8_t *data;
458 int datasize;
459 int ret = 0;
462 /* Do key exchange only if the algorithm permits it */
463 if (session->internals.auth_struct->gnutls_process_client_kx != NULL)
466 ret =
467 _gnutls_recv_handshake (session, &data,
468 &datasize,
469 GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE,
470 MANDATORY_PACKET);
471 if (ret < 0)
472 return ret;
474 ret =
475 session->internals.auth_struct->
476 gnutls_process_client_kx (session, data, datasize);
477 gnutls_free (data);
478 if (ret < 0)
479 return ret;
483 return ret;
487 /* This is called when we want send our certificate
490 _gnutls_send_client_certificate (gnutls_session_t session, int again)
492 uint8_t *data = NULL;
493 int data_size = 0;
494 int ret = 0;
497 if (session->key->certificate_requested == 0)
498 return 0;
500 if (session->internals.auth_struct->
501 gnutls_generate_client_certificate == NULL)
502 return 0;
504 data = NULL;
505 data_size = 0;
507 if (again == 0)
509 if (gnutls_protocol_get_version (session) != GNUTLS_SSL3 ||
510 session->internals.selected_cert_list_length > 0)
512 /* TLS 1.0 or SSL 3.0 with a valid certificate
514 data_size =
515 session->internals.auth_struct->
516 gnutls_generate_client_certificate (session, &data);
518 if (data_size < 0)
520 gnutls_assert ();
521 return data_size;
526 /* In the SSL 3.0 protocol we need to send a
527 * no certificate alert instead of an
528 * empty certificate.
530 if (gnutls_protocol_get_version (session) == GNUTLS_SSL3 &&
531 session->internals.selected_cert_list_length == 0)
533 ret =
534 gnutls_alert_send (session, GNUTLS_AL_WARNING,
535 GNUTLS_A_SSL3_NO_CERTIFICATE);
538 else
539 { /* TLS 1.0 or SSL 3.0 with a valid certificate
541 ret =
542 _gnutls_send_handshake (session, data, data_size,
543 GNUTLS_HANDSHAKE_CERTIFICATE_PKT);
544 gnutls_free (data);
547 if (ret < 0)
549 gnutls_assert ();
550 return ret;
553 return data_size;
557 /* This is called when we want send our certificate
560 _gnutls_send_server_certificate (gnutls_session_t session, int again)
562 uint8_t *data = NULL;
563 int data_size = 0;
564 int ret = 0;
567 if (session->internals.auth_struct->
568 gnutls_generate_server_certificate == NULL)
569 return 0;
571 data = NULL;
572 data_size = 0;
574 if (again == 0)
576 data_size =
577 session->internals.auth_struct->
578 gnutls_generate_server_certificate (session, &data);
580 if (data_size < 0)
582 gnutls_assert ();
583 return data_size;
586 ret =
587 _gnutls_send_handshake (session, data, data_size,
588 GNUTLS_HANDSHAKE_CERTIFICATE_PKT);
589 gnutls_free (data);
591 if (ret < 0)
593 gnutls_assert ();
594 return ret;
597 return data_size;
602 _gnutls_recv_client_certificate (gnutls_session_t session)
604 int datasize;
605 opaque *data;
606 int ret = 0;
607 int optional;
609 if (session->internals.auth_struct->
610 gnutls_process_client_certificate != NULL)
613 /* if we have not requested a certificate then just return
615 if (session->internals.send_cert_req == 0)
617 return 0;
620 if (session->internals.send_cert_req == GNUTLS_CERT_REQUIRE)
621 optional = MANDATORY_PACKET;
622 else
623 optional = OPTIONAL_PACKET;
625 ret =
626 _gnutls_recv_handshake (session, &data,
627 &datasize,
628 GNUTLS_HANDSHAKE_CERTIFICATE_PKT, optional);
630 if (ret < 0)
632 /* Handle the case of old SSL3 clients who send
633 * a warning alert instead of an empty certificate to indicate
634 * no certificate.
636 if (optional == OPTIONAL_PACKET &&
637 ret == GNUTLS_E_WARNING_ALERT_RECEIVED &&
638 gnutls_protocol_get_version (session) == GNUTLS_SSL3 &&
639 gnutls_alert_get (session) == GNUTLS_A_SSL3_NO_CERTIFICATE)
642 /* SSL3 does not send an empty certificate,
643 * but this alert. So we just ignore it.
645 gnutls_assert ();
646 return 0;
649 /* certificate was required
651 if ((ret == GNUTLS_E_WARNING_ALERT_RECEIVED
652 || ret == GNUTLS_E_FATAL_ALERT_RECEIVED)
653 && optional == MANDATORY_PACKET)
655 gnutls_assert ();
656 return GNUTLS_E_NO_CERTIFICATE_FOUND;
659 return ret;
662 if (ret == 0 && datasize == 0 && optional == OPTIONAL_PACKET)
664 /* Client has not sent the certificate message.
665 * well I'm not sure we should accept this
666 * behaviour.
668 gnutls_assert ();
669 return 0;
671 ret =
672 session->internals.auth_struct->
673 gnutls_process_client_certificate (session, data, datasize);
675 gnutls_free (data);
676 if (ret < 0 && ret != GNUTLS_E_NO_CERTIFICATE_FOUND)
678 gnutls_assert ();
679 return ret;
682 /* ok we should expect a certificate verify message now
684 if (ret == GNUTLS_E_NO_CERTIFICATE_FOUND && optional == OPTIONAL_PACKET)
685 ret = 0;
686 else
687 session->key->certificate_requested = 1;
691 return ret;
695 _gnutls_recv_server_certificate (gnutls_session_t session)
697 int datasize;
698 opaque *data;
699 int ret = 0;
701 if (session->internals.auth_struct->
702 gnutls_process_server_certificate != NULL)
705 ret =
706 _gnutls_recv_handshake (session, &data,
707 &datasize,
708 GNUTLS_HANDSHAKE_CERTIFICATE_PKT,
709 MANDATORY_PACKET);
710 if (ret < 0)
712 gnutls_assert ();
713 return ret;
716 ret =
717 session->internals.auth_struct->
718 gnutls_process_server_certificate (session, data, datasize);
719 gnutls_free (data);
720 if (ret < 0)
722 gnutls_assert ();
723 return ret;
727 return ret;
731 /* Recv the client certificate verify. This packet may not
732 * arrive if the peer did not send us a certificate.
735 _gnutls_recv_client_certificate_verify_message (gnutls_session_t session)
737 uint8_t *data;
738 int datasize;
739 int ret = 0;
742 if (session->internals.auth_struct->gnutls_process_client_cert_vrfy != NULL)
745 if (session->internals.send_cert_req == 0 ||
746 session->key->certificate_requested == 0)
748 return 0;
751 ret =
752 _gnutls_recv_handshake (session, &data,
753 &datasize,
754 GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY,
755 OPTIONAL_PACKET);
756 if (ret < 0)
757 return ret;
759 if (ret == 0 && datasize == 0
760 && session->internals.send_cert_req == GNUTLS_CERT_REQUIRE)
762 /* certificate was required */
763 gnutls_assert ();
764 return GNUTLS_E_NO_CERTIFICATE_FOUND;
767 ret =
768 session->internals.auth_struct->
769 gnutls_process_client_cert_vrfy (session, data, datasize);
770 gnutls_free (data);
771 if (ret < 0)
772 return ret;
776 return ret;