Corrected SRP-RSA ciphersuites when used under TLS 1.2.
[gnutls.git] / lib / gnutls_handshake.c
blobe4a63e4d563d4a2e714e544016708de231760ab4
1 /*
2 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 * 2009, 2010 Free Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * USA
26 /* Functions that relate to the TLS handshake procedure.
29 #include "gnutls_int.h"
30 #include "gnutls_errors.h"
31 #include "gnutls_dh.h"
32 #include "debug.h"
33 #include "gnutls_algorithms.h"
34 #include "gnutls_compress.h"
35 #include "gnutls_cipher.h"
36 #include "gnutls_buffers.h"
37 #include "gnutls_mbuffers.h"
38 #include "gnutls_kx.h"
39 #include "gnutls_handshake.h"
40 #include "gnutls_num.h"
41 #include "gnutls_hash_int.h"
42 #include "gnutls_db.h"
43 #include "gnutls_extensions.h"
44 #include "gnutls_supplemental.h"
45 #include "gnutls_auth.h"
46 #include "gnutls_v2_compat.h"
47 #include "auth_cert.h"
48 #include "gnutls_cert.h"
49 #include "gnutls_constate.h"
50 #include <gnutls_record.h>
51 #include <gnutls_state.h>
52 #include <ext_srp.h>
53 #include <ext_session_ticket.h>
54 #include <ext_safe_renegotiation.h>
55 #include <gnutls_rsa_export.h> /* for gnutls_get_rsa_params() */
56 #include <auth_anon.h> /* for gnutls_anon_server_credentials_t */
57 #include <auth_psk.h> /* for gnutls_psk_server_credentials_t */
58 #include <random.h>
60 #ifdef HANDSHAKE_DEBUG
61 #define ERR(x, y) _gnutls_handshake_log("HSK[%p]: %s (%d)\n", session, x,y)
62 #else
63 #define ERR(x, y)
64 #endif
66 #define TRUE 1
67 #define FALSE 0
69 static int _gnutls_handshake_hash_init (gnutls_session_t session);
70 static int _gnutls_server_select_comp_method (gnutls_session_t session,
71 opaque * data, int datalen);
72 static int
73 _gnutls_handshake_hash_add_recvd (gnutls_session_t session,
74 gnutls_handshake_description_t recv_type,
75 opaque * header, uint16_t header_size,
76 opaque * dataptr, uint32_t datalen);
80 /* Clears the handshake hash buffers and handles.
82 void
83 _gnutls_handshake_hash_buffers_clear (gnutls_session_t session)
85 if (session->security_parameters.handshake_mac_handle_type ==
86 HANDSHAKE_MAC_TYPE_10)
88 _gnutls_hash_deinit (&session->internals.handshake_mac_handle.tls10.md5,
89 NULL);
90 _gnutls_hash_deinit (&session->internals.handshake_mac_handle.tls10.sha,
91 NULL);
93 else if (session->security_parameters.handshake_mac_handle_type ==
94 HANDSHAKE_MAC_TYPE_12)
96 _gnutls_hash_deinit (&session->internals.handshake_mac_handle.tls12.
97 sha256, NULL);
98 _gnutls_hash_deinit (&session->internals.handshake_mac_handle.tls12.
99 sha1, NULL);
101 session->security_parameters.handshake_mac_handle_type = 0;
102 session->internals.handshake_mac_handle_init = 0;
103 _gnutls_handshake_buffer_clear (session);
106 /* this will copy the required values for resuming to
107 * internals, and to security_parameters.
108 * this will keep as less data to security_parameters.
110 static void
111 resume_copy_required_values (gnutls_session_t session)
113 /* get the new random values */
114 memcpy (session->internals.resumed_security_parameters.server_random,
115 session->security_parameters.server_random, GNUTLS_RANDOM_SIZE);
116 memcpy (session->internals.resumed_security_parameters.client_random,
117 session->security_parameters.client_random, GNUTLS_RANDOM_SIZE);
119 /* keep the ciphersuite and compression
120 * That is because the client must see these in our
121 * hello message.
123 memcpy (session->security_parameters.current_cipher_suite.suite,
124 session->internals.resumed_security_parameters.current_cipher_suite.
125 suite, 2);
126 session->internals.compression_method = session->internals.resumed_compression_method;
128 _gnutls_epoch_set_cipher_suite (session, EPOCH_NEXT,
129 &session->
130 internals.resumed_security_parameters.current_cipher_suite);
131 _gnutls_epoch_set_compression (session, EPOCH_NEXT,
132 session->
133 internals.resumed_compression_method);
135 /* or write_compression_algorithm
136 * they are the same
139 session->security_parameters.entity =
140 session->internals.resumed_security_parameters.entity;
142 _gnutls_set_current_version (session,
143 session->internals.resumed_security_parameters.
144 version);
146 session->security_parameters.cert_type =
147 session->internals.resumed_security_parameters.cert_type;
149 memcpy (session->security_parameters.session_id,
150 session->internals.resumed_security_parameters.session_id,
151 sizeof (session->security_parameters.session_id));
152 session->security_parameters.session_id_size =
153 session->internals.resumed_security_parameters.session_id_size;
157 void
158 _gnutls_set_server_random (gnutls_session_t session, uint8_t * rnd)
160 memcpy (session->security_parameters.server_random, rnd,
161 GNUTLS_RANDOM_SIZE);
164 void
165 _gnutls_set_client_random (gnutls_session_t session, uint8_t * rnd)
167 memcpy (session->security_parameters.client_random, rnd,
168 GNUTLS_RANDOM_SIZE);
171 /* Calculate The SSL3 Finished message
173 #define SSL3_CLIENT_MSG "CLNT"
174 #define SSL3_SERVER_MSG "SRVR"
175 #define SSL_MSG_LEN 4
176 static int
177 _gnutls_ssl3_finished (gnutls_session_t session, int type, opaque * ret)
179 const int siz = SSL_MSG_LEN;
180 digest_hd_st td_md5;
181 digest_hd_st td_sha;
182 const char *mesg;
183 int rc;
185 if (session->security_parameters.handshake_mac_handle_type ==
186 HANDSHAKE_MAC_TYPE_10)
188 rc =
189 _gnutls_hash_copy (&td_md5,
190 &session->internals.handshake_mac_handle.tls10.
191 md5);
192 if (rc < 0)
194 gnutls_assert ();
195 return rc;
198 rc =
199 _gnutls_hash_copy (&td_sha,
200 &session->internals.handshake_mac_handle.tls10.
201 sha);
202 if (rc < 0)
204 gnutls_assert ();
205 _gnutls_hash_deinit (&td_md5, NULL);
206 return rc;
209 else
211 gnutls_assert ();
212 return GNUTLS_E_INTERNAL_ERROR;
215 if (type == GNUTLS_SERVER)
217 mesg = SSL3_SERVER_MSG;
219 else
221 mesg = SSL3_CLIENT_MSG;
224 _gnutls_hash (&td_md5, mesg, siz);
225 _gnutls_hash (&td_sha, mesg, siz);
227 _gnutls_mac_deinit_ssl3_handshake (&td_md5, ret,
228 session->
229 security_parameters.master_secret,
230 GNUTLS_MASTER_SIZE);
231 _gnutls_mac_deinit_ssl3_handshake (&td_sha, &ret[16],
232 session->
233 security_parameters.master_secret,
234 GNUTLS_MASTER_SIZE);
236 return 0;
239 /* Hash the handshake messages as required by TLS 1.0
241 #define SERVER_MSG "server finished"
242 #define CLIENT_MSG "client finished"
243 #define TLS_MSG_LEN 15
244 static int
245 _gnutls_finished (gnutls_session_t session, int type, void *ret)
247 const int siz = TLS_MSG_LEN;
248 opaque concat[MAX_HASH_SIZE + 16 /*MD5 */ ];
249 size_t len = 20 + 16;
250 const char *mesg;
251 digest_hd_st td_md5;
252 digest_hd_st td_sha;
253 int rc;
255 if (session->security_parameters.handshake_mac_handle_type ==
256 HANDSHAKE_MAC_TYPE_10)
258 rc =
259 _gnutls_hash_copy (&td_md5,
260 &session->internals.handshake_mac_handle.tls10.
261 md5);
262 if (rc < 0)
264 gnutls_assert ();
265 return rc;
268 rc =
269 _gnutls_hash_copy (&td_sha,
270 &session->internals.handshake_mac_handle.tls10.
271 sha);
272 if (rc < 0)
274 gnutls_assert ();
275 _gnutls_hash_deinit (&td_md5, NULL);
276 return rc;
279 _gnutls_hash_deinit (&td_md5, concat);
280 _gnutls_hash_deinit (&td_sha, &concat[16]);
282 else if (session->security_parameters.handshake_mac_handle_type ==
283 HANDSHAKE_MAC_TYPE_12)
285 rc =
286 _gnutls_hash_copy (&td_sha,
287 &session->internals.handshake_mac_handle.tls12.
288 sha256);
289 if (rc < 0)
291 gnutls_assert ();
292 return rc;
295 _gnutls_hash_deinit (&td_sha, concat);
296 len = _gnutls_hash_get_algo_len (td_sha.algorithm);
299 if (type == GNUTLS_SERVER)
301 mesg = SERVER_MSG;
303 else
305 mesg = CLIENT_MSG;
308 return _gnutls_PRF (session, session->security_parameters.master_secret,
309 GNUTLS_MASTER_SIZE, mesg, siz, concat, len, 12, ret);
312 /* this function will produce GNUTLS_RANDOM_SIZE==32 bytes of random data
313 * and put it to dst.
316 _gnutls_tls_create_random (opaque * dst)
318 uint32_t tim;
319 int ret;
321 /* Use weak random numbers for the most of the
322 * buffer except for the first 4 that are the
323 * system's time.
326 tim = gnutls_time (NULL);
327 /* generate server random value */
328 _gnutls_write_uint32 (tim, dst);
330 ret = _gnutls_rnd (GNUTLS_RND_NONCE, &dst[4], GNUTLS_RANDOM_SIZE - 4);
331 if (ret < 0)
333 gnutls_assert ();
334 return ret;
337 return 0;
340 /* returns the 0 on success or a negative value.
343 _gnutls_negotiate_version (gnutls_session_t session,
344 gnutls_protocol_t adv_version)
346 int ret;
348 /* if we do not support that version */
349 if (_gnutls_version_is_supported (session, adv_version) == 0)
351 /* If he requested something we do not support
352 * then we send him the highest we support.
354 ret = _gnutls_version_max (session);
355 if (ret == GNUTLS_VERSION_UNKNOWN)
357 /* this check is not really needed.
359 gnutls_assert ();
360 return GNUTLS_E_UNKNOWN_CIPHER_SUITE;
363 else
365 ret = adv_version;
368 _gnutls_set_current_version (session, ret);
370 return ret;
374 _gnutls_user_hello_func (gnutls_session_t session,
375 gnutls_protocol_t adv_version)
377 int ret;
379 if (session->internals.user_hello_func != NULL)
381 ret = session->internals.user_hello_func (session);
382 if (ret < 0)
384 gnutls_assert ();
385 return ret;
387 /* Here we need to renegotiate the version since the callee might
388 * have disabled some TLS versions.
390 ret = _gnutls_negotiate_version (session, adv_version);
391 if (ret < 0)
393 gnutls_assert ();
394 return ret;
397 return 0;
400 /* Read a client hello packet.
401 * A client hello must be a known version client hello
402 * or version 2.0 client hello (only for compatibility
403 * since SSL version 2.0 is not supported).
405 static int
406 _gnutls_read_client_hello (gnutls_session_t session, opaque * data,
407 int datalen)
409 uint8_t session_id_len;
410 int pos = 0, ret;
411 uint16_t suite_size, comp_size;
412 gnutls_protocol_t adv_version;
413 int neg_version;
414 int len = datalen;
415 opaque rnd[GNUTLS_RANDOM_SIZE], *suite_ptr, *comp_ptr, *session_id;
417 if (session->internals.v2_hello != 0)
418 { /* version 2.0 */
419 return _gnutls_read_client_hello_v2 (session, data, datalen);
421 DECR_LEN (len, 2);
423 _gnutls_handshake_log ("HSK[%p]: Client's version: %d.%d\n", session,
424 data[pos], data[pos + 1]);
426 adv_version = _gnutls_version_get (data[pos], data[pos + 1]);
427 set_adv_version (session, data[pos], data[pos + 1]);
428 pos += 2;
430 neg_version = _gnutls_negotiate_version (session, adv_version);
431 if (neg_version < 0)
433 gnutls_assert ();
434 return neg_version;
437 /* Read client random value.
439 DECR_LEN (len, GNUTLS_RANDOM_SIZE);
440 _gnutls_set_client_random (session, &data[pos]);
441 pos += GNUTLS_RANDOM_SIZE;
443 _gnutls_tls_create_random (rnd);
444 _gnutls_set_server_random (session, rnd);
446 session->security_parameters.timestamp = gnutls_time (NULL);
448 DECR_LEN (len, 1);
449 session_id_len = data[pos++];
451 /* RESUME SESSION
453 if (session_id_len > TLS_MAX_SESSION_ID_SIZE)
455 gnutls_assert ();
456 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
458 DECR_LEN (len, session_id_len);
460 session_id = &data[pos];
461 ret = _gnutls_server_restore_session (session, session_id, session_id_len);
462 pos += session_id_len;
464 if (ret == 0)
465 { /* resumed using default TLS resumption! */
466 /* Parse only the safe renegotiation extension
467 * We don't want to parse any other extensions since
468 * we don't want new extension values to overwrite the
469 * resumed ones.
472 /* move forward to extensions */
473 DECR_LEN (len, 2);
474 suite_size = _gnutls_read_uint16 (&data[pos]);
475 pos += 2;
477 DECR_LEN (len, suite_size);
478 pos += suite_size;
480 DECR_LEN (len, 1);
481 comp_size = data[pos++]; /* z is the number of compression methods */
482 DECR_LEN (len, comp_size);
483 pos += comp_size;
485 ret = _gnutls_parse_extensions (session, GNUTLS_EXT_MANDATORY,
486 &data[pos], len);
487 if (ret < 0)
489 gnutls_assert ();
490 return ret;
493 resume_copy_required_values (session);
494 session->internals.resumed = RESUME_TRUE;
496 return _gnutls_user_hello_func (session, adv_version);
498 else
500 _gnutls_generate_session_id (session->security_parameters.session_id,
501 &session->
502 security_parameters.session_id_size);
504 session->internals.resumed = RESUME_FALSE;
507 /* Remember ciphersuites for later
509 DECR_LEN (len, 2);
510 suite_size = _gnutls_read_uint16 (&data[pos]);
511 pos += 2;
513 DECR_LEN (len, suite_size);
514 suite_ptr = &data[pos];
515 pos += suite_size;
517 /* Point to the compression methods
519 DECR_LEN (len, 1);
520 comp_size = data[pos++]; /* z is the number of compression methods */
522 DECR_LEN (len, comp_size);
523 comp_ptr = &data[pos];
524 pos += comp_size;
526 /* Parse the extensions (if any)
528 * Unconditionally try to parse extensions; safe renegotiation uses them in
529 * sslv3 and higher, even though sslv3 doesn't officially support them.
531 ret = _gnutls_parse_extensions (session, GNUTLS_EXT_APPLICATION,
532 &data[pos], len);
533 /* len is the rest of the parsed length */
534 if (ret < 0)
536 gnutls_assert ();
537 return ret;
540 ret = _gnutls_user_hello_func (session, adv_version);
541 if (ret < 0)
543 gnutls_assert ();
544 return ret;
547 ret = _gnutls_parse_extensions (session, GNUTLS_EXT_MANDATORY,
548 &data[pos], len);
549 if (ret < 0)
551 gnutls_assert ();
552 return ret;
555 ret = _gnutls_parse_extensions (session, GNUTLS_EXT_TLS, &data[pos], len);
556 if (ret < 0)
558 gnutls_assert ();
559 return ret;
562 /* resumed by session_ticket extension */
563 if (session->internals.resumed == RESUME_TRUE)
565 /* to indicate the client that the current session is resumed */
566 memcpy (session->internals.resumed_security_parameters.session_id,
567 session_id, session_id_len);
568 session->internals.resumed_security_parameters.session_id_size =
569 session_id_len;
571 session->internals.resumed_security_parameters.max_record_recv_size =
572 session->security_parameters.max_record_recv_size;
573 session->internals.resumed_security_parameters.max_record_send_size =
574 session->security_parameters.max_record_send_size;
576 resume_copy_required_values (session);
578 return _gnutls_user_hello_func (session, adv_version);
581 /* select an appropriate cipher suite
583 ret = _gnutls_server_select_suite (session, suite_ptr, suite_size);
584 if (ret < 0)
586 gnutls_assert ();
587 return ret;
590 /* select appropriate compression method */
591 ret = _gnutls_server_select_comp_method (session, comp_ptr, comp_size);
592 if (ret < 0)
594 gnutls_assert ();
595 return ret;
598 return 0;
601 /* here we hash all pending data.
603 inline static int
604 _gnutls_handshake_hash_pending (gnutls_session_t session)
606 size_t siz;
607 int ret;
608 opaque *data;
610 if (session->internals.handshake_mac_handle_init == 0)
612 gnutls_assert ();
613 return GNUTLS_E_INTERNAL_ERROR;
616 /* We check if there are pending data to hash.
618 if ((ret = _gnutls_handshake_buffer_get_ptr (session, &data, &siz)) < 0)
620 gnutls_assert ();
621 return ret;
624 if (siz > 0)
626 if (session->security_parameters.handshake_mac_handle_type ==
627 HANDSHAKE_MAC_TYPE_10)
629 _gnutls_hash (&session->internals.handshake_mac_handle.tls10.sha,
630 data, siz);
631 _gnutls_hash (&session->internals.handshake_mac_handle.tls10.md5,
632 data, siz);
634 else if (session->security_parameters.handshake_mac_handle_type ==
635 HANDSHAKE_MAC_TYPE_12)
637 _gnutls_hash (&session->internals.handshake_mac_handle.tls12.sha256,
638 data, siz);
639 _gnutls_hash (&session->internals.handshake_mac_handle.tls12.sha1,
640 data, siz);
644 _gnutls_handshake_buffer_empty (session);
646 return 0;
650 /* This is to be called after sending CHANGE CIPHER SPEC packet
651 * and initializing encryption. This is the first encrypted message
652 * we send.
654 static int
655 _gnutls_send_finished (gnutls_session_t session, int again)
657 mbuffer_st *bufel;
658 opaque *data;
659 int ret;
660 size_t vdata_size = 0;
662 if (again == 0)
664 bufel =
665 _gnutls_handshake_alloc (MAX_VERIFY_DATA_SIZE, MAX_VERIFY_DATA_SIZE);
666 if (bufel == NULL)
668 gnutls_assert ();
669 return GNUTLS_E_MEMORY_ERROR;
671 data = _mbuffer_get_udata_ptr (bufel);
673 /* This is needed in order to hash all the required
674 * messages.
676 if ((ret = _gnutls_handshake_hash_pending (session)) < 0)
678 gnutls_assert ();
679 return ret;
682 if (gnutls_protocol_get_version (session) == GNUTLS_SSL3)
684 ret =
685 _gnutls_ssl3_finished (session,
686 session->security_parameters.entity, data);
687 _mbuffer_set_udata_size (bufel, 36);
689 else
690 { /* TLS 1.0+ */
691 ret = _gnutls_finished (session,
692 session->security_parameters.entity, data);
693 _mbuffer_set_udata_size (bufel, 12);
696 if (ret < 0)
698 gnutls_assert ();
699 return ret;
702 vdata_size = _mbuffer_get_udata_size (bufel);
704 if (session->internals.finished_func)
705 session->internals.finished_func (session, data, vdata_size);
707 ret = _gnutls_ext_sr_finished (session, data, vdata_size, 0);
708 if (ret < 0)
710 gnutls_assert ();
711 return ret;
714 if ((session->internals.resumed == RESUME_FALSE
715 && session->security_parameters.entity == GNUTLS_CLIENT)
716 || (session->internals.resumed == RESUME_TRUE
717 && session->security_parameters.entity == GNUTLS_SERVER))
719 /* if we are a client not resuming - or we are a server resuming */
720 _gnutls_handshake_log ("HSK[%p]: recording tls-unique CB (send)\n",
721 session);
722 memcpy (session->internals.cb_tls_unique, data, vdata_size);
723 session->internals.cb_tls_unique_len = vdata_size;
726 ret =
727 _gnutls_send_handshake (session, bufel, GNUTLS_HANDSHAKE_FINISHED);
729 else
731 ret = _gnutls_send_handshake (session, NULL, GNUTLS_HANDSHAKE_FINISHED);
734 return ret;
737 /* This is to be called after sending our finished message. If everything
738 * went fine we have negotiated a secure connection
740 static int
741 _gnutls_recv_finished (gnutls_session_t session)
743 uint8_t data[MAX_VERIFY_DATA_SIZE], *vrfy;
744 int data_size;
745 int ret;
746 int vrfysize;
748 ret =
749 _gnutls_recv_handshake (session, &vrfy, &vrfysize,
750 GNUTLS_HANDSHAKE_FINISHED, MANDATORY_PACKET);
751 if (ret < 0)
753 ERR ("recv finished int", ret);
754 gnutls_assert ();
755 return ret;
759 if (gnutls_protocol_get_version (session) == GNUTLS_SSL3)
761 data_size = 36;
763 else
765 data_size = 12;
768 if (vrfysize != data_size)
770 gnutls_assert ();
771 gnutls_free (vrfy);
772 return GNUTLS_E_ERROR_IN_FINISHED_PACKET;
775 if (gnutls_protocol_get_version (session) == GNUTLS_SSL3)
777 ret =
778 _gnutls_ssl3_finished (session,
779 (session->security_parameters.entity + 1) % 2,
780 data);
782 else
783 { /* TLS 1.0 */
784 ret =
785 _gnutls_finished (session,
786 (session->security_parameters.entity +
787 1) % 2, data);
790 if (ret < 0)
792 gnutls_assert ();
793 gnutls_free (vrfy);
794 return ret;
797 ret = memcmp (vrfy, data, data_size);
798 gnutls_free (vrfy);
800 if (ret != 0)
802 gnutls_assert ();
803 return GNUTLS_E_ERROR_IN_FINISHED_PACKET;
806 ret = _gnutls_ext_sr_finished (session, data, data_size, 1);
807 if (ret < 0)
809 gnutls_assert ();
810 return ret;
813 if ((session->internals.resumed == RESUME_TRUE
814 && session->security_parameters.entity == GNUTLS_CLIENT)
815 || (session->internals.resumed == RESUME_FALSE
816 && session->security_parameters.entity == GNUTLS_SERVER))
818 /* if we are a client resuming - or we are a server not resuming */
819 _gnutls_handshake_log ("HSK[%p]: recording tls-unique CB (recv)\n",
820 session);
821 memcpy (session->internals.cb_tls_unique, data, data_size);
822 session->internals.cb_tls_unique_len = data_size;
825 session->internals.initial_negotiation_completed = 1;
827 return 0;
830 /* returns PK_RSA if the given cipher suite list only supports,
831 * RSA algorithms, PK_DSA if DSS, and PK_ANY for both or PK_NONE for none.
833 static int
834 _gnutls_server_find_pk_algos_in_ciphersuites (const opaque *
835 data, unsigned int datalen)
837 unsigned int j;
838 gnutls_pk_algorithm_t algo = GNUTLS_PK_NONE, prev_algo = 0;
839 gnutls_kx_algorithm_t kx;
840 cipher_suite_st cs;
842 if (datalen % 2 != 0)
844 gnutls_assert ();
845 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
848 for (j = 0; j < datalen; j += 2)
850 memcpy (&cs.suite, &data[j], 2);
851 kx = _gnutls_cipher_suite_get_kx_algo (&cs);
853 if (_gnutls_map_kx_get_cred (kx, 1) == GNUTLS_CRD_CERTIFICATE)
855 algo = _gnutls_map_pk_get_pk (kx);
857 if (algo != prev_algo && prev_algo != 0)
858 return GNUTLS_PK_ANY;
859 prev_algo = algo;
863 return algo;
866 /* This selects the best supported ciphersuite from the given ones. Then
867 * it adds the suite to the session and performs some checks.
870 _gnutls_server_select_suite (gnutls_session_t session, opaque * data,
871 int datalen)
873 int x, i, j;
874 cipher_suite_st *ciphers, cs;
875 int retval, err;
876 gnutls_pk_algorithm_t pk_algo; /* will hold the pk algorithms
877 * supported by the peer.
880 /* First, check for safe renegotiation SCSV.
882 if (session->internals.priorities.sr != SR_DISABLED)
884 int offset;
886 for (offset = 0; offset < datalen; offset += 2)
888 /* TLS_RENEGO_PROTECTION_REQUEST = { 0x00, 0xff } */
889 if (data[offset] == GNUTLS_RENEGO_PROTECTION_REQUEST_MAJOR &&
890 data[offset + 1] == GNUTLS_RENEGO_PROTECTION_REQUEST_MINOR)
892 _gnutls_handshake_log
893 ("HSK[%p]: Received safe renegotiation CS\n", session);
894 retval = _gnutls_ext_sr_recv_cs (session);
895 if (retval < 0)
897 gnutls_assert ();
898 return retval;
900 break;
905 pk_algo = _gnutls_server_find_pk_algos_in_ciphersuites (data, datalen);
907 x = _gnutls_supported_ciphersuites (session, &ciphers);
908 if (x < 0)
909 { /* the case x==0 is handled within the function. */
910 gnutls_assert ();
911 return x;
914 /* Here we remove any ciphersuite that does not conform
915 * the certificate requested, or to the
916 * authentication requested (e.g. SRP).
918 x = _gnutls_remove_unwanted_ciphersuites (session, &ciphers, x, pk_algo);
919 if (x <= 0)
921 gnutls_assert ();
922 gnutls_free (ciphers);
923 if (x < 0)
924 return x;
925 else
926 return GNUTLS_E_UNKNOWN_CIPHER_SUITE;
929 /* Data length should be zero mod 2 since
930 * every ciphersuite is 2 bytes. (this check is needed
931 * see below).
933 if (datalen % 2 != 0)
935 gnutls_assert ();
936 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
938 #ifdef HANDSHAKE_DEBUG
940 _gnutls_handshake_log ("HSK[%p]: Requested cipher suites: \n", session);
941 for (j = 0; j < datalen; j += 2)
943 memcpy (&cs.suite, &data[j], 2);
944 _gnutls_handshake_log ("\t%s\n", _gnutls_cipher_suite_get_name (&cs));
946 _gnutls_handshake_log ("HSK[%p]: Supported cipher suites: \n", session);
947 for (j = 0; j < x; j++)
948 _gnutls_handshake_log ("\t%s\n",
949 _gnutls_cipher_suite_get_name (&ciphers[j]));
950 #endif
951 memset (session->security_parameters.current_cipher_suite.suite, '\0', 2);
953 retval = GNUTLS_E_UNKNOWN_CIPHER_SUITE;
955 for (j = 0; j < datalen; j += 2)
957 for (i = 0; i < x; i++)
959 if (memcmp (ciphers[i].suite, &data[j], 2) == 0)
961 memcpy (&cs.suite, &data[j], 2);
963 _gnutls_handshake_log
964 ("HSK[%p]: Selected cipher suite: %s\n", session,
965 _gnutls_cipher_suite_get_name (&cs));
966 memcpy (session->security_parameters.current_cipher_suite.suite,
967 ciphers[i].suite, 2);
968 _gnutls_epoch_set_cipher_suite (session, EPOCH_NEXT,
969 &session->
970 security_parameters.current_cipher_suite);
973 retval = 0;
974 goto finish;
979 finish:
980 gnutls_free (ciphers);
982 if (retval != 0)
984 gnutls_assert ();
985 return retval;
988 /* check if the credentials (username, public key etc.) are ok
990 if (_gnutls_get_kx_cred
991 (session,
992 _gnutls_cipher_suite_get_kx_algo (&session->
993 security_parameters.current_cipher_suite),
994 &err) == NULL && err != 0)
996 gnutls_assert ();
997 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1001 /* set the mod_auth_st to the appropriate struct
1002 * according to the KX algorithm. This is needed since all the
1003 * handshake functions are read from there;
1005 session->internals.auth_struct =
1006 _gnutls_kx_auth_struct (_gnutls_cipher_suite_get_kx_algo
1007 (&session->
1008 security_parameters.current_cipher_suite));
1009 if (session->internals.auth_struct == NULL)
1012 _gnutls_handshake_log
1013 ("HSK[%p]: Cannot find the appropriate handler for the KX algorithm\n",
1014 session);
1015 gnutls_assert ();
1016 return GNUTLS_E_INTERNAL_ERROR;
1019 return 0;
1024 /* This selects the best supported compression method from the ones provided
1026 static int
1027 _gnutls_server_select_comp_method (gnutls_session_t session,
1028 opaque * data, int datalen)
1030 int x, i, j;
1031 uint8_t *comps;
1033 x = _gnutls_supported_compression_methods (session, &comps);
1034 if (x < 0)
1036 gnutls_assert ();
1037 return x;
1040 memset (&session->internals.compression_method, 0,
1041 sizeof (gnutls_compression_method_t));
1043 for (j = 0; j < datalen; j++)
1045 for (i = 0; i < x; i++)
1047 if (comps[i] == data[j])
1049 gnutls_compression_method_t method =
1050 _gnutls_compression_get_id (comps[i]);
1052 session->internals.compression_method = method;
1053 gnutls_free (comps);
1055 _gnutls_epoch_set_compression (session, EPOCH_NEXT, method);
1057 _gnutls_handshake_log
1058 ("HSK[%p]: Selected Compression Method: %s\n", session,
1059 gnutls_compression_get_name (session->
1060 internals.compression_method));
1063 return 0;
1068 /* we were not able to find a compatible compression
1069 * algorithm
1071 gnutls_free (comps);
1072 gnutls_assert ();
1073 return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM;
1077 /* This function sends an empty handshake packet. (like hello request).
1078 * If the previous _gnutls_send_empty_handshake() returned
1079 * GNUTLS_E_AGAIN or GNUTLS_E_INTERRUPTED, then it must be called again
1080 * (until it returns ok), with NULL parameters.
1082 static int
1083 _gnutls_send_empty_handshake (gnutls_session_t session,
1084 gnutls_handshake_description_t type, int again)
1086 mbuffer_st *bufel;
1088 if (again == 0)
1090 bufel = _gnutls_handshake_alloc (0, 0);
1091 if (bufel == NULL)
1093 gnutls_assert ();
1094 return GNUTLS_E_MEMORY_ERROR;
1097 else
1098 bufel = NULL;
1100 return _gnutls_send_handshake (session, bufel, type);
1104 /* This function will hash the handshake message we sent.
1106 static int
1107 _gnutls_handshake_hash_add_sent (gnutls_session_t session,
1108 gnutls_handshake_description_t type,
1109 opaque * dataptr, uint32_t datalen)
1111 int ret;
1113 if (session->security_parameters.entity == GNUTLS_CLIENT
1114 && type == GNUTLS_HANDSHAKE_CLIENT_HELLO)
1116 /* do not hash immediatelly since the hash has not yet been initialized */
1117 if ((ret =
1118 _gnutls_handshake_buffer_put (session, dataptr, datalen)) < 0)
1120 gnutls_assert ();
1121 return ret;
1123 return 0;
1126 if ((ret = _gnutls_handshake_hash_pending (session)) < 0)
1128 gnutls_assert ();
1129 return ret;
1132 if (type != GNUTLS_HANDSHAKE_HELLO_REQUEST)
1134 if (session->security_parameters.handshake_mac_handle_type ==
1135 HANDSHAKE_MAC_TYPE_10)
1137 _gnutls_hash (&session->internals.handshake_mac_handle.tls10.sha,
1138 dataptr, datalen);
1139 _gnutls_hash (&session->internals.handshake_mac_handle.tls10.md5,
1140 dataptr, datalen);
1142 else if (session->security_parameters.handshake_mac_handle_type ==
1143 HANDSHAKE_MAC_TYPE_12)
1145 _gnutls_hash (&session->internals.handshake_mac_handle.tls12.sha256,
1146 dataptr, datalen);
1147 _gnutls_hash (&session->internals.handshake_mac_handle.tls12.sha1,
1148 dataptr, datalen);
1152 return 0;
1156 /* This function sends a handshake message of type 'type' containing the
1157 * data specified here. If the previous _gnutls_send_handshake() returned
1158 * GNUTLS_E_AGAIN or GNUTLS_E_INTERRUPTED, then it must be called again
1159 * (until it returns ok), with NULL parameters.
1162 _gnutls_send_handshake (gnutls_session_t session, mbuffer_st * bufel,
1163 gnutls_handshake_description_t type)
1165 int ret;
1166 uint8_t *data;
1167 uint32_t datasize;
1168 int pos = 0;
1170 if (bufel == NULL)
1172 /* we are resuming a previously interrupted
1173 * send.
1175 ret = _gnutls_handshake_io_write_flush (session);
1176 return ret;
1180 /* first run */
1181 data = _mbuffer_get_uhead_ptr (bufel);
1182 datasize =
1183 _mbuffer_get_udata_size (bufel) + _mbuffer_get_uhead_size (bufel);
1185 data[pos++] = (uint8_t) type;
1186 _gnutls_write_uint24 (_mbuffer_get_udata_size (bufel), &data[pos]);
1187 pos += 3;
1189 _gnutls_handshake_log ("HSK[%p]: %s was sent [%ld bytes]\n",
1190 session, _gnutls_handshake2str (type),
1191 (long) datasize);
1194 /* Here we keep the handshake messages in order to hash them...
1196 if (type != GNUTLS_HANDSHAKE_HELLO_REQUEST)
1197 if ((ret =
1198 _gnutls_handshake_hash_add_sent (session, type, data, datasize)) < 0)
1200 gnutls_assert ();
1201 gnutls_free (bufel);
1202 return ret;
1205 session->internals.last_handshake_out = type;
1207 _gnutls_handshake_io_cache_int (session, type, bufel);
1209 switch (type)
1211 case GNUTLS_HANDSHAKE_CERTIFICATE_PKT: /* this one is followed by ServerHelloDone
1212 * or ClientKeyExchange always.
1214 case GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE: /* as above */
1215 case GNUTLS_HANDSHAKE_SERVER_HELLO: /* as above */
1216 case GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST: /* as above */
1217 case GNUTLS_HANDSHAKE_NEW_SESSION_TICKET: /* followed by ChangeCipherSpec */
1219 /* now for client Certificate, ClientKeyExchange and
1220 * CertificateVerify are always followed by ChangeCipherSpec
1222 case GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY:
1223 case GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE:
1224 ret = 0;
1225 break;
1226 default:
1227 /* send cached messages */
1228 ret = _gnutls_handshake_io_write_flush (session);
1229 break;
1232 return ret;
1235 /* This function will read the handshake header and return it to the caller. If the
1236 * received handshake packet is not the one expected then it buffers the header, and
1237 * returns UNEXPECTED_HANDSHAKE_PACKET.
1239 * FIXME: This function is complex.
1241 #define SSL2_HEADERS 1
1242 static int
1243 _gnutls_recv_handshake_header (gnutls_session_t session,
1244 gnutls_handshake_description_t type,
1245 gnutls_handshake_description_t * recv_type)
1247 int ret;
1248 uint32_t length32 = 0;
1249 uint8_t *dataptr = NULL; /* for realloc */
1250 size_t handshake_header_size = HANDSHAKE_HEADER_SIZE;
1252 /* if we have data into the buffer then return them, do not read the next packet.
1253 * In order to return we need a full TLS handshake header, or in case of a version 2
1254 * packet, then we return the first byte.
1256 if (session->internals.handshake_header_buffer.header_size ==
1257 handshake_header_size || (session->internals.v2_hello != 0
1258 && type == GNUTLS_HANDSHAKE_CLIENT_HELLO
1259 && session->internals.handshake_header_buffer.
1260 packet_length > 0))
1263 *recv_type = session->internals.handshake_header_buffer.recv_type;
1265 if (*recv_type != type)
1267 gnutls_assert ();
1268 _gnutls_handshake_log
1269 ("HSK[%p]: Handshake type mismatch (under attack?)\n", session);
1270 return GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET;
1273 return session->internals.handshake_header_buffer.packet_length;
1276 /* Note: SSL2_HEADERS == 1 */
1278 dataptr = session->internals.handshake_header_buffer.header;
1280 /* If we haven't already read the handshake headers.
1282 if (session->internals.handshake_header_buffer.header_size < SSL2_HEADERS)
1284 ret =
1285 _gnutls_handshake_io_recv_int (session, GNUTLS_HANDSHAKE,
1286 type, dataptr, SSL2_HEADERS);
1288 if (ret < 0)
1290 return ret;
1293 /* The case ret==0 is caught here.
1295 if (ret != SSL2_HEADERS)
1297 gnutls_assert ();
1298 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
1300 session->internals.handshake_header_buffer.header_size = SSL2_HEADERS;
1303 if (session->internals.v2_hello == 0
1304 || type != GNUTLS_HANDSHAKE_CLIENT_HELLO)
1306 ret =
1307 _gnutls_handshake_io_recv_int (session, GNUTLS_HANDSHAKE,
1308 type,
1309 &dataptr
1310 [session->
1311 internals.handshake_header_buffer.
1312 header_size],
1313 HANDSHAKE_HEADER_SIZE -
1314 session->
1315 internals.handshake_header_buffer.
1316 header_size);
1317 if (ret <= 0)
1319 gnutls_assert ();
1320 return (ret < 0) ? ret : GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
1322 if ((size_t) ret !=
1323 HANDSHAKE_HEADER_SIZE -
1324 session->internals.handshake_header_buffer.header_size)
1326 gnutls_assert ();
1327 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
1329 *recv_type = dataptr[0];
1331 /* we do not use DECR_LEN because we know
1332 * that the packet has enough data.
1334 length32 = _gnutls_read_uint24 (&dataptr[1]);
1335 handshake_header_size = HANDSHAKE_HEADER_SIZE;
1337 _gnutls_handshake_log ("HSK[%p]: %s was received [%ld bytes]\n",
1338 session, _gnutls_handshake2str (dataptr[0]),
1339 (long int) (length32 + HANDSHAKE_HEADER_SIZE));
1342 else
1343 { /* v2 hello */
1344 length32 = session->internals.v2_hello - SSL2_HEADERS; /* we've read the first byte */
1346 handshake_header_size = SSL2_HEADERS; /* we've already read one byte */
1348 *recv_type = dataptr[0];
1350 _gnutls_handshake_log ("HSK[%p]: %s(v2) was received [%ld bytes]\n",
1351 session, _gnutls_handshake2str (*recv_type),
1352 (long int) (length32 + handshake_header_size));
1354 if (*recv_type != GNUTLS_HANDSHAKE_CLIENT_HELLO)
1355 { /* it should be one or nothing */
1356 gnutls_assert ();
1357 return GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET;
1361 /* put the packet into the buffer */
1362 session->internals.handshake_header_buffer.header_size =
1363 handshake_header_size;
1364 session->internals.handshake_header_buffer.packet_length = length32;
1365 session->internals.handshake_header_buffer.recv_type = *recv_type;
1367 if (*recv_type != type)
1369 gnutls_assert ();
1370 return GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET;
1373 return length32;
1376 #define _gnutls_handshake_header_buffer_clear( session) session->internals.handshake_header_buffer.header_size = 0
1380 /* This function will hash the handshake headers and the
1381 * handshake data.
1383 static int
1384 _gnutls_handshake_hash_add_recvd (gnutls_session_t session,
1385 gnutls_handshake_description_t recv_type,
1386 opaque * header, uint16_t header_size,
1387 opaque * dataptr, uint32_t datalen)
1389 int ret;
1391 /* The idea here is to hash the previous message we received,
1392 * and add the one we just received into the handshake_hash_buffer.
1394 if ((session->security_parameters.entity == GNUTLS_SERVER
1395 || recv_type != GNUTLS_HANDSHAKE_SERVER_HELLO)
1396 && (session->security_parameters.entity == GNUTLS_CLIENT
1397 || recv_type != GNUTLS_HANDSHAKE_CLIENT_HELLO))
1399 if ((ret = _gnutls_handshake_hash_pending (session)) < 0)
1401 gnutls_assert ();
1402 return ret;
1406 /* here we buffer the handshake messages - needed at Finished message */
1407 if (recv_type != GNUTLS_HANDSHAKE_HELLO_REQUEST)
1410 if ((ret =
1411 _gnutls_handshake_buffer_put (session, header, header_size)) < 0)
1413 gnutls_assert ();
1414 return ret;
1417 if (datalen > 0)
1419 if ((ret =
1420 _gnutls_handshake_buffer_put (session, dataptr, datalen)) < 0)
1422 gnutls_assert ();
1423 return ret;
1428 return 0;
1432 /* This function will receive handshake messages of the given types,
1433 * and will pass the message to the right place in order to be processed.
1434 * E.g. for the SERVER_HELLO message (if it is expected), it will be
1435 * passed to _gnutls_recv_hello().
1438 _gnutls_recv_handshake (gnutls_session_t session, uint8_t ** data,
1439 int *datalen, gnutls_handshake_description_t type,
1440 Optional optional)
1442 int ret;
1443 uint32_t length32 = 0;
1444 opaque *dataptr = NULL;
1445 gnutls_handshake_description_t recv_type;
1447 ret = _gnutls_recv_handshake_header (session, type, &recv_type);
1448 if (ret < 0)
1451 if (ret == GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET
1452 && optional == OPTIONAL_PACKET)
1454 if (datalen != NULL)
1455 *datalen = 0;
1456 if (data != NULL)
1457 *data = NULL;
1458 return 0; /* ok just ignore the packet */
1461 return ret;
1464 session->internals.last_handshake_in = recv_type;
1466 length32 = ret;
1468 if (length32 > 0)
1469 dataptr = gnutls_malloc (length32);
1470 else if (recv_type != GNUTLS_HANDSHAKE_SERVER_HELLO_DONE)
1472 gnutls_assert ();
1473 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
1476 if (dataptr == NULL && length32 > 0)
1478 gnutls_assert ();
1479 return GNUTLS_E_MEMORY_ERROR;
1482 if (datalen != NULL)
1483 *datalen = length32;
1485 if (length32 > 0)
1487 ret =
1488 _gnutls_handshake_io_recv_int (session, GNUTLS_HANDSHAKE,
1489 type, dataptr, length32);
1490 if (ret <= 0)
1492 gnutls_assert ();
1493 gnutls_free (dataptr);
1494 return (ret == 0) ? GNUTLS_E_UNEXPECTED_PACKET_LENGTH : ret;
1498 if (data != NULL && length32 > 0)
1499 *data = dataptr;
1502 ret = _gnutls_handshake_hash_add_recvd (session, recv_type,
1503 session->
1504 internals.handshake_header_buffer.
1505 header,
1506 session->
1507 internals.handshake_header_buffer.
1508 header_size, dataptr, length32);
1509 if (ret < 0)
1511 gnutls_assert ();
1512 _gnutls_handshake_header_buffer_clear (session);
1513 return ret;
1516 /* If we fail before this then we will reuse the handshake header
1517 * have have received above. if we get here the we clear the handshake
1518 * header we received.
1520 _gnutls_handshake_header_buffer_clear (session);
1522 switch (recv_type)
1524 case GNUTLS_HANDSHAKE_CLIENT_HELLO:
1525 case GNUTLS_HANDSHAKE_SERVER_HELLO:
1526 ret = _gnutls_recv_hello (session, dataptr, length32);
1528 /* dataptr is freed because the caller does not
1529 * need it */
1530 gnutls_free (dataptr);
1531 if (data != NULL)
1532 *data = NULL;
1534 if (ret < 0)
1535 break;
1537 /* initialize the hashes for both - (client will know server's version
1538 * and server as well at this point) */
1539 if ((ret = _gnutls_handshake_hash_init (session)) < 0)
1541 gnutls_assert ();
1542 return ret;
1545 break;
1546 case GNUTLS_HANDSHAKE_SERVER_HELLO_DONE:
1547 if (length32 == 0)
1548 ret = 0;
1549 else
1550 ret = GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
1551 break;
1552 case GNUTLS_HANDSHAKE_CERTIFICATE_PKT:
1553 case GNUTLS_HANDSHAKE_FINISHED:
1554 case GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE:
1555 case GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE:
1556 case GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST:
1557 case GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY:
1558 case GNUTLS_HANDSHAKE_SUPPLEMENTAL:
1559 case GNUTLS_HANDSHAKE_NEW_SESSION_TICKET:
1560 ret = length32;
1561 break;
1562 default:
1563 gnutls_assert ();
1564 gnutls_free (dataptr);
1565 if (data != NULL)
1566 *data = NULL;
1567 ret = GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET;
1570 return ret;
1573 /* This function checks if the given cipher suite is supported, and sets it
1574 * to the session;
1576 static int
1577 _gnutls_client_set_ciphersuite (gnutls_session_t session, opaque suite[2])
1579 uint8_t z;
1580 cipher_suite_st *cipher_suites;
1581 int cipher_suite_num;
1582 int i, err;
1584 z = 1;
1585 cipher_suite_num = _gnutls_supported_ciphersuites (session, &cipher_suites);
1586 if (cipher_suite_num < 0)
1588 gnutls_assert ();
1589 return cipher_suite_num;
1592 for (i = 0; i < cipher_suite_num; i++)
1594 if (memcmp (&cipher_suites[i], suite, 2) == 0)
1596 z = 0;
1597 break;
1601 gnutls_free (cipher_suites);
1603 if (z != 0)
1605 gnutls_assert ();
1606 return GNUTLS_E_UNKNOWN_CIPHER_SUITE;
1609 memcpy (session->security_parameters.current_cipher_suite.suite, suite, 2);
1610 _gnutls_epoch_set_cipher_suite (session, EPOCH_NEXT,
1611 &session->
1612 security_parameters.current_cipher_suite);
1614 _gnutls_handshake_log ("HSK[%p]: Selected cipher suite: %s\n", session,
1615 _gnutls_cipher_suite_get_name
1616 (&session->
1617 security_parameters.current_cipher_suite));
1620 /* check if the credentials (username, public key etc.) are ok.
1621 * Actually checks if they exist.
1623 if (_gnutls_get_kx_cred
1624 (session,
1625 _gnutls_cipher_suite_get_kx_algo
1626 (&session->security_parameters.current_cipher_suite), &err) == NULL
1627 && err != 0)
1629 gnutls_assert ();
1630 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1634 /* set the mod_auth_st to the appropriate struct
1635 * according to the KX algorithm. This is needed since all the
1636 * handshake functions are read from there;
1638 session->internals.auth_struct =
1639 _gnutls_kx_auth_struct (_gnutls_cipher_suite_get_kx_algo
1640 (&session->
1641 security_parameters.current_cipher_suite));
1643 if (session->internals.auth_struct == NULL)
1646 _gnutls_handshake_log
1647 ("HSK[%p]: Cannot find the appropriate handler for the KX algorithm\n",
1648 session);
1649 gnutls_assert ();
1650 return GNUTLS_E_INTERNAL_ERROR;
1654 return 0;
1657 /* This function sets the given comp method to the session.
1659 static int
1660 _gnutls_client_set_comp_method (gnutls_session_t session, opaque comp_method)
1662 int comp_methods_num;
1663 uint8_t *compression_methods;
1664 int i;
1666 comp_methods_num = _gnutls_supported_compression_methods (session,
1667 &compression_methods);
1668 if (comp_methods_num < 0)
1670 gnutls_assert ();
1671 return comp_methods_num;
1674 for (i = 0; i < comp_methods_num; i++)
1676 if (compression_methods[i] == comp_method)
1678 comp_methods_num = 0;
1679 break;
1683 gnutls_free (compression_methods);
1685 if (comp_methods_num != 0)
1687 gnutls_assert ();
1688 return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM;
1691 session->internals.compression_method =
1692 _gnutls_compression_get_id (comp_method);
1693 _gnutls_epoch_set_compression (session, EPOCH_NEXT,
1694 session->internals.compression_method);
1696 return 0;
1699 /* This function returns 0 if we are resuming a session or -1 otherwise.
1700 * This also sets the variables in the session. Used only while reading a server
1701 * hello.
1703 static int
1704 _gnutls_client_check_if_resuming (gnutls_session_t session,
1705 opaque * session_id, int session_id_len)
1707 opaque buf[2 * TLS_MAX_SESSION_ID_SIZE + 1];
1709 _gnutls_handshake_log ("HSK[%p]: SessionID length: %d\n", session,
1710 session_id_len);
1711 _gnutls_handshake_log ("HSK[%p]: SessionID: %s\n", session,
1712 _gnutls_bin2hex (session_id, session_id_len, buf,
1713 sizeof (buf), NULL));
1715 if (session_id_len > 0 &&
1716 session->internals.resumed_security_parameters.session_id_size ==
1717 session_id_len
1718 && memcmp (session_id,
1719 session->internals.resumed_security_parameters.session_id,
1720 session_id_len) == 0)
1722 /* resume session */
1723 memcpy (session->internals.resumed_security_parameters.server_random,
1724 session->security_parameters.server_random, GNUTLS_RANDOM_SIZE);
1725 memcpy (session->internals.resumed_security_parameters.client_random,
1726 session->security_parameters.client_random, GNUTLS_RANDOM_SIZE);
1728 _gnutls_epoch_set_cipher_suite
1729 (session, EPOCH_NEXT,
1730 &session->internals.
1731 resumed_security_parameters.current_cipher_suite);
1732 _gnutls_epoch_set_compression (session, EPOCH_NEXT,
1733 session->
1734 internals.resumed_compression_method);
1736 session->internals.resumed = RESUME_TRUE; /* we are resuming */
1738 return 0;
1740 else
1742 /* keep the new session id */
1743 session->internals.resumed = RESUME_FALSE; /* we are not resuming */
1744 session->security_parameters.session_id_size = session_id_len;
1745 memcpy (session->security_parameters.session_id,
1746 session_id, session_id_len);
1748 return -1;
1753 /* This function reads and parses the server hello handshake message.
1754 * This function also restores resumed parameters if we are resuming a
1755 * session.
1757 static int
1758 _gnutls_read_server_hello (gnutls_session_t session,
1759 opaque * data, int datalen)
1761 uint8_t session_id_len = 0;
1762 int pos = 0;
1763 int ret = 0;
1764 gnutls_protocol_t version;
1765 int len = datalen;
1767 if (datalen < 38)
1769 gnutls_assert ();
1770 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
1773 _gnutls_handshake_log ("HSK[%p]: Server's version: %d.%d\n",
1774 session, data[pos], data[pos + 1]);
1776 DECR_LEN (len, 2);
1777 version = _gnutls_version_get (data[pos], data[pos + 1]);
1778 if (_gnutls_version_is_supported (session, version) == 0)
1780 gnutls_assert ();
1781 return GNUTLS_E_UNSUPPORTED_VERSION_PACKET;
1783 else
1785 _gnutls_set_current_version (session, version);
1788 pos += 2;
1790 DECR_LEN (len, GNUTLS_RANDOM_SIZE);
1791 _gnutls_set_server_random (session, &data[pos]);
1792 pos += GNUTLS_RANDOM_SIZE;
1795 /* Read session ID
1797 DECR_LEN (len, 1);
1798 session_id_len = data[pos++];
1800 if (len < session_id_len)
1802 gnutls_assert ();
1803 return GNUTLS_E_UNSUPPORTED_VERSION_PACKET;
1805 DECR_LEN (len, session_id_len);
1807 /* check if we are resuming and set the appropriate
1808 * values;
1810 if (_gnutls_client_check_if_resuming
1811 (session, &data[pos], session_id_len) == 0)
1813 pos += session_id_len + 2 + 1;
1814 DECR_LEN (len, 2 + 1);
1816 ret = _gnutls_parse_extensions (session, GNUTLS_EXT_MANDATORY,
1817 &data[pos], len);
1818 if (ret < 0)
1820 gnutls_assert ();
1821 return ret;
1823 return 0;
1826 pos += session_id_len;
1828 /* Check if the given cipher suite is supported and copy
1829 * it to the session.
1832 DECR_LEN (len, 2);
1833 ret = _gnutls_client_set_ciphersuite (session, &data[pos]);
1834 if (ret < 0)
1836 gnutls_assert ();
1837 return ret;
1839 pos += 2;
1841 /* move to compression
1843 DECR_LEN (len, 1);
1845 ret = _gnutls_client_set_comp_method (session, data[pos++]);
1846 if (ret < 0)
1848 gnutls_assert ();
1849 return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM;
1852 /* Parse extensions.
1854 ret = _gnutls_parse_extensions (session, GNUTLS_EXT_ANY, &data[pos], len);
1855 if (ret < 0)
1857 gnutls_assert ();
1858 return ret;
1861 return ret;
1865 /* This function copies the appropriate ciphersuites to a locally allocated buffer
1866 * Needed in client hello messages. Returns the new data length. If add_scsv is
1867 * true, add the special safe renegotiation CS.
1869 static int
1870 _gnutls_copy_ciphersuites (gnutls_session_t session,
1871 opaque * ret_data, size_t ret_data_size,
1872 int add_scsv)
1874 int ret, i;
1875 cipher_suite_st *cipher_suites;
1876 uint16_t cipher_num;
1877 int datalen, pos;
1878 uint16_t loop_max;
1880 ret = _gnutls_supported_ciphersuites_sorted (session, &cipher_suites);
1881 if (ret < 0)
1883 gnutls_assert ();
1884 return ret;
1887 /* Here we remove any ciphersuite that does not conform
1888 * the certificate requested, or to the
1889 * authentication requested (eg SRP).
1891 ret =
1892 _gnutls_remove_unwanted_ciphersuites (session, &cipher_suites, ret, -1);
1893 if (ret < 0)
1895 gnutls_assert ();
1896 gnutls_free (cipher_suites);
1897 return ret;
1900 /* If no cipher suites were enabled.
1902 if (ret == 0)
1904 gnutls_assert ();
1905 gnutls_free (cipher_suites);
1906 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
1909 if (add_scsv)
1910 ++ret;
1912 cipher_num = ret;
1914 cipher_num *= sizeof (uint16_t); /* in order to get bytes */
1916 datalen = pos = 0;
1918 datalen += sizeof (uint16_t) + cipher_num;
1920 if ((size_t) datalen > ret_data_size)
1922 gnutls_assert ();
1923 return GNUTLS_E_INTERNAL_ERROR;
1926 _gnutls_write_uint16 (cipher_num, ret_data);
1927 pos += 2;
1929 loop_max = add_scsv ? cipher_num - 2 : cipher_num;
1931 for (i = 0; i < (loop_max / 2); i++)
1933 memcpy (&ret_data[pos], cipher_suites[i].suite, 2);
1934 pos += 2;
1937 if (add_scsv)
1939 /* Safe renegotiation signalling CS value is { 0x00, 0xff } */
1940 ret_data[pos++] = 0x00;
1941 ret_data[pos++] = 0xff;
1942 ret = _gnutls_ext_sr_send_cs (session);
1943 if (ret < 0)
1945 gnutls_assert ();
1946 gnutls_free (cipher_suites);
1947 return ret;
1951 gnutls_free (cipher_suites);
1953 return datalen;
1957 /* This function copies the appropriate compression methods, to a locally allocated buffer
1958 * Needed in hello messages. Returns the new data length.
1960 static int
1961 _gnutls_copy_comp_methods (gnutls_session_t session,
1962 opaque * ret_data, size_t ret_data_size)
1964 int ret, i;
1965 uint8_t *compression_methods, comp_num;
1966 int datalen, pos;
1968 ret = _gnutls_supported_compression_methods (session, &compression_methods);
1969 if (ret < 0)
1971 gnutls_assert ();
1972 return ret;
1975 comp_num = ret;
1977 datalen = pos = 0;
1978 datalen += comp_num + 1;
1980 if ((size_t) datalen > ret_data_size)
1982 gnutls_assert ();
1983 return GNUTLS_E_INTERNAL_ERROR;
1986 ret_data[pos++] = comp_num; /* put the number of compression methods */
1988 for (i = 0; i < comp_num; i++)
1990 ret_data[pos++] = compression_methods[i];
1993 gnutls_free (compression_methods);
1995 return datalen;
1998 /* This should be sufficient by now. It should hold all the extensions
1999 * plus the headers in a hello message.
2001 #define MAX_EXT_DATA_LENGTH 32*1024
2003 /* This function sends the client hello handshake message.
2005 static int
2006 _gnutls_send_client_hello (gnutls_session_t session, int again)
2008 mbuffer_st *bufel = NULL;
2009 opaque *data = NULL;
2010 int extdatalen;
2011 int pos = 0, type;
2012 int datalen = 0, ret = 0;
2013 opaque rnd[GNUTLS_RANDOM_SIZE];
2014 gnutls_protocol_t hver;
2015 opaque *extdata = NULL;
2016 int rehandshake = 0;
2017 uint8_t session_id_len =
2018 session->internals.resumed_security_parameters.session_id_size;
2020 /* note that rehandshake is different than resuming
2022 if (session->security_parameters.session_id_size)
2023 rehandshake = 1;
2025 if (again == 0)
2028 datalen = 2 + (session_id_len + 1) + GNUTLS_RANDOM_SIZE;
2029 /* 2 for version, (4 for unix time + 28 for random bytes==GNUTLS_RANDOM_SIZE)
2032 bufel =
2033 _gnutls_handshake_alloc (datalen, datalen + MAX_EXT_DATA_LENGTH);
2034 if (bufel == NULL)
2036 gnutls_assert ();
2037 return GNUTLS_E_MEMORY_ERROR;
2039 data = _mbuffer_get_udata_ptr (bufel);
2040 extdatalen = MAX_EXT_DATA_LENGTH;
2042 extdata = gnutls_malloc (extdatalen);
2043 if (extdata == NULL)
2045 gnutls_assert ();
2046 return GNUTLS_E_MEMORY_ERROR;
2049 /* if we are resuming a session then we set the
2050 * version number to the previously established.
2052 if (session_id_len == 0)
2054 if (rehandshake) /* already negotiated version thus version_max == negotiated version */
2055 hver = session->security_parameters.version;
2056 else /* new handshake. just get the max */
2057 hver = _gnutls_version_max (session);
2059 else
2061 /* we are resuming a session */
2062 hver = session->internals.resumed_security_parameters.version;
2065 if (hver == GNUTLS_VERSION_UNKNOWN || hver == 0)
2067 gnutls_assert ();
2068 gnutls_free (bufel);
2069 gnutls_free (extdata);
2070 return GNUTLS_E_INTERNAL_ERROR;
2073 data[pos++] = _gnutls_version_get_major (hver);
2074 data[pos++] = _gnutls_version_get_minor (hver);
2076 /* Set the version we advertized as maximum
2077 * (RSA uses it).
2079 _gnutls_set_adv_version (session, hver);
2080 _gnutls_set_current_version (session, hver);
2082 if (session->internals.priorities.ssl3_record_version != 0)
2084 /* Advertize the SSL 3.0 record packet version in
2085 * record packets during the handshake.
2086 * That is to avoid confusing implementations
2087 * that do not support TLS 1.2 and don't know
2088 * how 3,3 version of record packets look like.
2090 _gnutls_record_set_default_version (session, 3, 0);
2093 /* In order to know when this session was initiated.
2095 session->security_parameters.timestamp = gnutls_time (NULL);
2097 /* Generate random data
2099 _gnutls_tls_create_random (rnd);
2100 _gnutls_set_client_random (session, rnd);
2102 memcpy (&data[pos], rnd, GNUTLS_RANDOM_SIZE);
2103 pos += GNUTLS_RANDOM_SIZE;
2105 /* Copy the Session ID
2107 data[pos++] = session_id_len;
2109 if (session_id_len > 0)
2111 memcpy (&data[pos],
2112 session->internals.resumed_security_parameters.session_id,
2113 session_id_len);
2114 pos += session_id_len;
2118 /* Copy the ciphersuites.
2120 * If using SSLv3 Send TLS_RENEGO_PROTECTION_REQUEST SCSV for MITM
2121 * prevention on initial negotiation (but not renegotiation; that's
2122 * handled with the RI extension below).
2124 if (!session->internals.initial_negotiation_completed &&
2125 session->security_parameters.entity == GNUTLS_CLIENT &&
2126 gnutls_protocol_get_version (session) == GNUTLS_SSL3)
2128 ret =
2129 _gnutls_copy_ciphersuites (session, extdata, extdatalen, TRUE);
2130 _gnutls_extension_list_add (session,
2131 GNUTLS_EXTENSION_SAFE_RENEGOTIATION);
2133 else
2134 ret = _gnutls_copy_ciphersuites (session, extdata, extdatalen, FALSE);
2136 if (ret > 0)
2138 ret = _mbuffer_append_data (bufel, extdata, ret);
2139 if (ret < 0)
2141 gnutls_assert ();
2142 gnutls_free (extdata);
2143 return ret;
2146 else
2148 if (extdatalen == 0)
2149 extdatalen = GNUTLS_E_INTERNAL_ERROR;
2150 gnutls_free (bufel);
2151 gnutls_free (extdata);
2152 gnutls_assert ();
2153 return ret;
2157 /* Copy the compression methods.
2159 ret = _gnutls_copy_comp_methods (session, extdata, extdatalen);
2160 if (ret > 0)
2162 ret = _mbuffer_append_data (bufel, extdata, ret);
2163 if (ret < 0)
2165 gnutls_assert ();
2166 gnutls_free (extdata);
2167 return ret;
2170 else
2172 if (extdatalen == 0)
2173 extdatalen = GNUTLS_E_INTERNAL_ERROR;
2174 gnutls_free (bufel);
2175 gnutls_free (extdata);
2176 gnutls_assert ();
2177 return ret;
2180 /* Generate and copy TLS extensions.
2182 if (_gnutls_version_has_extensions (hver))
2183 type = GNUTLS_EXT_ANY;
2184 else
2186 if (session->internals.initial_negotiation_completed != 0)
2187 type = GNUTLS_EXT_MANDATORY;
2188 else
2189 type = GNUTLS_EXT_NONE;
2192 ret = _gnutls_gen_extensions (session, extdata, extdatalen, type);
2194 if (ret > 0)
2196 ret = _mbuffer_append_data (bufel, extdata, ret);
2197 if (ret < 0)
2199 gnutls_assert ();
2200 gnutls_free (extdata);
2201 return ret;
2204 else if (ret < 0)
2206 gnutls_assert ();
2207 gnutls_free (bufel);
2208 gnutls_free (extdata);
2209 return ret;
2213 gnutls_free (extdata);
2215 ret =
2216 _gnutls_send_handshake (session, bufel, GNUTLS_HANDSHAKE_CLIENT_HELLO);
2218 return ret;
2221 static int
2222 _gnutls_send_server_hello (gnutls_session_t session, int again)
2224 mbuffer_st *bufel = NULL;
2225 opaque *data = NULL;
2226 opaque *extdata = NULL;
2227 int extdatalen;
2228 int pos = 0;
2229 int datalen, ret = 0;
2230 uint8_t comp;
2231 uint8_t session_id_len = session->security_parameters.session_id_size;
2232 opaque buf[2 * TLS_MAX_SESSION_ID_SIZE + 1];
2234 datalen = 0;
2236 if (again == 0)
2239 extdata = gnutls_malloc (MAX_EXT_DATA_LENGTH);
2240 if (extdata == NULL)
2242 gnutls_assert ();
2243 return GNUTLS_E_MEMORY_ERROR;
2246 datalen = 2 + session_id_len + 1 + GNUTLS_RANDOM_SIZE + 3;
2247 ret =
2248 _gnutls_gen_extensions (session, extdata, MAX_EXT_DATA_LENGTH,
2249 GNUTLS_EXT_ANY);
2251 if (ret < 0)
2253 gnutls_assert ();
2254 goto fail;
2256 extdatalen = ret;
2258 bufel =
2259 _gnutls_handshake_alloc (datalen + extdatalen, datalen + extdatalen);
2260 if (bufel == NULL)
2262 gnutls_assert ();
2263 ret = GNUTLS_E_MEMORY_ERROR;
2264 goto fail;
2266 data = _mbuffer_get_udata_ptr (bufel);
2268 data[pos++] =
2269 _gnutls_version_get_major (session->security_parameters.version);
2270 data[pos++] =
2271 _gnutls_version_get_minor (session->security_parameters.version);
2273 memcpy (&data[pos],
2274 session->security_parameters.server_random, GNUTLS_RANDOM_SIZE);
2275 pos += GNUTLS_RANDOM_SIZE;
2277 data[pos++] = session_id_len;
2278 if (session_id_len > 0)
2280 memcpy (&data[pos], session->security_parameters.session_id,
2281 session_id_len);
2283 pos += session_id_len;
2285 _gnutls_handshake_log ("HSK[%p]: SessionID: %s\n", session,
2286 _gnutls_bin2hex (session->security_parameters.
2287 session_id, session_id_len, buf,
2288 sizeof (buf), NULL));
2290 memcpy (&data[pos],
2291 session->security_parameters.current_cipher_suite.suite, 2);
2292 pos += 2;
2294 comp =
2295 (uint8_t) _gnutls_compression_get_num (session->internals.
2296 compression_method);
2297 data[pos++] = comp;
2300 if (extdatalen > 0)
2302 datalen += extdatalen;
2304 memcpy (&data[pos], extdata, extdatalen);
2308 ret =
2309 _gnutls_send_handshake (session, bufel, GNUTLS_HANDSHAKE_SERVER_HELLO);
2311 fail:
2312 gnutls_free (extdata);
2313 return ret;
2317 _gnutls_send_hello (gnutls_session_t session, int again)
2319 int ret;
2321 if (session->security_parameters.entity == GNUTLS_CLIENT)
2323 ret = _gnutls_send_client_hello (session, again);
2326 else
2327 { /* SERVER */
2328 ret = _gnutls_send_server_hello (session, again);
2331 return ret;
2334 /* RECEIVE A HELLO MESSAGE. This should be called from gnutls_recv_handshake_int only if a
2335 * hello message is expected. It uses the security_parameters.current_cipher_suite
2336 * and internals.compression_method.
2339 _gnutls_recv_hello (gnutls_session_t session, opaque * data, int datalen)
2341 int ret;
2343 if (session->security_parameters.entity == GNUTLS_CLIENT)
2345 ret = _gnutls_read_server_hello (session, data, datalen);
2346 if (ret < 0)
2348 gnutls_assert ();
2349 return ret;
2352 else
2353 { /* Server side reading a client hello */
2355 ret = _gnutls_read_client_hello (session, data, datalen);
2356 if (ret < 0)
2358 gnutls_assert ();
2359 return ret;
2363 ret = _gnutls_ext_sr_verify (session);
2364 if (ret < 0)
2366 gnutls_assert ();
2367 return ret;
2370 return 0;
2373 /* The packets in gnutls_handshake (it's more broad than original TLS handshake)
2375 * Client Server
2377 * ClientHello -------->
2378 * <-------- ServerHello
2380 * Certificate*
2381 * ServerKeyExchange*
2382 * <-------- CertificateRequest*
2384 * <-------- ServerHelloDone
2385 * Certificate*
2386 * ClientKeyExchange
2387 * CertificateVerify*
2388 * [ChangeCipherSpec]
2389 * Finished -------->
2390 * NewSessionTicket
2391 * [ChangeCipherSpec]
2392 * <-------- Finished
2394 * (*): means optional packet.
2397 /* Handshake when resumming session:
2398 * Client Server
2400 * ClientHello -------->
2401 * ServerHello
2402 * [ChangeCipherSpec]
2403 * <-------- Finished
2404 * [ChangeCipherSpec]
2405 * Finished -------->
2410 * gnutls_rehandshake:
2411 * @session: is a #gnutls_session_t structure.
2413 * This function will renegotiate security parameters with the
2414 * client. This should only be called in case of a server.
2416 * This message informs the peer that we want to renegotiate
2417 * parameters (perform a handshake).
2419 * If this function succeeds (returns 0), you must call the
2420 * gnutls_handshake() function in order to negotiate the new
2421 * parameters.
2423 * Since TLS is full duplex some application data might have been
2424 * sent during peer's processing of this message. In that case
2425 * one should call gnutls_record_recv() until GNUTLS_E_REHANDSHAKE
2426 * is returned to clear any pending data. Care must be taken if
2427 * rehandshake is mandatory to terminate if it does not start after
2428 * some threshold.
2430 * If the client does not wish to renegotiate parameters he will
2431 * should with an alert message, thus the return code will be
2432 * %GNUTLS_E_WARNING_ALERT_RECEIVED and the alert will be
2433 * %GNUTLS_A_NO_RENEGOTIATION. A client may also choose to ignore
2434 * this message.
2436 * Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
2439 gnutls_rehandshake (gnutls_session_t session)
2441 int ret;
2443 /* only server sends that handshake packet */
2444 if (session->security_parameters.entity == GNUTLS_CLIENT)
2445 return GNUTLS_E_INVALID_REQUEST;
2447 ret =
2448 _gnutls_send_empty_handshake (session, GNUTLS_HANDSHAKE_HELLO_REQUEST,
2449 AGAIN (STATE50));
2450 STATE = STATE50;
2452 if (ret < 0)
2454 gnutls_assert ();
2455 return ret;
2457 STATE = STATE0;
2459 return 0;
2462 inline static int
2463 _gnutls_abort_handshake (gnutls_session_t session, int ret)
2465 if (((ret == GNUTLS_E_WARNING_ALERT_RECEIVED) &&
2466 (gnutls_alert_get (session) == GNUTLS_A_NO_RENEGOTIATION))
2467 || ret == GNUTLS_E_GOT_APPLICATION_DATA)
2468 return 0;
2470 /* this doesn't matter */
2471 return GNUTLS_E_INTERNAL_ERROR;
2475 /* This function initialized the handshake hash session.
2476 * required for finished messages.
2478 static int
2479 _gnutls_handshake_hash_init (gnutls_session_t session)
2481 gnutls_protocol_t ver = gnutls_protocol_get_version (session);
2483 if (session->internals.handshake_mac_handle_init == 0)
2485 int ret;
2487 /* set the hash type for handshake message hashing */
2488 if (_gnutls_version_has_selectable_prf (ver))
2489 session->security_parameters.handshake_mac_handle_type =
2490 HANDSHAKE_MAC_TYPE_12;
2491 else
2492 session->security_parameters.handshake_mac_handle_type =
2493 HANDSHAKE_MAC_TYPE_10;
2495 if (session->security_parameters.handshake_mac_handle_type ==
2496 HANDSHAKE_MAC_TYPE_10)
2498 ret =
2499 _gnutls_hash_init (&session->internals.handshake_mac_handle.tls10.
2500 md5, GNUTLS_MAC_MD5);
2502 if (ret < 0)
2504 gnutls_assert ();
2505 return ret;
2508 ret =
2509 _gnutls_hash_init (&session->internals.handshake_mac_handle.tls10.
2510 sha, GNUTLS_MAC_SHA1);
2511 if (ret < 0)
2513 gnutls_assert ();
2514 _gnutls_hash_deinit (&session->internals.handshake_mac_handle.
2515 tls10.md5, NULL);
2516 return GNUTLS_E_MEMORY_ERROR;
2519 else if (session->security_parameters.handshake_mac_handle_type ==
2520 HANDSHAKE_MAC_TYPE_12)
2522 /* The algorithm to compute hash over handshake messages must be
2523 same as the one used as the basis for PRF. By now we use
2524 SHA256. */
2525 ret =
2526 _gnutls_hash_init (&session->internals.handshake_mac_handle.tls12.
2527 sha256, GNUTLS_DIG_SHA256);
2528 if (ret < 0)
2530 gnutls_assert ();
2531 return GNUTLS_E_MEMORY_ERROR;
2534 ret =
2535 _gnutls_hash_init (&session->internals.handshake_mac_handle.tls12.
2536 sha1, GNUTLS_DIG_SHA1);
2537 if (ret < 0)
2539 gnutls_assert ();
2540 _gnutls_hash_deinit (&session->internals.handshake_mac_handle.
2541 tls12.sha256, NULL);
2542 return GNUTLS_E_MEMORY_ERROR;
2546 session->internals.handshake_mac_handle_init = 1;
2549 return 0;
2552 static int
2553 _gnutls_send_supplemental (gnutls_session_t session, int again)
2555 mbuffer_st *bufel;
2556 int ret = 0;
2558 _gnutls_debug_log ("EXT[%p]: Sending supplemental data\n", session);
2560 if (again)
2561 ret =
2562 _gnutls_send_handshake (session, NULL, GNUTLS_HANDSHAKE_SUPPLEMENTAL);
2563 else
2565 gnutls_buffer_st buf;
2566 _gnutls_buffer_init (&buf);
2568 ret = _gnutls_gen_supplemental (session, &buf);
2569 if (ret < 0)
2571 gnutls_assert ();
2572 return ret;
2575 bufel = _gnutls_handshake_alloc (buf.length, buf.length);
2576 if (bufel == NULL)
2578 gnutls_assert ();
2579 return GNUTLS_E_MEMORY_ERROR;
2582 _mbuffer_set_udata (bufel, buf.data, buf.length);
2583 _gnutls_buffer_clear (&buf);
2585 ret = _gnutls_send_handshake (session, bufel,
2586 GNUTLS_HANDSHAKE_SUPPLEMENTAL);
2589 return ret;
2592 static int
2593 _gnutls_recv_supplemental (gnutls_session_t session)
2595 uint8_t *data = NULL;
2596 int datalen = 0;
2597 int ret;
2599 _gnutls_debug_log ("EXT[%p]: Expecting supplemental data\n", session);
2601 ret = _gnutls_recv_handshake (session, &data, &datalen,
2602 GNUTLS_HANDSHAKE_SUPPLEMENTAL,
2603 OPTIONAL_PACKET);
2604 if (ret < 0)
2606 gnutls_assert ();
2607 return ret;
2610 ret = _gnutls_parse_supplemental (session, data, datalen);
2611 if (ret < 0)
2613 gnutls_assert ();
2614 return ret;
2617 gnutls_free (data);
2619 return ret;
2623 * gnutls_handshake:
2624 * @session: is a #gnutls_session_t structure.
2626 * This function does the handshake of the TLS/SSL protocol, and
2627 * initializes the TLS connection.
2629 * This function will fail if any problem is encountered, and will
2630 * return a negative error code. In case of a client, if the client
2631 * has asked to resume a session, but the server couldn't, then a
2632 * full handshake will be performed.
2634 * The non-fatal errors such as %GNUTLS_E_AGAIN and
2635 * %GNUTLS_E_INTERRUPTED interrupt the handshake procedure, which
2636 * should be later be resumed. Call this function again, until it
2637 * returns 0; cf. gnutls_record_get_direction() and
2638 * gnutls_error_is_fatal().
2640 * If this function is called by a server after a rehandshake request
2641 * then %GNUTLS_E_GOT_APPLICATION_DATA or
2642 * %GNUTLS_E_WARNING_ALERT_RECEIVED may be returned. Note that these
2643 * are non fatal errors, only in the specific case of a rehandshake.
2644 * Their meaning is that the client rejected the rehandshake request or
2645 * in the case of %GNUTLS_E_GOT_APPLICATION_DATA it might also mean that
2646 * some data were pending.
2648 * Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
2651 gnutls_handshake (gnutls_session_t session)
2653 int ret;
2654 record_parameters_st *params;
2656 ret = _gnutls_epoch_get (session, session->security_parameters.epoch_next,
2657 &params);
2658 if (ret < 0)
2660 /* We assume the epoch is not allocated if _gnutls_epoch_get fails. */
2661 ret =
2662 _gnutls_epoch_alloc (session, session->security_parameters.epoch_next,
2663 NULL);
2664 if (ret < 0)
2666 gnutls_assert ();
2667 return ret;
2671 if (session->security_parameters.entity == GNUTLS_CLIENT)
2673 ret = _gnutls_handshake_client (session);
2675 else
2677 ret = _gnutls_handshake_server (session);
2679 if (ret < 0)
2681 /* In the case of a rehandshake abort
2682 * we should reset the handshake's internal state.
2684 if (_gnutls_abort_handshake (session, ret) == 0)
2685 STATE = STATE0;
2687 return ret;
2690 ret = _gnutls_handshake_common (session);
2692 if (ret < 0)
2694 if (_gnutls_abort_handshake (session, ret) == 0)
2695 STATE = STATE0;
2697 return ret;
2700 STATE = STATE0;
2702 _gnutls_handshake_io_buffer_clear (session);
2703 _gnutls_handshake_internal_state_clear (session);
2705 session->security_parameters.epoch_next++;
2707 return 0;
2711 #define IMED_RET( str, ret, allow_alert) do { \
2712 if (ret < 0) { \
2713 /* EAGAIN and INTERRUPTED are always non-fatal */ \
2714 if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED) \
2715 return ret; \
2716 /* a warning alert might interrupt handshake */ \
2717 if (allow_alert != 0 && ret==GNUTLS_E_WARNING_ALERT_RECEIVED) return ret; \
2718 gnutls_assert(); \
2719 ERR( str, ret); \
2720 _gnutls_handshake_hash_buffers_clear(session); \
2721 return ret; \
2722 } } while (0)
2727 * _gnutls_handshake_client
2728 * This function performs the client side of the handshake of the TLS/SSL protocol.
2731 _gnutls_handshake_client (gnutls_session_t session)
2733 int ret = 0;
2735 #ifdef HANDSHAKE_DEBUG
2736 char buf[64];
2738 if (session->internals.resumed_security_parameters.session_id_size > 0)
2739 _gnutls_handshake_log ("HSK[%p]: Ask to resume: %s\n", session,
2740 _gnutls_bin2hex (session->
2741 internals.resumed_security_parameters.session_id,
2742 session->
2743 internals.resumed_security_parameters.session_id_size,
2744 buf, sizeof (buf), NULL));
2745 #endif
2747 switch (STATE)
2749 case STATE0:
2750 case STATE1:
2751 ret = _gnutls_send_hello (session, AGAIN (STATE1));
2752 STATE = STATE1;
2753 IMED_RET ("send hello", ret, 1);
2755 case STATE2:
2756 /* receive the server hello */
2757 ret =
2758 _gnutls_recv_handshake (session, NULL, NULL,
2759 GNUTLS_HANDSHAKE_SERVER_HELLO,
2760 MANDATORY_PACKET);
2761 STATE = STATE2;
2762 IMED_RET ("recv hello", ret, 1);
2764 case STATE70:
2765 if (session->security_parameters.do_recv_supplemental)
2767 ret = _gnutls_recv_supplemental (session);
2768 STATE = STATE70;
2769 IMED_RET ("recv supplemental", ret, 1);
2772 case STATE3:
2773 /* RECV CERTIFICATE */
2774 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
2775 ret = _gnutls_recv_server_certificate (session);
2776 STATE = STATE3;
2777 IMED_RET ("recv server certificate", ret, 1);
2779 case STATE4:
2780 /* receive the server key exchange */
2781 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
2782 ret = _gnutls_recv_server_kx_message (session);
2783 STATE = STATE4;
2784 IMED_RET ("recv server kx message", ret, 1);
2786 case STATE5:
2787 /* receive the server certificate request - if any
2790 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
2791 ret = _gnutls_recv_server_certificate_request (session);
2792 STATE = STATE5;
2793 IMED_RET ("recv server certificate request message", ret, 1);
2795 case STATE6:
2796 /* receive the server hello done */
2797 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
2798 ret =
2799 _gnutls_recv_handshake (session, NULL, NULL,
2800 GNUTLS_HANDSHAKE_SERVER_HELLO_DONE,
2801 MANDATORY_PACKET);
2802 STATE = STATE6;
2803 IMED_RET ("recv server hello done", ret, 1);
2805 case STATE71:
2806 if (session->security_parameters.do_send_supplemental)
2808 ret = _gnutls_send_supplemental (session, AGAIN (STATE71));
2809 STATE = STATE71;
2810 IMED_RET ("send supplemental", ret, 0);
2813 case STATE7:
2814 /* send our certificate - if any and if requested
2816 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
2817 ret = _gnutls_send_client_certificate (session, AGAIN (STATE7));
2818 STATE = STATE7;
2819 IMED_RET ("send client certificate", ret, 0);
2821 case STATE8:
2822 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
2823 ret = _gnutls_send_client_kx_message (session, AGAIN (STATE8));
2824 STATE = STATE8;
2825 IMED_RET ("send client kx", ret, 0);
2827 case STATE9:
2828 /* send client certificate verify */
2829 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
2830 ret =
2831 _gnutls_send_client_certificate_verify (session, AGAIN (STATE9));
2832 STATE = STATE9;
2833 IMED_RET ("send client certificate verify", ret, 1);
2835 STATE = STATE0;
2836 default:
2837 break;
2841 return 0;
2844 /* This function sends the final handshake packets and initializes connection
2846 static int
2847 _gnutls_send_handshake_final (gnutls_session_t session, int init)
2849 int ret = 0;
2851 /* Send the CHANGE CIPHER SPEC PACKET */
2853 switch (STATE)
2855 case STATE0:
2856 case STATE20:
2858 STATE = STATE20;
2859 ret = _gnutls_handshake_io_write_flush (session);
2860 if (ret < 0)
2862 gnutls_assert ();
2863 return ret;
2866 case STATE21:
2867 ret = _gnutls_send_change_cipher_spec (session, AGAIN (STATE21));
2868 STATE = STATE21;
2870 if (ret < 0)
2872 ERR ("send ChangeCipherSpec", ret);
2873 gnutls_assert ();
2874 return ret;
2876 /* Initialize the connection session (start encryption) - in case of client
2878 if (init == TRUE)
2880 ret = _gnutls_connection_state_init (session);
2881 if (ret < 0)
2883 gnutls_assert ();
2884 return ret;
2888 ret = _gnutls_write_connection_state_init (session);
2889 if (ret < 0)
2891 gnutls_assert ();
2892 return ret;
2895 case STATE22:
2896 /* send the finished message */
2897 ret = _gnutls_send_finished (session, AGAIN (STATE22));
2898 STATE = STATE22;
2899 if (ret < 0)
2901 ERR ("send Finished", ret);
2902 gnutls_assert ();
2903 return ret;
2906 STATE = STATE0;
2907 default:
2908 break;
2911 return 0;
2914 /* This function receives the final handshake packets
2915 * And executes the appropriate function to initialize the
2916 * read session.
2918 static int
2919 _gnutls_recv_handshake_final (gnutls_session_t session, int init)
2921 int ret = 0;
2922 uint8_t ch;
2924 switch (STATE)
2926 case STATE0:
2927 case STATE30:
2928 ret = _gnutls_recv_int (session, GNUTLS_CHANGE_CIPHER_SPEC, -1, &ch, 1);
2929 STATE = STATE30;
2930 if (ret <= 0)
2932 ERR ("recv ChangeCipherSpec", ret);
2933 gnutls_assert ();
2934 return (ret < 0) ? ret : GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
2937 /* Initialize the connection session (start encryption) - in case of server */
2938 if (init == TRUE)
2940 ret = _gnutls_connection_state_init (session);
2941 if (ret < 0)
2943 gnutls_assert ();
2944 return ret;
2948 ret = _gnutls_read_connection_state_init (session);
2949 if (ret < 0)
2951 gnutls_assert ();
2952 return ret;
2955 case STATE31:
2956 ret = _gnutls_recv_finished (session);
2957 STATE = STATE31;
2958 if (ret < 0)
2960 ERR ("recv finished", ret);
2961 gnutls_assert ();
2962 return ret;
2964 STATE = STATE0;
2965 default:
2966 break;
2970 return 0;
2974 * _gnutls_handshake_server
2975 * This function does the server stuff of the handshake protocol.
2978 _gnutls_handshake_server (gnutls_session_t session)
2980 int ret = 0;
2982 switch (STATE)
2984 case STATE0:
2985 case STATE1:
2986 ret =
2987 _gnutls_recv_handshake (session, NULL, NULL,
2988 GNUTLS_HANDSHAKE_CLIENT_HELLO,
2989 MANDATORY_PACKET);
2990 STATE = STATE1;
2991 IMED_RET ("recv hello", ret, 1);
2993 case STATE2:
2994 ret = _gnutls_send_hello (session, AGAIN (STATE2));
2995 STATE = STATE2;
2996 IMED_RET ("send hello", ret, 1);
2998 case STATE70:
2999 if (session->security_parameters.do_send_supplemental)
3001 ret = _gnutls_send_supplemental (session, AGAIN (STATE70));
3002 STATE = STATE70;
3003 IMED_RET ("send supplemental data", ret, 0);
3006 /* SEND CERTIFICATE + KEYEXCHANGE + CERTIFICATE_REQUEST */
3007 case STATE3:
3008 /* NOTE: these should not be send if we are resuming */
3010 if (session->internals.resumed == RESUME_FALSE)
3011 ret = _gnutls_send_server_certificate (session, AGAIN (STATE3));
3012 STATE = STATE3;
3013 IMED_RET ("send server certificate", ret, 0);
3015 case STATE4:
3016 /* send server key exchange (A) */
3017 if (session->internals.resumed == RESUME_FALSE)
3018 ret = _gnutls_send_server_kx_message (session, AGAIN (STATE4));
3019 STATE = STATE4;
3020 IMED_RET ("send server kx", ret, 0);
3022 case STATE5:
3023 /* Send certificate request - if requested to */
3024 if (session->internals.resumed == RESUME_FALSE)
3025 ret =
3026 _gnutls_send_server_certificate_request (session, AGAIN (STATE5));
3027 STATE = STATE5;
3028 IMED_RET ("send server cert request", ret, 0);
3030 case STATE6:
3031 /* send the server hello done */
3032 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
3033 ret =
3034 _gnutls_send_empty_handshake (session,
3035 GNUTLS_HANDSHAKE_SERVER_HELLO_DONE,
3036 AGAIN (STATE6));
3037 STATE = STATE6;
3038 IMED_RET ("send server hello done", ret, 1);
3040 case STATE71:
3041 if (session->security_parameters.do_recv_supplemental)
3043 ret = _gnutls_recv_supplemental (session);
3044 STATE = STATE71;
3045 IMED_RET ("recv client supplemental", ret, 1);
3048 /* RECV CERTIFICATE + KEYEXCHANGE + CERTIFICATE_VERIFY */
3049 case STATE7:
3050 /* receive the client certificate message */
3051 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
3052 ret = _gnutls_recv_client_certificate (session);
3053 STATE = STATE7;
3054 IMED_RET ("recv client certificate", ret, 1);
3056 case STATE8:
3057 /* receive the client key exchange message */
3058 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
3059 ret = _gnutls_recv_client_kx_message (session);
3060 STATE = STATE8;
3061 IMED_RET ("recv client kx", ret, 1);
3063 case STATE9:
3064 /* receive the client certificate verify message */
3065 if (session->internals.resumed == RESUME_FALSE) /* if we are not resuming */
3066 ret = _gnutls_recv_client_certificate_verify_message (session);
3067 STATE = STATE9;
3068 IMED_RET ("recv client certificate verify", ret, 1);
3070 STATE = STATE0; /* finished thus clear session */
3071 default:
3072 break;
3075 return 0;
3079 _gnutls_handshake_common (gnutls_session_t session)
3081 int ret = 0;
3083 /* send and recv the change cipher spec and finished messages */
3084 if ((session->internals.resumed == RESUME_TRUE
3085 && session->security_parameters.entity == GNUTLS_CLIENT)
3086 || (session->internals.resumed == RESUME_FALSE
3087 && session->security_parameters.entity == GNUTLS_SERVER))
3089 /* if we are a client resuming - or we are a server not resuming */
3091 ret = _gnutls_recv_handshake_final (session, TRUE);
3092 IMED_RET ("recv handshake final", ret, 1);
3094 #ifdef ENABLE_SESSION_TICKET
3095 switch (STATE)
3097 case STATE0:
3098 case STATE40:
3099 ret = _gnutls_send_new_session_ticket (session, AGAIN (STATE40));
3100 STATE = STATE40;
3101 IMED_RET ("send handshake new session ticket", ret, 0);
3102 STATE = STATE0;
3103 default:
3104 break;
3106 #endif
3108 ret = _gnutls_send_handshake_final (session, FALSE);
3109 IMED_RET ("send handshake final", ret, 1);
3111 /* only store if we are not resuming */
3112 if (session->security_parameters.entity == GNUTLS_SERVER)
3114 /* in order to support session resuming */
3115 _gnutls_server_register_current_session (session);
3118 else
3119 { /* if we are a client not resuming - or we are a server resuming */
3121 ret = _gnutls_send_handshake_final (session, TRUE);
3122 IMED_RET ("send handshake final 2", ret, 1);
3124 #ifdef ENABLE_SESSION_TICKET
3125 switch (STATE)
3127 case STATE0:
3128 case STATE41:
3129 ret = _gnutls_recv_new_session_ticket (session);
3130 STATE = STATE41;
3131 IMED_RET ("recv handshake new session ticket", ret, 1);
3132 STATE = STATE0;
3133 default:
3134 break;
3136 #endif
3138 ret = _gnutls_recv_handshake_final (session, FALSE);
3139 IMED_RET ("recv handshake final 2", ret, 1);
3144 /* clear handshake buffer */
3145 _gnutls_handshake_hash_buffers_clear (session);
3146 return ret;
3151 _gnutls_generate_session_id (opaque * session_id, uint8_t * len)
3153 int ret;
3155 *len = TLS_MAX_SESSION_ID_SIZE;
3157 ret = _gnutls_rnd (GNUTLS_RND_NONCE, session_id, *len);
3158 if (ret < 0)
3160 gnutls_assert ();
3161 return ret;
3164 return 0;
3168 _gnutls_recv_hello_request (gnutls_session_t session, void *data,
3169 uint32_t data_size)
3171 uint8_t type;
3173 if (session->security_parameters.entity == GNUTLS_SERVER)
3175 gnutls_assert ();
3176 return GNUTLS_E_UNEXPECTED_PACKET;
3178 if (data_size < 1)
3180 gnutls_assert ();
3181 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
3183 type = ((uint8_t *) data)[0];
3184 if (type == GNUTLS_HANDSHAKE_HELLO_REQUEST)
3185 return GNUTLS_E_REHANDSHAKE;
3186 else
3188 gnutls_assert ();
3189 return GNUTLS_E_UNEXPECTED_PACKET;
3193 /* Returns 1 if the given KX has not the corresponding parameters
3194 * (DH or RSA) set up. Otherwise returns 0.
3196 inline static int
3197 check_server_params (gnutls_session_t session,
3198 gnutls_kx_algorithm_t kx,
3199 gnutls_kx_algorithm_t * alg, int alg_size)
3201 int cred_type;
3202 gnutls_dh_params_t dh_params = NULL;
3203 gnutls_rsa_params_t rsa_params = NULL;
3204 int j;
3206 cred_type = _gnutls_map_kx_get_cred (kx, 1);
3208 /* Read the Diffie-Hellman parameters, if any.
3210 if (cred_type == GNUTLS_CRD_CERTIFICATE)
3212 int delete;
3213 gnutls_certificate_credentials_t x509_cred =
3214 (gnutls_certificate_credentials_t) _gnutls_get_cred (session->key,
3215 cred_type, NULL);
3217 if (x509_cred != NULL)
3219 dh_params =
3220 _gnutls_get_dh_params (x509_cred->dh_params,
3221 x509_cred->params_func, session);
3222 rsa_params =
3223 _gnutls_certificate_get_rsa_params (x509_cred->rsa_params,
3224 x509_cred->params_func,
3225 session);
3228 /* Check also if the certificate supports the
3229 * KX method.
3231 delete = 1;
3232 for (j = 0; j < alg_size; j++)
3234 if (alg[j] == kx)
3236 delete = 0;
3237 break;
3241 if (delete == 1)
3242 return 1;
3244 #ifdef ENABLE_ANON
3246 else if (cred_type == GNUTLS_CRD_ANON)
3248 gnutls_anon_server_credentials_t anon_cred =
3249 (gnutls_anon_server_credentials_t) _gnutls_get_cred (session->key,
3250 cred_type, NULL);
3252 if (anon_cred != NULL)
3254 dh_params =
3255 _gnutls_get_dh_params (anon_cred->dh_params,
3256 anon_cred->params_func, session);
3258 #endif
3259 #ifdef ENABLE_PSK
3261 else if (cred_type == GNUTLS_CRD_PSK)
3263 gnutls_psk_server_credentials_t psk_cred =
3264 (gnutls_psk_server_credentials_t) _gnutls_get_cred (session->key,
3265 cred_type, NULL);
3267 if (psk_cred != NULL)
3269 dh_params =
3270 _gnutls_get_dh_params (psk_cred->dh_params, psk_cred->params_func,
3271 session);
3273 #endif
3275 else
3276 return 0; /* no need for params */
3279 /* If the key exchange method needs RSA or DH params,
3280 * but they are not set then remove it.
3282 if (_gnutls_kx_needs_rsa_params (kx) != 0)
3284 /* needs rsa params. */
3285 if (_gnutls_rsa_params_to_mpi (rsa_params) == NULL)
3287 gnutls_assert ();
3288 return 1;
3292 if (_gnutls_kx_needs_dh_params (kx) != 0)
3294 /* needs DH params. */
3295 if (_gnutls_dh_params_to_mpi (dh_params) == NULL)
3297 gnutls_assert ();
3298 return 1;
3302 return 0;
3305 /* This function will remove algorithms that are not supported by
3306 * the requested authentication method. We remove an algorithm if
3307 * we have a certificate with keyUsage bits set.
3309 * This does a more high level check than gnutls_supported_ciphersuites(),
3310 * by checking certificates etc.
3313 _gnutls_remove_unwanted_ciphersuites (gnutls_session_t session,
3314 cipher_suite_st ** cipherSuites,
3315 int numCipherSuites,
3316 gnutls_pk_algorithm_t requested_pk_algo)
3319 int ret = 0;
3320 cipher_suite_st *newSuite, cs;
3321 int newSuiteSize = 0, i;
3322 gnutls_certificate_credentials_t cert_cred;
3323 gnutls_kx_algorithm_t kx;
3324 int server = session->security_parameters.entity == GNUTLS_SERVER ? 1 : 0;
3325 gnutls_kx_algorithm_t *alg = NULL;
3326 int alg_size = 0;
3328 /* if we should use a specific certificate,
3329 * we should remove all algorithms that are not supported
3330 * by that certificate and are on the same authentication
3331 * method (CERTIFICATE).
3334 cert_cred =
3335 (gnutls_certificate_credentials_t) _gnutls_get_cred (session->key,
3336 GNUTLS_CRD_CERTIFICATE,
3337 NULL);
3339 /* If there are certificate credentials, find an appropriate certificate
3340 * or disable them;
3342 if (session->security_parameters.entity == GNUTLS_SERVER
3343 && cert_cred != NULL)
3345 ret = _gnutls_server_select_cert (session, requested_pk_algo);
3346 if (ret < 0)
3348 gnutls_assert ();
3349 _gnutls_x509_log ("Could not find an appropriate certificate: %s\n",
3350 gnutls_strerror (ret));
3351 cert_cred = NULL;
3355 /* get all the key exchange algorithms that are
3356 * supported by the X509 certificate parameters.
3358 if ((ret =
3359 _gnutls_selected_cert_supported_kx (session, &alg, &alg_size)) < 0)
3361 gnutls_assert ();
3362 return ret;
3365 newSuite = gnutls_malloc (numCipherSuites * sizeof (cipher_suite_st));
3366 if (newSuite == NULL)
3368 gnutls_assert ();
3369 gnutls_free (alg);
3370 return GNUTLS_E_MEMORY_ERROR;
3373 /* now removes ciphersuites based on the KX algorithm
3375 for (i = 0; i < numCipherSuites; i++)
3377 int delete = 0;
3379 /* finds the key exchange algorithm in
3380 * the ciphersuite
3382 kx = _gnutls_cipher_suite_get_kx_algo (&(*cipherSuites)[i]);
3384 /* if it is defined but had no credentials
3386 if (_gnutls_get_kx_cred (session, kx, NULL) == NULL)
3388 delete = 1;
3390 else
3392 delete = 0;
3394 if (server)
3395 delete = check_server_params (session, kx, alg, alg_size);
3398 /* These two SRP kx's are marked to require a CRD_CERTIFICATE,
3399 (see cred_mappings in gnutls_algorithms.c), but it also
3400 requires a SRP credential. Don't use SRP kx unless we have a
3401 SRP credential too. */
3402 if (kx == GNUTLS_KX_SRP_RSA || kx == GNUTLS_KX_SRP_DSS)
3404 if (!_gnutls_get_cred (session->key, GNUTLS_CRD_SRP, NULL))
3405 delete = 1;
3408 memcpy (&cs.suite, &(*cipherSuites)[i].suite, 2);
3410 if (delete == 0)
3413 _gnutls_handshake_log ("HSK[%p]: Keeping ciphersuite: %s\n",
3414 session,
3415 _gnutls_cipher_suite_get_name (&cs));
3417 memcpy (newSuite[newSuiteSize].suite, (*cipherSuites)[i].suite, 2);
3418 newSuiteSize++;
3420 else
3422 _gnutls_handshake_log ("HSK[%p]: Removing ciphersuite: %s\n",
3423 session,
3424 _gnutls_cipher_suite_get_name (&cs));
3429 gnutls_free (alg);
3430 gnutls_free (*cipherSuites);
3431 *cipherSuites = newSuite;
3433 ret = newSuiteSize;
3435 return ret;
3440 * gnutls_handshake_set_max_packet_length:
3441 * @session: is a #gnutls_session_t structure.
3442 * @max: is the maximum number.
3444 * This function will set the maximum size of all handshake messages.
3445 * Handshakes over this size are rejected with
3446 * %GNUTLS_E_HANDSHAKE_TOO_LARGE error code. The default value is
3447 * 48kb which is typically large enough. Set this to 0 if you do not
3448 * want to set an upper limit.
3450 * The reason for restricting the handshake message sizes are to
3451 * limit Denial of Service attacks.
3453 void
3454 gnutls_handshake_set_max_packet_length (gnutls_session_t session, size_t max)
3456 session->internals.max_handshake_data_buffer_size = max;
3459 void
3460 _gnutls_set_adv_version (gnutls_session_t session, gnutls_protocol_t ver)
3462 set_adv_version (session, _gnutls_version_get_major (ver),
3463 _gnutls_version_get_minor (ver));
3466 gnutls_protocol_t
3467 _gnutls_get_adv_version (gnutls_session_t session)
3469 return _gnutls_version_get (_gnutls_get_adv_version_major (session),
3470 _gnutls_get_adv_version_minor (session));
3474 * gnutls_handshake_get_last_in:
3475 * @session: is a #gnutls_session_t structure.
3477 * This function is only useful to check where the last performed
3478 * handshake failed. If the previous handshake succeed or was not
3479 * performed at all then no meaningful value will be returned.
3481 * Check %gnutls_handshake_description_t in gnutls.h for the
3482 * available handshake descriptions.
3484 * Returns: the last handshake message type received, a
3485 * %gnutls_handshake_description_t.
3487 gnutls_handshake_description_t
3488 gnutls_handshake_get_last_in (gnutls_session_t session)
3490 return session->internals.last_handshake_in;
3494 * gnutls_handshake_get_last_out:
3495 * @session: is a #gnutls_session_t structure.
3497 * This function is only useful to check where the last performed
3498 * handshake failed. If the previous handshake succeed or was not
3499 * performed at all then no meaningful value will be returned.
3501 * Check %gnutls_handshake_description_t in gnutls.h for the
3502 * available handshake descriptions.
3504 * Returns: the last handshake message type sent, a
3505 * %gnutls_handshake_description_t.
3507 gnutls_handshake_description_t
3508 gnutls_handshake_get_last_out (gnutls_session_t session)
3510 return session->internals.last_handshake_out;