2 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS 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 3 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 License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 /* Functions that relate to the TLS handshake procedure.
26 #include "gnutls_int.h"
27 #include "gnutls_errors.h"
28 #include "gnutls_dh.h"
30 #include "algorithms.h"
31 #include "gnutls_compress.h"
32 #include "gnutls_cipher.h"
33 #include "gnutls_buffers.h"
34 #include "gnutls_mbuffers.h"
35 #include "gnutls_kx.h"
36 #include "gnutls_handshake.h"
37 #include "gnutls_num.h"
38 #include "gnutls_hash_int.h"
39 #include "gnutls_db.h"
40 #include "gnutls_extensions.h"
41 #include "gnutls_supplemental.h"
42 #include "gnutls_auth.h"
43 #include "gnutls_v2_compat.h"
44 #include <auth/cert.h>
45 #include "gnutls_constate.h"
46 #include <gnutls_record.h>
47 #include <gnutls_state.h>
49 #include <ext/session_ticket.h>
50 #include <ext/safe_renegotiation.h>
51 #include <gnutls_rsa_export.h> /* for gnutls_get_rsa_params() */
52 #include <auth/anon.h> /* for gnutls_anon_server_credentials_t */
53 #include <auth/psk.h> /* for gnutls_psk_server_credentials_t */
55 #include <gnutls_dtls.h>
57 #ifdef HANDSHAKE_DEBUG
58 #define ERR(x, y) _gnutls_handshake_log("HSK[%p]: %s (%d)\n", session, x,y)
66 static int _gnutls_server_select_comp_method (gnutls_session_t session
,
67 uint8_t * data
, int datalen
);
69 _gnutls_remove_unwanted_ciphersuites (gnutls_session_t session
,
70 uint8_t * cipher_suites
,
71 int cipher_suites_size
,
72 gnutls_pk_algorithm_t
*pk_algos
,
73 size_t pk_algos_size
);
74 static int _gnutls_handshake_common (gnutls_session_t session
);
75 static int _gnutls_handshake_client (gnutls_session_t session
);
76 static int _gnutls_handshake_server (gnutls_session_t session
);
78 /* Empties but does not free the buffer
81 _gnutls_handshake_hash_buffer_empty (gnutls_session_t session
)
84 _gnutls_buffers_log ("BUF[HSK]: Emptied buffer\n");
86 session
->internals
.handshake_hash_buffer_prev_len
= 0;
87 session
->internals
.handshake_hash_buffer
.length
= 0;
92 _gnutls_handshake_hash_add_recvd (gnutls_session_t session
,
93 gnutls_handshake_description_t recv_type
,
94 uint8_t * header
, uint16_t header_size
,
95 uint8_t * dataptr
, uint32_t datalen
);
98 _gnutls_handshake_hash_add_sent (gnutls_session_t session
,
99 gnutls_handshake_description_t type
,
100 uint8_t * dataptr
, uint32_t datalen
);
103 _gnutls_recv_hello_verify_request (gnutls_session_t session
,
104 uint8_t * data
, int datalen
);
107 /* Clears the handshake hash buffers and handles.
110 _gnutls_handshake_hash_buffers_clear (gnutls_session_t session
)
112 session
->internals
.handshake_hash_buffer_prev_len
= 0;
113 _gnutls_buffer_clear(&session
->internals
.handshake_hash_buffer
);
116 /* this will copy the required values for resuming to
117 * internals, and to security_parameters.
118 * this will keep as less data to security_parameters.
121 resume_copy_required_values (gnutls_session_t session
)
123 /* get the new random values */
124 memcpy (session
->internals
.resumed_security_parameters
.server_random
,
125 session
->security_parameters
.server_random
, GNUTLS_RANDOM_SIZE
);
126 memcpy (session
->internals
.resumed_security_parameters
.client_random
,
127 session
->security_parameters
.client_random
, GNUTLS_RANDOM_SIZE
);
129 /* keep the ciphersuite and compression
130 * That is because the client must see these in our
133 memcpy (session
->security_parameters
.cipher_suite
,
134 session
->internals
.resumed_security_parameters
.cipher_suite
, 2);
135 session
->security_parameters
.compression_method
= session
->internals
.resumed_security_parameters
.compression_method
;
137 _gnutls_epoch_set_cipher_suite (session
, EPOCH_NEXT
,
139 internals
.resumed_security_parameters
.cipher_suite
);
140 _gnutls_epoch_set_compression (session
, EPOCH_NEXT
,
142 internals
.resumed_security_parameters
.compression_method
);
144 /* or write_compression_algorithm
148 session
->security_parameters
.entity
=
149 session
->internals
.resumed_security_parameters
.entity
;
151 _gnutls_set_current_version (session
,
152 session
->internals
.resumed_security_parameters
.
155 session
->security_parameters
.cert_type
=
156 session
->internals
.resumed_security_parameters
.cert_type
;
158 memcpy (session
->security_parameters
.session_id
,
159 session
->internals
.resumed_security_parameters
.session_id
,
160 sizeof (session
->security_parameters
.session_id
));
161 session
->security_parameters
.session_id_size
=
162 session
->internals
.resumed_security_parameters
.session_id_size
;
167 _gnutls_set_server_random (gnutls_session_t session
, uint8_t * rnd
)
169 memcpy (session
->security_parameters
.server_random
, rnd
,
174 _gnutls_set_client_random (gnutls_session_t session
, uint8_t * rnd
)
176 memcpy (session
->security_parameters
.client_random
, rnd
,
180 /* Calculate The SSL3 Finished message
182 #define SSL3_CLIENT_MSG "CLNT"
183 #define SSL3_SERVER_MSG "SRVR"
184 #define SSL_MSG_LEN 4
186 _gnutls_ssl3_finished (gnutls_session_t session
, int type
, uint8_t * ret
, int sending
)
194 len
= session
->internals
.handshake_hash_buffer
.length
;
196 len
= session
->internals
.handshake_hash_buffer_prev_len
;
198 rc
= _gnutls_hash_init (&td_sha
, GNUTLS_DIG_SHA1
);
200 return gnutls_assert_val(rc
);
202 rc
= _gnutls_hash_init (&td_md5
, GNUTLS_DIG_MD5
);
205 _gnutls_hash_deinit (&td_sha
, NULL
);
206 return gnutls_assert_val(rc
);
209 _gnutls_hash(&td_sha
, session
->internals
.handshake_hash_buffer
.data
, len
);
210 _gnutls_hash(&td_md5
, session
->internals
.handshake_hash_buffer
.data
, len
);
212 if (type
== GNUTLS_SERVER
)
213 mesg
= SSL3_SERVER_MSG
;
215 mesg
= SSL3_CLIENT_MSG
;
217 _gnutls_hash (&td_md5
, mesg
, SSL_MSG_LEN
);
218 _gnutls_hash (&td_sha
, mesg
, SSL_MSG_LEN
);
220 rc
= _gnutls_mac_deinit_ssl3_handshake (&td_md5
, ret
,
222 security_parameters
.master_secret
,
226 _gnutls_hash_deinit (&td_md5
, NULL
);
227 _gnutls_hash_deinit (&td_sha
, NULL
);
228 return gnutls_assert_val(rc
);
231 rc
= _gnutls_mac_deinit_ssl3_handshake (&td_sha
, &ret
[16],
233 security_parameters
.master_secret
,
237 _gnutls_hash_deinit (&td_sha
, NULL
);
238 return gnutls_assert_val(rc
);
244 /* Hash the handshake messages as required by TLS 1.0
246 #define SERVER_MSG "server finished"
247 #define CLIENT_MSG "client finished"
248 #define TLS_MSG_LEN 15
250 _gnutls_finished (gnutls_session_t session
, int type
, void *ret
, int sending
)
252 const int siz
= TLS_MSG_LEN
;
253 uint8_t concat
[MAX_HASH_SIZE
+ 16 /*MD5 */ ];
259 len
= session
->internals
.handshake_hash_buffer
.length
;
261 len
= session
->internals
.handshake_hash_buffer_prev_len
;
263 if (!_gnutls_version_has_selectable_prf (gnutls_protocol_get_version(session
)))
265 rc
= _gnutls_hash_fast( GNUTLS_DIG_SHA1
, session
->internals
.handshake_hash_buffer
.data
, len
, &concat
[16]);
267 return gnutls_assert_val(rc
);
269 rc
= _gnutls_hash_fast( GNUTLS_DIG_MD5
, session
->internals
.handshake_hash_buffer
.data
, len
, concat
);
271 return gnutls_assert_val(rc
);
277 int algorithm
= _gnutls_cipher_suite_get_prf(session
->security_parameters
.cipher_suite
);
279 rc
= _gnutls_hash_fast( algorithm
, session
->internals
.handshake_hash_buffer
.data
, len
, concat
);
281 return gnutls_assert_val(rc
);
283 hash_len
= _gnutls_hash_get_algo_len (algorithm
);
286 if (type
== GNUTLS_SERVER
)
295 return _gnutls_PRF (session
, session
->security_parameters
.master_secret
,
296 GNUTLS_MASTER_SIZE
, mesg
, siz
, concat
, hash_len
, 12, ret
);
299 /* this function will produce GNUTLS_RANDOM_SIZE==32 bytes of random data
303 _gnutls_tls_create_random (uint8_t * dst
)
308 /* Use weak random numbers for the most of the
309 * buffer except for the first 4 that are the
313 tim
= gnutls_time (NULL
);
314 /* generate server random value */
315 _gnutls_write_uint32 (tim
, dst
);
317 ret
= _gnutls_rnd (GNUTLS_RND_NONCE
, &dst
[4], GNUTLS_RANDOM_SIZE
- 4);
327 /* returns the 0 on success or a negative error code.
330 _gnutls_negotiate_version (gnutls_session_t session
,
331 gnutls_protocol_t adv_version
)
335 /* if we do not support that version */
336 if (_gnutls_version_is_supported (session
, adv_version
) == 0)
338 /* If he requested something we do not support
339 * then we send him the highest we support.
341 ret
= _gnutls_version_max (session
);
342 if (ret
== GNUTLS_VERSION_UNKNOWN
)
344 /* this check is not really needed.
347 return GNUTLS_E_UNKNOWN_CIPHER_SUITE
;
355 _gnutls_set_current_version (session
, ret
);
361 _gnutls_user_hello_func (gnutls_session_t session
,
362 gnutls_protocol_t adv_version
)
366 if (session
->internals
.user_hello_func
!= NULL
)
368 ret
= session
->internals
.user_hello_func (session
);
374 /* Here we need to renegotiate the version since the callee might
375 * have disabled some TLS versions.
377 ret
= _gnutls_negotiate_version (session
, adv_version
);
387 /* Read a client hello packet.
388 * A client hello must be a known version client hello
389 * or version 2.0 client hello (only for compatibility
390 * since SSL version 2.0 is not supported).
393 _gnutls_read_client_hello (gnutls_session_t session
, uint8_t * data
,
396 uint8_t session_id_len
;
398 uint16_t suite_size
, comp_size
;
399 gnutls_protocol_t adv_version
;
402 uint8_t rnd
[GNUTLS_RANDOM_SIZE
], *suite_ptr
, *comp_ptr
, *session_id
;
406 _gnutls_handshake_log ("HSK[%p]: Client's version: %d.%d\n", session
,
407 data
[pos
], data
[pos
+ 1]);
409 adv_version
= _gnutls_version_get (data
[pos
], data
[pos
+ 1]);
410 set_adv_version (session
, data
[pos
], data
[pos
+ 1]);
413 neg_version
= _gnutls_negotiate_version (session
, adv_version
);
420 /* Read client random value.
422 DECR_LEN (len
, GNUTLS_RANDOM_SIZE
);
423 _gnutls_set_client_random (session
, &data
[pos
]);
424 pos
+= GNUTLS_RANDOM_SIZE
;
426 _gnutls_tls_create_random (rnd
);
427 _gnutls_set_server_random (session
, rnd
);
429 session
->security_parameters
.timestamp
= gnutls_time (NULL
);
432 session_id_len
= data
[pos
++];
436 if (session_id_len
> TLS_MAX_SESSION_ID_SIZE
)
439 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
441 DECR_LEN (len
, session_id_len
);
442 session_id
= &data
[pos
];
443 pos
+= session_id_len
;
445 if (IS_DTLS(session
))
450 cookie_size
= data
[pos
++];
451 DECR_LEN (len
, cookie_size
);
455 ret
= _gnutls_server_restore_session (session
, session_id
, session_id_len
);
457 if (session_id_len
> 0) session
->internals
.resumption_requested
= 1;
460 { /* resumed using default TLS resumption! */
461 /* Parse only the safe renegotiation extension
462 * We don't want to parse any other extensions since
463 * we don't want new extension values to overwrite the
467 /* move forward to extensions */
469 suite_size
= _gnutls_read_uint16 (&data
[pos
]);
472 DECR_LEN (len
, suite_size
);
476 comp_size
= data
[pos
++]; /* z is the number of compression methods */
477 DECR_LEN (len
, comp_size
);
480 ret
= _gnutls_parse_extensions (session
, GNUTLS_EXT_MANDATORY
,
488 resume_copy_required_values (session
);
489 session
->internals
.resumed
= RESUME_TRUE
;
491 return _gnutls_user_hello_func (session
, adv_version
);
495 _gnutls_generate_session_id (session
->security_parameters
.session_id
,
497 security_parameters
.session_id_size
);
499 session
->internals
.resumed
= RESUME_FALSE
;
502 /* Remember ciphersuites for later
505 suite_size
= _gnutls_read_uint16 (&data
[pos
]);
508 DECR_LEN (len
, suite_size
);
509 suite_ptr
= &data
[pos
];
512 /* Point to the compression methods
515 comp_size
= data
[pos
++]; /* z is the number of compression methods */
517 DECR_LEN (len
, comp_size
);
518 comp_ptr
= &data
[pos
];
521 /* Parse the extensions (if any)
523 * Unconditionally try to parse extensions; safe renegotiation uses them in
524 * sslv3 and higher, even though sslv3 doesn't officially support them.
526 ret
= _gnutls_parse_extensions (session
, GNUTLS_EXT_APPLICATION
,
528 /* len is the rest of the parsed length */
535 ret
= _gnutls_user_hello_func (session
, adv_version
);
542 ret
= _gnutls_parse_extensions (session
, GNUTLS_EXT_MANDATORY
,
550 ret
= _gnutls_parse_extensions (session
, GNUTLS_EXT_TLS
, &data
[pos
], len
);
557 /* resumed by session_ticket extension */
558 if (session
->internals
.resumed
!= RESUME_FALSE
)
560 /* to indicate the client that the current session is resumed */
561 memcpy (session
->internals
.resumed_security_parameters
.session_id
,
562 session_id
, session_id_len
);
563 session
->internals
.resumed_security_parameters
.session_id_size
=
566 session
->internals
.resumed_security_parameters
.max_record_recv_size
=
567 session
->security_parameters
.max_record_recv_size
;
568 session
->internals
.resumed_security_parameters
.max_record_send_size
=
569 session
->security_parameters
.max_record_send_size
;
571 resume_copy_required_values (session
);
573 return _gnutls_user_hello_func (session
, adv_version
);
576 /* select an appropriate cipher suite
578 ret
= _gnutls_server_select_suite (session
, suite_ptr
, suite_size
);
585 /* select appropriate compression method */
586 ret
= _gnutls_server_select_comp_method (session
, comp_ptr
, comp_size
);
596 /* This is to be called after sending CHANGE CIPHER SPEC packet
597 * and initializing encryption. This is the first encrypted message
601 _gnutls_send_finished (gnutls_session_t session
, int again
)
606 size_t vdata_size
= 0;
610 bufel
= _gnutls_handshake_alloc (session
, MAX_VERIFY_DATA_SIZE
, MAX_VERIFY_DATA_SIZE
);
614 return GNUTLS_E_MEMORY_ERROR
;
616 data
= _mbuffer_get_udata_ptr (bufel
);
618 if (gnutls_protocol_get_version (session
) == GNUTLS_SSL3
)
621 _gnutls_ssl3_finished (session
,
622 session
->security_parameters
.entity
, data
, 1);
623 _mbuffer_set_udata_size (bufel
, 36);
627 ret
= _gnutls_finished (session
,
628 session
->security_parameters
.entity
, data
, 1);
629 _mbuffer_set_udata_size (bufel
, 12);
638 vdata_size
= _mbuffer_get_udata_size (bufel
);
640 ret
= _gnutls_ext_sr_finished (session
, data
, vdata_size
, 0);
647 if ((session
->internals
.resumed
== RESUME_FALSE
648 && session
->security_parameters
.entity
== GNUTLS_CLIENT
)
649 || (session
->internals
.resumed
!= RESUME_FALSE
650 && session
->security_parameters
.entity
== GNUTLS_SERVER
))
652 /* if we are a client not resuming - or we are a server resuming */
653 _gnutls_handshake_log ("HSK[%p]: recording tls-unique CB (send)\n",
655 memcpy (session
->internals
.cb_tls_unique
, data
, vdata_size
);
656 session
->internals
.cb_tls_unique_len
= vdata_size
;
660 _gnutls_send_handshake (session
, bufel
, GNUTLS_HANDSHAKE_FINISHED
);
664 ret
= _gnutls_send_handshake (session
, NULL
, GNUTLS_HANDSHAKE_FINISHED
);
670 /* This is to be called after sending our finished message. If everything
671 * went fine we have negotiated a secure connection
674 _gnutls_recv_finished (gnutls_session_t session
)
676 uint8_t data
[MAX_VERIFY_DATA_SIZE
], *vrfy
;
677 gnutls_buffer_st buf
;
683 _gnutls_recv_handshake (session
, GNUTLS_HANDSHAKE_FINISHED
,
687 ERR ("recv finished int", ret
);
693 vrfy_size
= buf
.length
;
695 if (gnutls_protocol_get_version (session
) == GNUTLS_SSL3
)
704 if (vrfy_size
!= data_size
)
707 ret
= GNUTLS_E_ERROR_IN_FINISHED_PACKET
;
711 if (gnutls_protocol_get_version (session
) == GNUTLS_SSL3
)
714 _gnutls_ssl3_finished (session
,
715 (session
->security_parameters
.entity
+ 1) % 2,
721 _gnutls_finished (session
,
722 (session
->security_parameters
.entity
+
732 if (memcmp (vrfy
, data
, data_size
) != 0)
735 ret
= GNUTLS_E_ERROR_IN_FINISHED_PACKET
;
739 ret
= _gnutls_ext_sr_finished (session
, data
, data_size
, 1);
746 if ((session
->internals
.resumed
!= RESUME_FALSE
747 && session
->security_parameters
.entity
== GNUTLS_CLIENT
)
748 || (session
->internals
.resumed
== RESUME_FALSE
749 && session
->security_parameters
.entity
== GNUTLS_SERVER
))
751 /* if we are a client resuming - or we are a server not resuming */
752 _gnutls_handshake_log ("HSK[%p]: recording tls-unique CB (recv)\n",
754 memcpy (session
->internals
.cb_tls_unique
, data
, data_size
);
755 session
->internals
.cb_tls_unique_len
= data_size
;
759 session
->internals
.initial_negotiation_completed
= 1;
762 _gnutls_buffer_clear(&buf
);
767 /* returns PK_RSA if the given cipher suite list only supports,
768 * RSA algorithms, PK_DSA if DSS, and PK_ANY for both or PK_NONE for none.
771 server_find_pk_algos_in_ciphersuites (const uint8_t *
772 data
, unsigned int datalen
,
773 gnutls_pk_algorithm_t
* algos
,
777 gnutls_kx_algorithm_t kx
;
778 unsigned int max
= *algos_size
;
780 if (datalen
% 2 != 0)
783 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
787 for (j
= 0; j
< datalen
; j
+= 2)
789 kx
= _gnutls_cipher_suite_get_kx_algo (&data
[j
]);
790 if (_gnutls_map_kx_get_cred (kx
, 1) == GNUTLS_CRD_CERTIFICATE
)
792 algos
[(*algos_size
)++] = _gnutls_map_pk_get_pk (kx
);
794 if ((*algos_size
) >= max
)
802 /* This selects the best supported ciphersuite from the given ones. Then
803 * it adds the suite to the session and performs some checks.
806 _gnutls_server_select_suite (gnutls_session_t session
, uint8_t * data
,
807 unsigned int datalen
)
810 unsigned int i
, j
, cipher_suites_size
;
811 size_t pk_algos_size
;
812 uint8_t cipher_suites
[MAX_CIPHERSUITE_SIZE
];
814 gnutls_pk_algorithm_t pk_algos
[MAX_ALGOS
]; /* will hold the pk algorithms
815 * supported by the peer.
818 /* First, check for safe renegotiation SCSV.
820 if (session
->internals
.priorities
.sr
!= SR_DISABLED
)
824 for (offset
= 0; offset
< datalen
; offset
+= 2)
826 /* TLS_RENEGO_PROTECTION_REQUEST = { 0x00, 0xff } */
827 if (data
[offset
] == GNUTLS_RENEGO_PROTECTION_REQUEST_MAJOR
&&
828 data
[offset
+ 1] == GNUTLS_RENEGO_PROTECTION_REQUEST_MINOR
)
830 _gnutls_handshake_log
831 ("HSK[%p]: Received safe renegotiation CS\n", session
);
832 retval
= _gnutls_ext_sr_recv_cs (session
);
843 pk_algos_size
= MAX_ALGOS
;
844 ret
= server_find_pk_algos_in_ciphersuites (data
, datalen
, pk_algos
, &pk_algos_size
);
846 return gnutls_assert_val(ret
);
848 ret
= _gnutls_supported_ciphersuites (session
, cipher_suites
, sizeof(cipher_suites
));
850 return gnutls_assert_val(ret
);
852 cipher_suites_size
= ret
;
854 /* Here we remove any ciphersuite that does not conform
855 * the certificate requested, or to the
856 * authentication requested (e.g. SRP).
858 ret
= _gnutls_remove_unwanted_ciphersuites (session
, cipher_suites
, cipher_suites_size
, pk_algos
, pk_algos_size
);
865 return GNUTLS_E_UNKNOWN_CIPHER_SUITE
;
868 cipher_suites_size
= ret
;
870 /* Data length should be zero mod 2 since
871 * every ciphersuite is 2 bytes. (this check is needed
874 if (datalen
% 2 != 0)
877 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
880 memset (session
->security_parameters
.cipher_suite
, 0, 2);
882 retval
= GNUTLS_E_UNKNOWN_CIPHER_SUITE
;
884 _gnutls_handshake_log ("HSK[%p]: Requested cipher suites[size: %d]: \n", session
, (int)datalen
);
886 if (session
->internals
.priorities
.server_precedence
== 0)
888 for (j
= 0; j
< datalen
; j
+= 2)
890 _gnutls_handshake_log ("\t0x%.2x, 0x%.2x %s\n", data
[j
], data
[j
+1], _gnutls_cipher_suite_get_name (&data
[j
]));
891 for (i
= 0; i
< cipher_suites_size
; i
+=2)
893 if (memcmp (&cipher_suites
[i
], &data
[j
], 2) == 0)
895 _gnutls_handshake_log
896 ("HSK[%p]: Selected cipher suite: %s\n", session
,
897 _gnutls_cipher_suite_get_name (&data
[j
]));
898 memcpy (session
->security_parameters
.cipher_suite
,
899 &cipher_suites
[i
], 2);
900 _gnutls_epoch_set_cipher_suite (session
, EPOCH_NEXT
,
902 security_parameters
.cipher_suite
);
911 else /* server selects */
913 for (i
= 0; i
< cipher_suites_size
; i
+=2)
915 for (j
= 0; j
< datalen
; j
+= 2)
917 if (memcmp (&cipher_suites
[i
], &data
[j
], 2) == 0)
919 _gnutls_handshake_log
920 ("HSK[%p]: Selected cipher suite: %s\n", session
,
921 _gnutls_cipher_suite_get_name (&data
[j
]));
922 memcpy (session
->security_parameters
.cipher_suite
,
923 &cipher_suites
[i
], 2);
924 _gnutls_epoch_set_cipher_suite (session
, EPOCH_NEXT
,
926 security_parameters
.cipher_suite
);
943 /* check if the credentials (username, public key etc.) are ok
945 if (_gnutls_get_kx_cred
947 _gnutls_cipher_suite_get_kx_algo (session
->
948 security_parameters
.cipher_suite
),
949 &err
) == NULL
&& err
!= 0)
952 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
956 /* set the mod_auth_st to the appropriate struct
957 * according to the KX algorithm. This is needed since all the
958 * handshake functions are read from there;
960 session
->internals
.auth_struct
=
961 _gnutls_kx_auth_struct (_gnutls_cipher_suite_get_kx_algo
963 security_parameters
.cipher_suite
));
964 if (session
->internals
.auth_struct
== NULL
)
967 _gnutls_handshake_log
968 ("HSK[%p]: Cannot find the appropriate handler for the KX algorithm\n",
971 return GNUTLS_E_INTERNAL_ERROR
;
979 /* This selects the best supported compression method from the ones provided
982 _gnutls_server_select_comp_method (gnutls_session_t session
,
983 uint8_t * data
, int datalen
)
986 uint8_t comps
[MAX_ALGOS
];
988 x
= _gnutls_supported_compression_methods (session
, comps
, MAX_ALGOS
);
995 if (session
->internals
.priorities
.server_precedence
== 0)
997 for (j
= 0; j
< datalen
; j
++)
999 for (i
= 0; i
< x
; i
++)
1001 if (comps
[i
] == data
[j
])
1003 gnutls_compression_method_t method
=
1004 _gnutls_compression_get_id (comps
[i
]);
1006 _gnutls_epoch_set_compression (session
, EPOCH_NEXT
, method
);
1007 session
->security_parameters
.compression_method
= method
;
1009 _gnutls_handshake_log
1010 ("HSK[%p]: Selected Compression Method: %s\n", session
,
1011 gnutls_compression_get_name (method
));
1019 for (i
= 0; i
< x
; i
++)
1021 for (j
= 0; j
< datalen
; j
++)
1023 if (comps
[i
] == data
[j
])
1025 gnutls_compression_method_t method
=
1026 _gnutls_compression_get_id (comps
[i
]);
1028 _gnutls_epoch_set_compression (session
, EPOCH_NEXT
, method
);
1029 session
->security_parameters
.compression_method
= method
;
1031 _gnutls_handshake_log
1032 ("HSK[%p]: Selected Compression Method: %s\n", session
,
1033 gnutls_compression_get_name (method
));
1040 /* we were not able to find a compatible compression
1044 return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM
;
1048 /* This function sends an empty handshake packet. (like hello request).
1049 * If the previous _gnutls_send_empty_handshake() returned
1050 * GNUTLS_E_AGAIN or GNUTLS_E_INTERRUPTED, then it must be called again
1051 * (until it returns ok), with NULL parameters.
1054 _gnutls_send_empty_handshake (gnutls_session_t session
,
1055 gnutls_handshake_description_t type
, int again
)
1061 bufel
= _gnutls_handshake_alloc (session
, 0, 0);
1065 return GNUTLS_E_MEMORY_ERROR
;
1071 return _gnutls_send_handshake (session
, bufel
, type
);
1077 /* This function sends a handshake message of type 'type' containing the
1078 * data specified here. If the previous _gnutls_send_handshake() returned
1079 * GNUTLS_E_AGAIN or GNUTLS_E_INTERRUPTED, then it must be called again
1080 * (until it returns ok), with NULL parameters.
1083 _gnutls_send_handshake (gnutls_session_t session
, mbuffer_st
* bufel
,
1084 gnutls_handshake_description_t type
)
1088 uint32_t datasize
, i_datasize
;
1093 /* we are resuming a previously interrupted
1096 ret
= _gnutls_handshake_io_write_flush (session
);
1102 data
= _mbuffer_get_uhead_ptr (bufel
);
1103 i_datasize
= _mbuffer_get_udata_size(bufel
);
1104 datasize
= i_datasize
+ _mbuffer_get_uhead_size (bufel
);
1106 data
[pos
++] = (uint8_t) type
;
1107 _gnutls_write_uint24 (_mbuffer_get_udata_size (bufel
), &data
[pos
]);
1110 /* Add DTLS handshake fragment headers. The message will be
1111 * fragmented later by the fragmentation sub-layer. All fields must
1112 * be set properly for HMAC. The HMAC requires we pretend that the
1113 * message was sent in a single fragment. */
1114 if (IS_DTLS(session
))
1116 _gnutls_write_uint16 (session
->internals
.dtls
.hsk_write_seq
++, &data
[pos
]);
1119 /* Fragment offset */
1120 _gnutls_write_uint24 (0, &data
[pos
]);
1123 /* Fragment length */
1124 _gnutls_write_uint24 (i_datasize
, &data
[pos
]);
1128 _gnutls_handshake_log ("HSK[%p]: %s was queued [%ld bytes]\n",
1129 session
, _gnutls_handshake2str (type
),
1132 /* Here we keep the handshake messages in order to hash them...
1134 if (type
!= GNUTLS_HANDSHAKE_HELLO_REQUEST
)
1136 _gnutls_handshake_hash_add_sent (session
, type
, data
, datasize
)) < 0)
1139 _mbuffer_xfree(&bufel
);
1143 session
->internals
.last_handshake_out
= type
;
1145 ret
= _gnutls_handshake_io_cache_int (session
, type
, bufel
);
1148 _mbuffer_xfree(&bufel
);
1155 case GNUTLS_HANDSHAKE_CERTIFICATE_PKT
: /* this one is followed by ServerHelloDone
1156 * or ClientKeyExchange always.
1158 case GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE
: /* as above */
1159 case GNUTLS_HANDSHAKE_SERVER_HELLO
: /* as above */
1160 case GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST
: /* as above */
1161 case GNUTLS_HANDSHAKE_NEW_SESSION_TICKET
: /* followed by ChangeCipherSpec */
1163 /* now for client Certificate, ClientKeyExchange and
1164 * CertificateVerify are always followed by ChangeCipherSpec
1166 case GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY
:
1167 case GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE
:
1171 /* send cached messages */
1172 ret
= _gnutls_handshake_io_write_flush (session
);
1179 #define CHECK_SIZE(ll) \
1180 if ((session->internals.max_handshake_data_buffer_size > 0) && \
1181 (((ll) + session->internals.handshake_hash_buffer.length) > \
1182 session->internals.max_handshake_data_buffer_size)) \
1183 return gnutls_assert_val(GNUTLS_E_HANDSHAKE_TOO_LARGE)
1185 /* This function add the handshake headers and the
1186 * handshake data to the handshake hash buffers. Needed
1187 * for the finished messages calculations.
1190 _gnutls_handshake_hash_add_recvd (gnutls_session_t session
,
1191 gnutls_handshake_description_t recv_type
,
1192 uint8_t * header
, uint16_t header_size
,
1193 uint8_t * dataptr
, uint32_t datalen
)
1197 if ((gnutls_protocol_get_version (session
) != GNUTLS_DTLS0_9
&&
1198 recv_type
== GNUTLS_HANDSHAKE_HELLO_VERIFY_REQUEST
) ||
1199 recv_type
== GNUTLS_HANDSHAKE_HELLO_REQUEST
)
1202 CHECK_SIZE(header_size
+ datalen
);
1204 session
->internals
.handshake_hash_buffer_prev_len
= session
->internals
.handshake_hash_buffer
.length
;
1206 if (gnutls_protocol_get_version (session
) != GNUTLS_DTLS0_9
)
1208 ret
= _gnutls_buffer_append_data(&session
->internals
.handshake_hash_buffer
,
1209 header
, header_size
);
1211 return gnutls_assert_val(ret
);
1215 ret
= _gnutls_buffer_append_data(&session
->internals
.handshake_hash_buffer
,
1218 return gnutls_assert_val(ret
);
1224 /* This function will store the handshake message we sent.
1227 _gnutls_handshake_hash_add_sent (gnutls_session_t session
,
1228 gnutls_handshake_description_t type
,
1229 uint8_t * dataptr
, uint32_t datalen
)
1233 /* We don't check for GNUTLS_HANDSHAKE_HELLO_VERIFY_REQUEST because it
1234 * is not sent via that channel.
1236 if (type
!= GNUTLS_HANDSHAKE_HELLO_REQUEST
)
1238 CHECK_SIZE(datalen
);
1240 if (gnutls_protocol_get_version (session
) == GNUTLS_DTLS0_9
)
1242 /* Old DTLS doesn't include the header in the MAC */
1246 return GNUTLS_E_INVALID_REQUEST
;
1252 ret
= _gnutls_buffer_append_data(&session
->internals
.handshake_hash_buffer
,
1255 return gnutls_assert_val(ret
);
1264 /* This function will receive handshake messages of the given types,
1265 * and will pass the message to the right place in order to be processed.
1266 * E.g. for the SERVER_HELLO message (if it is expected), it will be
1267 * passed to _gnutls_recv_hello().
1270 _gnutls_recv_handshake (gnutls_session_t session
,
1271 gnutls_handshake_description_t type
,
1272 unsigned int optional
, gnutls_buffer_st
* buf
)
1275 handshake_buffer_st hsk
;
1278 _gnutls_handshake_io_recv_int (session
, type
, &hsk
, optional
);
1281 if (optional
!= 0 && ret
== GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET
)
1283 if (buf
) _gnutls_buffer_init(buf
);
1287 return gnutls_assert_val_fatal(ret
);
1290 ret
= _gnutls_handshake_hash_add_recvd (session
, hsk
.htype
,
1291 hsk
.header
, hsk
.header_size
,
1292 hsk
.data
.data
, hsk
.data
.length
);
1301 case GNUTLS_HANDSHAKE_CLIENT_HELLO_V2
:
1302 case GNUTLS_HANDSHAKE_CLIENT_HELLO
:
1303 case GNUTLS_HANDSHAKE_SERVER_HELLO
:
1304 if (hsk
.htype
== GNUTLS_HANDSHAKE_CLIENT_HELLO_V2
)
1305 ret
= _gnutls_read_client_hello_v2 (session
, hsk
.data
.data
, hsk
.data
.length
);
1307 ret
= _gnutls_recv_hello (session
, hsk
.data
.data
, hsk
.data
.length
);
1315 goto cleanup
; /* caller doesn't need dataptr */
1318 case GNUTLS_HANDSHAKE_HELLO_VERIFY_REQUEST
:
1319 ret
= _gnutls_recv_hello_verify_request (session
, hsk
.data
.data
, hsk
.data
.length
);
1326 /* Signal our caller we have received a verification cookie
1327 and ClientHello needs to be sent again. */
1330 goto cleanup
; /* caller doesn't need dataptr */
1333 case GNUTLS_HANDSHAKE_SERVER_HELLO_DONE
:
1334 if (hsk
.data
.length
== 0)
1339 ret
= GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
1343 case GNUTLS_HANDSHAKE_CERTIFICATE_PKT
:
1344 case GNUTLS_HANDSHAKE_FINISHED
:
1345 case GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE
:
1346 case GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE
:
1347 case GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST
:
1348 case GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY
:
1349 case GNUTLS_HANDSHAKE_SUPPLEMENTAL
:
1350 case GNUTLS_HANDSHAKE_NEW_SESSION_TICKET
:
1351 ret
= hsk
.data
.length
;
1355 /* we shouldn't actually arrive here in any case .
1356 * unexpected messages should be catched after _gnutls_handshake_io_recv_int()
1358 ret
= GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET
;
1369 _gnutls_handshake_buffer_clear (&hsk
);
1373 /* This function checks if the given cipher suite is supported, and sets it
1377 _gnutls_client_set_ciphersuite (gnutls_session_t session
, uint8_t suite
[2])
1380 uint8_t cipher_suites
[MAX_CIPHERSUITE_SIZE
];
1381 int cipher_suite_size
;
1385 cipher_suite_size
= _gnutls_supported_ciphersuites (session
, cipher_suites
, sizeof(cipher_suites
));
1386 if (cipher_suite_size
< 0)
1389 return cipher_suite_size
;
1392 for (i
= 0; i
< cipher_suite_size
; i
+=2)
1394 if (memcmp (&cipher_suites
[i
], suite
, 2) == 0)
1404 _gnutls_handshake_log("HSK[%p]: unsupported cipher suite %.2X.%.2X\n", session
,
1405 (unsigned int)suite
[0], (unsigned int)suite
[1]);
1406 return GNUTLS_E_UNKNOWN_CIPHER_SUITE
;
1409 memcpy (session
->security_parameters
.cipher_suite
, suite
, 2);
1410 _gnutls_epoch_set_cipher_suite (session
, EPOCH_NEXT
,
1412 security_parameters
.cipher_suite
);
1414 _gnutls_handshake_log ("HSK[%p]: Selected cipher suite: %s\n", session
,
1415 _gnutls_cipher_suite_get_name
1417 security_parameters
.cipher_suite
));
1420 /* check if the credentials (username, public key etc.) are ok.
1421 * Actually checks if they exist.
1423 if (!session
->internals
.premaster_set
&&
1426 _gnutls_cipher_suite_get_kx_algo
1427 (session
->security_parameters
.cipher_suite
), &err
) == NULL
1431 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
1435 /* set the mod_auth_st to the appropriate struct
1436 * according to the KX algorithm. This is needed since all the
1437 * handshake functions are read from there;
1439 session
->internals
.auth_struct
=
1440 _gnutls_kx_auth_struct (_gnutls_cipher_suite_get_kx_algo
1442 security_parameters
.cipher_suite
));
1444 if (session
->internals
.auth_struct
== NULL
)
1447 _gnutls_handshake_log
1448 ("HSK[%p]: Cannot find the appropriate handler for the KX algorithm\n",
1451 return GNUTLS_E_INTERNAL_ERROR
;
1458 /* This function sets the given comp method to the session.
1461 _gnutls_client_set_comp_method (gnutls_session_t session
, uint8_t comp_method
)
1463 int comp_methods_num
;
1464 uint8_t compression_methods
[MAX_ALGOS
];
1465 int id
= _gnutls_compression_get_id(comp_method
);
1468 _gnutls_handshake_log ("HSK[%p]: Selected compression method: %s (%d)\n", session
,
1469 gnutls_compression_get_name(id
), (int)comp_method
);
1471 comp_methods_num
= _gnutls_supported_compression_methods (session
,
1472 compression_methods
, MAX_ALGOS
);
1473 if (comp_methods_num
< 0)
1476 return comp_methods_num
;
1479 for (i
= 0; i
< comp_methods_num
; i
++)
1481 if (compression_methods
[i
] == comp_method
)
1483 comp_methods_num
= 0;
1488 if (comp_methods_num
!= 0)
1491 return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM
;
1494 session
->security_parameters
.compression_method
= id
;
1495 _gnutls_epoch_set_compression (session
, EPOCH_NEXT
, id
);
1500 /* This function returns 0 if we are resuming a session or -1 otherwise.
1501 * This also sets the variables in the session. Used only while reading a server
1505 _gnutls_client_check_if_resuming (gnutls_session_t session
,
1506 uint8_t * session_id
, int session_id_len
)
1508 char buf
[2 * TLS_MAX_SESSION_ID_SIZE
+ 1];
1510 _gnutls_handshake_log ("HSK[%p]: SessionID length: %d\n", session
,
1512 _gnutls_handshake_log ("HSK[%p]: SessionID: %s\n", session
,
1513 _gnutls_bin2hex (session_id
, session_id_len
, buf
,
1514 sizeof (buf
), NULL
));
1516 if (session_id_len
> 0 &&
1517 session
->internals
.resumed_security_parameters
.session_id_size
==
1519 && memcmp (session_id
,
1520 session
->internals
.resumed_security_parameters
.session_id
,
1521 session_id_len
) == 0)
1523 /* resume session */
1524 memcpy (session
->internals
.resumed_security_parameters
.server_random
,
1525 session
->security_parameters
.server_random
, GNUTLS_RANDOM_SIZE
);
1526 memcpy (session
->internals
.resumed_security_parameters
.client_random
,
1527 session
->security_parameters
.client_random
, GNUTLS_RANDOM_SIZE
);
1529 _gnutls_epoch_set_cipher_suite
1530 (session
, EPOCH_NEXT
,
1532 resumed_security_parameters
.cipher_suite
);
1533 _gnutls_epoch_set_compression (session
, EPOCH_NEXT
,
1535 internals
.resumed_security_parameters
.compression_method
);
1537 session
->internals
.resumed
= RESUME_TRUE
; /* we are resuming */
1543 /* keep the new session id */
1544 session
->internals
.resumed
= RESUME_FALSE
; /* we are not resuming */
1545 session
->security_parameters
.session_id_size
= session_id_len
;
1546 memcpy (session
->security_parameters
.session_id
,
1547 session_id
, session_id_len
);
1554 /* This function reads and parses the server hello handshake message.
1555 * This function also restores resumed parameters if we are resuming a
1559 _gnutls_read_server_hello (gnutls_session_t session
,
1560 uint8_t * data
, int datalen
)
1562 uint8_t session_id_len
= 0;
1565 gnutls_protocol_t version
;
1571 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
1574 _gnutls_handshake_log ("HSK[%p]: Server's version: %d.%d\n",
1575 session
, data
[pos
], data
[pos
+ 1]);
1578 version
= _gnutls_version_get (data
[pos
], data
[pos
+ 1]);
1579 if (_gnutls_version_is_supported (session
, version
) == 0)
1582 return GNUTLS_E_UNSUPPORTED_VERSION_PACKET
;
1586 _gnutls_set_current_version (session
, version
);
1591 DECR_LEN (len
, GNUTLS_RANDOM_SIZE
);
1592 _gnutls_set_server_random (session
, &data
[pos
]);
1593 pos
+= GNUTLS_RANDOM_SIZE
;
1599 session_id_len
= data
[pos
++];
1601 if (len
< session_id_len
)
1604 return GNUTLS_E_UNSUPPORTED_VERSION_PACKET
;
1606 DECR_LEN (len
, session_id_len
);
1608 /* check if we are resuming and set the appropriate
1611 if (_gnutls_client_check_if_resuming
1612 (session
, &data
[pos
], session_id_len
) == 0)
1614 pos
+= session_id_len
+ 2 + 1;
1615 DECR_LEN (len
, 2 + 1);
1617 ret
= _gnutls_parse_extensions (session
, GNUTLS_EXT_MANDATORY
,
1627 pos
+= session_id_len
;
1629 /* Check if the given cipher suite is supported and copy
1630 * it to the session.
1634 ret
= _gnutls_client_set_ciphersuite (session
, &data
[pos
]);
1642 /* move to compression
1646 ret
= _gnutls_client_set_comp_method (session
, data
[pos
++]);
1650 return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM
;
1653 /* Parse extensions.
1655 ret
= _gnutls_parse_extensions (session
, GNUTLS_EXT_ANY
, &data
[pos
], len
);
1666 /* This function copies the appropriate ciphersuites to a locally allocated buffer
1667 * Needed in client hello messages. Returns the new data length. If add_scsv is
1668 * true, add the special safe renegotiation CS.
1671 _gnutls_copy_ciphersuites (gnutls_session_t session
,
1672 gnutls_buffer_st
* cdata
,
1676 uint8_t cipher_suites
[MAX_CIPHERSUITE_SIZE
+2];
1677 int cipher_suites_size
;
1678 size_t init_length
= cdata
->length
;
1680 ret
= _gnutls_supported_ciphersuites (session
, cipher_suites
, sizeof(cipher_suites
)-2);
1682 return gnutls_assert_val(ret
);
1684 /* Here we remove any ciphersuite that does not conform
1685 * the certificate requested, or to the
1686 * authentication requested (eg SRP).
1689 _gnutls_remove_unwanted_ciphersuites (session
, cipher_suites
, ret
, NULL
, 0);
1691 return gnutls_assert_val(ret
);
1693 /* If no cipher suites were enabled.
1696 return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS
);
1698 cipher_suites_size
= ret
;
1701 cipher_suites
[cipher_suites_size
] = 0x00;
1702 cipher_suites
[cipher_suites_size
+1] = 0xff;
1703 cipher_suites_size
+= 2;
1705 ret
= _gnutls_ext_sr_send_cs (session
);
1707 return gnutls_assert_val(ret
);
1710 ret
= _gnutls_buffer_append_data_prefix(cdata
, 16, cipher_suites
, cipher_suites_size
);
1712 return gnutls_assert_val(ret
);
1714 ret
= cdata
->length
- init_length
;
1720 /* This function copies the appropriate compression methods, to a locally allocated buffer
1721 * Needed in hello messages. Returns the new data length.
1724 _gnutls_copy_comp_methods (gnutls_session_t session
,
1725 gnutls_buffer_st
* cdata
)
1728 uint8_t compression_methods
[MAX_ALGOS
], comp_num
;
1729 size_t init_length
= cdata
->length
;
1731 ret
= _gnutls_supported_compression_methods (session
, compression_methods
, MAX_ALGOS
);
1733 return gnutls_assert_val(ret
);
1737 /* put the number of compression methods */
1738 ret
= _gnutls_buffer_append_prefix(cdata
, 8, comp_num
);
1740 return gnutls_assert_val(ret
);
1742 ret
= _gnutls_buffer_append_data(cdata
, compression_methods
, comp_num
);
1744 return gnutls_assert_val(ret
);
1746 ret
= cdata
->length
- init_length
;
1751 /* This should be sufficient by now. It should hold all the extensions
1752 * plus the headers in a hello message.
1754 #define MAX_EXT_DATA_LENGTH 32*1024
1756 /* This function sends the client hello handshake message.
1759 _gnutls_send_client_hello (gnutls_session_t session
, int again
)
1761 mbuffer_st
*bufel
= NULL
;
1762 uint8_t *data
= NULL
;
1764 int datalen
= 0, ret
= 0;
1765 uint8_t rnd
[GNUTLS_RANDOM_SIZE
];
1766 gnutls_protocol_t hver
;
1767 gnutls_buffer_st extdata
;
1768 int rehandshake
= 0;
1769 uint8_t session_id_len
=
1770 session
->internals
.resumed_security_parameters
.session_id_size
;
1773 _gnutls_buffer_init(&extdata
);
1775 /* note that rehandshake is different than resuming
1777 if (session
->security_parameters
.session_id_size
)
1782 if(IS_DTLS(session
))
1784 cookie_len
= session
->internals
.dtls
.cookie_len
+ 1;
1791 datalen
= 2 + (session_id_len
+ 1) + GNUTLS_RANDOM_SIZE
+ cookie_len
;
1792 /* 2 for version, (4 for unix time + 28 for random bytes==GNUTLS_RANDOM_SIZE)
1795 bufel
= _gnutls_handshake_alloc (session
, datalen
, datalen
+MAX_EXT_DATA_LENGTH
);
1799 return GNUTLS_E_MEMORY_ERROR
;
1801 data
= _mbuffer_get_udata_ptr (bufel
);
1803 /* if we are resuming a session then we set the
1804 * version number to the previously established.
1806 if (session_id_len
== 0)
1808 if (rehandshake
) /* already negotiated version thus version_max == negotiated version */
1809 hver
= session
->security_parameters
.version
;
1810 else /* new handshake. just get the max */
1811 hver
= _gnutls_version_max (session
);
1815 /* we are resuming a session */
1816 hver
= session
->internals
.resumed_security_parameters
.version
;
1819 if (hver
== GNUTLS_VERSION_UNKNOWN
|| hver
== 0)
1822 gnutls_free (bufel
);
1823 return GNUTLS_E_INTERNAL_ERROR
;
1826 data
[pos
++] = _gnutls_version_get_major (hver
);
1827 data
[pos
++] = _gnutls_version_get_minor (hver
);
1829 /* Set the version we advertized as maximum
1832 _gnutls_set_adv_version (session
, hver
);
1833 _gnutls_set_current_version (session
, hver
);
1835 if (session
->internals
.priorities
.ssl3_record_version
!= 0)
1837 /* Advertize the SSL 3.0 record packet version in
1838 * record packets during the handshake.
1839 * That is to avoid confusing implementations
1840 * that do not support TLS 1.2 and don't know
1841 * how 3,3 version of record packets look like.
1843 if (!IS_DTLS(session
))
1844 _gnutls_record_set_default_version (session
, 3, 0);
1845 else if (gnutls_protocol_get_version (session
) == GNUTLS_DTLS0_9
)
1846 _gnutls_record_set_default_version (session
, 1, 0);
1848 _gnutls_record_set_default_version (session
, 254, 255);
1851 /* In order to know when this session was initiated.
1853 session
->security_parameters
.timestamp
= gnutls_time (NULL
);
1855 /* Generate random data
1857 if (!IS_DTLS (session
)
1858 || session
->internals
.dtls
.hsk_hello_verify_requests
== 0)
1860 _gnutls_tls_create_random (rnd
);
1861 _gnutls_set_client_random (session
, rnd
);
1863 memcpy (&data
[pos
], rnd
, GNUTLS_RANDOM_SIZE
);
1866 memcpy (&data
[pos
], session
->security_parameters
.client_random
, GNUTLS_RANDOM_SIZE
);
1868 pos
+= GNUTLS_RANDOM_SIZE
;
1870 /* Copy the Session ID
1872 data
[pos
++] = session_id_len
;
1874 if (session_id_len
> 0)
1877 session
->internals
.resumed_security_parameters
.session_id
,
1879 pos
+= session_id_len
;
1882 /* Copy the DTLS cookie
1884 if (IS_DTLS(session
))
1886 data
[pos
++] = session
->internals
.dtls
.cookie_len
;
1887 memcpy(&data
[pos
], &session
->internals
.dtls
.cookie
, session
->internals
.dtls
.cookie_len
);
1888 /* pos += session->internals.dtls.cookie_len; */
1891 /* Copy the ciphersuites.
1893 * If using SSLv3 Send TLS_RENEGO_PROTECTION_REQUEST SCSV for MITM
1894 * prevention on initial negotiation (but not renegotiation; that's
1895 * handled with the RI extension below).
1897 if (!session
->internals
.initial_negotiation_completed
&&
1898 session
->security_parameters
.entity
== GNUTLS_CLIENT
&&
1899 (gnutls_protocol_get_version (session
) == GNUTLS_SSL3
||
1900 session
->internals
.priorities
.no_extensions
!= 0))
1903 _gnutls_copy_ciphersuites (session
, &extdata
, TRUE
);
1904 _gnutls_extension_list_add (session
,
1905 GNUTLS_EXTENSION_SAFE_RENEGOTIATION
);
1908 ret
= _gnutls_copy_ciphersuites (session
, &extdata
, FALSE
);
1916 /* Copy the compression methods.
1918 ret
= _gnutls_copy_comp_methods (session
, &extdata
);
1925 /* Generate and copy TLS extensions.
1927 if (session
->internals
.priorities
.no_extensions
== 0)
1929 if (_gnutls_version_has_extensions (hver
))
1930 type
= GNUTLS_EXT_ANY
;
1933 if (session
->internals
.initial_negotiation_completed
!= 0)
1934 type
= GNUTLS_EXT_MANDATORY
;
1936 type
= GNUTLS_EXT_NONE
;
1939 ret
= _gnutls_gen_extensions (session
, &extdata
, type
);
1948 ret
= _mbuffer_append_data (bufel
, extdata
.data
, extdata
.length
);
1956 _gnutls_buffer_clear(&extdata
);
1959 _gnutls_send_handshake (session
, bufel
, GNUTLS_HANDSHAKE_CLIENT_HELLO
);
1962 _mbuffer_xfree(&bufel
);
1963 _gnutls_buffer_clear(&extdata
);
1968 _gnutls_send_server_hello (gnutls_session_t session
, int again
)
1970 mbuffer_st
*bufel
= NULL
;
1971 uint8_t *data
= NULL
;
1972 gnutls_buffer_st extdata
;
1974 int datalen
, ret
= 0;
1976 uint8_t session_id_len
= session
->security_parameters
.session_id_size
;
1977 char buf
[2 * TLS_MAX_SESSION_ID_SIZE
+ 1];
1979 _gnutls_buffer_init(&extdata
);
1983 datalen
= 2 + session_id_len
+ 1 + GNUTLS_RANDOM_SIZE
+ 3;
1985 _gnutls_gen_extensions (session
, &extdata
, GNUTLS_EXT_ANY
);
1992 bufel
= _gnutls_handshake_alloc (session
, datalen
+ extdata
.length
, datalen
+ extdata
.length
);
1996 ret
= GNUTLS_E_MEMORY_ERROR
;
1999 data
= _mbuffer_get_udata_ptr (bufel
);
2002 _gnutls_version_get_major (session
->security_parameters
.version
);
2004 _gnutls_version_get_minor (session
->security_parameters
.version
);
2007 session
->security_parameters
.server_random
, GNUTLS_RANDOM_SIZE
);
2008 pos
+= GNUTLS_RANDOM_SIZE
;
2010 data
[pos
++] = session_id_len
;
2011 if (session_id_len
> 0)
2013 memcpy (&data
[pos
], session
->security_parameters
.session_id
,
2016 pos
+= session_id_len
;
2018 _gnutls_handshake_log ("HSK[%p]: SessionID: %s\n", session
,
2019 _gnutls_bin2hex (session
->security_parameters
.
2020 session_id
, session_id_len
, buf
,
2021 sizeof (buf
), NULL
));
2024 session
->security_parameters
.cipher_suite
, 2);
2027 comp
= _gnutls_compression_get_num ( session
->security_parameters
.compression_method
);
2030 if (extdata
.length
> 0)
2032 memcpy (&data
[pos
], extdata
.data
, extdata
.length
);
2037 _gnutls_send_handshake (session
, bufel
, GNUTLS_HANDSHAKE_SERVER_HELLO
);
2040 _gnutls_buffer_clear(&extdata
);
2045 _gnutls_send_hello (gnutls_session_t session
, int again
)
2049 if (session
->security_parameters
.entity
== GNUTLS_CLIENT
)
2051 ret
= _gnutls_send_client_hello (session
, again
);
2056 ret
= _gnutls_send_server_hello (session
, again
);
2062 /* RECEIVE A HELLO MESSAGE. This should be called from gnutls_recv_handshake_int only if a
2063 * hello message is expected. It uses the security_parameters.cipher_suite
2064 * and internals.compression_method.
2067 _gnutls_recv_hello (gnutls_session_t session
, uint8_t * data
, int datalen
)
2071 if (session
->security_parameters
.entity
== GNUTLS_CLIENT
)
2073 ret
= _gnutls_read_server_hello (session
, data
, datalen
);
2081 { /* Server side reading a client hello */
2083 ret
= _gnutls_read_client_hello (session
, data
, datalen
);
2091 ret
= _gnutls_ext_sr_verify (session
);
2102 _gnutls_recv_hello_verify_request (gnutls_session_t session
,
2103 uint8_t * data
, int datalen
)
2105 ssize_t len
= datalen
;
2108 unsigned int nb_verifs
;
2110 if (!IS_DTLS (session
)
2111 || session
->security_parameters
.entity
== GNUTLS_SERVER
)
2114 return GNUTLS_E_INTERNAL_ERROR
;
2117 nb_verifs
= ++session
->internals
.dtls
.hsk_hello_verify_requests
;
2118 if (nb_verifs
>= MAX_HANDSHAKE_HELLO_VERIFY_REQUESTS
)
2120 /* The server is either buggy, malicious or changing cookie
2121 secrets _way_ too fast. */
2123 return GNUTLS_E_UNEXPECTED_PACKET
;
2126 /* TODO: determine if we need to do anything with the server version field */
2131 cookie_len
= data
[pos
];
2134 if (cookie_len
> DTLS_MAX_COOKIE_SIZE
)
2137 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
2140 DECR_LEN (len
, cookie_len
);
2142 session
->internals
.dtls
.cookie_len
= cookie_len
;
2143 memcpy (session
->internals
.dtls
.cookie
, &data
[pos
], cookie_len
);
2148 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
2151 /* reset handshake hash buffers */
2152 _gnutls_handshake_hash_buffer_empty (session
);
2157 /* The packets in gnutls_handshake (it's more broad than original TLS handshake)
2161 * ClientHello -------->
2162 * <-------- ServerHello
2165 * ServerKeyExchange*
2166 * <-------- CertificateRequest*
2168 * <-------- ServerHelloDone
2171 * CertificateVerify*
2172 * [ChangeCipherSpec]
2173 * Finished -------->
2175 * [ChangeCipherSpec]
2176 * <-------- Finished
2178 * (*): means optional packet.
2181 /* Handshake when resumming session:
2184 * ClientHello -------->
2186 * [ChangeCipherSpec]
2187 * <-------- Finished
2188 * [ChangeCipherSpec]
2189 * Finished -------->
2194 * gnutls_rehandshake:
2195 * @session: is a #gnutls_session_t structure.
2197 * This function will renegotiate security parameters with the
2198 * client. This should only be called in case of a server.
2200 * This message informs the peer that we want to renegotiate
2201 * parameters (perform a handshake).
2203 * If this function succeeds (returns 0), you must call the
2204 * gnutls_handshake() function in order to negotiate the new
2207 * Since TLS is full duplex some application data might have been
2208 * sent during peer's processing of this message. In that case
2209 * one should call gnutls_record_recv() until GNUTLS_E_REHANDSHAKE
2210 * is returned to clear any pending data. Care must be taken if
2211 * rehandshake is mandatory to terminate if it does not start after
2214 * If the client does not wish to renegotiate parameters he will
2215 * should with an alert message, thus the return code will be
2216 * %GNUTLS_E_WARNING_ALERT_RECEIVED and the alert will be
2217 * %GNUTLS_A_NO_RENEGOTIATION. A client may also choose to ignore
2220 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
2223 gnutls_rehandshake (gnutls_session_t session
)
2227 /* only server sends that handshake packet */
2228 if (session
->security_parameters
.entity
== GNUTLS_CLIENT
)
2229 return GNUTLS_E_INVALID_REQUEST
;
2231 _dtls_async_timer_delete(session
);
2234 _gnutls_send_empty_handshake (session
, GNUTLS_HANDSHAKE_HELLO_REQUEST
,
2249 _gnutls_abort_handshake (gnutls_session_t session
, int ret
)
2251 if (((ret
== GNUTLS_E_WARNING_ALERT_RECEIVED
) &&
2252 (gnutls_alert_get (session
) == GNUTLS_A_NO_RENEGOTIATION
))
2253 || ret
== GNUTLS_E_GOT_APPLICATION_DATA
)
2256 /* this doesn't matter */
2257 return GNUTLS_E_INTERNAL_ERROR
;
2263 _gnutls_send_supplemental (gnutls_session_t session
, int again
)
2268 _gnutls_debug_log ("EXT[%p]: Sending supplemental data\n", session
);
2272 _gnutls_send_handshake (session
, NULL
, GNUTLS_HANDSHAKE_SUPPLEMENTAL
);
2275 gnutls_buffer_st buf
;
2276 _gnutls_buffer_init (&buf
);
2278 ret
= _gnutls_gen_supplemental (session
, &buf
);
2285 bufel
= _gnutls_handshake_alloc(session
, buf
.length
, buf
.length
);
2289 return GNUTLS_E_MEMORY_ERROR
;
2292 _mbuffer_set_udata (bufel
, buf
.data
, buf
.length
);
2293 _gnutls_buffer_clear (&buf
);
2295 ret
= _gnutls_send_handshake (session
, bufel
,
2296 GNUTLS_HANDSHAKE_SUPPLEMENTAL
);
2303 _gnutls_recv_supplemental (gnutls_session_t session
)
2305 gnutls_buffer_st buf
;
2308 _gnutls_debug_log ("EXT[%p]: Expecting supplemental data\n", session
);
2310 ret
= _gnutls_recv_handshake (session
, GNUTLS_HANDSHAKE_SUPPLEMENTAL
,
2318 ret
= _gnutls_parse_supplemental (session
, buf
.data
, buf
.length
);
2326 _gnutls_buffer_clear(&buf
);
2333 * @session: is a #gnutls_session_t structure.
2335 * This function does the handshake of the TLS/SSL protocol, and
2336 * initializes the TLS connection.
2338 * This function will fail if any problem is encountered, and will
2339 * return a negative error code. In case of a client, if the client
2340 * has asked to resume a session, but the server couldn't, then a
2341 * full handshake will be performed.
2343 * The non-fatal errors such as %GNUTLS_E_AGAIN and
2344 * %GNUTLS_E_INTERRUPTED interrupt the handshake procedure, which
2345 * should be resumed later. Call this function again, until it
2346 * returns 0; cf. gnutls_record_get_direction() and
2347 * gnutls_error_is_fatal().
2349 * If this function is called by a server after a rehandshake request
2350 * then %GNUTLS_E_GOT_APPLICATION_DATA or
2351 * %GNUTLS_E_WARNING_ALERT_RECEIVED may be returned. Note that these
2352 * are non fatal errors, only in the specific case of a rehandshake.
2353 * Their meaning is that the client rejected the rehandshake request or
2354 * in the case of %GNUTLS_E_GOT_APPLICATION_DATA it might also mean that
2355 * some data were pending.
2357 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
2360 gnutls_handshake (gnutls_session_t session
)
2363 record_parameters_st
*params
;
2365 /* sanity check. Verify that there are priorities setup.
2367 if (session
->internals
.priorities
.protocol
.algorithms
== 0)
2368 return gnutls_assert_val(GNUTLS_E_NO_PRIORITIES_WERE_SET
);
2370 if (session
->internals
.handshake_timeout_ms
&&
2371 session
->internals
.handshake_endtime
== 0)
2372 session
->internals
.handshake_endtime
= gnutls_time(0) +
2373 session
->internals
.handshake_timeout_ms
/ 1000;
2375 ret
= _gnutls_epoch_get (session
, session
->security_parameters
.epoch_next
,
2379 /* We assume the epoch is not allocated if _gnutls_epoch_get fails. */
2381 _gnutls_epoch_alloc (session
, session
->security_parameters
.epoch_next
,
2384 return gnutls_assert_val(ret
);
2387 if (session
->security_parameters
.entity
== GNUTLS_CLIENT
)
2391 ret
= _gnutls_handshake_client (session
);
2396 ret
= _gnutls_handshake_server (session
);
2400 /* In the case of a rehandshake abort
2401 * we should reset the handshake's internal state.
2403 if (_gnutls_abort_handshake (session
, ret
) == 0)
2409 ret
= _gnutls_handshake_common (session
);
2413 if (_gnutls_abort_handshake (session
, ret
) == 0)
2421 if (IS_DTLS(session
)==0)
2423 _gnutls_handshake_io_buffer_clear (session
);
2427 _dtls_async_timer_init(session
);
2430 _gnutls_handshake_internal_state_clear (session
);
2432 session
->security_parameters
.epoch_next
++;
2438 * gnutls_handshake_set_timeout:
2439 * @session: is a #gnutls_session_t structure.
2440 * @ms: is a timeout value in milliseconds
2442 * This function sets the timeout for the handshake process
2443 * to the provided value. Use an @ms value of zero to disable
2446 * Note that in order for the timeout to be enforced
2447 * gnutls_transport_set_pull_timeout_function() must be set.
2451 gnutls_handshake_set_timeout (gnutls_session_t session
, unsigned int ms
)
2453 if (ms
== GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT
)
2455 session
->internals
.handshake_timeout_ms
= ms
;
2459 #define IMED_RET( str, ret, allow_alert) do { \
2461 /* EAGAIN and INTERRUPTED are always non-fatal */ \
2462 if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED) \
2464 /* a warning alert might interrupt handshake */ \
2465 if (allow_alert != 0 && ret==GNUTLS_E_WARNING_ALERT_RECEIVED) return ret; \
2468 _gnutls_handshake_hash_buffers_clear(session); \
2475 * _gnutls_handshake_client
2476 * This function performs the client side of the handshake of the TLS/SSL protocol.
2479 _gnutls_handshake_client (gnutls_session_t session
)
2483 #ifdef HANDSHAKE_DEBUG
2486 if (session
->internals
.resumed_security_parameters
.session_id_size
> 0)
2487 _gnutls_handshake_log ("HSK[%p]: Ask to resume: %s\n", session
,
2488 _gnutls_bin2hex (session
->
2489 internals
.resumed_security_parameters
.session_id
,
2491 internals
.resumed_security_parameters
.session_id_size
,
2492 buf
, sizeof (buf
), NULL
));
2499 ret
= _gnutls_send_hello (session
, AGAIN (STATE1
));
2501 IMED_RET ("send hello", ret
, 1);
2504 if (IS_DTLS (session
))
2507 _gnutls_recv_handshake (session
,
2508 GNUTLS_HANDSHAKE_HELLO_VERIFY_REQUEST
,
2511 IMED_RET ("recv hello verify", ret
, 1);
2520 /* receive the server hello */
2522 _gnutls_recv_handshake (session
,
2523 GNUTLS_HANDSHAKE_SERVER_HELLO
,
2526 IMED_RET ("recv hello", ret
, 1);
2529 if (session
->security_parameters
.do_recv_supplemental
)
2531 ret
= _gnutls_recv_supplemental (session
);
2533 IMED_RET ("recv supplemental", ret
, 1);
2537 /* RECV CERTIFICATE */
2538 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2539 ret
= _gnutls_recv_server_certificate (session
);
2541 IMED_RET ("recv server certificate", ret
, 1);
2544 /* receive the server key exchange */
2545 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2546 ret
= _gnutls_recv_server_kx_message (session
);
2548 IMED_RET ("recv server kx message", ret
, 1);
2551 /* receive the server certificate request - if any
2554 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2555 ret
= _gnutls_recv_server_crt_request (session
);
2557 IMED_RET ("recv server certificate request message", ret
, 1);
2560 /* receive the server hello done */
2561 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2563 _gnutls_recv_handshake (session
,
2564 GNUTLS_HANDSHAKE_SERVER_HELLO_DONE
,
2567 IMED_RET ("recv server hello done", ret
, 1);
2569 if (session
->security_parameters
.do_send_supplemental
)
2571 ret
= _gnutls_send_supplemental (session
, AGAIN (STATE71
));
2573 IMED_RET ("send supplemental", ret
, 0);
2577 /* send our certificate - if any and if requested
2579 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2580 ret
= _gnutls_send_client_certificate (session
, AGAIN (STATE7
));
2582 IMED_RET ("send client certificate", ret
, 0);
2585 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2586 ret
= _gnutls_send_client_kx_message (session
, AGAIN (STATE8
));
2588 IMED_RET ("send client kx", ret
, 0);
2591 /* send client certificate verify */
2592 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2594 _gnutls_send_client_certificate_verify (session
, AGAIN (STATE9
));
2596 IMED_RET ("send client certificate verify", ret
, 1);
2609 /* This function is to be called if the handshake was successfully
2610 * completed. This sends a Change Cipher Spec packet to the peer.
2613 send_change_cipher_spec (gnutls_session_t session
, int again
)
2621 bufel
= _gnutls_handshake_alloc (session
, 1, 1);
2623 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR
);
2625 if (gnutls_protocol_get_version (session
) == GNUTLS_DTLS0_9
)
2626 _mbuffer_set_uhead_size(bufel
, 3);
2628 _mbuffer_set_uhead_size(bufel
, 1);
2629 _mbuffer_set_udata_size(bufel
, 0);
2631 data
= _mbuffer_get_uhead_ptr (bufel
);
2634 if (gnutls_protocol_get_version (session
) == GNUTLS_DTLS0_9
)
2636 _gnutls_write_uint16 (session
->internals
.dtls
.hsk_write_seq
, &data
[1]);
2637 session
->internals
.dtls
.hsk_write_seq
++;
2640 ret
= _gnutls_handshake_io_cache_int (session
, GNUTLS_HANDSHAKE_CHANGE_CIPHER_SPEC
, bufel
);
2643 _mbuffer_xfree(&bufel
);
2644 return gnutls_assert_val(ret
);
2647 _gnutls_handshake_log ("REC[%p]: Sent ChangeCipherSpec\n", session
);
2653 /* This function sends the final handshake packets and initializes connection
2656 _gnutls_send_handshake_final (gnutls_session_t session
, int init
)
2660 /* Send the CHANGE CIPHER SPEC PACKET */
2666 ret
= send_change_cipher_spec (session
, AGAIN (STATE20
));
2671 ERR ("send ChangeCipherSpec", ret
);
2675 /* Initialize the connection session (start encryption) - in case of client
2679 ret
= _gnutls_connection_state_init (session
);
2687 ret
= _gnutls_write_connection_state_init (session
);
2695 /* send the finished message */
2696 ret
= _gnutls_send_finished (session
, AGAIN (STATE21
));
2700 ERR ("send Finished", ret
);
2713 /* This function receives the final handshake packets
2714 * And executes the appropriate function to initialize the
2718 _gnutls_recv_handshake_final (gnutls_session_t session
, int init
)
2722 unsigned int ccs_len
= 1;
2725 ret
= handshake_remaining_time(session
);
2727 return gnutls_assert_val(ret
);
2736 /* This is the last flight and peer cannot be sure
2737 * we have received it unless we notify him. So we
2738 * wait for a message and retransmit if needed. */
2739 if (IS_DTLS(session
) && !_dtls_is_async(session
) &&
2740 (gnutls_record_check_pending (session
) +
2741 record_check_unprocessed (session
)) == 0)
2743 ret
= _dtls_wait_and_retransmit(session
);
2745 return gnutls_assert_val(ret
);
2748 if (gnutls_protocol_get_version (session
) == GNUTLS_DTLS0_9
)
2751 ret
= _gnutls_recv_int (session
, GNUTLS_CHANGE_CIPHER_SPEC
, -1, &ch
, ccs_len
, NULL
,
2755 ERR ("recv ChangeCipherSpec", ret
);
2757 return (ret
< 0) ? ret
: GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
2760 if (gnutls_protocol_get_version (session
) == GNUTLS_DTLS0_9
)
2761 session
->internals
.dtls
.hsk_read_seq
++;
2763 /* Initialize the connection session (start encryption) - in case of server */
2766 ret
= _gnutls_connection_state_init (session
);
2774 ret
= _gnutls_read_connection_state_init (session
);
2784 if (IS_DTLS(session
) && !_dtls_is_async(session
) &&
2785 (gnutls_record_check_pending( session
) +
2786 record_check_unprocessed (session
)) == 0)
2788 ret
= _dtls_wait_and_retransmit(session
);
2790 return gnutls_assert_val(ret
);
2793 ret
= _gnutls_recv_finished (session
);
2796 ERR ("recv finished", ret
);
2810 * _gnutls_handshake_server
2811 * This function does the server stuff of the handshake protocol.
2814 _gnutls_handshake_server (gnutls_session_t session
)
2823 _gnutls_recv_handshake (session
,
2824 GNUTLS_HANDSHAKE_CLIENT_HELLO
,
2827 IMED_RET ("recv hello", ret
, 1);
2830 ret
= _gnutls_send_hello (session
, AGAIN (STATE2
));
2832 IMED_RET ("send hello", ret
, 1);
2835 if (session
->security_parameters
.do_send_supplemental
)
2837 ret
= _gnutls_send_supplemental (session
, AGAIN (STATE70
));
2839 IMED_RET ("send supplemental data", ret
, 0);
2842 /* SEND CERTIFICATE + KEYEXCHANGE + CERTIFICATE_REQUEST */
2844 /* NOTE: these should not be send if we are resuming */
2846 if (session
->internals
.resumed
== RESUME_FALSE
)
2847 ret
= _gnutls_send_server_certificate (session
, AGAIN (STATE3
));
2849 IMED_RET ("send server certificate", ret
, 0);
2852 /* send server key exchange (A) */
2853 if (session
->internals
.resumed
== RESUME_FALSE
)
2854 ret
= _gnutls_send_server_kx_message (session
, AGAIN (STATE4
));
2856 IMED_RET ("send server kx", ret
, 0);
2859 /* Send certificate request - if requested to */
2860 if (session
->internals
.resumed
== RESUME_FALSE
)
2862 _gnutls_send_server_crt_request (session
, AGAIN (STATE5
));
2864 IMED_RET ("send server cert request", ret
, 0);
2867 /* send the server hello done */
2868 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2870 _gnutls_send_empty_handshake (session
,
2871 GNUTLS_HANDSHAKE_SERVER_HELLO_DONE
,
2874 IMED_RET ("send server hello done", ret
, 1);
2877 if (session
->security_parameters
.do_recv_supplemental
)
2879 ret
= _gnutls_recv_supplemental (session
);
2881 IMED_RET ("recv client supplemental", ret
, 1);
2884 /* RECV CERTIFICATE + KEYEXCHANGE + CERTIFICATE_VERIFY */
2886 /* receive the client certificate message */
2887 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2888 ret
= _gnutls_recv_client_certificate (session
);
2890 IMED_RET ("recv client certificate", ret
, 1);
2893 /* receive the client key exchange message */
2894 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2895 ret
= _gnutls_recv_client_kx_message (session
);
2897 IMED_RET ("recv client kx", ret
, 1);
2900 /* receive the client certificate verify message */
2901 if (session
->internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2902 ret
= _gnutls_recv_client_certificate_verify_message (session
);
2904 IMED_RET ("recv client certificate verify", ret
, 1);
2906 STATE
= STATE0
; /* finished thus clear session */
2915 _gnutls_handshake_common (gnutls_session_t session
)
2919 /* send and recv the change cipher spec and finished messages */
2920 if ((session
->internals
.resumed
!= RESUME_FALSE
2921 && session
->security_parameters
.entity
== GNUTLS_CLIENT
)
2922 || (session
->internals
.resumed
== RESUME_FALSE
2923 && session
->security_parameters
.entity
== GNUTLS_SERVER
))
2925 /* if we are a client resuming - or we are a server not resuming */
2926 ret
= _gnutls_recv_handshake_final (session
, TRUE
);
2927 IMED_RET ("recv handshake final", ret
, 1);
2933 ret
= _gnutls_send_new_session_ticket (session
, AGAIN (STATE40
));
2935 IMED_RET ("send handshake new session ticket", ret
, 0);
2941 ret
= _gnutls_send_handshake_final (session
, FALSE
);
2942 IMED_RET ("send handshake final", ret
, 1);
2944 /* only store if we are not resuming a session and we didn't previously send a ticket
2946 if (session
->security_parameters
.entity
== GNUTLS_SERVER
&& session
->internals
.ticket_sent
== 0)
2948 /* in order to support session resuming */
2949 _gnutls_server_register_current_session (session
);
2953 { /* if we are a client not resuming - or we are a server resuming */
2955 ret
= _gnutls_send_handshake_final (session
, TRUE
);
2956 IMED_RET ("send handshake final 2", ret
, 1);
2962 ret
= _gnutls_recv_new_session_ticket (session
);
2964 IMED_RET ("recv handshake new session ticket", ret
, 1);
2970 ret
= _gnutls_recv_handshake_final (session
, FALSE
);
2971 IMED_RET ("recv handshake final 2", ret
, 1);
2976 /* clear handshake buffer */
2977 _gnutls_handshake_hash_buffers_clear (session
);
2983 _gnutls_generate_session_id (uint8_t * session_id
, uint8_t * len
)
2987 *len
= TLS_MAX_SESSION_ID_SIZE
;
2989 ret
= _gnutls_rnd (GNUTLS_RND_NONCE
, session_id
, *len
);
3000 _gnutls_recv_hello_request (gnutls_session_t session
, void *data
,
3005 if (session
->security_parameters
.entity
== GNUTLS_SERVER
)
3008 return GNUTLS_E_UNEXPECTED_PACKET
;
3013 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
3015 type
= ((uint8_t *) data
)[0];
3016 if (type
== GNUTLS_HANDSHAKE_HELLO_REQUEST
)
3018 if (IS_DTLS(session
))
3019 session
->internals
.dtls
.hsk_read_seq
++;
3020 return GNUTLS_E_REHANDSHAKE
;
3025 return GNUTLS_E_UNEXPECTED_PACKET
;
3029 /* Returns 1 if the given KX has not the corresponding parameters
3030 * (DH or RSA) set up. Otherwise returns 0.
3033 check_server_params (gnutls_session_t session
,
3034 gnutls_kx_algorithm_t kx
,
3035 gnutls_kx_algorithm_t
* alg
, int alg_size
)
3038 gnutls_dh_params_t dh_params
= NULL
;
3039 gnutls_rsa_params_t rsa_params
= NULL
;
3042 cred_type
= _gnutls_map_kx_get_cred (kx
, 1);
3044 /* Read the Diffie-Hellman parameters, if any.
3046 if (cred_type
== GNUTLS_CRD_CERTIFICATE
)
3049 gnutls_certificate_credentials_t x509_cred
=
3050 (gnutls_certificate_credentials_t
) _gnutls_get_cred (session
->key
,
3053 if (x509_cred
!= NULL
)
3056 _gnutls_get_dh_params (x509_cred
->dh_params
,
3057 x509_cred
->params_func
, session
);
3059 _gnutls_certificate_get_rsa_params (x509_cred
->rsa_params
,
3060 x509_cred
->params_func
,
3064 /* Check also if the certificate supports the
3068 for (j
= 0; j
< alg_size
; j
++)
3082 else if (cred_type
== GNUTLS_CRD_ANON
)
3084 gnutls_anon_server_credentials_t anon_cred
=
3085 (gnutls_anon_server_credentials_t
) _gnutls_get_cred (session
->key
,
3088 if (anon_cred
!= NULL
)
3091 _gnutls_get_dh_params (anon_cred
->dh_params
,
3092 anon_cred
->params_func
, session
);
3097 else if (cred_type
== GNUTLS_CRD_PSK
)
3099 gnutls_psk_server_credentials_t psk_cred
=
3100 (gnutls_psk_server_credentials_t
) _gnutls_get_cred (session
->key
,
3103 if (psk_cred
!= NULL
)
3106 _gnutls_get_dh_params (psk_cred
->dh_params
, psk_cred
->params_func
,
3112 return 0; /* no need for params */
3115 /* If the key exchange method needs RSA or DH params,
3116 * but they are not set then remove it.
3118 if (_gnutls_kx_needs_rsa_params (kx
) != 0)
3120 /* needs rsa params. */
3121 if (_gnutls_rsa_params_to_mpi (rsa_params
) == NULL
)
3128 if (_gnutls_kx_needs_dh_params (kx
) != 0)
3130 /* needs DH params. */
3131 if (_gnutls_dh_params_to_mpi (dh_params
) == NULL
)
3141 /* This function will remove algorithms that are not supported by
3142 * the requested authentication method. We remove an algorithm if
3143 * we have a certificate with keyUsage bits set.
3145 * This does a more high level check than gnutls_supported_ciphersuites(),
3146 * by checking certificates etc.
3149 _gnutls_remove_unwanted_ciphersuites (gnutls_session_t session
,
3150 uint8_t * cipher_suites
,
3151 int cipher_suites_size
,
3152 gnutls_pk_algorithm_t
*pk_algos
,
3153 size_t pk_algos_size
)
3157 int i
, new_suites_size
;
3158 gnutls_certificate_credentials_t cert_cred
;
3159 gnutls_kx_algorithm_t kx
;
3160 int server
= session
->security_parameters
.entity
== GNUTLS_SERVER
? 1 : 0;
3161 gnutls_kx_algorithm_t alg
[MAX_ALGOS
];
3162 int alg_size
= MAX_ALGOS
;
3164 /* if we should use a specific certificate,
3165 * we should remove all algorithms that are not supported
3166 * by that certificate and are on the same authentication
3167 * method (CERTIFICATE).
3171 (gnutls_certificate_credentials_t
) _gnutls_get_cred (session
->key
,
3172 GNUTLS_CRD_CERTIFICATE
,
3175 /* If there are certificate credentials, find an appropriate certificate
3178 if (session
->security_parameters
.entity
== GNUTLS_SERVER
3179 && cert_cred
!= NULL
&& pk_algos_size
> 0)
3181 ret
= _gnutls_server_select_cert (session
, pk_algos
, pk_algos_size
);
3185 _gnutls_debug_log ("Could not find an appropriate certificate: %s\n",
3186 gnutls_strerror (ret
));
3190 /* get all the key exchange algorithms that are
3191 * supported by the X509 certificate parameters.
3194 _gnutls_selected_cert_supported_kx (session
, alg
, &alg_size
)) < 0)
3200 new_suites_size
= 0;
3202 /* now removes ciphersuites based on the KX algorithm
3204 for (i
= 0; i
< cipher_suites_size
; i
+=2)
3208 /* finds the key exchange algorithm in
3211 kx
= _gnutls_cipher_suite_get_kx_algo (&cipher_suites
[i
]);
3213 /* if it is defined but had no credentials
3215 if (!session
->internals
.premaster_set
&&
3216 _gnutls_get_kx_cred (session
, kx
, NULL
) == NULL
)
3225 delete = check_server_params (session
, kx
, alg
, alg_size
);
3228 /* If we have not agreed to a common curve with the peer don't bother
3231 if (server
!= 0 && _gnutls_kx_is_ecc(kx
))
3233 if (_gnutls_session_ecc_curve_get(session
) == GNUTLS_ECC_CURVE_INVALID
)
3239 /* These two SRP kx's are marked to require a CRD_CERTIFICATE,
3240 (see cred_mappings in gnutls_algorithms.c), but it also
3241 requires a SRP credential. Don't use SRP kx unless we have a
3242 SRP credential too. */
3243 if (kx
== GNUTLS_KX_SRP_RSA
|| kx
== GNUTLS_KX_SRP_DSS
)
3245 if (!_gnutls_get_cred (session
->key
, GNUTLS_CRD_SRP
, NULL
))
3254 _gnutls_handshake_log ("HSK[%p]: Keeping ciphersuite: %s (%.2X.%.2X)\n",
3256 _gnutls_cipher_suite_get_name (&cipher_suites
[i
]),
3257 cipher_suites
[i
], cipher_suites
[i
+1]);
3259 if (i
!= new_suites_size
)
3260 memmove( &cipher_suites
[new_suites_size
], &cipher_suites
[i
], 2);
3265 _gnutls_handshake_log ("HSK[%p]: Removing ciphersuite: %s\n",
3267 _gnutls_cipher_suite_get_name (&cipher_suites
[i
]));
3272 ret
= new_suites_size
;
3279 * gnutls_handshake_set_max_packet_length:
3280 * @session: is a #gnutls_session_t structure.
3281 * @max: is the maximum number.
3283 * This function will set the maximum size of all handshake messages.
3284 * Handshakes over this size are rejected with
3285 * %GNUTLS_E_HANDSHAKE_TOO_LARGE error code. The default value is
3286 * 48kb which is typically large enough. Set this to 0 if you do not
3287 * want to set an upper limit.
3289 * The reason for restricting the handshake message sizes are to
3290 * limit Denial of Service attacks.
3293 gnutls_handshake_set_max_packet_length (gnutls_session_t session
, size_t max
)
3295 session
->internals
.max_handshake_data_buffer_size
= max
;
3299 _gnutls_set_adv_version (gnutls_session_t session
, gnutls_protocol_t ver
)
3301 set_adv_version (session
, _gnutls_version_get_major (ver
),
3302 _gnutls_version_get_minor (ver
));
3306 _gnutls_get_adv_version (gnutls_session_t session
)
3308 return _gnutls_version_get (_gnutls_get_adv_version_major (session
),
3309 _gnutls_get_adv_version_minor (session
));
3313 * gnutls_handshake_get_last_in:
3314 * @session: is a #gnutls_session_t structure.
3316 * This function is only useful to check where the last performed
3317 * handshake failed. If the previous handshake succeed or was not
3318 * performed at all then no meaningful value will be returned.
3320 * Check %gnutls_handshake_description_t in gnutls.h for the
3321 * available handshake descriptions.
3323 * Returns: the last handshake message type received, a
3324 * %gnutls_handshake_description_t.
3326 gnutls_handshake_description_t
3327 gnutls_handshake_get_last_in (gnutls_session_t session
)
3329 return session
->internals
.last_handshake_in
;
3333 * gnutls_handshake_get_last_out:
3334 * @session: is a #gnutls_session_t structure.
3336 * This function is only useful to check where the last performed
3337 * handshake failed. If the previous handshake succeed or was not
3338 * performed at all then no meaningful value will be returned.
3340 * Check %gnutls_handshake_description_t in gnutls.h for the
3341 * available handshake descriptions.
3343 * Returns: the last handshake message type sent, a
3344 * %gnutls_handshake_description_t.
3346 gnutls_handshake_description_t
3347 gnutls_handshake_get_last_out (gnutls_session_t session
)
3349 return session
->internals
.last_handshake_out
;