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 /* This file contains functions which are wrappers for the key exchange
24 * part of TLS. They are called by the handshake functions (gnutls_handshake)
27 #include "gnutls_int.h"
28 #include "gnutls_handshake.h"
29 #include "gnutls_kx.h"
30 #include "gnutls_dh.h"
31 #include "gnutls_errors.h"
32 #include "algorithms.h"
34 #include "gnutls_mpi.h"
35 #include <gnutls_state.h>
36 #include <gnutls_datum.h>
37 #include <gnutls_rsa_export.h>
38 #include <gnutls_mbuffers.h>
40 /* This is a temporary function to be used before the generate_*
41 internal API is changed to use mbuffers. For now we don't avoid the
42 extra alloc + memcpy. */
44 send_handshake (gnutls_session_t session
, uint8_t * data
, size_t size
,
45 gnutls_handshake_description_t type
)
49 if (data
== NULL
&& size
== 0)
50 return _gnutls_send_handshake (session
, NULL
, type
);
52 if (data
== NULL
&& size
> 0)
55 return GNUTLS_E_INVALID_REQUEST
;
58 bufel
= _gnutls_handshake_alloc(session
, size
, size
);
62 return GNUTLS_E_MEMORY_ERROR
;
65 _mbuffer_set_udata (bufel
, data
, size
);
67 return _gnutls_send_handshake (session
, bufel
, type
);
71 /* This file contains important thing for the TLS handshake procedure.
74 #define MASTER_SECRET "master secret"
75 #define MASTER_SECRET_SIZE (sizeof(MASTER_SECRET)-1)
76 static int generate_normal_master (gnutls_session_t session
, gnutls_datum_t
*, int);
79 _gnutls_generate_master (gnutls_session_t session
, int keep_premaster
)
81 if (session
->internals
.resumed
== RESUME_FALSE
)
82 return generate_normal_master (session
, &session
->key
.key
, keep_premaster
);
83 else if (session
->internals
.premaster_set
)
85 gnutls_datum_t premaster
;
86 premaster
.size
= sizeof(session
->internals
.resumed_security_parameters
.master_secret
);
87 premaster
.data
= session
->internals
.resumed_security_parameters
.master_secret
;
88 return generate_normal_master(session
, &premaster
, 1);
93 /* here we generate the TLS Master secret.
96 generate_normal_master (gnutls_session_t session
, gnutls_datum_t
*premaster
,
102 _gnutls_hard_log ("INT: PREMASTER SECRET[%d]: %s\n", premaster
->size
,
103 _gnutls_bin2hex (premaster
->data
, premaster
->size
, buf
,
104 sizeof (buf
), NULL
));
105 _gnutls_hard_log ("INT: CLIENT RANDOM[%d]: %s\n", 32,
106 _gnutls_bin2hex (session
->
107 security_parameters
.client_random
, 32,
108 buf
, sizeof (buf
), NULL
));
109 _gnutls_hard_log ("INT: SERVER RANDOM[%d]: %s\n", 32,
110 _gnutls_bin2hex (session
->
111 security_parameters
.server_random
, 32,
112 buf
, sizeof (buf
), NULL
));
114 if (gnutls_protocol_get_version (session
) == GNUTLS_SSL3
)
116 uint8_t rnd
[2 * GNUTLS_RANDOM_SIZE
+ 1];
118 memcpy (rnd
, session
->security_parameters
.client_random
,
120 memcpy (&rnd
[GNUTLS_RANDOM_SIZE
],
121 session
->security_parameters
.server_random
, GNUTLS_RANDOM_SIZE
);
124 _gnutls_ssl3_generate_random (premaster
->data
, premaster
->size
,
125 rnd
, 2 * GNUTLS_RANDOM_SIZE
,
128 security_parameters
.master_secret
);
133 uint8_t rnd
[2 * GNUTLS_RANDOM_SIZE
+ 1];
135 memcpy (rnd
, session
->security_parameters
.client_random
,
137 memcpy (&rnd
[GNUTLS_RANDOM_SIZE
],
138 session
->security_parameters
.server_random
, GNUTLS_RANDOM_SIZE
);
141 _gnutls_PRF (session
, premaster
->data
, premaster
->size
,
142 MASTER_SECRET
, MASTER_SECRET_SIZE
,
143 rnd
, 2 * GNUTLS_RANDOM_SIZE
, GNUTLS_MASTER_SIZE
,
144 session
->security_parameters
.master_secret
);
148 _gnutls_free_datum (premaster
);
153 _gnutls_hard_log ("INT: MASTER SECRET: %s\n",
154 _gnutls_bin2hex (session
->
155 security_parameters
.master_secret
,
156 GNUTLS_MASTER_SIZE
, buf
, sizeof (buf
),
163 /* This is called when we want to receive the key exchange message of the
164 * server. It does nothing if this type of message is not required
165 * by the selected ciphersuite.
168 _gnutls_send_server_kx_message (gnutls_session_t session
, int again
)
170 gnutls_buffer_st data
;
173 if (session
->internals
.auth_struct
->gnutls_generate_server_kx
== NULL
)
176 _gnutls_buffer_init( &data
);
181 session
->internals
.auth_struct
->gnutls_generate_server_kx (session
,
184 if (ret
== GNUTLS_E_INT_RET_0
)
198 ret
= send_handshake (session
, data
.data
, data
.length
,
199 GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE
);
206 _gnutls_buffer_clear (&data
);
210 /* This function sends a certificate request message to the
214 _gnutls_send_server_crt_request (gnutls_session_t session
, int again
)
216 gnutls_buffer_st data
;
219 if (session
->internals
.
220 auth_struct
->gnutls_generate_server_crt_request
== NULL
)
223 if (session
->internals
.send_cert_req
<= 0)
226 _gnutls_buffer_init( &data
);
232 auth_struct
->gnutls_generate_server_crt_request (session
,
242 ret
= send_handshake (session
, data
.data
, data
.length
,
243 GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST
);
250 _gnutls_buffer_clear (&data
);
255 /* This is the function for the client to send the key
259 _gnutls_send_client_kx_message (gnutls_session_t session
, int again
)
261 gnutls_buffer_st data
;
264 if (session
->internals
.auth_struct
->gnutls_generate_client_kx
== NULL
)
267 _gnutls_buffer_init( &data
);
272 session
->internals
.auth_struct
->gnutls_generate_client_kx (session
,
280 ret
= send_handshake (session
, data
.data
, data
.length
,
281 GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE
);
288 _gnutls_buffer_clear (&data
);
293 /* This is the function for the client to send the certificate
297 _gnutls_send_client_certificate_verify (gnutls_session_t session
, int again
)
299 gnutls_buffer_st data
;
302 /* This is a packet that is only sent by the client
304 if (session
->security_parameters
.entity
== GNUTLS_SERVER
)
307 /* if certificate verify is not needed just exit
309 if (session
->key
.crt_requested
== 0)
313 if (session
->internals
.auth_struct
->gnutls_generate_client_crt_vrfy
==
317 return 0; /* this algorithm does not support cli_crt_vrfy
321 _gnutls_buffer_init( &data
);
327 auth_struct
->gnutls_generate_client_crt_vrfy (session
, &data
);
338 ret
= send_handshake (session
, data
.data
, data
.length
,
339 GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY
);
347 _gnutls_buffer_clear (&data
);
351 /* This is called when we want send our certificate
354 _gnutls_send_client_certificate (gnutls_session_t session
, int again
)
356 gnutls_buffer_st data
;
360 if (session
->key
.crt_requested
== 0)
363 if (session
->internals
.auth_struct
->gnutls_generate_client_certificate
==
367 _gnutls_buffer_init( &data
);
371 if (gnutls_protocol_get_version (session
) != GNUTLS_SSL3
||
372 session
->internals
.selected_cert_list_length
> 0)
374 /* TLS 1.0 or SSL 3.0 with a valid certificate
378 auth_struct
->gnutls_generate_client_certificate (session
, &data
);
388 /* In the SSL 3.0 protocol we need to send a
389 * no certificate alert instead of an
392 if (gnutls_protocol_get_version (session
) == GNUTLS_SSL3
&&
393 session
->internals
.selected_cert_list_length
== 0)
396 gnutls_alert_send (session
, GNUTLS_AL_WARNING
,
397 GNUTLS_A_SSL3_NO_CERTIFICATE
);
401 { /* TLS 1.0 or SSL 3.0 with a valid certificate
403 ret
= send_handshake (session
, data
.data
, data
.length
,
404 GNUTLS_HANDSHAKE_CERTIFICATE_PKT
);
408 _gnutls_buffer_clear (&data
);
413 /* This is called when we want send our certificate
416 _gnutls_send_server_certificate (gnutls_session_t session
, int again
)
418 gnutls_buffer_st data
;
422 if (session
->internals
.auth_struct
->gnutls_generate_server_certificate
==
426 _gnutls_buffer_init( &data
);
432 auth_struct
->gnutls_generate_server_certificate (session
, &data
);
440 ret
= send_handshake (session
, data
.data
, data
.length
,
441 GNUTLS_HANDSHAKE_CERTIFICATE_PKT
);
448 _gnutls_buffer_clear (&data
);
454 _gnutls_recv_server_kx_message (gnutls_session_t session
)
456 gnutls_buffer_st buf
;
458 unsigned int optflag
= 0;
460 if (session
->internals
.auth_struct
->gnutls_process_server_kx
!= NULL
)
463 /* EXCEPTION FOR RSA_EXPORT cipher suite
465 if (_gnutls_session_is_export (session
) != 0 &&
466 _gnutls_peers_cert_less_512 (session
) != 0)
472 /* Server key exchange packet is optional for PSK. */
473 if (_gnutls_session_is_psk (session
))
477 _gnutls_recv_handshake (session
,
478 GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE
,
487 session
->internals
.auth_struct
->gnutls_process_server_kx (session
,
490 _gnutls_buffer_clear(&buf
);
503 _gnutls_recv_server_crt_request (gnutls_session_t session
)
505 gnutls_buffer_st buf
;
508 if (session
->internals
.
509 auth_struct
->gnutls_process_server_crt_request
!= NULL
)
513 _gnutls_recv_handshake (session
,
514 GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST
,
519 if (ret
== 0 && buf
.length
== 0)
521 _gnutls_buffer_clear(&buf
);
522 return 0; /* ignored */
527 auth_struct
->gnutls_process_server_crt_request (session
, buf
.data
,
529 _gnutls_buffer_clear (&buf
);
538 _gnutls_recv_client_kx_message (gnutls_session_t session
)
540 gnutls_buffer_st buf
;
544 /* Do key exchange only if the algorithm permits it */
545 if (session
->internals
.auth_struct
->gnutls_process_client_kx
!= NULL
)
549 _gnutls_recv_handshake (session
,
550 GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE
,
556 session
->internals
.auth_struct
->gnutls_process_client_kx (session
,
559 _gnutls_buffer_clear (&buf
);
570 _gnutls_recv_client_certificate (gnutls_session_t session
)
572 gnutls_buffer_st buf
;
576 if (session
->internals
.auth_struct
->gnutls_process_client_certificate
==
580 /* if we have not requested a certificate then just return
582 if (session
->internals
.send_cert_req
== 0)
587 if (session
->internals
.send_cert_req
== GNUTLS_CERT_REQUIRE
)
593 _gnutls_recv_handshake (session
, GNUTLS_HANDSHAKE_CERTIFICATE_PKT
,
598 /* Handle the case of old SSL3 clients who send
599 * a warning alert instead of an empty certificate to indicate
603 ret
== GNUTLS_E_WARNING_ALERT_RECEIVED
&&
604 gnutls_protocol_get_version (session
) == GNUTLS_SSL3
&&
605 gnutls_alert_get (session
) == GNUTLS_A_SSL3_NO_CERTIFICATE
)
608 /* SSL3 does not send an empty certificate,
609 * but this alert. So we just ignore it.
615 /* certificate was required
617 if ((ret
== GNUTLS_E_WARNING_ALERT_RECEIVED
618 || ret
== GNUTLS_E_FATAL_ALERT_RECEIVED
)
622 return GNUTLS_E_NO_CERTIFICATE_FOUND
;
628 if (ret
== 0 && buf
.length
== 0 && optional
!= 0)
630 /* Client has not sent the certificate message.
631 * well I'm not sure we should accept this
640 auth_struct
->gnutls_process_client_certificate (session
, buf
.data
,
643 if (ret
< 0 && ret
!= GNUTLS_E_NO_CERTIFICATE_FOUND
)
649 /* ok we should expect a certificate verify message now
651 if (ret
== GNUTLS_E_NO_CERTIFICATE_FOUND
&& optional
!= 0)
654 session
->key
.crt_requested
= 1;
657 _gnutls_buffer_clear(&buf
);
662 _gnutls_recv_server_certificate (gnutls_session_t session
)
664 gnutls_buffer_st buf
;
667 if (session
->internals
.auth_struct
->gnutls_process_server_certificate
!=
672 _gnutls_recv_handshake (session
,
673 GNUTLS_HANDSHAKE_CERTIFICATE_PKT
,
683 auth_struct
->gnutls_process_server_certificate (session
, buf
.data
,
685 _gnutls_buffer_clear(&buf
);
697 /* Recv the client certificate verify. This packet may not
698 * arrive if the peer did not send us a certificate.
701 _gnutls_recv_client_certificate_verify_message (gnutls_session_t session
)
703 gnutls_buffer_st buf
;
707 if (session
->internals
.auth_struct
->gnutls_process_client_crt_vrfy
== NULL
)
710 if (session
->internals
.send_cert_req
== 0 ||
711 session
->key
.crt_requested
== 0)
717 _gnutls_recv_handshake (session
,
718 GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY
,
723 if (ret
== 0 && buf
.length
== 0
724 && session
->internals
.send_cert_req
== GNUTLS_CERT_REQUIRE
)
726 /* certificate was required */
728 ret
= GNUTLS_E_NO_CERTIFICATE_FOUND
;
734 auth_struct
->gnutls_process_client_crt_vrfy (session
, buf
.data
,
738 _gnutls_buffer_clear(&buf
);