2 * Copyright (C) 2000, 2001, 2004, 2005, 2006, 2008 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 /* This file contains functions which are wrappers for the key exchange
26 * part of TLS. They are called by the handshake functions (gnutls_handshake)
29 #include "gnutls_int.h"
30 #include "gnutls_handshake.h"
31 #include "gnutls_kx.h"
32 #include "gnutls_dh.h"
33 #include "gnutls_errors.h"
34 #include "gnutls_algorithms.h"
36 #include "gnutls_mpi.h"
37 #include <gnutls_state.h>
38 #include <gnutls_datum.h>
39 #include <gnutls_rsa_export.h>
41 /* This file contains important thing for the TLS handshake procedure.
44 #define MASTER_SECRET "master secret"
45 static int generate_normal_master (gnutls_session_t session
, int);
48 _gnutls_generate_master (gnutls_session_t session
, int keep_premaster
)
50 if (session
->internals
.resumed
== RESUME_FALSE
)
51 return generate_normal_master (session
, keep_premaster
);
55 /* here we generate the TLS Master secret.
57 #define PREMASTER session->key->key
59 generate_normal_master (gnutls_session_t session
, int keep_premaster
)
64 _gnutls_hard_log ("INT: PREMASTER SECRET[%d]: %s\n", PREMASTER
.size
,
65 _gnutls_bin2hex (PREMASTER
.data
, PREMASTER
.size
, buf
,
67 _gnutls_hard_log ("INT: CLIENT RANDOM[%d]: %s\n", 32,
68 _gnutls_bin2hex (session
->security_parameters
.
69 client_random
, 32, buf
, sizeof (buf
)));
70 _gnutls_hard_log ("INT: SERVER RANDOM[%d]: %s\n", 32,
71 _gnutls_bin2hex (session
->security_parameters
.
72 server_random
, 32, buf
, sizeof (buf
)));
74 if (gnutls_protocol_get_version (session
) == GNUTLS_SSL3
)
76 opaque rnd
[2 * TLS_RANDOM_SIZE
+ 1];
78 memcpy (rnd
, session
->security_parameters
.client_random
, TLS_RANDOM_SIZE
);
79 memcpy (&rnd
[TLS_RANDOM_SIZE
],
80 session
->security_parameters
.server_random
, TLS_RANDOM_SIZE
);
83 _gnutls_ssl3_generate_random (PREMASTER
.data
, PREMASTER
.size
,
84 rnd
, 2 * TLS_RANDOM_SIZE
,
86 session
->security_parameters
.
90 else if (session
->security_parameters
.extensions
.oprfi_client_len
> 0 &&
91 session
->security_parameters
.extensions
.oprfi_server_len
> 0)
94 size_t rndlen
= 2 * TLS_RANDOM_SIZE
;
96 rndlen
+= session
->security_parameters
.extensions
.oprfi_client_len
;
97 rndlen
+= session
->security_parameters
.extensions
.oprfi_server_len
;
99 rnd
= gnutls_malloc (rndlen
+ 1);
103 return GNUTLS_E_MEMORY_ERROR
;
106 _gnutls_hard_log ("INT: CLIENT OPRFI[%d]: %s\n",
107 session
->security_parameters
.
108 extensions
.oprfi_server_len
,
109 _gnutls_bin2hex (session
->security_parameters
.
110 extensions
.oprfi_client
,
111 session
->security_parameters
.
112 extensions
.oprfi_client_len
,
114 _gnutls_hard_log ("INT: SERVER OPRFI[%d]: %s\n",
115 session
->security_parameters
.
116 extensions
.oprfi_server_len
,
117 _gnutls_bin2hex (session
->security_parameters
.
118 extensions
.oprfi_server
,
119 session
->security_parameters
.
120 extensions
.oprfi_server_len
,
123 memcpy (rnd
, session
->security_parameters
.client_random
,
125 memcpy (rnd
+ TLS_RANDOM_SIZE
,
126 session
->security_parameters
.extensions
.oprfi_client
,
127 session
->security_parameters
.extensions
.oprfi_client_len
);
128 memcpy (rnd
+ TLS_RANDOM_SIZE
+
129 session
->security_parameters
.extensions
.oprfi_client_len
,
130 session
->security_parameters
.server_random
,
132 memcpy (rnd
+ TLS_RANDOM_SIZE
+
133 session
->security_parameters
.extensions
.oprfi_client_len
+
135 session
->security_parameters
.extensions
.oprfi_server
,
136 session
->security_parameters
.extensions
.oprfi_server_len
);
138 ret
= _gnutls_PRF (session
, PREMASTER
.data
, PREMASTER
.size
,
139 MASTER_SECRET
, strlen (MASTER_SECRET
),
140 rnd
, rndlen
, TLS_MASTER_SIZE
,
141 session
->security_parameters
.master_secret
);
147 opaque rnd
[2 * TLS_RANDOM_SIZE
+ 1];
149 memcpy (rnd
, session
->security_parameters
.client_random
, TLS_RANDOM_SIZE
);
150 memcpy (&rnd
[TLS_RANDOM_SIZE
],
151 session
->security_parameters
.server_random
, TLS_RANDOM_SIZE
);
154 _gnutls_PRF (session
, PREMASTER
.data
, PREMASTER
.size
,
155 MASTER_SECRET
, strlen (MASTER_SECRET
),
156 rnd
, 2 * TLS_RANDOM_SIZE
, TLS_MASTER_SIZE
,
157 session
->security_parameters
.master_secret
);
160 /* TLS/IA inner secret is derived from the master secret. */
161 memcpy (session
->security_parameters
.inner_secret
,
162 session
->security_parameters
.master_secret
, TLS_MASTER_SIZE
);
165 _gnutls_free_datum (&PREMASTER
);
170 _gnutls_hard_log ("INT: MASTER SECRET: %s\n",
171 _gnutls_bin2hex (session
->security_parameters
.
172 master_secret
, TLS_MASTER_SIZE
, buf
,
179 /* This is called when we want to receive the key exchange message of the
180 * server. It does nothing if this type of message is not required
181 * by the selected ciphersuite.
184 _gnutls_send_server_kx_message (gnutls_session_t session
, int again
)
186 uint8_t *data
= NULL
;
190 if (session
->internals
.auth_struct
->gnutls_generate_server_kx
== NULL
)
199 session
->internals
.auth_struct
->
200 gnutls_generate_server_kx (session
, &data
);
202 if (data_size
== GNUTLS_E_INT_RET_0
)
216 _gnutls_send_handshake (session
, data
, data_size
,
217 GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE
);
228 /* This function sends a certificate request message to the
232 _gnutls_send_server_certificate_request (gnutls_session_t session
, int again
)
234 uint8_t *data
= NULL
;
238 if (session
->internals
.auth_struct
->
239 gnutls_generate_server_certificate_request
== NULL
)
242 if (session
->internals
.send_cert_req
<= 0)
251 session
->internals
.auth_struct
->
252 gnutls_generate_server_certificate_request (session
, &data
);
261 _gnutls_send_handshake (session
, data
, data_size
,
262 GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST
);
274 /* This is the function for the client to send the key
278 _gnutls_send_client_kx_message (gnutls_session_t session
, int again
)
284 if (session
->internals
.auth_struct
->gnutls_generate_client_kx
== NULL
)
294 session
->internals
.auth_struct
->
295 gnutls_generate_client_kx (session
, &data
);
303 _gnutls_send_handshake (session
, data
, data_size
,
304 GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE
);
317 /* This is the function for the client to send the certificate
321 _gnutls_send_client_certificate_verify (gnutls_session_t session
, int again
)
327 /* This is a packet that is only sent by the client
329 if (session
->security_parameters
.entity
== GNUTLS_SERVER
)
332 /* if certificate verify is not needed just exit
334 if (session
->key
->certificate_requested
== 0)
337 if (session
->internals
.auth_struct
->gnutls_generate_client_cert_vrfy
==
341 return 0; /* this algorithm does not support cli_cert_vrfy
351 session
->internals
.auth_struct
->
352 gnutls_generate_client_cert_vrfy (session
, &data
);
363 _gnutls_send_handshake (session
, data
,
364 data_size
, GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY
);
372 _gnutls_recv_server_kx_message (gnutls_session_t session
)
374 uint8_t *data
= NULL
;
377 Optional optflag
= MANDATORY_PACKET
;
379 if (session
->internals
.auth_struct
->gnutls_process_server_kx
!= NULL
)
382 /* EXCEPTION FOR RSA_EXPORT cipher suite
384 if (_gnutls_session_is_export (session
) != 0 &&
385 _gnutls_peers_cert_less_512 (session
) != 0)
391 /* Server key exchange packet is optional for PSK. */
392 if (_gnutls_session_is_psk (session
))
393 optflag
= OPTIONAL_PACKET
;
396 _gnutls_recv_handshake (session
, &data
,
398 GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE
,
407 session
->internals
.auth_struct
->
408 gnutls_process_server_kx (session
, data
, datasize
);
422 _gnutls_recv_server_certificate_request (gnutls_session_t session
)
428 if (session
->internals
.auth_struct
->
429 gnutls_process_server_certificate_request
!= NULL
)
433 _gnutls_recv_handshake (session
, &data
,
435 GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST
,
440 if (ret
== 0 && datasize
== 0)
441 return 0; /* ignored */
444 session
->internals
.auth_struct
->
445 gnutls_process_server_certificate_request (session
, data
, datasize
);
455 _gnutls_recv_client_kx_message (gnutls_session_t session
)
462 /* Do key exchange only if the algorithm permits it */
463 if (session
->internals
.auth_struct
->gnutls_process_client_kx
!= NULL
)
467 _gnutls_recv_handshake (session
, &data
,
469 GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE
,
475 session
->internals
.auth_struct
->
476 gnutls_process_client_kx (session
, data
, datasize
);
487 /* This is called when we want send our certificate
490 _gnutls_send_client_certificate (gnutls_session_t session
, int again
)
492 uint8_t *data
= NULL
;
497 if (session
->key
->certificate_requested
== 0)
500 if (session
->internals
.auth_struct
->
501 gnutls_generate_client_certificate
== NULL
)
509 if (gnutls_protocol_get_version (session
) != GNUTLS_SSL3
||
510 session
->internals
.selected_cert_list_length
> 0)
512 /* TLS 1.0 or SSL 3.0 with a valid certificate
515 session
->internals
.auth_struct
->
516 gnutls_generate_client_certificate (session
, &data
);
526 /* In the SSL 3.0 protocol we need to send a
527 * no certificate alert instead of an
530 if (gnutls_protocol_get_version (session
) == GNUTLS_SSL3
&&
531 session
->internals
.selected_cert_list_length
== 0)
534 gnutls_alert_send (session
, GNUTLS_AL_WARNING
,
535 GNUTLS_A_SSL3_NO_CERTIFICATE
);
539 { /* TLS 1.0 or SSL 3.0 with a valid certificate
542 _gnutls_send_handshake (session
, data
, data_size
,
543 GNUTLS_HANDSHAKE_CERTIFICATE_PKT
);
557 /* This is called when we want send our certificate
560 _gnutls_send_server_certificate (gnutls_session_t session
, int again
)
562 uint8_t *data
= NULL
;
567 if (session
->internals
.auth_struct
->
568 gnutls_generate_server_certificate
== NULL
)
577 session
->internals
.auth_struct
->
578 gnutls_generate_server_certificate (session
, &data
);
587 _gnutls_send_handshake (session
, data
, data_size
,
588 GNUTLS_HANDSHAKE_CERTIFICATE_PKT
);
602 _gnutls_recv_client_certificate (gnutls_session_t session
)
609 if (session
->internals
.auth_struct
->
610 gnutls_process_client_certificate
!= NULL
)
613 /* if we have not requested a certificate then just return
615 if (session
->internals
.send_cert_req
== 0)
620 if (session
->internals
.send_cert_req
== GNUTLS_CERT_REQUIRE
)
621 optional
= MANDATORY_PACKET
;
623 optional
= OPTIONAL_PACKET
;
626 _gnutls_recv_handshake (session
, &data
,
628 GNUTLS_HANDSHAKE_CERTIFICATE_PKT
, optional
);
632 /* Handle the case of old SSL3 clients who send
633 * a warning alert instead of an empty certificate to indicate
636 if (optional
== OPTIONAL_PACKET
&&
637 ret
== GNUTLS_E_WARNING_ALERT_RECEIVED
&&
638 gnutls_protocol_get_version (session
) == GNUTLS_SSL3
&&
639 gnutls_alert_get (session
) == GNUTLS_A_SSL3_NO_CERTIFICATE
)
642 /* SSL3 does not send an empty certificate,
643 * but this alert. So we just ignore it.
649 /* certificate was required
651 if ((ret
== GNUTLS_E_WARNING_ALERT_RECEIVED
652 || ret
== GNUTLS_E_FATAL_ALERT_RECEIVED
)
653 && optional
== MANDATORY_PACKET
)
656 return GNUTLS_E_NO_CERTIFICATE_FOUND
;
662 if (ret
== 0 && datasize
== 0 && optional
== OPTIONAL_PACKET
)
664 /* Client has not sent the certificate message.
665 * well I'm not sure we should accept this
672 session
->internals
.auth_struct
->
673 gnutls_process_client_certificate (session
, data
, datasize
);
676 if (ret
< 0 && ret
!= GNUTLS_E_NO_CERTIFICATE_FOUND
)
682 /* ok we should expect a certificate verify message now
684 if (ret
== GNUTLS_E_NO_CERTIFICATE_FOUND
&& optional
== OPTIONAL_PACKET
)
687 session
->key
->certificate_requested
= 1;
695 _gnutls_recv_server_certificate (gnutls_session_t session
)
701 if (session
->internals
.auth_struct
->
702 gnutls_process_server_certificate
!= NULL
)
706 _gnutls_recv_handshake (session
, &data
,
708 GNUTLS_HANDSHAKE_CERTIFICATE_PKT
,
717 session
->internals
.auth_struct
->
718 gnutls_process_server_certificate (session
, data
, datasize
);
731 /* Recv the client certificate verify. This packet may not
732 * arrive if the peer did not send us a certificate.
735 _gnutls_recv_client_certificate_verify_message (gnutls_session_t session
)
742 if (session
->internals
.auth_struct
->gnutls_process_client_cert_vrfy
!= NULL
)
745 if (session
->internals
.send_cert_req
== 0 ||
746 session
->key
->certificate_requested
== 0)
752 _gnutls_recv_handshake (session
, &data
,
754 GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY
,
759 if (ret
== 0 && datasize
== 0
760 && session
->internals
.send_cert_req
== GNUTLS_CERT_REQUIRE
)
762 /* certificate was required */
764 return GNUTLS_E_NO_CERTIFICATE_FOUND
;
768 session
->internals
.auth_struct
->
769 gnutls_process_client_cert_vrfy (session
, data
, datasize
);