2 * Copyright (C) 2000,2001,2002 Nikos Mavroyanopoulos
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "gnutls_int.h"
23 #include "gnutls_errors.h"
24 #include "gnutls_dh.h"
26 #include "gnutls_algorithms.h"
27 #include "gnutls_compress.h"
28 #include "gnutls_cipher.h"
29 #include "gnutls_buffers.h"
30 #include "gnutls_kx.h"
31 #include "gnutls_handshake.h"
32 #include "gnutls_num.h"
33 #include "gnutls_hash_int.h"
34 #include "gnutls_db.h"
35 #include "gnutls_extensions.h"
36 #include "gnutls_random.h"
37 #include "gnutls_auth_int.h"
38 #include "gnutls_v2_compat.h"
39 #include "auth_cert.h"
40 #include "gnutls_cert.h"
41 #include "gnutls_constate.h"
42 #include <gnutls_record.h>
43 #include <gnutls_alert.h>
44 #include <gnutls_state.h>
46 #ifdef HANDSHAKE_DEBUG
47 #define ERR(x, y) _gnutls_handshake_log( "HSK: %s (%d)\n", x,y)
55 int _gnutls_server_select_comp_method(GNUTLS_STATE state
,
56 opaque
* data
, int datalen
);
59 /* Clears the handshake hash buffers and handles.
62 void _gnutls_handshake_hash_buffers_clear( GNUTLS_STATE state
) {
63 _gnutls_hash_deinit( state
->gnutls_internals
.handshake_mac_handle_md5
, NULL
);
64 _gnutls_hash_deinit( state
->gnutls_internals
.handshake_mac_handle_sha
, NULL
);
65 state
->gnutls_internals
.handshake_mac_handle_md5
= NULL
;
66 state
->gnutls_internals
.handshake_mac_handle_sha
= NULL
;
67 _gnutls_handshake_buffer_clear( state
);
70 /* this will copy the required values for resuming to
71 * gnutls_internals, and to security_parameters.
72 * this will keep as less data to security_parameters.
74 static void resume_copy_required_values(GNUTLS_STATE state
)
76 /* get the new random values */
77 memcpy(state
->gnutls_internals
.resumed_security_parameters
.
79 state
->security_parameters
.server_random
, TLS_RANDOM_SIZE
);
80 memcpy(state
->gnutls_internals
.resumed_security_parameters
.
82 state
->security_parameters
.client_random
, TLS_RANDOM_SIZE
);
84 /* keep the ciphersuite and compression
85 * That is because the client must see these in our
88 memcpy(state
->security_parameters
.current_cipher_suite
.
90 state
->gnutls_internals
.resumed_security_parameters
.
91 current_cipher_suite
.CipherSuite
, 2);
93 state
->gnutls_internals
.compression_method
= state
->gnutls_internals
.resumed_security_parameters
.read_compression_algorithm
; /* or write_compression_algorithm
97 state
->security_parameters
.entity
=
98 state
->gnutls_internals
.resumed_security_parameters
.entity
;
100 _gnutls_set_current_version( state
, state
->gnutls_internals
.resumed_security_parameters
.version
);
102 state
->security_parameters
.cert_type
=
103 state
->gnutls_internals
.resumed_security_parameters
.cert_type
;
105 memcpy(state
->security_parameters
.session_id
,
106 state
->gnutls_internals
.resumed_security_parameters
.
107 session_id
, sizeof(state
->security_parameters
.session_id
));
108 state
->security_parameters
.session_id_size
=
109 state
->gnutls_internals
.resumed_security_parameters
.
115 void _gnutls_set_server_random(GNUTLS_STATE state
, uint8
* random
)
117 memcpy(state
->security_parameters
.server_random
, random
,
121 void _gnutls_set_client_random(GNUTLS_STATE state
, uint8
* random
)
123 memcpy(state
->security_parameters
.client_random
, random
,
127 /* Calculate The SSL3 Finished message
129 #define SSL3_CLIENT_MSG "CLNT"
130 #define SSL3_SERVER_MSG "SRVR"
131 #define SSL_MSG_LEN 4
132 static int _gnutls_ssl3_finished(GNUTLS_STATE state
, int type
, opaque
* ret
)
134 const int siz
= SSL_MSG_LEN
;
135 GNUTLS_MAC_HANDLE td_md5
;
136 GNUTLS_MAC_HANDLE td_sha
;
139 td_md5
= _gnutls_hash_copy( state
->gnutls_internals
.handshake_mac_handle_md5
);
140 if (td_md5
== NULL
) {
142 return GNUTLS_E_HASH_FAILED
;
145 td_sha
= _gnutls_hash_copy( state
->gnutls_internals
.handshake_mac_handle_sha
);
146 if (td_sha
== NULL
) {
148 _gnutls_hash_deinit( td_md5
, NULL
);
149 return GNUTLS_E_HASH_FAILED
;
152 if (type
== GNUTLS_SERVER
) {
153 mesg
= SSL3_SERVER_MSG
;
155 mesg
= SSL3_CLIENT_MSG
;
158 _gnutls_hash(td_md5
, mesg
, siz
);
159 _gnutls_hash(td_sha
, mesg
, siz
);
161 _gnutls_mac_deinit_ssl3_handshake(td_md5
, ret
, state
->security_parameters
.master_secret
, TLS_MASTER_SIZE
);
162 _gnutls_mac_deinit_ssl3_handshake(td_sha
, &ret
[16], state
->security_parameters
.master_secret
, TLS_MASTER_SIZE
);
167 /* Hash the handshake messages as required by TLS 1.0
169 #define SERVER_MSG "server finished"
170 #define CLIENT_MSG "client finished"
171 #define TLS_MSG_LEN 15
172 int _gnutls_finished(GNUTLS_STATE state
, int type
, void *ret
)
174 const int siz
= TLS_MSG_LEN
;
177 GNUTLS_MAC_HANDLE td_md5
;
178 GNUTLS_MAC_HANDLE td_sha
;
181 td_md5
= _gnutls_hash_copy( state
->gnutls_internals
.handshake_mac_handle_md5
);
182 if (td_md5
== NULL
) {
184 return GNUTLS_E_HASH_FAILED
;
187 td_sha
= _gnutls_hash_copy( state
->gnutls_internals
.handshake_mac_handle_sha
);
188 if (td_sha
== NULL
) {
190 _gnutls_hash_deinit( td_md5
, NULL
);
191 return GNUTLS_E_HASH_FAILED
;
195 _gnutls_hash_deinit(td_md5
, concat
);
196 _gnutls_hash_deinit(td_sha
, &concat
[16]);
198 if (type
== GNUTLS_SERVER
) {
204 return _gnutls_PRF(state
->security_parameters
.master_secret
,
205 TLS_MASTER_SIZE
, mesg
, siz
, concat
, 36,
209 /* this function will produce TLS_RANDOM_SIZE bytes of random data
212 int _gnutls_create_random(opaque
* dst
)
215 opaque rand
[TLS_RANDOM_SIZE
- 4];
218 /* generate server random value */
219 _gnutls_write_uint32(tim
, dst
);
221 if (_gnutls_get_random
222 (rand
, TLS_RANDOM_SIZE
- 4, GNUTLS_STRONG_RANDOM
) < 0) {
224 return GNUTLS_E_MEMORY_ERROR
;
226 memcpy(&dst
[4], rand
, 28);
232 /* Read a client hello packet.
233 * A client hello must be a known version client hello
234 * or version 2.0 client hello (only for compatibility
235 * since SSL version 2.0 is not supported).
237 int _gnutls_read_client_hello(GNUTLS_STATE state
, opaque
* data
,
240 uint8 session_id_len
= 0, z
;
244 GNUTLS_Version version
;
246 opaque random
[TLS_RANDOM_SIZE
], *suite_ptr
;
249 if (state
->gnutls_internals
.v2_hello
!= 0) { /* version 2.0 */
250 return _gnutls_read_client_hello_v2(state
, data
, datalen
);
254 _gnutls_handshake_log("HSK: Client's version: %d.%d\n", data
[pos
], data
[pos
+ 1]);
256 version
= _gnutls_version_get(data
[pos
], data
[pos
+ 1]);
257 set_adv_version(state
, data
[pos
], data
[pos
+ 1]);
260 /* if we do not support that version */
261 if (_gnutls_version_is_supported(state
, version
) == 0) {
262 /* If he requested something we do not support
263 * then we send him the lowest we support.
265 ver
= _gnutls_version_lowest(state
);
270 /* he should have send us the highest version
273 if (ver
== GNUTLS_VERSION_UNKNOWN
|| ver
> version
) {
275 return GNUTLS_E_UNSUPPORTED_VERSION_PACKET
;
277 _gnutls_set_current_version(state
, ver
);
279 /* Read client random value.
281 DECR_LEN(len
, TLS_RANDOM_SIZE
);
282 _gnutls_set_client_random(state
, &data
[pos
]);
283 pos
+= TLS_RANDOM_SIZE
;
285 _gnutls_create_random(random
);
286 _gnutls_set_server_random(state
, random
);
288 state
->security_parameters
.timestamp
= time(NULL
);
291 session_id_len
= data
[pos
++];
295 if (session_id_len
> TLS_MAX_SESSION_ID_SIZE
) {
297 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
299 DECR_LEN(len
, session_id_len
);
300 ret
= _gnutls_server_restore_session(state
, &data
[pos
], session_id_len
);
301 pos
+= session_id_len
;
303 if (ret
== 0) { /* resumed! */
304 resume_copy_required_values(state
);
306 state
->gnutls_internals
.resumed
= RESUME_TRUE
;
309 _gnutls_generate_session_id(state
->security_parameters
.
311 &state
->security_parameters
.
314 state
->gnutls_internals
.resumed
= RESUME_FALSE
;
317 /* Select a ciphersuite
320 sizeOfSuites
= _gnutls_read_uint16(&data
[pos
]);
323 DECR_LEN(len
, sizeOfSuites
);
324 suite_ptr
= &data
[pos
];
327 /* Select an appropriate compression method
330 z
= data
[pos
++]; /* z is the number of compression methods */
333 ret
= _gnutls_server_select_comp_method(state
, &data
[pos
], z
);
341 /* Parse the extensions (if any)
343 if (ver
>= GNUTLS_TLS1
) {
344 ret
= _gnutls_parse_extensions(state
, &data
[pos
], len
); /* len is the rest of the parsed length */
351 /* select an appropriate cipher suite
353 ret
= _gnutls_server_select_suite(state
, suite_ptr
, sizeOfSuites
);
362 /* here we hash all pending data.
365 _gnutls_handshake_hash_pending( GNUTLS_STATE state
) {
369 if (state
->gnutls_internals
.handshake_mac_handle_sha
==NULL
||
370 state
->gnutls_internals
.handshake_mac_handle_md5
==NULL
) {
372 return GNUTLS_E_INTERNAL_ERROR
;
375 /* We check if there are pending data to hash.
377 if ((ret
=_gnutls_handshake_buffer_get_ptr(state
, &data
, &siz
)) < 0) {
383 _gnutls_hash( state
->gnutls_internals
.handshake_mac_handle_sha
, data
, siz
);
384 _gnutls_hash( state
->gnutls_internals
.handshake_mac_handle_md5
, data
, siz
);
387 _gnutls_handshake_buffer_empty( state
);
393 /* This is to be called after sending CHANGE CIPHER SPEC packet
394 * and initializing encryption. This is the first encrypted message
397 int _gnutls_send_finished(GNUTLS_STATE state
, int again
)
406 /* This needed in order to hash all the required
409 if ((ret
=_gnutls_handshake_hash_pending(state
)) < 0) {
414 if (gnutls_protocol_get_version( state
) == GNUTLS_SSL3
) {
416 _gnutls_ssl3_finished(state
,
421 } else { /* TLS 1.0 */
423 _gnutls_finished(state
,
424 state
->security_parameters
.
436 _gnutls_send_handshake(state
, data
, data_size
,
442 /* This is to be called after sending our finished message. If everything
443 * went fine we have negotiated a secure connection
445 int _gnutls_recv_finished(GNUTLS_STATE state
)
447 uint8 data
[36], *vrfy
;
455 _gnutls_recv_handshake(state
, &vrfy
, &vrfysize
,
456 GNUTLS_FINISHED
, MANDATORY_PACKET
);
458 ERR("recv finished int", ret
);
464 if ( gnutls_protocol_get_version( state
) == GNUTLS_SSL3
) {
470 if (vrfysize
!= data_size
) {
472 return GNUTLS_E_ERROR_IN_FINISHED_PACKET
;
475 if (gnutls_protocol_get_version( state
) == GNUTLS_SSL3
) {
477 _gnutls_ssl3_finished(state
,
478 (state
->security_parameters
.
479 entity
+ 1) % 2, data
);
480 } else { /* TLS 1.0 */
482 _gnutls_finished(state
,
483 (state
->security_parameters
.entity
+
492 if (memcmp(vrfy
, data
, data_size
) != 0) {
494 ret
= GNUTLS_E_ERROR_IN_FINISHED_PACKET
;
501 /* returns PK_RSA if the given cipher suite list only supports,
502 * RSA algorithms, PK_DSA if DSS, and -1 if both or none.
504 int _gnutls_find_pk_algos_in_ciphersuites( opaque
* data
, int datalen
) {
506 PKAlgorithm algo
=-1, prev_algo
= 0;
509 for (j
= 0; j
< datalen
; j
+= 2) {
510 kx
= _gnutls_cipher_suite_get_kx_algo(*((GNUTLS_CipherSuite
*) & data
[j
]));
512 if ( _gnutls_map_kx_get_cred( kx
) == GNUTLS_CRD_CERTIFICATE
) {
513 algo
= _gnutls_map_pk_get_pk( kx
);
515 if (algo
!=prev_algo
&& prev_algo
!=0) return -1;
524 /* This selects the best supported ciphersuite from the ones supported. Then
525 * it adds the suite into the state and performs some checks.
527 int _gnutls_server_select_suite(GNUTLS_STATE state
, opaque
*data
, int datalen
)
530 GNUTLS_CipherSuite
*ciphers
;
532 PKAlgorithm pk_algo
; /* will hold the pk algorithms
533 * supported by the peer.
536 pk_algo
= _gnutls_find_pk_algos_in_ciphersuites( data
, datalen
);
538 x
= _gnutls_supported_ciphersuites(state
, &ciphers
);
542 else return GNUTLS_E_INVALID_REQUEST
;
545 /* Here we remove any ciphersuite that does not conform
546 * the certificate requested, or to the
547 * authentication requested (eg SRP).
549 x
= _gnutls_remove_unwanted_ciphersuites(state
, &ciphers
, x
, pk_algo
);
553 else return GNUTLS_E_INSUFICIENT_CRED
;
556 #ifdef HANDSHAKE_DEBUG
557 _gnutls_handshake_log("HSK: Requested cipher suites: \n");
558 for (j
= 0; j
< datalen
; j
+= 2)
559 _gnutls_handshake_log("\t%s\n",
560 _gnutls_cipher_suite_get_name(*
561 ((GNUTLS_CipherSuite
*) & data
[j
])));
562 _gnutls_handshake_log("HSK: Supported cipher suites: \n");
563 for (j
= 0; j
< x
; j
++)
564 _gnutls_handshake_log("\t%s\n",
565 _gnutls_cipher_suite_get_name(ciphers
[j
]));
567 memset(state
->security_parameters
.current_cipher_suite
.CipherSuite
, '\0', 2);
569 retval
= GNUTLS_E_UNKNOWN_CIPHER_SUITE
;
571 for (j
= 0; j
< datalen
; j
+= 2) {
572 for (i
= 0; i
< x
; i
++) {
573 if (memcmp(ciphers
[i
].CipherSuite
, &data
[j
], 2) ==
575 _gnutls_handshake_log("HSK: Selected cipher suite: ");
576 _gnutls_handshake_log("%s\n",
577 _gnutls_cipher_suite_get_name(*
578 ((GNUTLS_CipherSuite
*) & data
[j
])));
579 memcpy(state
->security_parameters
.current_cipher_suite
.CipherSuite
, ciphers
[i
].CipherSuite
, 2);
587 gnutls_free(ciphers
);
594 /* check if the credentials (username, public key etc. are ok)
596 if (_gnutls_get_kx_cred
598 _gnutls_cipher_suite_get_kx_algo(state
->security_parameters
.
599 current_cipher_suite
),
600 &err
) == NULL
&& err
!= 0) {
602 return GNUTLS_E_INSUFICIENT_CRED
;
606 /* set the MOD_AUTH_STRUCT to the appropriate struct
607 * according to the KX algorithm. This is needed since all the
608 * handshake functions are read from there;
610 state
->gnutls_internals
.auth_struct
=
611 _gnutls_kx_auth_struct(_gnutls_cipher_suite_get_kx_algo
612 (state
->security_parameters
.
613 current_cipher_suite
));
614 if (state
->gnutls_internals
.auth_struct
== NULL
) {
616 _gnutls_handshake_log
617 ("HSK: Cannot find the appropriate handler for the KX algorithm\n");
619 return GNUTLS_E_UNKNOWN_CIPHER_TYPE
;
627 /* This selects the best supported compression method from the ones provided
629 int _gnutls_server_select_comp_method(GNUTLS_STATE state
, opaque
* data
,
635 x
= _gnutls_supported_compression_methods(state
, &ciphers
);
636 memset( &state
->gnutls_internals
.compression_method
, '\0', sizeof(CompressionMethod
));
638 for (j
= 0; j
< datalen
; j
++) {
639 for (i
= 0; i
< x
; i
++) {
640 if (ciphers
[i
] == data
[j
]) {
641 state
->gnutls_internals
.compression_method
=
642 _gnutls_compression_get_id(ciphers
[i
]);
643 gnutls_free(ciphers
);
645 _gnutls_handshake_log("HSK: Selected Compression Method: %s\n",
646 gnutls_compression_get_name(state
->gnutls_internals
.
647 compression_method
));
655 /* we were not able to find a compatible compression
658 gnutls_free(ciphers
);
660 return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM
;
664 /* This function sends an empty handshake packet. (like hello request).
665 * If the previous _gnutls_send_empty_handshake() returned
666 * GNUTLS_E_AGAIN or GNUTLS_E_INTERRUPTED, then it must be called again
667 * (until it returns ok), with NULL parameters.
669 int _gnutls_send_empty_handshake(GNUTLS_STATE state
, HandshakeType type
,
680 return _gnutls_send_handshake(state
, ptr
, 0, type
);
684 /* This function will hash the handshake message we sent.
687 int _gnutls_handshake_hash_add_sent( GNUTLS_STATE state
, HandshakeType type
,
688 opaque
* dataptr
, uint32 datalen
) {
691 if ( (ret
=_gnutls_handshake_hash_pending( state
)) < 0) {
696 if ( type
!= GNUTLS_HELLO_REQUEST
) {
697 _gnutls_hash( state
->gnutls_internals
.handshake_mac_handle_sha
, dataptr
, datalen
);
698 _gnutls_hash( state
->gnutls_internals
.handshake_mac_handle_md5
, dataptr
, datalen
);
705 /* This function sends a handshake message of type 'type' containing the
706 * data specified here. If the previous _gnutls_send_handshake() returned
707 * GNUTLS_E_AGAIN or GNUTLS_E_INTERRUPTED, then it must be called again
708 * (until it returns ok), with NULL parameters.
710 int _gnutls_send_handshake(GNUTLS_STATE state
, void *i_data
,
711 uint32 i_datasize
, HandshakeType type
)
718 if (i_data
== NULL
&& i_datasize
== 0) {
719 /* we are resuming a previously interrupted
722 ret
= _gnutls_handshake_io_write_flush(state
);
727 if (i_data
== NULL
&& i_datasize
> 0) {
729 return GNUTLS_E_INVALID_PARAMETERS
;
733 datasize
= i_datasize
+ HANDSHAKE_HEADER_SIZE
;
734 data
= gnutls_alloca(datasize
);
737 return GNUTLS_E_MEMORY_ERROR
;
740 data
[pos
++] = (uint8
) type
;
741 _gnutls_write_uint24(i_datasize
, &data
[pos
]);
745 memcpy(&data
[pos
], i_data
, i_datasize
);
747 _gnutls_handshake_log("HSK: %s was send [%ld bytes]\n",
748 _gnutls_handshake2str(type
), datasize
);
751 /* Here we keep the handshake messages in order to hash them...
753 if ( type
!= GNUTLS_HELLO_REQUEST
)
754 if ( (ret
= _gnutls_handshake_hash_add_sent( state
, type
, data
, datasize
)) < 0) {
761 _gnutls_handshake_io_send_int(state
, GNUTLS_HANDSHAKE
, type
,
769 /* This function will read the handshake header, and return it to the called. If the
770 * received handshake packet is not the one expected then it buffers the header, and
771 * returns UNEXPECTED_HANDSHAKE_PACKET.
773 * FIXME: This function is complex.
775 #define SSL2_HEADERS 1
776 static int _gnutls_recv_handshake_header(GNUTLS_STATE state
,
778 HandshakeType
* recv_type
)
782 uint8
*dataptr
= NULL
; /* for realloc */
783 int handshake_header_size
= HANDSHAKE_HEADER_SIZE
;
785 /* if we have data into the buffer then return them, do not read the next packet.
786 * In order to return we need a full TLS handshake header, or in case of a version 2
787 * packet, then we return the first byte.
789 if (state
->gnutls_internals
.handshake_header_buffer
.header_size
==
790 handshake_header_size
|| (state
->gnutls_internals
.v2_hello
!= 0
791 && type
== GNUTLS_CLIENT_HELLO
792 && state
->gnutls_internals
.
793 handshake_header_buffer
.
794 packet_length
> 0)) {
797 state
->gnutls_internals
.handshake_header_buffer
.
800 return state
->gnutls_internals
.handshake_header_buffer
.
804 /* Note: SSL2_HEADERS == 1 */
806 dataptr
= state
->gnutls_internals
.handshake_header_buffer
.header
;
808 /* If we haven't already read the handshake headers.
810 if (state
->gnutls_internals
.handshake_header_buffer
.header_size
<
813 _gnutls_handshake_io_recv_int(state
, GNUTLS_HANDSHAKE
,
821 GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
824 if (ret
!= SSL2_HEADERS
) {
826 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
828 state
->gnutls_internals
.handshake_header_buffer
.
829 header_size
= SSL2_HEADERS
;
832 if (state
->gnutls_internals
.v2_hello
== 0
833 || type
!= GNUTLS_CLIENT_HELLO
) {
835 _gnutls_handshake_io_recv_int(state
, GNUTLS_HANDSHAKE
,
839 handshake_header_buffer
.
841 HANDSHAKE_HEADER_SIZE
-
842 state
->gnutls_internals
.
843 handshake_header_buffer
.
849 GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
852 HANDSHAKE_HEADER_SIZE
-
853 state
->gnutls_internals
.handshake_header_buffer
.
856 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
858 *recv_type
= dataptr
[0];
860 /* we do not use DECR_LEN because we know
861 * that the packet has enough data.
863 length32
= _gnutls_read_uint24(&dataptr
[1]);
864 handshake_header_size
= HANDSHAKE_HEADER_SIZE
;
866 _gnutls_handshake_log("HSK: %s was received [%ld bytes]\n",
867 _gnutls_handshake2str(dataptr
[0]),
868 length32
+ HANDSHAKE_HEADER_SIZE
);
870 } else { /* v2 hello */
871 length32
= state
->gnutls_internals
.v2_hello
- SSL2_HEADERS
; /* we've read the first byte */
873 handshake_header_size
= SSL2_HEADERS
; /* we've already read one byte */
875 *recv_type
= dataptr
[0];
877 _gnutls_handshake_log("HSK: %s(v2) was received [%ld bytes]\n",
878 _gnutls_handshake2str(*recv_type
),
879 length32
+ handshake_header_size
);
881 if (*recv_type
!= GNUTLS_CLIENT_HELLO
) { /* it should be one or nothing */
883 return GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET
;
887 /* put the packet into the buffer */
888 state
->gnutls_internals
.handshake_header_buffer
.header_size
=
889 handshake_header_size
;
890 state
->gnutls_internals
.handshake_header_buffer
.packet_length
=
892 state
->gnutls_internals
.handshake_header_buffer
.recv_type
=
895 if (*recv_type
!= type
) {
897 return GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET
;
903 #define _gnutls_handshake_header_buffer_clear( state) state->gnutls_internals.handshake_header_buffer.header_size = 0
907 /* This function will hash the handshake headers and the
911 int _gnutls_handshake_hash_add_recvd( GNUTLS_STATE state
, HandshakeType recv_type
,
912 opaque
* header
, uint16 header_size
, opaque
* dataptr
, uint32 datalen
) {
915 /* The idea here is to hash the previous message we received,
916 * and add the one we just received into the handshake_hash_buffer.
919 if ( (ret
=_gnutls_handshake_hash_pending( state
)) < 0) {
924 /* here we buffer the handshake messages - needed at Finished message */
925 if ( recv_type
!= GNUTLS_HELLO_REQUEST
) {
928 _gnutls_handshake_buffer_put(state
,
929 header
, header_size
)) < 0) {
936 _gnutls_handshake_buffer_put(state
, dataptr
,
948 /* This function will receive handshake messages of the given types,
949 * and will pass the message to the right place in order to be processed.
950 * Eg. for the SERVER_HELLO message (if it is expected), it will be
951 * send to _gnutls_recv_hello().
953 int _gnutls_recv_handshake(GNUTLS_STATE state
, uint8
** data
,
954 int *datalen
, HandshakeType type
,
959 opaque
*dataptr
= NULL
;
960 HandshakeType recv_type
;
962 ret
= _gnutls_recv_handshake_header(state
, type
, &recv_type
);
964 if (ret
== GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET
965 && optional
== OPTIONAL_PACKET
) {
969 return 0; /* ok just ignore the packet */
979 dataptr
= gnutls_malloc(length32
);
980 else if (recv_type
!= GNUTLS_SERVER_HELLO_DONE
) {
982 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
985 if (dataptr
== NULL
&& length32
> 0) {
987 return GNUTLS_E_MEMORY_ERROR
;
995 _gnutls_handshake_io_recv_int(state
, GNUTLS_HANDSHAKE
,
996 type
, dataptr
, length32
);
999 gnutls_free(dataptr
);
1001 0) ? GNUTLS_E_UNEXPECTED_PACKET_LENGTH
:
1007 ret
= GNUTLS_E_UNKNOWN_ERROR
;
1009 if (data
!= NULL
&& length32
> 0)
1013 if ( (ret
=_gnutls_handshake_hash_add_recvd( state
, recv_type
,
1014 state
->gnutls_internals
.handshake_header_buffer
.header
,
1015 state
->gnutls_internals
.handshake_header_buffer
.header_size
,
1016 dataptr
, length32
)) < 0) {
1018 _gnutls_handshake_header_buffer_clear(state
);
1022 /* If we fail before this then we will reuse the handshake header
1023 * have have received above. if we get here the we clear the handshake
1024 * header we received.
1026 _gnutls_handshake_header_buffer_clear(state
);
1028 switch (recv_type
) {
1029 case GNUTLS_CLIENT_HELLO
:
1030 case GNUTLS_SERVER_HELLO
:
1031 ret
= _gnutls_recv_hello(state
, dataptr
, length32
);
1032 /* dataptr is freed because the caller does not
1034 gnutls_free(dataptr
);
1037 case GNUTLS_SERVER_HELLO_DONE
:
1038 if (length32
==0) ret
= 0;
1039 else ret
= GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
1041 case GNUTLS_CERTIFICATE_PKT
:
1042 case GNUTLS_FINISHED
:
1043 case GNUTLS_SERVER_KEY_EXCHANGE
:
1044 case GNUTLS_CLIENT_KEY_EXCHANGE
:
1045 case GNUTLS_CERTIFICATE_REQUEST
:
1046 case GNUTLS_CERTIFICATE_VERIFY
:
1051 gnutls_free(dataptr
);
1053 ret
= GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET
;
1059 /* This function checks if the given cipher suite is supported, and sets it
1062 static int _gnutls_client_set_ciphersuite(GNUTLS_STATE state
,
1066 GNUTLS_CipherSuite
*cipher_suites
;
1071 x
= _gnutls_supported_ciphersuites(state
, &cipher_suites
);
1072 for (i
= 0; i
< x
; i
++) {
1073 if (memcmp(&cipher_suites
[i
], suite
, 2) == 0) {
1078 gnutls_free(cipher_suites
);
1082 return GNUTLS_E_UNKNOWN_CIPHER_TYPE
;
1085 memcpy(state
->security_parameters
.
1086 current_cipher_suite
.CipherSuite
, suite
, 2);
1088 _gnutls_handshake_log("HSK: Selected cipher suite: ");
1089 _gnutls_handshake_log("%s\n",
1090 _gnutls_cipher_suite_get_name(state
->
1091 security_parameters
.
1092 current_cipher_suite
));
1095 /* check if the credentials (username, public key etc. are ok).
1096 * Actually checks if they exist.
1098 if (_gnutls_get_kx_cred
1100 _gnutls_cipher_suite_get_kx_algo(state
->
1101 security_parameters
.
1102 current_cipher_suite
),
1103 &err
) == NULL
&& err
!= 0) {
1105 return GNUTLS_E_INSUFICIENT_CRED
;
1109 /* set the MOD_AUTH_STRUCT to the appropriate struct
1110 * according to the KX algorithm. This is needed since all the
1111 * handshake functions are read from there;
1113 state
->gnutls_internals
.auth_struct
=
1114 _gnutls_kx_auth_struct(_gnutls_cipher_suite_get_kx_algo
1115 (state
->security_parameters
.
1116 current_cipher_suite
));
1118 if (state
->gnutls_internals
.auth_struct
== NULL
) {
1120 _gnutls_handshake_log
1121 ("HSK: Cannot find the appropriate handler for the KX algorithm\n");
1123 return GNUTLS_E_UNKNOWN_CIPHER_TYPE
;
1130 /* This function sets the given comp method to the state.
1132 static int _gnutls_client_set_comp_method(GNUTLS_STATE state
,
1136 uint8
*compression_methods
;
1139 z
= _gnutls_supported_compression_methods(state
,
1140 &compression_methods
);
1141 for (i
= 0; i
< z
; i
++) {
1142 if (compression_methods
[i
] == comp_method
) {
1147 gnutls_free(compression_methods
);
1151 return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM
;
1154 state
->gnutls_internals
.compression_method
=
1155 _gnutls_compression_get_id(comp_method
);
1161 /* This function returns 0 if we are resuming a session or -1 otherwise.
1162 * This also sets the variables in the state. Used only while reading a server
1165 static int _gnutls_client_check_if_resuming(GNUTLS_STATE state
,
1166 opaque
* session_id
,
1170 _gnutls_handshake_log("HSK: SessionID length: %d\n", session_id_len
);
1171 _gnutls_handshake_log("HSK: SessionID: %s\n",
1172 _gnutls_bin2hex(session_id
, session_id_len
));
1174 if ((state
->gnutls_internals
.resumed_security_parameters
.
1175 session_id_size
> 0)
1176 && memcmp(session_id
,
1177 state
->gnutls_internals
.
1178 resumed_security_parameters
.session_id
,
1179 session_id_len
) == 0) {
1180 /* resume session */
1181 memcpy(state
->gnutls_internals
.
1182 resumed_security_parameters
.server_random
,
1183 state
->security_parameters
.server_random
,
1185 memcpy(state
->gnutls_internals
.
1186 resumed_security_parameters
.client_random
,
1187 state
->security_parameters
.client_random
,
1189 state
->gnutls_internals
.resumed
= RESUME_TRUE
; /* we are resuming */
1193 /* keep the new session id */
1194 state
->gnutls_internals
.resumed
= RESUME_FALSE
; /* we are not resuming */
1195 state
->security_parameters
.session_id_size
=
1197 memcpy(state
->security_parameters
.session_id
,
1198 session_id
, session_id_len
);
1205 /* This function read and parse the server hello handshake message.
1206 * This function also restores resumed parameters if we are resuming a
1209 static int _gnutls_read_server_hello(GNUTLS_STATE state
, char *data
,
1212 uint8 session_id_len
= 0;
1215 GNUTLS_Version version
;
1220 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
1223 _gnutls_handshake_log("HSK: Server's version: %d.%d\n", data
[pos
], data
[pos
+ 1]);
1226 version
= _gnutls_version_get(data
[pos
], data
[pos
+ 1]);
1227 if (_gnutls_version_is_supported(state
, version
) == 0) {
1229 return GNUTLS_E_UNSUPPORTED_VERSION_PACKET
;
1231 _gnutls_set_current_version(state
, version
);
1236 DECR_LEN(len
, TLS_RANDOM_SIZE
);
1237 _gnutls_set_server_random(state
, &data
[pos
]);
1238 pos
+= TLS_RANDOM_SIZE
;
1244 session_id_len
= data
[pos
++];
1246 if (len
< session_id_len
) {
1248 return GNUTLS_E_UNSUPPORTED_VERSION_PACKET
;
1250 DECR_LEN(len
, session_id_len
);
1253 /* check if we are resuming and set the appropriate
1256 if (_gnutls_client_check_if_resuming
1257 (state
, &data
[pos
], session_id_len
) == 0)
1259 pos
+= session_id_len
;
1262 /* Check if the given cipher suite is supported and copy
1267 ret
= _gnutls_client_set_ciphersuite(state
, &data
[pos
]);
1276 /* move to compression
1280 ret
= _gnutls_client_set_comp_method(state
, data
[pos
++]);
1283 return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM
;
1286 /* Parse extensions.
1288 if (version
>= GNUTLS_TLS1
) {
1289 ret
= _gnutls_parse_extensions(state
, &data
[pos
], len
); /* len is the rest of the parsed length */
1298 /* This function copies the appropriate ciphersuites, to a localy allocated buffer
1299 * Needed in client hello messages. Returns the new data length.
1301 static int _gnutls_copy_ciphersuites(GNUTLS_STATE state
,
1305 GNUTLS_CipherSuite
*cipher_suites
;
1309 ret
= _gnutls_supported_ciphersuites_sorted(state
, &cipher_suites
);
1312 if (ret
==0) return GNUTLS_E_INVALID_REQUEST
;
1316 /* Here we remove any ciphersuite that does not conform
1317 * the certificate requested, or to the
1318 * authentication requested (eg SRP).
1321 _gnutls_remove_unwanted_ciphersuites(state
, &cipher_suites
,
1329 return GNUTLS_E_INSUFICIENT_CRED
;
1334 x
*= sizeof(uint16
); /* in order to get bytes */
1338 datalen
+= sizeof(uint16
) + x
;
1340 *ret_data
= gnutls_malloc(datalen
);
1343 if (*ret_data
== NULL
) {
1345 return GNUTLS_E_MEMORY_ERROR
;
1349 _gnutls_write_uint16(x
, *ret_data
);
1352 for (i
= 0; i
< x
/ 2; i
++) {
1353 memcpy(&(*ret_data
)[pos
], cipher_suites
[i
].CipherSuite
, 2);
1356 gnutls_free(cipher_suites
);
1362 /* This function copies the appropriate compression methods, to a localy allocated buffer
1363 * Needed in hello messages. Returns the new data length.
1365 static int _gnutls_copy_comp_methods(GNUTLS_STATE state
,
1369 uint8
*compression_methods
, z
;
1373 _gnutls_supported_compression_methods(state
,
1374 &compression_methods
);
1385 *ret_data
= gnutls_malloc(datalen
);
1386 if (*ret_data
== NULL
) {
1388 return GNUTLS_E_MEMORY_ERROR
;
1391 (*ret_data
)[pos
++] = z
; /* put the number of compression methods */
1393 for (i
= 0; i
< z
; i
++) {
1394 (*ret_data
)[pos
++] = compression_methods
[i
];
1397 gnutls_free(compression_methods
);
1402 /* This function sends the client hello handshake message.
1404 static int _gnutls_send_client_hello(GNUTLS_STATE state
, int again
)
1406 opaque
*data
= NULL
;
1410 int datalen
, ret
= 0;
1411 opaque random
[TLS_RANDOM_SIZE
];
1412 GNUTLS_Version hver
;
1415 state
->gnutls_internals
.resumed_security_parameters
.session_id
;
1416 uint8 session_id_len
=
1417 state
->gnutls_internals
.resumed_security_parameters
.
1420 if (SessionID
== NULL
|| session_id_len
== 0) {
1429 datalen
= 2 + (session_id_len
+ 1) + TLS_RANDOM_SIZE
;
1430 /* 2 for version, (4 for unix time + 28 for random bytes==TLS_RANDOM_SIZE)
1433 data
= gnutls_malloc(datalen
+ 16); /* 16 is added to avoid realloc
1434 * if no much data are added.
1438 return GNUTLS_E_MEMORY_ERROR
;
1441 /* if we are resuming a session then we set the
1442 * version number to the previously established.
1444 if (SessionID
== NULL
)
1445 hver
= _gnutls_version_max(state
);
1446 else { /* we are resuming a session */
1448 state
->gnutls_internals
.
1449 resumed_security_parameters
.version
;
1454 hver
= GNUTLS_E_UNKNOWN_ERROR
;
1459 data
[pos
++] = _gnutls_version_get_major(hver
);
1460 data
[pos
++] = _gnutls_version_get_minor(hver
);
1462 /* Set the version we advertized as maximum
1465 _gnutls_set_adv_version( state
, hver
);
1467 /* Some implementations do not interoperate if we send a
1468 * different version in the record layer.
1469 * It seems they prefer to read the record's version
1470 * as the one we actually requested.
1471 * The proper behaviour is to use the one in the client hello
1472 * handshake packet and ignore the one in the packet's record
1475 if (state
->gnutls_internals
.default_record_version
==0)
1476 _gnutls_set_current_version(state
, hver
);
1477 else _gnutls_set_current_version(state
,
1478 state
->gnutls_internals
.default_record_version
);
1480 /* In order to know when this session was initiated.
1482 state
->security_parameters
.timestamp
= time(NULL
);
1484 /* Generate random data
1486 _gnutls_create_random(random
);
1487 _gnutls_set_client_random(state
, random
);
1489 memcpy(&data
[pos
], random
, TLS_RANDOM_SIZE
);
1490 pos
+= TLS_RANDOM_SIZE
;
1492 /* Copy the Session ID
1494 data
[pos
++] = session_id_len
;
1496 if (session_id_len
> 0) {
1497 memcpy(&data
[pos
], SessionID
, session_id_len
);
1499 pos
+= session_id_len
;
1502 /* Copy the ciphersuites.
1504 extdatalen
= _gnutls_copy_ciphersuites(state
, &extdata
);
1505 if (extdatalen
> 0) {
1506 datalen
+= extdatalen
;
1507 data
= gnutls_realloc(data
, datalen
);
1510 gnutls_free(extdata
);
1511 return GNUTLS_E_MEMORY_ERROR
;
1514 memcpy(&data
[pos
], extdata
, extdatalen
);
1515 gnutls_free(extdata
);
1519 if (extdatalen
== 0)
1520 extdatalen
= GNUTLS_E_UNKNOWN_ERROR
;
1527 /* Copy the compression methods.
1529 extdatalen
= _gnutls_copy_comp_methods(state
, &extdata
);
1530 if (extdatalen
> 0) {
1531 datalen
+= extdatalen
;
1532 data
= gnutls_realloc(data
, datalen
);
1535 gnutls_free(extdata
);
1536 return GNUTLS_E_MEMORY_ERROR
;
1539 memcpy(&data
[pos
], extdata
, extdatalen
);
1540 gnutls_free(extdata
);
1544 if (extdatalen
== 0)
1545 extdatalen
= GNUTLS_E_UNKNOWN_ERROR
;
1551 /* Generate and copy TLS extensions.
1553 if (hver
>= GNUTLS_TLS1
) {
1554 extdatalen
= _gnutls_gen_extensions(state
, &extdata
);
1555 if (extdatalen
> 0) {
1556 datalen
+= extdatalen
;
1557 data
= gnutls_realloc(data
, datalen
);
1560 gnutls_free(extdata
);
1561 return GNUTLS_E_MEMORY_ERROR
;
1564 memcpy(&data
[pos
], extdata
, extdatalen
);
1565 gnutls_free(extdata
);
1571 _gnutls_send_handshake(state
, data
, datalen
,
1572 GNUTLS_CLIENT_HELLO
);
1578 static int _gnutls_send_server_hello(GNUTLS_STATE state
, int again
)
1584 int datalen
, ret
= 0;
1586 opaque
*SessionID
= state
->security_parameters
.session_id
;
1587 uint8 session_id_len
= state
->security_parameters
.session_id_size
;
1589 if (SessionID
== NULL
)
1596 datalen
= 2 + session_id_len
+ 1 + TLS_RANDOM_SIZE
+ 3;
1597 extdatalen
= _gnutls_gen_extensions(state
, &extdata
);
1599 data
= gnutls_alloca(datalen
+ extdatalen
);
1602 gnutls_free(extdata
);
1603 return GNUTLS_E_MEMORY_ERROR
;
1607 _gnutls_version_get_major(state
->security_parameters
.
1610 _gnutls_version_get_minor(state
->security_parameters
.
1614 state
->security_parameters
.server_random
,
1616 pos
+= TLS_RANDOM_SIZE
;
1618 data
[pos
++] = session_id_len
;
1619 if (session_id_len
> 0) {
1620 memcpy(&data
[pos
], SessionID
, session_id_len
);
1622 pos
+= session_id_len
;
1624 _gnutls_handshake_log("HSK: SessionID: %s\n",
1625 _gnutls_bin2hex(SessionID
, session_id_len
));
1628 state
->security_parameters
.
1629 current_cipher_suite
.CipherSuite
, 2);
1633 (uint8
) _gnutls_compression_get_num(state
->
1635 compression_method
);
1639 if (extdatalen
> 0) {
1640 datalen
+= extdatalen
;
1642 memcpy(&data
[pos
], extdata
, extdatalen
);
1643 gnutls_free(extdata
);
1648 _gnutls_send_handshake(state
, data
, datalen
,
1649 GNUTLS_SERVER_HELLO
);
1655 int _gnutls_send_hello(GNUTLS_STATE state
, int again
)
1659 if (state
->security_parameters
.entity
== GNUTLS_CLIENT
) {
1660 ret
= _gnutls_send_client_hello(state
, again
);
1662 } else { /* SERVER */
1663 ret
= _gnutls_send_server_hello(state
, again
);
1669 /* RECEIVE A HELLO MESSAGE. This should be called from gnutls_recv_handshake_int only if a
1670 * hello message is expected. It uses the security_parameters.current_cipher_suite
1671 * and gnutls_internals.compression_method.
1673 int _gnutls_recv_hello(GNUTLS_STATE state
, char *data
, int datalen
)
1677 if (state
->security_parameters
.entity
== GNUTLS_CLIENT
) {
1678 ret
= _gnutls_read_server_hello(state
, data
, datalen
);
1683 } else { /* Server side reading a client hello */
1685 ret
= _gnutls_read_client_hello(state
, data
, datalen
);
1695 /* The packets in gnutls_handshake (it's more broad than original TLS handshake)
1699 * ClientHello -------->
1700 * <-------- ServerHello
1703 * ServerKeyExchange*
1704 * Client Key Exchange0 -------->
1705 * <-------- CertificateRequest*
1707 * <-------- Server Key Exchange2
1708 * <-------- ServerHelloDone
1711 * CertificateVerify*
1712 * [ChangeCipherSpec]
1713 * Finished -------->
1714 * [ChangeCipherSpec]
1715 * <-------- Finished
1717 * (*): means optional packet.
1721 * gnutls_rehandshake - This function will renegotiate security parameters
1722 * @state: is a a &GNUTLS_STATE structure.
1724 * This function will renegotiate security parameters with the
1725 * client. This should only be called in case of a server.
1727 * This message informs the peer that we want to renegotiate
1728 * parameters (perform a handshake).
1730 * If this function succeeds (returns 0), you must call
1731 * the gnutls_handshake() function in order to negotiate
1732 * the new parameters.
1734 * If the client does not wish to renegotiate parameters he
1735 * will reply with an alert message, thus the return code will be
1736 * GNUTLS_E_WARNING_ALERT_RECEIVED and the alert will be
1737 * GNUTLS_A_NO_RENEGOTIATION.
1739 int gnutls_rehandshake(GNUTLS_STATE state
)
1743 /* only server sends that handshake packet */
1744 if (state
->security_parameters
.entity
== GNUTLS_CLIENT
)
1745 return GNUTLS_E_INVALID_REQUEST
;
1748 _gnutls_send_empty_handshake(state
, GNUTLS_HELLO_REQUEST
,
1761 static int _gnutls_abort_handshake( GNUTLS_STATE state
, int ret
) {
1762 if ( ((ret
==GNUTLS_E_WARNING_ALERT_RECEIVED
) &&
1763 ( gnutls_alert_get(state
) == GNUTLS_A_NO_RENEGOTIATION
))
1764 || ret
==GNUTLS_E_GOT_APPLICATION_DATA
)
1767 /* this doesn't matter */
1768 return GNUTLS_E_UNKNOWN_ERROR
;
1772 /* This function initialized the handshake hash state.
1773 * required for finished messages.
1776 static int _gnutls_handshake_hash_init( GNUTLS_STATE state
) {
1778 if ( state
->gnutls_internals
.handshake_mac_handle_md5
==NULL
) {
1779 state
->gnutls_internals
.handshake_mac_handle_md5
= _gnutls_hash_init( GNUTLS_MAC_MD5
);
1781 if (state
->gnutls_internals
.handshake_mac_handle_md5
==GNUTLS_HASH_FAILED
) {
1783 return GNUTLS_E_MEMORY_ERROR
;
1787 if ( state
->gnutls_internals
.handshake_mac_handle_sha
==NULL
) {
1788 state
->gnutls_internals
.handshake_mac_handle_sha
= _gnutls_hash_init( GNUTLS_MAC_SHA
);
1789 if (state
->gnutls_internals
.handshake_mac_handle_sha
==GNUTLS_HASH_FAILED
) {
1791 return GNUTLS_E_MEMORY_ERROR
;
1799 * gnutls_handshake - This the main function in the handshake protocol.
1800 * @state: is a a &GNUTLS_STATE structure.
1802 * This function does the handshake of the TLS/SSL protocol,
1803 * and initializes the TLS connection.
1805 * This function will fail if any problem is encountered,
1806 * and will return a negative error code. In case of a client,
1807 * if it has been asked to resume a session, but the server didn't, then
1808 * a full handshake will be performed.
1810 * This function may also return the non-fatal errors GNUTLS_E_AGAIN, or
1811 * GNUTLS_E_INTERRUPTED. In that case you may resume the handshake
1812 * (call this function again, until it returns ok)
1814 * If this function is called by a server after a rehandshake request then
1815 * GNUTLS_E_GOT_APPLICATION_DATA or GNUTLS_E_WARNING_ALERT_RECEIVED
1816 * may be returned. Note that these are non fatal errors, only in the
1817 * case of a rehandshake. In that case they mean that the client
1818 * rejected the rehandshake request.
1821 int gnutls_handshake(GNUTLS_STATE state
)
1825 if ( (ret
=_gnutls_handshake_hash_init( state
)) < 0) {
1830 if (state
->security_parameters
.entity
== GNUTLS_CLIENT
) {
1831 ret
= gnutls_handshake_client(state
);
1833 ret
= gnutls_handshake_server(state
);
1836 /* In the case of a rehandshake abort
1837 * we should reset the handshake's state
1839 if (_gnutls_abort_handshake( state
, ret
) == 0)
1844 ret
= gnutls_handshake_common(state
);
1847 if (_gnutls_abort_handshake( state
, ret
) == 0)
1854 _gnutls_handshake_io_buffer_clear(state
);
1855 _gnutls_handshake_internal_state_clear(state
);
1860 #define IMED_RET( str, ret) \
1862 if (gnutls_error_is_fatal(ret)==0) return ret; \
1865 _gnutls_handshake_hash_buffers_clear(state); \
1872 * gnutls_handshake_client
1873 * This function performs the client side of the handshake of the TLS/SSL protocol.
1875 int gnutls_handshake_client(GNUTLS_STATE state
)
1879 #ifdef HANDSHAKE_DEBUG
1880 if (state
->gnutls_internals
.resumed_security_parameters
.
1881 session_id_size
> 0)
1882 _gnutls_handshake_log("HSK: Ask to resume: %s\n",
1883 _gnutls_bin2hex(state
->gnutls_internals
.
1884 resumed_security_parameters
.
1886 state
->gnutls_internals
.
1887 resumed_security_parameters
.
1894 ret
= _gnutls_send_hello(state
, AGAIN(STATE1
));
1896 IMED_RET("send hello", ret
);
1899 /* receive the server hello */
1901 _gnutls_recv_handshake(state
, NULL
, NULL
,
1902 GNUTLS_SERVER_HELLO
,
1905 IMED_RET("recv hello", ret
);
1908 /* RECV CERTIFICATE */
1909 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
1910 ret
= _gnutls_recv_server_certificate(state
);
1912 IMED_RET("recv server certificate", ret
);
1915 /* receive the server key exchange */
1916 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
1917 ret
= _gnutls_recv_server_kx_message(state
);
1919 IMED_RET("recv server kx message", ret
);
1923 * send the client key exchange for SRP
1925 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
1927 _gnutls_send_client_kx_message0(state
,
1930 IMED_RET("send client kx0", ret
);
1933 /* receive the server certificate request - if any
1936 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
1938 _gnutls_recv_server_certificate_request(state
);
1940 IMED_RET("recv server certificate request message", ret
);
1943 /* receive the server key exchange (B) (SRP only)
1946 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
1947 ret
= _gnutls_recv_server_kx_message2(state
);
1949 IMED_RET("recv server kx message2", ret
);
1952 /* receive the server hello done */
1953 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
1955 _gnutls_recv_handshake(state
, NULL
, NULL
,
1956 GNUTLS_SERVER_HELLO_DONE
,
1959 IMED_RET("recv server hello done", ret
);
1962 /* send our certificate - if any and if requested
1964 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
1966 _gnutls_send_client_certificate(state
,
1969 IMED_RET("send client certificate", ret
);
1972 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
1974 _gnutls_send_client_kx_message(state
,
1977 IMED_RET("send client kx", ret
);
1980 /* send client certificate verify */
1981 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
1983 _gnutls_send_client_certificate_verify(state
,
1987 IMED_RET("send client certificate verify", ret
);
1996 /* This function sends the final handshake packets and initializes connection
1998 static int _gnutls_send_handshake_final(GNUTLS_STATE state
, int init
)
2002 /* Send the CHANGE CIPHER SPEC PACKET */
2008 _gnutls_send_change_cipher_spec(state
, AGAIN(STATE20
));
2011 ERR("send ChangeCipherSpec", ret
);
2016 /* Initialize the connection state (start encryption) - in case of client
2019 ret
= _gnutls_connection_state_init(state
);
2026 ret
= _gnutls_write_connection_state_init(state
);
2033 /* send the finished message */
2034 ret
= _gnutls_send_finished(state
, AGAIN(STATE21
));
2037 ERR("send Finished", ret
);
2048 /* This function receives the final handshake packets
2049 * And executes the appropriate function to initialize the
2052 static int _gnutls_recv_handshake_final(GNUTLS_STATE state
, int init
)
2061 gnutls_recv_int(state
, GNUTLS_CHANGE_CIPHER_SPEC
, -1,
2065 ERR("recv ChangeCipherSpec", ret
);
2069 GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
2072 /* Initialize the connection state (start encryption) - in case of server */
2074 ret
= _gnutls_connection_state_init(state
);
2081 ret
= _gnutls_read_connection_state_init(state
);
2088 ret
= _gnutls_recv_finished(state
);
2091 ERR("recv finished", ret
);
2103 * gnutls_handshake_server
2104 * This function does the server stuff of the handshake protocol.
2107 int gnutls_handshake_server(GNUTLS_STATE state
)
2115 _gnutls_recv_handshake(state
, NULL
, NULL
,
2116 GNUTLS_CLIENT_HELLO
,
2119 IMED_RET("recv hello", ret
);
2122 ret
= _gnutls_send_hello(state
, AGAIN(STATE2
));
2124 IMED_RET("send hello", ret
);
2126 /* SEND CERTIFICATE + KEYEXCHANGE + CERTIFICATE_REQUEST */
2128 /* NOTE: these should not be send if we are resuming */
2130 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
)
2132 _gnutls_send_server_certificate(state
,
2135 IMED_RET("send server certificate", ret
);
2138 /* send server key exchange (A) */
2139 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
)
2141 _gnutls_send_server_kx_message(state
,
2144 IMED_RET("send server kx", ret
);
2147 /* Send certificate request - if requested to */
2148 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
)
2150 _gnutls_send_server_certificate_request(state
,
2154 IMED_RET("send server cert request", ret
);
2157 /* Added for SRP which uses a different handshake */
2158 /* receive the client key exchange message */
2160 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2161 ret
= _gnutls_recv_client_kx_message0(state
);
2163 IMED_RET("recv client kx0", ret
);
2166 /* send server key exchange (B) */
2167 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
)
2169 _gnutls_send_server_kx_message2(state
,
2172 IMED_RET("send server kx2", ret
);
2175 /* send the server hello done */
2176 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2178 _gnutls_send_empty_handshake(state
,
2179 GNUTLS_SERVER_HELLO_DONE
,
2182 IMED_RET("send server hello done", ret
);
2185 /* RECV CERTIFICATE + KEYEXCHANGE + CERTIFICATE_VERIFY */
2187 /* receive the client certificate message */
2188 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2189 ret
= _gnutls_recv_client_certificate(state
);
2191 IMED_RET("recv client certificate", ret
);
2194 /* receive the client key exchange message */
2195 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2196 ret
= _gnutls_recv_client_kx_message(state
);
2198 IMED_RET("recv client kx", ret
);
2201 /* receive the client certificate verify message */
2202 if (state
->gnutls_internals
.resumed
== RESUME_FALSE
) /* if we are not resuming */
2204 _gnutls_recv_client_certificate_verify_message
2207 IMED_RET("recv client certificate verify", ret
);
2209 STATE
= STATE0
; /* finished thus clear state */
2215 int gnutls_handshake_common(GNUTLS_STATE state
)
2220 /* send and recv the change cipher spec and finished messages */
2221 if ((state
->gnutls_internals
.resumed
== RESUME_TRUE
2222 && state
->security_parameters
.entity
== GNUTLS_CLIENT
)
2223 || (state
->gnutls_internals
.resumed
== RESUME_FALSE
2224 && state
->security_parameters
.entity
== GNUTLS_SERVER
)) {
2225 /* if we are a client resuming - or we are a server not resuming */
2227 ret
= _gnutls_recv_handshake_final(state
, TRUE
);
2228 IMED_RET("recv handshake final", ret
);
2230 ret
= _gnutls_send_handshake_final(state
, FALSE
);
2231 IMED_RET("send handshake final", ret
);
2232 } else { /* if we are a client not resuming - or we are a server resuming */
2234 ret
= _gnutls_send_handshake_final(state
, TRUE
);
2235 IMED_RET("send handshake final 2", ret
);
2237 ret
= _gnutls_recv_handshake_final(state
, FALSE
);
2238 IMED_RET("recv handshake final 2", ret
);
2241 if (state
->security_parameters
.entity
== GNUTLS_SERVER
) {
2242 /* in order to support session resuming */
2243 _gnutls_server_register_current_session(state
);
2246 /* clear handshake buffer */
2247 _gnutls_handshake_hash_buffers_clear(state
);
2252 int _gnutls_generate_session_id(char *session_id
, uint8
* len
)
2254 opaque rand
[TLS_RANDOM_SIZE
];
2255 if (_gnutls_get_random(rand
, TLS_RANDOM_SIZE
, GNUTLS_WEAK_RANDOM
) <
2258 return GNUTLS_E_MEMORY_ERROR
;
2260 memcpy(session_id
, rand
, TLS_RANDOM_SIZE
);
2261 *len
= TLS_RANDOM_SIZE
;
2263 _gnutls_handshake_log("HSK: Generated SessionID: %s\n",
2264 _gnutls_bin2hex(session_id
, TLS_RANDOM_SIZE
));
2269 int _gnutls_recv_hello_request(GNUTLS_STATE state
, void *data
,
2274 if (state
->security_parameters
.entity
== GNUTLS_SERVER
) {
2276 return GNUTLS_E_UNEXPECTED_PACKET
;
2278 if (data_size
< 1) {
2280 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
2282 type
= ((uint8
*) data
)[0];
2283 if (type
== GNUTLS_HELLO_REQUEST
)
2284 return GNUTLS_E_REHANDSHAKE
;
2287 return GNUTLS_E_UNEXPECTED_PACKET
;
2291 /* This function will remove algorithms that are not supported by
2292 * the requested authentication method. We remove algorithm if
2293 * we have a certificate with keyUsage bits set.
2295 * This does a more high level check than gnutls_supported_ciphersuites(),
2296 * by checking certificates etc.
2298 int _gnutls_remove_unwanted_ciphersuites(GNUTLS_STATE state
,
2299 GNUTLS_CipherSuite
**
2300 cipherSuites
, int numCipherSuites
,
2301 PKAlgorithm requested_pk_algo
)
2305 GNUTLS_CipherSuite
*newSuite
;
2306 int newSuiteSize
= 0, i
, j
, keep
;
2307 const GNUTLS_CERTIFICATE_CREDENTIALS x509_cred
;
2308 const gnutls_cert
*cert
= NULL
;
2314 /* if we should use a specific certificate,
2315 * we should remove all algorithms that are not supported
2316 * by that certificate and are on the same authentication
2317 * method (CERTIFICATE).
2321 _gnutls_get_cred(state
->gnutls_key
, GNUTLS_CRD_CERTIFICATE
, NULL
);
2323 /* if x509_cred==NULL we should remove all X509 ciphersuites
2327 if (state
->security_parameters
.entity
== GNUTLS_SERVER
)
2328 cert
= _gnutls_server_find_cert(state
, requested_pk_algo
);
2331 /* No certificate was found
2337 /* get all the key exchange algorithms that are
2338 * supported by the X509 certificate parameters.
2341 _gnutls_cert_supported_kx(cert
, &alg
,
2349 gnutls_malloc(numCipherSuites
* sizeof(GNUTLS_CipherSuite
));
2350 if (newSuite
== NULL
) {
2353 return GNUTLS_E_MEMORY_ERROR
;
2356 /* now removes ciphersuites based on the KX algorithm
2358 for (i
= 0; i
< numCipherSuites
; i
++) {
2359 /* finds the key exchange algorithm in
2362 kx
= _gnutls_cipher_suite_get_kx_algo((*cipherSuites
)[i
]);
2366 /* if it is defined but had no credentials
2368 if (_gnutls_get_kx_cred
2369 (state
->gnutls_key
, kx
, NULL
) == NULL
) {
2372 /* If there was no credentials to use with the specified
2373 * key exchange method, then just remove it.
2375 if (_gnutls_map_kx_get_cred(kx
) == GNUTLS_CRD_CERTIFICATE
) {
2376 keep
= 1; /* do not keep */
2377 if (x509_cred
!= NULL
) {
2378 if (state
->security_parameters
.entity
==
2380 /* here we check if the KX algorithm
2381 * is compatible with the certificate.
2383 for (j
= 0; j
< alg_size
; j
++) {
2396 _gnutls_handshake_log("HSK: Keeping ciphersuite: ");
2397 _gnutls_handshake_log("%s\n",
2398 _gnutls_cipher_suite_get_name(*
2399 ((GNUTLS_CipherSuite
*) & (*cipherSuites
)[i
].CipherSuite
)));
2401 memcpy(newSuite
[newSuiteSize
].CipherSuite
,
2402 (*cipherSuites
)[i
].CipherSuite
, 2);
2405 _gnutls_handshake_log("HSK: Removing ciphersuite: ");
2406 _gnutls_handshake_log("%s\n",
2407 _gnutls_cipher_suite_get_name(*
2408 ((GNUTLS_CipherSuite
*) & (*cipherSuites
)[i
].CipherSuite
)));
2414 gnutls_free(*cipherSuites
);
2415 *cipherSuites
= newSuite
;
2424 * gnutls_handshake_set_max_packet_length - This function will set the maximum length of a handshake message
2425 * @state: is a a &GNUTLS_STATE structure.
2426 * @max: is the maximum number.
2428 * This function will set the maximum size of a handshake message.
2429 * Handshake messages over this size are rejected.
2430 * The default value is 16kb which is large enough. Set this to 0 if you do not want
2431 * to set an upper limit.
2434 void gnutls_handshake_set_max_packet_length(GNUTLS_STATE state
, int max
)
2436 state
->gnutls_internals
.max_handshake_data_buffer_size
= max
;
2439 void _gnutls_set_adv_version( GNUTLS_STATE state
, GNUTLS_Version ver
) {
2440 set_adv_version( state
, _gnutls_version_get_major(ver
), _gnutls_version_get_minor(ver
));
2443 GNUTLS_Version
_gnutls_get_adv_version( GNUTLS_STATE state
) {
2444 return _gnutls_version_get( _gnutls_get_adv_version_major( state
),
2445 _gnutls_get_adv_version_minor( state
));