blob: 4fab4ed165a103cdca4aa77f7484c6b6c6f7c033 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
SimonBd5800b72016-04-26 07:43:27 +010030#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
34#define mbedtls_calloc calloc
35#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010036#endif
37
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/debug.h"
39#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020040#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia84914062018-04-24 08:40:46 -050041#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000042
43#include <string.h>
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010050#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
54int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020055 const unsigned char *info,
56 size_t ilen )
57{
Hanno Becker2d9623f2019-06-13 12:07:05 +010058 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020062
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020063 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020064 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020065
66 memcpy( ssl->cli_id, info, ilen );
67 ssl->cli_id_len = ilen;
68
69 return( 0 );
70}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020071
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020072void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_ssl_cookie_write_t *f_cookie_write,
74 mbedtls_ssl_cookie_check_t *f_cookie_check,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020075 void *p_cookie )
76{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020077 conf->f_cookie_write = f_cookie_write;
78 conf->f_cookie_check = f_cookie_check;
79 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +000085 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +000086 size_t len )
87{
88 int ret;
89 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000090 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010093
Philippe Antoine747fd532018-05-30 09:13:21 +020094 if( len < 2 )
95 {
96 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
97 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
98 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
99 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
100 }
Paul Bakker5701cdc2012-09-27 21:49:42 +0000101 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
102 if( servername_list_size + 2 != len )
103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200105 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
106 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000108 }
109
110 p = buf + 2;
Philippe Antoine747fd532018-05-30 09:13:21 +0200111 while( servername_list_size > 2 )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000112 {
113 hostname_len = ( ( p[1] << 8 ) | p[2] );
114 if( hostname_len + 3 > servername_list_size )
115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200117 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
118 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000120 }
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000123 {
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200124 ret = ssl->conf->f_sni( ssl->conf->p_sni,
125 ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000126 if( ret != 0 )
127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
129 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
130 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
131 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000132 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000133 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000134 }
135
136 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000137 p += hostname_len + 3;
138 }
139
140 if( servername_list_size != 0 )
141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200143 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
144 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000146 }
147
148 return( 0 );
149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000153 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000154 size_t len )
155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_SSL_RENEGOTIATION)
157 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100158 {
159 /* Check verify-data in constant-time. The length OTOH is no secret */
160 if( len != 1 + ssl->verify_data_len ||
161 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data,
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100163 ssl->verify_data_len ) != 0 )
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200166 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
167 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100169 }
170 }
171 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000173 {
174 if( len != 1 || buf[0] != 0x0 )
175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200177 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
178 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +0000180 }
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000183 }
Paul Bakker48916f92012-09-16 19:57:18 +0000184
185 return( 0 );
186}
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
189 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100190
191/*
192 * Status of the implementation of signature-algorithms extension:
193 *
194 * Currently, we are only considering the signature-algorithm extension
195 * to pick a ciphersuite which allows us to send the ServerKeyExchange
196 * message with a signature-hash combination that the user allows.
197 *
198 * We do *not* check whether all certificates in our certificate
199 * chain are signed with an allowed signature-hash pair.
200 * This needs to be done at a later stage.
201 *
202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000204 const unsigned char *buf,
205 size_t len )
206{
207 size_t sig_alg_list_size;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100208
Paul Bakker23f36802012-09-28 14:15:14 +0000209 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200210 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200211
Hanno Becker7e5437a2017-04-28 17:15:26 +0100212 mbedtls_md_type_t md_cur;
213 mbedtls_pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000214
Philippe Antoine747fd532018-05-30 09:13:21 +0200215 if ( len < 2 ) {
216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
217 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
218 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
219 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
220 }
Paul Bakker23f36802012-09-28 14:15:14 +0000221 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
222 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200223 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200226 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
227 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker23f36802012-09-28 14:15:14 +0000229 }
230
Hanno Becker7e5437a2017-04-28 17:15:26 +0100231 /* Currently we only guarantee signing the ServerKeyExchange message according
232 * to the constraints specified in this extension (see above), so it suffices
233 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200234 *
Hanno Becker7e5437a2017-04-28 17:15:26 +0100235 * This will change when we also consider certificate signatures,
236 * in which case we will need to remember the whole signature-hash
237 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200238 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100239
240 for( p = buf + 2; p < end; p += 2 )
241 {
242 /* Silently ignore unknown signature or hash algorithms. */
243
244 if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
245 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
247 " unknown sig alg encoding %d", p[1] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100248 continue;
249 }
250
251 /* Check if we support the hash the user proposes */
252 md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
253 if( md_cur == MBEDTLS_MD_NONE )
254 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
256 " unknown hash alg encoding %d", p[0] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100257 continue;
258 }
259
260 if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
261 {
262 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
264 " match sig %d and hash %d",
Hanno Becker7e5437a2017-04-28 17:15:26 +0100265 sig_cur, md_cur ) );
266 }
267 else
268 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
270 "hash alg %d not supported", md_cur ) );
Paul Bakker23f36802012-09-28 14:15:14 +0000271 }
Paul Bakker23f36802012-09-28 14:15:14 +0000272 }
273
Paul Bakker23f36802012-09-28 14:15:14 +0000274 return( 0 );
275}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
277 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000278
Robert Cragie136884c2015-10-02 13:34:31 +0100279#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100280 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100282 const unsigned char *buf, size_t len,
283 mbedtls_ecp_group_id curve_ids[ MBEDTLS_ECP_DP_MAX ] )
Paul Bakker41c83d32013-03-20 14:39:14 +0100284{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200285 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100286 const unsigned char *p;
287
Philippe Antoine747fd532018-05-30 09:13:21 +0200288 if ( len < 2 ) {
289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
290 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
291 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
292 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
293 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100294 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
295 if( list_size + 2 != len ||
296 list_size % 2 != 0 )
297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200299 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
300 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100302 }
303
304 p = buf + 2;
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100305 our_size = MBEDTLS_ECP_DP_MAX;
306
307 /* Leave room for final 0-entry */
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200308 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100309 {
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100310 uint16_t const tls_id = ( p[0] << 8 ) | p[1];
311 mbedtls_ecp_curve_info const * const info =
312 mbedtls_ecp_curve_info_from_tls_id( tls_id );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200313
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100314 if( info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100315 {
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100316 mbedtls_ecp_group_id const *gid;
317 /* Remember the first curve that we also support. */
318 for( gid = ssl->conf->curve_list;
319 *gid != MBEDTLS_ECP_DP_NONE; gid++ )
320 {
321 if( info->grp_id != *gid )
322 continue;
323
324 if( ssl->handshake->curve_info == NULL )
325 ssl->handshake->curve_info = info;
326 }
327
328 *curve_ids++ = info->grp_id;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200329 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100330 }
331
332 list_size -= 2;
333 p += 2;
334 }
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100335 *curve_ids = MBEDTLS_ECP_DP_NONE;
Paul Bakker41c83d32013-03-20 14:39:14 +0100336
337 return( 0 );
338}
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200341 const unsigned char *buf,
342 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100343{
344 size_t list_size;
345 const unsigned char *p;
346
Philippe Antoine747fd532018-05-30 09:13:21 +0200347 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200350 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
351 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100353 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200354 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100355
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200356 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100357 while( list_size > 0 )
358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
360 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Paul Bakker41c83d32013-03-20 14:39:14 +0100361 {
Robert Cragie136884c2015-10-02 13:34:31 +0100362#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200363 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100364#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100365#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Robert Cragie136884c2015-10-02 13:34:31 +0100366 ssl->handshake->ecjpake_ctx.point_format = p[0];
367#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100369 return( 0 );
370 }
371
372 list_size--;
373 p++;
374 }
375
376 return( 0 );
377}
Robert Cragieae8535d2015-10-06 17:11:18 +0100378#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
379 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100380
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200381#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
382static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
383 const unsigned char *buf,
384 size_t len )
385{
386 int ret;
387
388 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
389 {
390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
391 return( 0 );
392 }
393
394 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
395 buf, len ) ) != 0 )
396 {
397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200398 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
399 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200400 return( ret );
401 }
402
403 /* Only mark the extension as OK when we're sure it is */
404 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
405
406 return( 0 );
407}
408#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
411static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200412 const unsigned char *buf,
413 size_t len )
414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200418 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
419 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200421 }
422
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200423 ssl->session_negotiate->mfl_code = buf[0];
424
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200425 return( 0 );
426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200428
Hanno Beckera5a2b082019-05-15 14:03:01 +0100429#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc403b262019-04-26 13:56:39 +0100430static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
431 const unsigned char *buf,
432 size_t len )
433{
434 size_t peer_cid_len;
435
436 /* CID extension only makes sense in DTLS */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200437 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Beckerc403b262019-04-26 13:56:39 +0100438 {
439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
440 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
441 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
442 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
443 }
444
445 /*
Hanno Becker3cdf8fe2019-05-15 10:26:32 +0100446 * Quoting draft-ietf-tls-dtls-connection-id-05
447 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Beckerc403b262019-04-26 13:56:39 +0100448 *
449 * struct {
450 * opaque cid<0..2^8-1>;
451 * } ConnectionId;
452 */
453
454 if( len < 1 )
455 {
456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
457 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
458 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
459 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
460 }
461
462 peer_cid_len = *buf++;
463 len--;
464
465 if( len != peer_cid_len )
466 {
467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
468 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
469 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
470 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
471 }
472
473 /* Ignore CID if the user has disabled its use. */
474 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
475 {
476 /* Leave ssl->handshake->cid_in_use in its default
477 * value of MBEDTLS_SSL_CID_DISABLED. */
478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
479 return( 0 );
480 }
481
482 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
483 {
484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
485 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
486 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
487 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
488 }
489
Hanno Becker19976b52019-05-03 12:43:44 +0100490 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckerc403b262019-04-26 13:56:39 +0100491 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
492 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
493
494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
495 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
496
Hanno Beckerc403b262019-04-26 13:56:39 +0100497 return( 0 );
498}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100499#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc403b262019-04-26 13:56:39 +0100500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
502static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200503 const unsigned char *buf,
504 size_t len )
505{
506 if( len != 0 )
507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
510 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200512 }
513
514 ((void) buf);
515
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200516 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200518
519 return( 0 );
520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
524static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100525 const unsigned char *buf,
526 size_t len )
527{
528 if( len != 0 )
529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200531 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
532 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100534 }
535
536 ((void) buf);
537
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200538 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100542 }
543
544 return( 0 );
545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
549static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200550 const unsigned char *buf,
551 size_t len )
552{
553 if( len != 0 )
554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200556 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
557 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200559 }
560
561 ((void) buf);
562
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200563 return( 0 );
564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SSL_SESSION_TICKETS)
568static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200569 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200570 size_t len )
571{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200572 int ret;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200573 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200574
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200575 mbedtls_ssl_session_init( &session );
576
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200577 if( ssl->conf->f_ticket_parse == NULL ||
578 ssl->conf->f_ticket_write == NULL )
579 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200580 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200581 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200582
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200583 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200584 ssl->handshake->new_session_ticket = 1;
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200587
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200588 if( len == 0 )
589 return( 0 );
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_RENEGOTIATION)
592 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200595 return( 0 );
596 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200598
599 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200600 * Failures are ok: just ignore the ticket and proceed.
601 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200602 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
603 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200604 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200605 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200606
607 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
609 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
611 else
612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
613
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200614 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200615 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200616
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200617 /*
618 * Keep the session ID sent by the client, since we MUST send it back to
619 * inform them we're accepting the ticket (RFC 5077 section 3.4)
620 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200621 session.id_len = ssl->session_negotiate->id_len;
622 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200623
624 mbedtls_ssl_session_free( ssl->session_negotiate );
625 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
626
627 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500628 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200631
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200632 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200633
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200634 /* Don't send a new ticket after all, this one is OK */
635 ssl->handshake->new_session_ticket = 0;
636
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200637 return( 0 );
638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_SSL_ALPN)
642static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200643 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200644{
Paul Bakker14b16c62014-05-28 11:33:54 +0200645 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200646 const unsigned char *theirs, *start, *end;
647 const char **ours;
648
649 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200650 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200651 return( 0 );
652
653 /*
654 * opaque ProtocolName<1..2^8-1>;
655 *
656 * struct {
657 * ProtocolName protocol_name_list<2..2^16-1>
658 * } ProtocolNameList;
659 */
660
661 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
662 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200663 {
664 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
665 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200667 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200668
669 list_len = ( buf[0] << 8 ) | buf[1];
670 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200671 {
672 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
673 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200675 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200676
677 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100678 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200679 */
680 start = buf + 2;
681 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100682 for( theirs = start; theirs != end; theirs += cur_len )
683 {
684 cur_len = *theirs++;
685
686 /* Current identifier must fit in list */
687 if( cur_len > (size_t)( end - theirs ) )
688 {
689 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
690 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
691 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
692 }
693
694 /* Empty strings MUST NOT be included */
695 if( cur_len == 0 )
696 {
697 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
698 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
699 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
700 }
701 }
702
703 /*
704 * Use our order of preference
705 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200706 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200707 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200708 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200709 for( theirs = start; theirs != end; theirs += cur_len )
710 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200711 cur_len = *theirs++;
712
Paul Bakker14b16c62014-05-28 11:33:54 +0200713 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200714 memcmp( theirs, *ours, cur_len ) == 0 )
715 {
716 ssl->alpn_chosen = *ours;
717 return( 0 );
718 }
719 }
720 }
721
722 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
724 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
725 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200728
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100729/*
730 * Auxiliary functions for ServerHello parsing and related actions
731 */
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100734/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100735 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_ECDSA_C)
738static int ssl_check_key_curve( mbedtls_pk_context *pk,
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100739 mbedtls_ecp_group_id const *acceptable_ec_grp_ids )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100742
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100743 if( acceptable_ec_grp_ids == NULL )
744 return( -1 );
745
746 while( *acceptable_ec_grp_ids != MBEDTLS_ECP_DP_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100747 {
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100748 if( *acceptable_ec_grp_ids == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100749 return( 0 );
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100750 acceptable_ec_grp_ids++;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100751 }
752
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100753 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100756
757/*
758 * Try picking a certificate for this ciphersuite,
759 * return 0 on success and -1 on failure.
760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761static int ssl_pick_cert( mbedtls_ssl_context *ssl,
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100762 const mbedtls_ssl_ciphersuite_t * ciphersuite_info,
763 mbedtls_ecp_group_id const *acceptable_ec_grp_ids )
764
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100765{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100767 mbedtls_pk_type_t pk_alg =
768 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200769 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100772 if( ssl->handshake->sni_key_cert != NULL )
773 list = ssl->handshake->sni_key_cert;
774 else
775#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200776 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100779 return( 0 );
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000782
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200783 if( list == NULL )
784 {
785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
786 return( -1 );
787 }
788
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100789 for( cur = list; cur != NULL; cur = cur->next )
790 {
Hanno Becker30649f72019-02-26 16:49:40 +0000791 int match = 1;
792 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000793
Hanno Becker30649f72019-02-26 16:49:40 +0000794 /* WARNING: With the current X.509 caching architecture, this MUST
795 * happen outside of the PK acquire/release block, because it moves
796 * the cached PK context. In a threading-enabled build, this would
797 * rightfully fail, but lead to a use-after-free otherwise. */
798 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
799 cur->cert );
800
Hanno Becker74b89f62019-02-17 21:22:07 +0000801#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
802 /* ASYNC_PRIVATE may use a NULL entry for the opaque private key, so
803 * we have to use the public key context to infer the capabilities
804 * of the key. */
Hanno Becker30649f72019-02-26 16:49:40 +0000805 {
806 int ret;
807 ret = mbedtls_x509_crt_pk_acquire( cur->cert, &pk );
808 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +0100809 {
810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker30649f72019-02-26 16:49:40 +0000811 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +0100812 }
Hanno Becker30649f72019-02-26 16:49:40 +0000813 }
Hanno Becker74b89f62019-02-17 21:22:07 +0000814#else
815 /* Outside of ASYNC_PRIVATE, use private key context directly
816 * instead of querying for the public key context from the
817 * certificate, so save a few bytes of code. */
Hanno Becker30649f72019-02-26 16:49:40 +0000818 pk = cur->key;
Hanno Becker74b89f62019-02-17 21:22:07 +0000819#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100820
Hanno Becker74b89f62019-02-17 21:22:07 +0000821 if( ! mbedtls_pk_can_do( pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Hanno Becker30649f72019-02-26 16:49:40 +0000824 match = 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000825 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100826
Hanno Becker30649f72019-02-26 16:49:40 +0000827#if defined(MBEDTLS_ECDSA_C)
828 if( pk_alg == MBEDTLS_PK_ECDSA &&
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100829 ssl_check_key_curve( pk, acceptable_ec_grp_ids ) != 0 )
Hanno Becker30649f72019-02-26 16:49:40 +0000830 {
831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
832 match = 0;
833 }
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100834#else
835 ((void) acceptable_ec_grp_ids);
Hanno Becker30649f72019-02-26 16:49:40 +0000836#endif
837
838#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000839 mbedtls_x509_crt_pk_release( cur->cert );
Hanno Becker30649f72019-02-26 16:49:40 +0000840#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
841
842 if( match == 0 )
843 continue;
844
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200845 /*
846 * This avoids sending the client a cert it'll reject based on
847 * keyUsage or other extensions.
848 *
849 * It also allows the user to provision different certificates for
850 * different uses based on keyUsage, eg if they want to avoid signing
851 * and decrypting with the same RSA key.
852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100854 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000857 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200858 continue;
859 }
860
Hanno Becker828a8c02019-02-26 16:48:55 +0000861#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
862 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100863 /*
864 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
865 * present them a SHA-higher cert rather than failing if it's the only
866 * one we got that satisfies the other conditions.
867 */
Hanno Becker30649f72019-02-26 16:49:40 +0000868 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100869 {
Hanno Becker30649f72019-02-26 16:49:40 +0000870 mbedtls_md_type_t sig_md;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000871 {
Hanno Becker30649f72019-02-26 16:49:40 +0000872 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100873 mbedtls_x509_crt_frame const *frame;
Hanno Becker30649f72019-02-26 16:49:40 +0000874 ret = mbedtls_x509_crt_frame_acquire( cur->cert, &frame );
875 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +0100876 {
877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_frame_acquire", ret );
Hanno Becker30649f72019-02-26 16:49:40 +0000878 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +0100879 }
Hanno Becker30649f72019-02-26 16:49:40 +0000880 sig_md = frame->sig_md;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000881 mbedtls_x509_crt_frame_release( cur->cert );
Hanno Becker30649f72019-02-26 16:49:40 +0000882 }
883
884 if( sig_md != MBEDTLS_MD_SHA1 )
885 {
886 if( fallback == NULL )
887 fallback = cur;
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Hanno Becker30649f72019-02-26 16:49:40 +0000890 "sha-2 with pre-TLS 1.2 client" ) );
891 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000892 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100893 }
Hanno Becker828a8c02019-02-26 16:48:55 +0000894#endif /* MBEDTLS_SSL_PROTO_TLS1 ||
895 MBEDTLS_SSL_PROTO_TLS1_1 ||
896 MBEDTLS_SSL_PROTO_SSL3 */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100897
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100898 /* If we get there, we got a winner */
899 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100900 }
901
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000902 if( cur == NULL )
903 cur = fallback;
904
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200905 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100906 if( cur != NULL )
907 {
908 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000910 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100911 return( 0 );
912 }
913
914 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100917
918/*
919 * Check if a given ciphersuite is suitable for use with our config/keys/etc
920 * Sets ciphersuite_info only if the suite matches.
921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100923 const mbedtls_ssl_ciphersuite_t **ciphersuite_info,
924 mbedtls_ecp_group_id const *acceptable_ec_grp_ids )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100925{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100927
Hanno Becker7e5437a2017-04-28 17:15:26 +0100928#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100929 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100930 mbedtls_pk_type_t sig_type;
931#endif
932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100934 if( suite_info == NULL )
935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
937 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100938 }
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000941
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100942 if( suite_info->min_minor_ver > ssl->minor_ver ||
943 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100946 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000947 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200950 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100952 return( 0 );
953#endif
954
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +0200955#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200956 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100960 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000961 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +0200962#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100963
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +0200964#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
965 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200966 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +0200967 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
969 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +0200970 return( 0 );
971 }
972#endif
973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
975 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +0100976 ssl->handshake->curve_info == NULL )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000979 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100980 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000981 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100982#endif
983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100985 /* If the ciphersuite requires a pre-shared key and we don't
986 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200988 ssl->conf->f_psk == NULL &&
989 ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL ||
990 ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100993 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000994 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100995#endif
996
Hanno Becker7e5437a2017-04-28 17:15:26 +0100997#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
998 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
999 /* If the ciphersuite requires signing, check whether
1000 * a suitable hash algorithm is present. */
1001 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1002 {
1003 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1004 if( sig_type != MBEDTLS_PK_NONE &&
1005 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1006 {
1007 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1008 "for signature algorithm %d", sig_type ) );
1009 return( 0 );
1010 }
1011 }
1012
1013#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
1014 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001017 /*
1018 * Final check: if ciphersuite requires us to have a
1019 * certificate/key of a particular type:
1020 * - select the appropriate certificate if we have one, or
1021 * - try the next ciphersuite if we don't
1022 * This must be done last since we modify the key_cert list.
1023 */
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01001024 if( ssl_pick_cert( ssl, suite_info, acceptable_ec_grp_ids ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001027 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001028 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001029 }
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01001030#else
1031 ((void) acceptable_ec_grp_ids);
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001032#endif
1033
1034 *ciphersuite_info = suite_info;
1035 return( 0 );
1036}
1037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1039static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
Paul Bakker78a8c712013-03-06 17:01:52 +01001040{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001041 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001042 unsigned int i, j;
1043 size_t n;
1044 unsigned int ciph_len, sess_len, chal_len;
1045 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001046 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_SSL_RENEGOTIATION)
1052 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001055 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1056 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001058 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001060
1061 buf = ssl->in_hdr;
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
Paul Bakker78a8c712013-03-06 17:01:52 +01001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001066 buf[2] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001068 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
Paul Bakker78a8c712013-03-06 17:01:52 +01001070 buf[3], buf[4] ) );
1071
1072 /*
1073 * SSLv2 Client Hello
1074 *
1075 * Record layer:
1076 * 0 . 1 message length
1077 *
1078 * SSL layer:
1079 * 2 . 2 message type
1080 * 3 . 4 protocol version
1081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1083 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1086 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001087 }
1088
1089 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1090
1091 if( n < 17 || n > 512 )
1092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1094 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001095 }
1096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001098 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1099 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001100
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001101 if( ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker78a8c712013-03-06 17:01:52 +01001102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001104 " [%d:%d] < [%d:%d]",
1105 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001106 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1109 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1110 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker78a8c712013-03-06 17:01:52 +01001111 }
1112
Paul Bakker2fbefde2013-06-29 16:01:15 +02001113 ssl->handshake->max_major_ver = buf[3];
1114 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Paul Bakker78a8c712013-03-06 17:01:52 +01001119 return( ret );
1120 }
1121
1122 ssl->handshake->update_checksum( ssl, buf + 2, n );
1123
1124 buf = ssl->in_msg;
1125 n = ssl->in_left - 5;
1126
1127 /*
1128 * 0 . 1 ciphersuitelist length
1129 * 2 . 3 session id length
1130 * 4 . 5 challenge length
1131 * 6 . .. ciphersuitelist
1132 * .. . .. session id
1133 * .. . .. challenge
1134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
Paul Bakker78a8c712013-03-06 17:01:52 +01001136
1137 ciph_len = ( buf[0] << 8 ) | buf[1];
1138 sess_len = ( buf[2] << 8 ) | buf[3];
1139 chal_len = ( buf[4] << 8 ) | buf[5];
1140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001142 ciph_len, sess_len, chal_len ) );
1143
1144 /*
1145 * Make sure each parameter length is valid
1146 */
1147 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1150 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001151 }
1152
1153 if( sess_len > 32 )
1154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1156 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001157 }
1158
1159 if( chal_len < 8 || chal_len > 32 )
1160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1162 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001163 }
1164
1165 if( n != 6 + ciph_len + sess_len + chal_len )
1166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1168 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001169 }
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Paul Bakker78a8c712013-03-06 17:01:52 +01001172 buf + 6, ciph_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
Paul Bakker78a8c712013-03-06 17:01:52 +01001174 buf + 6 + ciph_len, sess_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
Paul Bakker78a8c712013-03-06 17:01:52 +01001176 buf + 6 + ciph_len + sess_len, chal_len );
1177
1178 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001179 ssl->session_negotiate->id_len = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001180 memset( ssl->session_negotiate->id, 0,
1181 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001182 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
Paul Bakker78a8c712013-03-06 17:01:52 +01001183
1184 p += sess_len;
1185 memset( ssl->handshake->randbytes, 0, 64 );
1186 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1187
1188 /*
1189 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1190 */
1191 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Paul Bakker78a8c712013-03-06 17:01:52 +01001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1196#if defined(MBEDTLS_SSL_RENEGOTIATION)
1197 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001200 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001201
Gilles Peskinec94f7352017-05-10 16:37:56 +02001202 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1203 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001205 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206#endif /* MBEDTLS_SSL_RENEGOTIATION */
1207 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001208 break;
1209 }
1210 }
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001213 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1214 {
1215 if( p[0] == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1217 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001220
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001221 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1226 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001229 }
1230
1231 break;
1232 }
1233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001235
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001236 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001237 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001238 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001240 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001241 for( i = 0; ciphersuites[i] != 0; i++ )
1242#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001243 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001244 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001245#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001246 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001247 if( p[0] != 0 ||
1248 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1249 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1250 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001251
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001252 got_common_suite = 1;
1253
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001254 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01001255 &ciphersuite_info,
1256 NULL ) ) )
1257 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001258 return( ret );
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01001259 }
Paul Bakker59c28a22013-06-29 15:33:42 +02001260
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001261 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001262 goto have_ciphersuite_v2;
1263 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001264
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001265 if( got_common_suite )
1266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001268 "but none of them usable" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001270 }
1271 else
1272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1274 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001275 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001276
1277have_ciphersuite_v2:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001279
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001280 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Becker8759e162017-12-27 21:34:08 +00001281 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001282
1283 /*
1284 * SSLv2 Client Hello relevant renegotiation security checks
1285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +01001287 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
1288 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001291 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1292 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001294 }
1295
1296 ssl->in_left = 0;
1297 ssl->state++;
1298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001300
1301 return( 0 );
1302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001304
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001305/* This function doesn't alert on errors that happen early during
1306 ClientHello parsing because they might indicate that the client is
1307 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001309{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001310 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001311 size_t i, j;
1312 size_t ciph_offset, comp_offset, ext_offset;
1313 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001315 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001316#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001317 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001319 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001320#endif
Hanno Becker03b64fa2019-06-11 14:39:38 +01001321#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1322 int extended_ms_seen = 0;
1323#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001324 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001325 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001327 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01001329#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1330 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1331 mbedtls_ecp_group_id acceptable_ec_grp_ids[ MBEDTLS_ECP_DP_MAX ];
1332#else
1333 mbedtls_ecp_group_id * acceptable_ec_grp_ids = NULL;
1334#endif
1335
Hanno Becker7e5437a2017-04-28 17:15:26 +01001336 /* If there is no signature-algorithm extension present,
1337 * we need to fall back to the default values for allowed
1338 * signature-hash pairs. */
1339#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1340 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
1341 int sig_hash_alg_ext_present = 0;
1342#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
1343 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001348read_record_header:
1349#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001350 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001352 * otherwise read it ourselves manually in order to support SSLv2
1353 * ClientHello, which doesn't use the same record layer format.
1354 */
Manuel Pégourié-Gonnard754b9f32019-07-01 12:20:54 +02001355 if( mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
1356 ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 {
Manuel Pégourié-Gonnard754b9f32019-07-01 12:20:54 +02001358 /* No alert on a read error. */
1359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
1360 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
1362
1363 buf = ssl->in_hdr;
1364
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001365#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) && \
1366 defined(MBEDTLS_SSL_PROTO_TLS)
1367 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) &&
1368 ( buf[0] & 0x80 ) != 0 )
1369 {
1370 return( ssl_parse_client_hello_v2( ssl ) );
1371 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001372#endif
1373
Hanno Becker43395762019-05-03 14:46:38 +01001374 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001375
Paul Bakkerec636f32012-09-09 19:17:02 +00001376 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001377 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001378 *
1379 * Record layer:
1380 * 0 . 0 message type
1381 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001382 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001383 * 3 . 4 message length
1384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001386 buf[0] ) );
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1391 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001392 }
1393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001395 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001398 buf[1], buf[2] ) );
1399
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001400 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001401
1402 /* According to RFC 5246 Appendix E.1, the version here is typically
1403 * "{03,00}, the lowest version number supported by the client, [or] the
1404 * value of ClientHello.client_version", so the only meaningful check here
1405 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1409 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001412 /* For DTLS if this is the initial handshake, remember the client sequence
1413 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard754b9f32019-07-01 12:20:54 +02001415 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
1416 mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001417 {
1418 /* Epoch should be 0 for initial handshakes */
1419 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1422 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001423 }
1424
Hanno Becker19859472018-08-06 09:40:20 +01001425 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1428 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001431 ssl->next_record_offset = 0;
1432 ssl->in_left = 0;
1433 goto read_record_header;
1434 }
1435
1436 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001438#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001441
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001442 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#if defined(MBEDTLS_SSL_RENEGOTIATION)
1445 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001448 msg_len = ssl->in_hslen;
1449 }
1450 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001451#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001452 {
Angus Grattond8213d02016-05-25 20:56:48 +10001453 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1456 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001457 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001458
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001459 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01001460 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001463 return( ret );
1464 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001465
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001466 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001468 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
1469 {
Hanno Becker43395762019-05-03 14:46:38 +01001470 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001471 }
1472 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001473#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001474#if defined(MBEDTLS_SSL_PROTO_TLS)
1475 {
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001476 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001477 }
1478#endif
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001479 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001480
1481 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001484
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001485 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001486
1487 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001488 * Handshake layer:
1489 * 0 . 0 handshake type
1490 * 1 . 3 handshake length
1491 * 4 . 5 DTLS only: message seqence number
1492 * 6 . 8 DTLS only: fragment offset
1493 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1498 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001499 }
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1506 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001507 }
1508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001510 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1511
1512 /* We don't support fragmentation of ClientHello (yet?) */
1513 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1517 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001518 }
1519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02001521 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001522 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001523 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001524 * Copy the client's handshake message_seq on initial handshakes,
1525 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_SSL_RENEGOTIATION)
1528 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001529 {
1530 /* This couldn't be done in ssl_prepare_handshake_record() */
1531 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1532 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001533
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001534 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001537 "%d (expected %d)", cli_msg_seq,
1538 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001540 }
1541
1542 ssl->handshake->in_msg_seq++;
1543 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001544 else
1545#endif
1546 {
1547 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1548 ssl->in_msg[5];
1549 ssl->handshake->out_msg_seq = cli_msg_seq;
1550 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1551 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001552
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001553 /*
1554 * For now we don't support fragmentation, so make sure
1555 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001556 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001557 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1558 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1561 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001562 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001563 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 buf += mbedtls_ssl_hs_hdr_len( ssl );
1567 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001568
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001569 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001570 * ClientHello layer:
1571 * 0 . 1 protocol version
1572 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1573 * 34 . 35 session id length (1 byte)
1574 * 35 . 34+x session id
1575 * 35+x . 35+x DTLS only: cookie length (1 byte)
1576 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001577 * .. . .. ciphersuite list length (2 bytes)
1578 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001579 * .. . .. compression alg. list length (1 byte)
1580 * .. . .. compression alg. list
1581 * .. . .. extensions length (2 bytes, optional)
1582 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001583 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001584
1585 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01001586 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001587 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1588 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001589 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001590 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1593 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001594 }
1595
1596 /*
1597 * Check and save the protocol version
1598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001602 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001603
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001604 ssl->handshake->max_major_ver = ssl->major_ver;
1605 ssl->handshake->max_minor_ver = ssl->minor_ver;
1606
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001607 if( ssl->major_ver < ssl->conf->min_major_ver ||
1608 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001611 " [%d:%d] < [%d:%d]",
1612 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001613 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1615 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001617 }
1618
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001619 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001620 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001621 ssl->major_ver = ssl->conf->max_major_ver;
1622 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001623 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001624 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1625 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001626
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001627 /*
1628 * Save client random (inc. Unix time)
1629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001631
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001632 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001633
1634 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001635 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001636 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001637 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001638
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001639 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001640 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001643 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1644 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001646 }
1647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001649
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001650 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001651 memset( ssl->session_negotiate->id, 0,
1652 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001653 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001654 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001655
1656 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001657 * Check the cookie length and content
1658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001660 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001661 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001662 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001663 cookie_len = buf[cookie_offset];
1664
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001665 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001668 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1669 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001671 }
1672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001674 buf + cookie_offset + 1, cookie_len );
1675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard754b9f32019-07-01 12:20:54 +02001677 if( ssl->conf->f_cookie_check != NULL &&
1678 mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001679 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001680 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001681 buf + cookie_offset + 1, cookie_len,
1682 ssl->cli_id, ssl->cli_id_len ) != 0 )
1683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001685 ssl->handshake->verify_cookie_len = 1;
1686 }
1687 else
1688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001690 ssl->handshake->verify_cookie_len = 0;
1691 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001692 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001693 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001695 {
1696 /* We know we didn't send a cookie, so it should be empty */
1697 if( cookie_len != 0 )
1698 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001699 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1701 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001702 }
1703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001705 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001706
1707 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001708 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001709 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001710 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001711 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001712 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001714#if defined(MBEDTLS_SSL_PROTO_TLS)
1715 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001716 ciph_offset = 35 + sess_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02001717 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02001718#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakkerec636f32012-09-09 19:17:02 +00001719
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001720 ciph_len = ( buf[ciph_offset + 0] << 8 )
1721 | ( buf[ciph_offset + 1] );
1722
1723 if( ciph_len < 2 ||
1724 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1725 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001728 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1729 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001731 }
1732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001734 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001735
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001736 /*
1737 * Check the compression algorithms length and pick one
1738 */
1739 comp_offset = ciph_offset + 2 + ciph_len;
1740
1741 comp_len = buf[comp_offset];
1742
1743 if( comp_len < 1 ||
1744 comp_len > 16 ||
1745 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001748 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1749 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001751 }
1752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001754 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1757#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakkerec636f32012-09-09 19:17:02 +00001758 for( i = 0; i < comp_len; ++i )
1759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001763 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 }
1765 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001766#endif
1767
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001768 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02001770 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001772#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001773
Janos Follathc6dab2b2016-05-23 14:27:02 +01001774 /* Do not parse the extensions if the protocol is SSLv3 */
1775#if defined(MBEDTLS_SSL_PROTO_SSL3)
1776 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1777 {
1778#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001779 /*
1780 * Check the extension length
1781 */
1782 ext_offset = comp_offset + 1 + comp_len;
1783 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001784 {
Simon Butcher584a5472016-05-23 16:24:52 +01001785 if( msg_len < ext_offset + 2 )
1786 {
1787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001788 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1789 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001790 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1791 }
1792
1793 ext_len = ( buf[ext_offset + 0] << 8 )
1794 | ( buf[ext_offset + 1] );
1795
1796 if( ( ext_len > 0 && ext_len < 4 ) ||
1797 msg_len != ext_offset + 2 + ext_len )
1798 {
1799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001800 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1801 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001802 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1803 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001804 }
Simon Butcher584a5472016-05-23 16:24:52 +01001805 else
1806 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001807
Simon Butcher584a5472016-05-23 16:24:52 +01001808 ext = buf + ext_offset + 2;
1809 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001810
Simon Butcher584a5472016-05-23 16:24:52 +01001811 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001812 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001813 unsigned int ext_id;
1814 unsigned int ext_size;
1815 if ( ext_len < 4 ) {
1816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1817 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1818 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1819 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1820 }
1821 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1822 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001823
Simon Butcher584a5472016-05-23 16:24:52 +01001824 if( ext_size + 4 > ext_len )
1825 {
1826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001827 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1828 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001829 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1830 }
1831 switch( ext_id )
1832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001834 case MBEDTLS_TLS_EXT_SERVERNAME:
1835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1836 if( ssl->conf->f_sni == NULL )
1837 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001838
Simon Butcher584a5472016-05-23 16:24:52 +01001839 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1840 if( ret != 0 )
1841 return( ret );
1842 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001844
Simon Butcher584a5472016-05-23 16:24:52 +01001845 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001848 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001849#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001850
Simon Butcher584a5472016-05-23 16:24:52 +01001851 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1852 if( ret != 0 )
1853 return( ret );
1854 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1857 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001858 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001859 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1860
Simon Butcher584a5472016-05-23 16:24:52 +01001861 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1862 if( ret != 0 )
1863 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001864
1865 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001866 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
1868 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001869
Robert Cragie136884c2015-10-02 13:34:31 +01001870#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001871 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001872 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001874
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01001875 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4,
1876 ext_size,
1877 acceptable_ec_grp_ids );
Simon Butcher584a5472016-05-23 16:24:52 +01001878 if( ret != 0 )
1879 return( ret );
1880 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001881
Simon Butcher584a5472016-05-23 16:24:52 +01001882 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1884 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001885
Simon Butcher584a5472016-05-23 16:24:52 +01001886 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1887 if( ret != 0 )
1888 return( ret );
1889 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001890#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1891 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001892
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001893#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001894 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001896
Simon Butcher584a5472016-05-23 16:24:52 +01001897 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1898 if( ret != 0 )
1899 return( ret );
1900 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001901#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001904 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001906
Simon Butcher584a5472016-05-23 16:24:52 +01001907 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1908 if( ret != 0 )
1909 return( ret );
1910 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001914 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001916
Simon Butcher584a5472016-05-23 16:24:52 +01001917 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1918 if( ret != 0 )
1919 return( ret );
1920 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001922
Hanno Beckera5a2b082019-05-15 14:03:01 +01001923#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc403b262019-04-26 13:56:39 +01001924 case MBEDTLS_TLS_EXT_CID:
1925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1926
1927 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
1928 if( ret != 0 )
1929 return( ret );
1930 break;
1931#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001934 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001936
Simon Butcher584a5472016-05-23 16:24:52 +01001937 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1938 if( ret != 0 )
1939 return( ret );
1940 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001944 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001946
Simon Butcher584a5472016-05-23 16:24:52 +01001947 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1948 if( ret != 0 )
1949 return( ret );
Hanno Becker03b64fa2019-06-11 14:39:38 +01001950 extended_ms_seen = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001951 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001955 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1956 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001957
Simon Butcher584a5472016-05-23 16:24:52 +01001958 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1959 if( ret != 0 )
1960 return( ret );
1961 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01001965 case MBEDTLS_TLS_EXT_ALPN:
1966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001967
Simon Butcher584a5472016-05-23 16:24:52 +01001968 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1969 if( ret != 0 )
1970 return( ret );
1971 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001973
Simon Butcher584a5472016-05-23 16:24:52 +01001974 default:
1975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1976 ext_id ) );
1977 }
1978
1979 ext_len -= 4 + ext_size;
1980 ext += 4 + ext_size;
1981
1982 if( ext_len > 0 && ext_len < 4 )
1983 {
1984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001985 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1986 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001987 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1988 }
Paul Bakker48916f92012-09-16 19:57:18 +00001989 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01001990#if defined(MBEDTLS_SSL_PROTO_SSL3)
1991 }
1992#endif
1993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskined50177f2017-05-16 17:53:03 +02001995 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1998 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001999 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002001
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002002 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002003 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2007 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002010 }
2011
2012 break;
2013 }
2014 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002016
Hanno Becker7e5437a2017-04-28 17:15:26 +01002017#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
2018 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
2019
2020 /*
2021 * Try to fall back to default hash SHA1 if the client
2022 * hasn't provided any preferred signature-hash combinations.
2023 */
2024 if( sig_hash_alg_ext_present == 0 )
2025 {
2026 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2027
2028 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2029 md_default = MBEDTLS_MD_NONE;
2030
2031 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2032 }
2033
2034#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
2035 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
2036
Paul Bakker48916f92012-09-16 19:57:18 +00002037 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002038 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2039 */
2040 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2045#if defined(MBEDTLS_SSL_RENEGOTIATION)
2046 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002047 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2049 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002050 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2051 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002053 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002054#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002056 break;
2057 }
2058 }
2059
2060 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002061 * Renegotiation security checks
2062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +01002064 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
2065 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002068 handshake_failure = 1;
2069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#if defined(MBEDTLS_SSL_RENEGOTIATION)
2071 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2072 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002073 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002076 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00002077 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2079 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +01002080 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf )
2081 == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002084 handshake_failure = 1;
2085 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2087 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002088 renegotiation_info_seen == 1 )
2089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002091 handshake_failure = 1;
2092 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002094
Jarno Lamsa842be162019-06-10 15:05:33 +03002095 /*
2096 * Check if extended master secret is being enforced
2097 */
2098#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +01002099 if( mbedtls_ssl_conf_get_ems( ssl->conf ) ==
Hanno Becker03b64fa2019-06-11 14:39:38 +01002100 MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Jarno Lamsa842be162019-06-10 15:05:33 +03002101 {
Hanno Becker03b64fa2019-06-11 14:39:38 +01002102 if( extended_ms_seen )
2103 {
Hanno Becker1ab322b2019-06-11 14:50:54 +01002104#if !defined(MBEDTLS_SSL_EXTENDED_MS_ENFORCED)
Hanno Becker03b64fa2019-06-11 14:39:38 +01002105 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Becker1ab322b2019-06-11 14:50:54 +01002106#endif /* !MBEDTLS_SSL_EXTENDED_MS_ENFORCED */
Hanno Becker03b64fa2019-06-11 14:39:38 +01002107 }
2108 else if( mbedtls_ssl_conf_get_ems_enforced( ssl->conf ) ==
2109 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED )
2110 {
2111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Peer not offering extended master "
2112 "secret, while it is enforced") );
2113 handshake_failure = 1;
2114 }
Jarno Lamsa842be162019-06-10 15:05:33 +03002115 }
2116#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
2117
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002118 if( handshake_failure == 1 )
2119 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02002120 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2121 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00002123 }
Paul Bakker380da532012-04-18 16:10:25 +00002124
Paul Bakker41c83d32013-03-20 14:39:14 +01002125 /*
2126 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002127 * (At the end because we need information from the EC-based extensions
2128 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002129 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002130 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002131 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002132 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002134 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002135 for( i = 0; ciphersuites[i] != 0; i++ )
2136#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002137 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002138 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002139#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002140 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002141 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2142 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2143 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002144
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002145 got_common_suite = 1;
2146
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002147 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01002148 &ciphersuite_info,
2149 acceptable_ec_grp_ids ) ) != 0 )
2150 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002151 return( ret );
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01002152 }
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002153
2154 if( ciphersuite_info != NULL )
2155 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002156 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002157
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002158 if( got_common_suite )
2159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002161 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002162 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2163 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002165 }
2166 else
2167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002169 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2170 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002172 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002173
2174have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002176
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002177 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Becker8759e162017-12-27 21:34:08 +00002178 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002179
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 ssl->state++;
2181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02002183 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002185#endif
2186
Hanno Becker7e5437a2017-04-28 17:15:26 +01002187 /* Debugging-only output for testsuite */
2188#if defined(MBEDTLS_DEBUG_C) && \
2189 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
2190 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
2191 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2192 {
2193 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2194 if( sig_alg != MBEDTLS_PK_NONE )
2195 {
2196 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2197 sig_alg );
2198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2199 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2200 }
2201 else
2202 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
2204 "%d - should not happen", sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01002205 }
2206 }
2207#endif
2208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
2211 return( 0 );
2212}
2213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2215static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002216 unsigned char *buf,
2217 size_t *olen )
2218{
2219 unsigned char *p = buf;
2220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002222 {
2223 *olen = 0;
2224 return;
2225 }
2226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2230 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002231
2232 *p++ = 0x00;
2233 *p++ = 0x00;
2234
2235 *olen = 4;
2236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002238
Hanno Beckera5a2b082019-05-15 14:03:01 +01002239#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker072d4ec2019-04-26 15:46:55 +01002240static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2241 unsigned char *buf,
2242 size_t *olen )
2243{
2244 unsigned char *p = buf;
2245 size_t ext_len;
2246 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2247
2248 *olen = 0;
2249
2250 /* Skip writing the extension if we don't want to use it or if
2251 * the client hasn't offered it. */
2252 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2253 return;
2254
2255 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2256 * which is at most 255, so the increment cannot overflow. */
2257 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2258 {
2259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2260 return;
2261 }
2262
2263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2264
2265 /*
Hanno Becker3cdf8fe2019-05-15 10:26:32 +01002266 * Quoting draft-ietf-tls-dtls-connection-id-05
2267 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker072d4ec2019-04-26 15:46:55 +01002268 *
2269 * struct {
2270 * opaque cid<0..2^8-1>;
2271 * } ConnectionId;
2272 */
2273
2274 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2275 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2276 ext_len = (size_t) ssl->own_cid_len + 1;
2277 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2278 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2279
2280 *p++ = (uint8_t) ssl->own_cid_len;
2281 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2282
2283 *olen = ssl->own_cid_len + 5;
2284}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002285#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker072d4ec2019-04-26 15:46:55 +01002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2288static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002289 unsigned char *buf,
2290 size_t *olen )
2291{
2292 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2294 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002295
Hanno Becker27b34d52017-10-20 14:24:51 +01002296 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002298 {
2299 *olen = 0;
2300 return;
2301 }
2302
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002303 /*
2304 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2305 * from a client and then selects a stream or Authenticated Encryption
2306 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2307 * encrypt-then-MAC response extension back to the client."
2308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002310 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2312 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002313 {
2314 *olen = 0;
2315 return;
2316 }
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2321 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002322
2323 *p++ = 0x00;
2324 *p++ = 0x00;
2325
2326 *olen = 4;
2327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2331static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002332 unsigned char *buf,
2333 size_t *olen )
2334{
2335 unsigned char *p = buf;
2336
Hanno Beckera49ec562019-06-11 14:47:55 +01002337 if( mbedtls_ssl_hs_get_extended_ms( ssl->handshake )
2338 == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002340 {
2341 *olen = 0;
2342 return;
2343 }
2344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002346 "extension" ) );
2347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2349 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002350
2351 *p++ = 0x00;
2352 *p++ = 0x00;
2353
2354 *olen = 4;
2355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2359static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002360 unsigned char *buf,
2361 size_t *olen )
2362{
2363 unsigned char *p = buf;
2364
2365 if( ssl->handshake->new_session_ticket == 0 )
2366 {
2367 *olen = 0;
2368 return;
2369 }
2370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2374 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002375
2376 *p++ = 0x00;
2377 *p++ = 0x00;
2378
2379 *olen = 4;
2380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002384 unsigned char *buf,
2385 size_t *olen )
2386{
2387 unsigned char *p = buf;
2388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002390 {
2391 *olen = 0;
2392 return;
2393 }
2394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2398 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#if defined(MBEDTLS_SSL_RENEGOTIATION)
2401 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002402 {
2403 *p++ = 0x00;
2404 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2405 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002406
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002407 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2408 p += ssl->verify_data_len;
2409 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2410 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002411 }
2412 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002414 {
2415 *p++ = 0x00;
2416 *p++ = 0x01;
2417 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002418 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002419
2420 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002421}
2422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2424static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002425 unsigned char *buf,
2426 size_t *olen )
2427{
2428 unsigned char *p = buf;
2429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002431 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002432 *olen = 0;
2433 return;
2434 }
2435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2439 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002440
2441 *p++ = 0x00;
2442 *p++ = 1;
2443
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002444 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002445
2446 *olen = 5;
2447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002449
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002450#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002451 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002453 unsigned char *buf,
2454 size_t *olen )
2455{
2456 unsigned char *p = buf;
2457 ((void) ssl);
2458
Paul Bakker677377f2013-10-28 12:54:26 +01002459 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002461 {
2462 *olen = 0;
2463 return;
2464 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2469 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002470
2471 *p++ = 0x00;
2472 *p++ = 2;
2473
2474 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002476
2477 *olen = 6;
2478}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002479#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002480
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002481#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2482static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2483 unsigned char *buf,
2484 size_t *olen )
2485{
2486 int ret;
2487 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002488 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002489 size_t kkpp_len;
2490
2491 *olen = 0;
2492
2493 /* Skip costly computation if not needed */
Hanno Becker8759e162017-12-27 21:34:08 +00002494 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002495 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2496 return;
2497
2498 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2499
2500 if( end - p < 4 )
2501 {
2502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2503 return;
2504 }
2505
2506 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2507 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2508
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002509 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2510 p + 2, end - p - 2, &kkpp_len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002511 mbedtls_ssl_conf_get_frng( ssl->conf ),
2512 ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002513 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002514 {
2515 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2516 return;
2517 }
2518
2519 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2520 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2521
2522 *olen = kkpp_len + 4;
2523}
2524#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#if defined(MBEDTLS_SSL_ALPN )
2527static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002528 unsigned char *buf, size_t *olen )
2529{
2530 if( ssl->alpn_chosen == NULL )
2531 {
2532 *olen = 0;
2533 return;
2534 }
2535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002537
2538 /*
2539 * 0 . 1 ext identifier
2540 * 2 . 3 ext length
2541 * 4 . 5 protocol list length
2542 * 6 . 6 protocol name length
2543 * 7 . 7+n protocol name
2544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2546 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002547
2548 *olen = 7 + strlen( ssl->alpn_chosen );
2549
2550 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2551 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2552
2553 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2554 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2555
2556 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2557
2558 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2559}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2563static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002564{
2565 int ret;
2566 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002567 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002570
2571 /*
2572 * struct {
2573 * ProtocolVersion server_version;
2574 * opaque cookie<0..2^8-1>;
2575 * } HelloVerifyRequest;
2576 */
2577
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002578 /* The RFC is not clear on this point, but sending the actual negotiated
2579 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002581 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002583 p += 2;
2584
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002585 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002586 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002590 }
2591
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002592 /* Skip length byte until we know the length */
2593 cookie_len_byte = p++;
2594
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002595 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002596 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002597 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002600 return( ret );
2601 }
2602
2603 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002606
2607 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2609 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002612
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002613 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002614 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002616 return( ret );
2617 }
2618
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02002620 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002621 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2622 {
2623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2624 return( ret );
2625 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002626#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002629
2630 return( 0 );
2631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002637 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002638#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002639 int ret;
2640 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 unsigned char *buf, *p;
2642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02002646 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002647 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002651
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002652 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002655
Hanno Beckerece325c2019-06-13 15:39:27 +01002656 if( mbedtls_ssl_conf_get_frng( ssl->conf ) == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2659 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002660 }
2661
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 /*
2663 * 0 . 0 handshake type
2664 * 1 . 3 handshake length
2665 * 4 . 5 protocol version
2666 * 6 . 9 UNIX time()
2667 * 10 . 37 random bytes
2668 */
2669 buf = ssl->out_msg;
2670 p = buf + 4;
2671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002673 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002674 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002677 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002680 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 *p++ = (unsigned char)( t >> 24 );
2682 *p++ = (unsigned char)( t >> 16 );
2683 *p++ = (unsigned char)( t >> 8 );
2684 *p++ = (unsigned char)( t );
2685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002687#else
Hanno Beckerece325c2019-06-13 15:39:27 +01002688 if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf )
2689 ( ssl->conf->p_rng, p, 4 ) ) != 0 )
2690 {
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002691 return( ret );
Hanno Beckerece325c2019-06-13 15:39:27 +01002692 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002693
2694 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Hanno Beckerece325c2019-06-13 15:39:27 +01002697 if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf )
2698 ( ssl->conf->p_rng, p, 28 ) ) != 0 )
2699 {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002700 return( ret );
Hanno Beckerece325c2019-06-13 15:39:27 +01002701 }
Paul Bakkera3d195c2011-11-27 21:07:34 +00002702
2703 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Paul Bakker48916f92012-09-16 19:57:18 +00002705 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002709#if !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002711 * Resume is 0 by default, see ssl_handshake_init().
2712 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2713 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 */
Manuel Pégourié-Gonnard3652e992019-07-01 12:09:22 +02002715 if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == 0 &&
Manuel Pégourié-Gonnard754b9f32019-07-01 12:20:54 +02002716 mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002717 ssl->session_negotiate->id_len != 0 &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002718 ssl->conf->f_get_cache != NULL &&
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002719 ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002722 ssl->handshake->resume = 1;
2723 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002724#endif /* !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002726#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard3652e992019-07-01 12:09:22 +02002727 if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == 1 )
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002728 {
2729 /*
2730 * Resuming a session
2731 */
2732 n = ssl->session_negotiate->id_len;
2733 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
2734
2735 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
2736 {
2737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
2738 return( ret );
2739 }
2740 }
2741 else
2742#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 {
2744 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002745 * New session, create a new session id,
2746 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 ssl->state++;
2749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002751 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002752#endif
2753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002755 if( ssl->handshake->new_session_ticket != 0 )
2756 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002757 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002758 memset( ssl->session_negotiate->id, 0, 32 );
2759 }
2760 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002762 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002763 ssl->session_negotiate->id_len = n = 32;
Hanno Beckerece325c2019-06-13 15:39:27 +01002764 if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf )
2765 ( ssl->conf->p_rng, ssl->session_negotiate->id, n ) ) != 0 )
2766 {
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002767 return( ret );
Hanno Beckerece325c2019-06-13 15:39:27 +01002768 }
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002769 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002772 /*
2773 * 38 . 38 session id length
2774 * 39 . 38+n session id
2775 * 39+n . 40+n chosen ciphersuite
2776 * 41+n . 41+n chosen compression alg.
2777 * 42+n . 43+n extensions length
2778 * 44+n . 43+n+m extensions
2779 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002780 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2781 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2782 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2785 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Manuel Pégourié-Gonnard3652e992019-07-01 12:09:22 +02002787 mbedtls_ssl_handshake_get_resume( ssl->handshake ) ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Paul Bakker48916f92012-09-16 19:57:18 +00002789 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2790 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2791 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2794 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2795 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002796 ssl->session_negotiate->compression ) );
2797
Janos Follathc6dab2b2016-05-23 14:27:02 +01002798 /* Do not write the extensions if the protocol is SSLv3 */
2799#if defined(MBEDTLS_SSL_PROTO_SSL3)
2800 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2801 {
2802#endif
2803
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002804 /*
2805 * First write extensions, then the total length
2806 */
2807 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2808 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002811 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2812 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002813#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002816 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2817 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002818#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002819
Hanno Beckera5a2b082019-05-15 14:03:01 +01002820#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker072d4ec2019-04-26 15:46:55 +01002821 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2822 ext_len += olen;
2823#endif
2824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002826 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2827 ext_len += olen;
2828#endif
2829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002831 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2832 ext_len += olen;
2833#endif
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002836 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2837 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002838#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002839
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002840#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002841 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002842 if ( mbedtls_ssl_ciphersuite_uses_ec(
2843 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2844 {
2845 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2846 ext_len += olen;
2847 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002848#endif
2849
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002850#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2851 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2852 ext_len += olen;
2853#endif
2854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002856 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2857 ext_len += olen;
2858#endif
2859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002861
Paul Bakkera7036632014-04-30 10:15:38 +02002862 if( ext_len > 0 )
2863 {
2864 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2865 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2866 p += ext_len;
2867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Janos Follathc6dab2b2016-05-23 14:27:02 +01002869#if defined(MBEDTLS_SSL_PROTO_SSL3)
2870 }
2871#endif
2872
Paul Bakker5121ce52009-01-03 21:22:43 +00002873 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2875 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002877 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
2881 return( ret );
2882}
2883
Hanno Beckerae39b9e2019-02-07 12:32:43 +00002884#if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002886{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002887 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00002888 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002891
Hanno Beckerae39b9e2019-02-07 12:32:43 +00002892 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002895 ssl->state++;
2896 return( 0 );
2897 }
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002901}
Hanno Beckerae39b9e2019-02-07 12:32:43 +00002902#else /* !MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002906 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00002907 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002908 size_t dn_size, total_dn_size; /* excluding length bytes */
2909 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002911 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002913 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
2917 ssl->state++;
2918
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002919#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2920 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2921 authmode = ssl->handshake->sni_authmode;
2922 else
2923#endif
Hanno Beckeracd4fc02019-06-12 16:40:50 +01002924 authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002925
Hanno Beckerae39b9e2019-02-07 12:32:43 +00002926 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002927 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 return( 0 );
2931 }
2932
2933 /*
2934 * 0 . 0 handshake type
2935 * 1 . 3 handshake length
2936 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002937 * 5 .. m-1 cert types
2938 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002939 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002940 * n .. n+1 length of all DNs
2941 * n+2 .. n+3 length of DN 1
2942 * n+4 .. ... Distinguished Name #1
2943 * ... .. ... length of DN 2, etc.
2944 */
2945 buf = ssl->out_msg;
2946 p = buf + 4;
2947
2948 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002949 * Supported certificate types
2950 *
2951 * ClientCertificateType certificate_types<1..2^8-1>;
2952 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002954 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956#if defined(MBEDTLS_RSA_C)
2957 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#if defined(MBEDTLS_ECDSA_C)
2960 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002961#endif
2962
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002963 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002964 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002965
Paul Bakker577e0062013-08-28 11:57:20 +02002966 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002968 /*
2969 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002970 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002971 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2972 *
2973 * struct {
2974 * HashAlgorithm hash;
2975 * SignatureAlgorithm signature;
2976 * } SignatureAndHashAlgorithm;
2977 *
2978 * enum { (255) } HashAlgorithm;
2979 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002982 {
Simon Butcher99000142016-10-13 17:21:01 +01002983 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002984
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002985 /*
2986 * Supported signature algorithms
2987 */
Simon Butcher99000142016-10-13 17:21:01 +01002988 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
2989 {
2990 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
2991
2992 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
2993 continue;
2994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01002996 p[2 + sa_len++] = hash;
2997 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002998#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003000 p[2 + sa_len++] = hash;
3001 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003002#endif
Simon Butcher99000142016-10-13 17:21:01 +01003003 }
Paul Bakker926af752012-11-23 13:38:07 +01003004
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003005 p[0] = (unsigned char)( sa_len >> 8 );
3006 p[1] = (unsigned char)( sa_len );
3007 sa_len += 2;
3008 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003009 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003012 /*
3013 * DistinguishedName certificate_authorities<0..2^16-1>;
3014 * opaque DistinguishedName<1..2^16-1>;
3015 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003016 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003018 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003019
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01003020 if( mbedtls_ssl_conf_get_cert_req_ca_list( ssl->conf )
3021 == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 {
Janos Follath088ce432017-04-10 12:42:31 +01003023#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3024 if( ssl->handshake->sni_ca_chain != NULL )
3025 crt = ssl->handshake->sni_ca_chain;
3026 else
3027#endif
3028 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003029
Hanno Becker371e0e42019-02-25 18:08:59 +00003030 while( crt != NULL && crt->raw.p != NULL )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003031 {
Hanno Becker5f268b32019-05-20 16:26:34 +01003032 mbedtls_x509_crt_frame const *frame;
Hanno Becker232f8fa2019-02-26 16:49:57 +00003033 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
3034 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01003035 {
3036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_frame_acquire", ret );
Hanno Becker232f8fa2019-02-26 16:49:57 +00003037 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01003038 }
Hanno Becker232f8fa2019-02-26 16:49:57 +00003039
Hanno Becker1e11f212019-03-04 14:43:43 +00003040 dn_size = frame->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003041
3042 if( end < p ||
3043 (size_t)( end - p ) < dn_size ||
3044 (size_t)( end - p ) < 2 + dn_size )
3045 {
3046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003047 mbedtls_x509_crt_frame_release( crt );
Janos Follath088ce432017-04-10 12:42:31 +01003048 break;
3049 }
3050
3051 *p++ = (unsigned char)( dn_size >> 8 );
3052 *p++ = (unsigned char)( dn_size );
Hanno Becker1e11f212019-03-04 14:43:43 +00003053 memcpy( p, frame->subject_raw.p, dn_size );
Janos Follath088ce432017-04-10 12:42:31 +01003054 p += dn_size;
3055
3056 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3057
3058 total_dn_size += 2 + dn_size;
Hanno Becker232f8fa2019-02-26 16:49:57 +00003059
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003060 mbedtls_x509_crt_frame_release( crt );
Hanno Becker232f8fa2019-02-26 16:49:57 +00003061
Janos Follath088ce432017-04-10 12:42:31 +01003062 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003063 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003064 }
3065
Paul Bakker926af752012-11-23 13:38:07 +01003066 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3068 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003069 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
3070 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00003071
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003072 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003075
3076 return( ret );
3077}
Hanno Beckerae39b9e2019-02-07 12:32:43 +00003078#endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3081 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3082static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003083{
3084 int ret;
3085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3089 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003090 }
3091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3093 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3094 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003097 return( ret );
3098 }
3099
3100 return( 0 );
3101}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3103 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003104
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003105#if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003106 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003107static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003108 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003109{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003110 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3111 * signature length which will be added in ssl_write_server_key_exchange
3112 * after the call to ssl_prepare_server_key_exchange.
3113 * ssl_write_server_key_exchange also takes care of incrementing
3114 * ssl->out_msglen. */
3115 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10003116 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003117 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02003118 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003119 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003120 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3121 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003122 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003123 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003124 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01003125 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003126 return( ret );
3127}
3128#endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003129 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003130
Gilles Peskined3eb0612018-01-08 17:07:44 +01003131/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003132 * calculating the signature if any, but excluding formatting the
3133 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003134static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3135 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00003136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00003138 ssl->handshake->ciphersuite_info;
3139
Hanno Becker1aa267c2017-04-28 17:08:27 +01003140#if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003141#if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003142 unsigned char *dig_signed = NULL;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003143#endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */
3144#endif /* MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003145
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003146 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskine22e695f2018-04-26 00:22:50 +02003147#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
3148 (void) signature_len;
3149#endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003150
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003151 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003152
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003153 /*
3154 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003155 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003156 *
3157 */
3158
3159 /*
3160 * - ECJPAKE key exchanges
3161 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003162#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3163 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3164 {
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003165 int ret;
Simon Butcher600c5e62018-06-14 08:58:59 +01003166 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003167
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003168 ret = mbedtls_ecjpake_write_round_two(
3169 &ssl->handshake->ecjpake_ctx,
3170 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003171 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01003172 mbedtls_ssl_conf_get_frng( ssl->conf ),
3173 ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003174 if( ret != 0 )
3175 {
3176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3177 return( ret );
3178 }
3179
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003180 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003181 }
3182#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3183
Hanno Becker1aa267c2017-04-28 17:08:27 +01003184 /*
3185 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3186 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3187 * we use empty support identity hints here.
3188 **/
3189#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3191 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3192 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003193 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003194 ssl->out_msg[ssl->out_msglen++] = 0x00;
3195 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003196 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3198 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003199
Hanno Becker7e5437a2017-04-28 17:15:26 +01003200 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003201 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003202 */
3203#if defined(MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED)
3204 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003205 {
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003206 int ret;
Simon Butcher600c5e62018-06-14 08:58:59 +01003207 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003208
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003209 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3210 {
3211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3213 }
3214
Paul Bakker41c83d32013-03-20 14:39:14 +01003215 /*
3216 * Ephemeral DH parameters:
3217 *
3218 * struct {
3219 * opaque dh_p<1..2^16-1>;
3220 * opaque dh_g<1..2^16-1>;
3221 * opaque dh_Ys<1..2^16-1>;
3222 * } ServerDHParams;
3223 */
Hanno Beckerab740562017-10-04 13:15:37 +01003224 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3225 &ssl->conf->dhm_P,
3226 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003227 {
Hanno Beckerab740562017-10-04 13:15:37 +01003228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003229 return( ret );
3230 }
Paul Bakker48916f92012-09-16 19:57:18 +00003231
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003232 if( ( ret = mbedtls_dhm_make_params(
3233 &ssl->handshake->dhm_ctx,
3234 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3235 ssl->out_msg + ssl->out_msglen, &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01003236 mbedtls_ssl_conf_get_frng( ssl->conf ),
3237 ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003240 return( ret );
3241 }
3242
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003243#if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003244 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003245#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003246
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003247 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3250 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3251 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3252 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003253 }
Hanno Becker1aa267c2017-04-28 17:08:27 +01003254#endif /* MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003255
Hanno Becker1aa267c2017-04-28 17:08:27 +01003256 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003257 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259#if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003260 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003262 /*
3263 * Ephemeral ECDH parameters:
3264 *
3265 * struct {
3266 * ECParameters curve_params;
3267 * ECPoint public;
3268 * } ServerECDHParams;
3269 */
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01003270 const mbedtls_ecp_curve_info *curve = ssl->handshake->curve_info;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003271 int ret;
Simon Butcher600c5e62018-06-14 08:58:59 +01003272 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003273
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01003274 if( curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3277 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003278 }
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01003279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", curve->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003280
Janos Follath3fbdada2018-08-15 10:26:53 +01003281 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
Hanno Beckerd3b2fcb2019-06-17 14:30:05 +01003282 curve->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003283 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003285 return( ret );
3286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003288 if( ( ret = mbedtls_ecdh_make_params(
3289 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003290 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003291 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Hanno Beckerece325c2019-06-13 15:39:27 +01003292 mbedtls_ssl_conf_get_frng( ssl->conf ),
3293 ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003296 return( ret );
3297 }
3298
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003299#if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003300 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003301#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003302
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003303 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003304
Janos Follath3fbdada2018-08-15 10:26:53 +01003305 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3306 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003309
Hanno Becker1aa267c2017-04-28 17:08:27 +01003310 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003311 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003312 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003313 * exchange parameters, compute and add the signature here.
3314 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003315 */
3316#if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
3317 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003318 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003319 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003320 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003321 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003322 int ret;
Paul Bakker23f36802012-09-28 14:15:14 +00003323
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003324 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003325 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003326 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003327 * to choose appropriate hash.
3328 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3329 * (RFC 4492, Sec. 5.4)
3330 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003331 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003332
3333 mbedtls_md_type_t md_alg;
3334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003336 mbedtls_pk_type_t sig_alg =
3337 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003339 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003340 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3341 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003342 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003343 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3344 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003347 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003348 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003350 }
3351 }
Paul Bakker577e0062013-08-28 11:57:20 +02003352 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3354#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3355 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003356 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003357 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003358 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003360 }
3361 else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003362#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3363 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003364 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003365 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003367 }
3368
Hanno Becker7e5437a2017-04-28 17:15:26 +01003369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
3370
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003371 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003372 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3375 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3376 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003377 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003378 hashlen = 36;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003379 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3380 dig_signed,
3381 dig_signed_len );
3382 if( ret != 0 )
3383 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003384 }
3385 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3387 MBEDTLS_SSL_PROTO_TLS1_1 */
3388#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3389 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3390 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003391 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003392 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003393 dig_signed,
3394 dig_signed_len,
3395 md_alg );
3396 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003397 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003398 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003399 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3401 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003405 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003406
Gilles Peskineebd652f2018-01-05 21:18:59 +01003407 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003408
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003409 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003410 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3413 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003414 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003415 /*
3416 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003417 * explicitly through a prefix to the signature.
3418 *
3419 * struct {
3420 * HashAlgorithm hash;
3421 * SignatureAlgorithm signature;
3422 * } SignatureAndHashAlgorithm;
3423 *
3424 * struct {
3425 * SignatureAndHashAlgorithm algorithm;
3426 * opaque signature<0..2^16-1>;
3427 * } DigitallySigned;
3428 *
3429 */
3430
Gilles Peskine1004c192018-01-08 16:59:14 +01003431 ssl->out_msg[ssl->out_msglen++] =
3432 mbedtls_ssl_hash_from_md_alg( md_alg );
3433 ssl->out_msg[ssl->out_msglen++] =
3434 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003435 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003437
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003438#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003439 if( ssl->conf->f_async_sign_start != NULL )
3440 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003441 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003442 mbedtls_ssl_own_cert( ssl ),
3443 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003444 switch( ret )
3445 {
3446 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3447 /* act as if f_async_sign was null */
3448 break;
3449 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003450 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003451 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003452 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003453 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003454 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3455 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003456 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003457 return( ret );
3458 }
3459 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003460#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003461
3462 if( mbedtls_ssl_own_key( ssl ) == NULL )
3463 {
3464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3465 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3466 }
3467
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003468 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3469 * signature length which will be added in ssl_write_server_key_exchange
3470 * after the call to ssl_prepare_server_key_exchange.
3471 * ssl_write_server_key_exchange also takes care of incrementing
3472 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003473 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3474 md_alg, hash, hashlen,
3475 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003476 signature_len,
Hanno Beckerece325c2019-06-13 15:39:27 +01003477 mbedtls_ssl_conf_get_frng( ssl->conf ),
Gilles Peskine1004c192018-01-08 16:59:14 +01003478 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003481 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003482 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003483 }
Hanno Becker1aa267c2017-04-28 17:08:27 +01003484#endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003485
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003486 return( 0 );
3487}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003488
Gilles Peskined3eb0612018-01-08 17:07:44 +01003489/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003490 * that do not include a ServerKeyExchange message, do nothing. Either
3491 * way, if successful, move on to the next step in the SSL state
3492 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003493static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3494{
3495 int ret;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003496 size_t signature_len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003497#if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED)
3498 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00003499 ssl->handshake->ciphersuite_info;
Gilles Peskinef1127252018-04-24 13:05:39 +02003500#endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003501
Gilles Peskined3eb0612018-01-08 17:07:44 +01003502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3503
3504#if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED)
3505 /* Extract static ECDH parameters and abort if ServerKeyExchange
3506 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003507 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3508 {
3509 /* For suites involving ECDH, extract DH parameters
3510 * from certificate at this point. */
3511#if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED)
3512 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3513 {
3514 ssl_get_ecdh_params_from_cert( ssl );
3515 }
3516#endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED */
3517
3518 /* Key exchanges not involving ephemeral keys don't use
3519 * ServerKeyExchange, so end here. */
3520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3521 ssl->state++;
3522 return( 0 );
3523 }
Gilles Peskinef1127252018-04-24 13:05:39 +02003524#endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003525
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003526#if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003527 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003528 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003529 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003530 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003531 {
3532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3533 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003534 }
3535 else
3536#endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003537 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003538 {
3539 /* ServerKeyExchange is needed. Prepare the message. */
3540 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003541 }
3542
3543 if( ret != 0 )
3544 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003545 /* If we're starting to write a new message, set ssl->out_msglen
3546 * to 0. But if we're resuming after an asynchronous message,
3547 * out_msglen is the amount of data written so far and mst be
3548 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003549 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3551 else
3552 ssl->out_msglen = 0;
3553 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003554 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003555
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003556 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003557 * ssl_prepare_server_key_exchange already wrote the signature
3558 * itself at its proper place in the output buffer. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003559#if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
3560 if( signature_len != 0 )
3561 {
3562 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3563 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3564
3565 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3566 ssl->out_msg + ssl->out_msglen,
3567 signature_len );
3568
3569 /* Skip over the already-written signature */
3570 ssl->out_msglen += signature_len;
3571 }
3572#endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */
3573
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003574 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3576 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003577
3578 ssl->state++;
3579
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003580 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003581 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003583 return( ret );
3584 }
3585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003587 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003588}
3589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003591{
3592 int ret;
3593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003595
3596 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3598 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003599
3600 ssl->state++;
3601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003603 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003605#endif
3606
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003607 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 return( ret );
3611 }
3612
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003614 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003615 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3616 {
3617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3618 return( ret );
3619 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003620#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003623
3624 return( 0 );
3625}
3626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3628 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3629static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003630 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003633 size_t n;
3634
3635 /*
3636 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3637 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003638 if( *p + 2 > end )
3639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3641 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003642 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003643
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003644 n = ( (*p)[0] << 8 ) | (*p)[1];
3645 *p += 2;
3646
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003647 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3650 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003651 }
3652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3656 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003657 }
3658
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003659 *p += n;
3660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003662
Paul Bakker70df2fb2013-04-17 17:19:09 +02003663 return( ret );
3664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3666 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3669 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003670
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003671#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003672static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3673 unsigned char *peer_pms,
3674 size_t *peer_pmslen,
3675 size_t peer_pmssize )
3676{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003677 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003678 peer_pms, peer_pmslen, peer_pmssize );
3679 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3680 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003681 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003682 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003683 }
3684 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3685 return( ret );
3686}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003687#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003688
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003689static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3690 const unsigned char *p,
3691 const unsigned char *end,
3692 unsigned char *peer_pms,
3693 size_t *peer_pmslen,
3694 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003695{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003696 int ret;
Hanno Becker81bb4d02019-02-16 11:03:48 +00003697 size_t len = (size_t)( end - p ); /* Cast is safe because p <= end. */
Gilles Peskine422ccab2018-01-11 18:29:01 +01003698 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003699
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003700#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003701 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003702 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003703 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003704 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3706 return( ssl_resume_decrypt_pms( ssl,
3707 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003708 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003709#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003710
3711 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003712 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3715 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker81bb4d02019-02-16 11:03:48 +00003716#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker81bb4d02019-02-16 11:03:48 +00003718#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003719 {
Hanno Becker81bb4d02019-02-16 11:03:48 +00003720 if( len < 2 )
3721 {
Philippe Antoine747fd532018-05-30 09:13:21 +02003722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3723 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3724 }
Hanno Becker81bb4d02019-02-16 11:03:48 +00003725 len -= 2;
3726
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003727 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3728 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3731 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003732 }
3733 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003734#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003735
Gilles Peskine422ccab2018-01-11 18:29:01 +01003736 /*
3737 * Decrypt the premaster secret
3738 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003739#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003740 if( ssl->conf->f_async_decrypt_start != NULL )
3741 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003742 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003743 mbedtls_ssl_own_cert( ssl ),
3744 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003745 switch( ret )
3746 {
3747 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3748 /* act as if f_async_decrypt_start was null */
3749 break;
3750 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003751 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003752 return( ssl_resume_decrypt_pms( ssl,
3753 peer_pms,
3754 peer_pmslen,
3755 peer_pmssize ) );
3756 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003757 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003758 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3759 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003760 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003761 return( ret );
3762 }
3763 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003764#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003765
Gilles Peskine422ccab2018-01-11 18:29:01 +01003766 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3767 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3769 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3770 }
3771
3772 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003773 peer_pms, peer_pmslen, peer_pmssize,
Hanno Beckerece325c2019-06-13 15:39:27 +01003774 mbedtls_ssl_conf_get_frng( ssl->conf ),
3775 ssl->conf->p_rng );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003776 return( ret );
3777}
3778
3779static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3780 const unsigned char *p,
3781 const unsigned char *end,
3782 size_t pms_offset )
3783{
3784 int ret;
3785 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3786 unsigned char ver[2];
3787 unsigned char fake_pms[48], peer_pms[48];
3788 unsigned char mask;
3789 size_t i, peer_pmslen;
3790 unsigned int diff;
3791
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003792 /* In case of a failure in decryption, the decryption may write less than
3793 * 2 bytes of output, but we always read the first two bytes. It doesn't
3794 * matter in the end because diff will be nonzero in that case due to
3795 * peer_pmslen being less than 48, and we only care whether diff is 0.
3796 * But do initialize peer_pms for robustness anyway. This also makes
3797 * memory analyzers happy (don't access uninitialized memory, even
3798 * if it's an unsigned char). */
3799 peer_pms[0] = peer_pms[1] = ~0;
3800
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003801 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3802 peer_pms,
3803 &peer_pmslen,
3804 sizeof( peer_pms ) );
3805
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003806#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003807 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3808 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003809#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003812 ssl->handshake->max_minor_ver,
3813 ssl->conf->transport, ver );
3814
3815 /* Avoid data-dependent branches while checking for invalid
3816 * padding, to protect against timing-based Bleichenbacher-type
3817 * attacks. */
3818 diff = (unsigned int) ret;
3819 diff |= peer_pmslen ^ 48;
3820 diff |= peer_pms[0] ^ ver[0];
3821 diff |= peer_pms[1] ^ ver[1];
3822
3823 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3824 /* MSVC has a warning about unary minus on unsigned, but this is
3825 * well-defined and precisely what we want to do here */
3826#if defined(_MSC_VER)
3827#pragma warning( push )
3828#pragma warning( disable : 4146 )
3829#endif
3830 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3831#if defined(_MSC_VER)
3832#pragma warning( pop )
3833#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003834
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003835 /*
3836 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3837 * must not cause the connection to end immediately; instead, send a
3838 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003839 * To protect against timing-based variants of the attack, we must
3840 * not have any branch that depends on whether the decryption was
3841 * successful. In particular, always generate the fake premaster secret,
3842 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003843 */
Hanno Beckerece325c2019-06-13 15:39:27 +01003844 ret = mbedtls_ssl_conf_get_frng( ssl->conf )
3845 ( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003846 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003847 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003848 /* It's ok to abort on an RNG failure, since this does not reveal
3849 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003850 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003851 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003852
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003853#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003854 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003856#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003857
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003858 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3859 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3862 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003863 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003864 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003865
Gilles Peskine422ccab2018-01-11 18:29:01 +01003866 /* Set pms to either the true or the fake PMS, without
3867 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003868 for( i = 0; i < ssl->handshake->pmslen; i++ )
3869 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3870
3871 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3874 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
3877static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003878 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003879{
Paul Bakker6db455e2013-09-18 17:29:31 +02003880 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003881 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003882
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003883 if( ssl->conf->f_psk == NULL &&
3884 ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL ||
3885 ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3888 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003889 }
3890
3891 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003892 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003893 */
Hanno Becker83c9f492017-06-26 13:52:14 +01003894 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3897 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003898 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003899
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003900 n = ( (*p)[0] << 8 ) | (*p)[1];
3901 *p += 2;
3902
Hanno Becker83c9f492017-06-26 13:52:14 +01003903 if( n < 1 || n > 65535 || n > (size_t) ( end - *p ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3906 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003907 }
3908
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003909 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02003910 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003911 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003913 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003914 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003915 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003916 /* Identity is not a big secret since clients send it in the clear,
3917 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003918 if( n != ssl->conf->psk_identity_len ||
3919 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003922 }
3923 }
3924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02003928 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3929 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003931 }
3932
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003933 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003934
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003935 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003940{
Paul Bakker23986e52011-04-24 08:57:21 +00003941 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003943 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003944
Hanno Becker8759e162017-12-27 21:34:08 +00003945 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003948
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003949#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003950 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3951 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
3952 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3953 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003954 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003955 {
3956 /* We've already read a record and there is an asynchronous
3957 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02003958 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
3960 }
3961 else
3962#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01003963 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003966 return( ret );
3967 }
3968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003970 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3975 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003976 }
3977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3981 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 }
3983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3985 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003986 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003987 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003990 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003992
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003993 if( p != end )
3994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3996 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003997 }
3998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004000 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01004001 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02004002 &ssl->handshake->pmslen,
Hanno Beckerece325c2019-06-13 15:39:27 +01004003 mbedtls_ssl_conf_get_frng( ssl->conf ),
4004 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4007 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004008 }
4009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004011 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004012 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4014#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4015 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4016 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4017 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4018 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4019 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4020 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4021 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02004022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004024 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4027 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004028 }
4029
Janos Follath3fbdada2018-08-15 10:26:53 +01004030 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4031 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004034 &ssl->handshake->pmslen,
4035 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 MBEDTLS_MPI_MAX_SIZE,
Hanno Beckerece325c2019-06-13 15:39:27 +01004037 mbedtls_ssl_conf_get_frng( ssl->conf ),
4038 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4041 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004042 }
4043
Janos Follath3fbdada2018-08-15 10:26:53 +01004044 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4045 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00004046 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004047 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4049 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4050 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4051 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4052#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4053 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004054 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004055 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004058 return( ret );
4059 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004060
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004061 if( p != end )
4062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4064 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004065 }
4066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004068 ciphersuite_info->key_exchange ) ) != 0 )
4069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004071 return( ret );
4072 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4076#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4077 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004078 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004079#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004080 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004081 {
4082 /* There is an asynchronous operation in progress to
4083 * decrypt the encrypted premaster secret, so skip
4084 * directly to resuming this operation. */
4085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4086 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4087 * won't actually use it, but maintain p anyway for robustness. */
4088 p += ssl->conf->psk_identity_len + 2;
4089 }
4090 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004091#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004092 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004095 return( ret );
4096 }
4097
4098 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004101 return( ret );
4102 }
4103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004105 ciphersuite_info->key_exchange ) ) != 0 )
4106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004108 return( ret );
4109 }
4110 }
4111 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4113#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4114 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004115 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004116 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004119 return( ret );
4120 }
4121 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004124 return( ret );
4125 }
4126
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004127 if( p != end )
4128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4130 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004131 }
4132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004134 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004137 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004138 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004139 }
4140 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004141#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4142#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4143 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004144 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004145 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004148 return( ret );
4149 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004151 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004152 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4155 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004156 }
4157
Janos Follath3fbdada2018-08-15 10:26:53 +01004158 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4159 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004162 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004165 return( ret );
4166 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004167 }
4168 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4170#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4171 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004172 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004173 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004176 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004177 }
4178 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004179 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004181#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4182 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4183 {
4184 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4185 p, end - p );
4186 if( ret != 0 )
4187 {
4188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4189 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4190 }
4191
4192 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4193 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
Hanno Beckerece325c2019-06-13 15:39:27 +01004194 mbedtls_ssl_conf_get_frng( ssl->conf ),
4195 ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004196 if( ret != 0 )
4197 {
4198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4199 return( ret );
4200 }
4201 }
4202 else
4203#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004212 return( ret );
4213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004214
Paul Bakker5121ce52009-01-03 21:22:43 +00004215 ssl->state++;
4216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004218
4219 return( 0 );
4220}
4221
Hanno Beckerae39b9e2019-02-07 12:32:43 +00004222#if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004224{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004225 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00004226 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004229
Hanno Beckerae39b9e2019-02-07 12:32:43 +00004230 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004233 ssl->state++;
4234 return( 0 );
4235 }
4236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004239}
Hanno Beckerae39b9e2019-02-07 12:32:43 +00004240#else /* !MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004244 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004245 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004246 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004247 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4249 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004252 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00004253 ssl->handshake->ciphersuite_info;
Hanno Becker5882dd02019-06-06 16:25:57 +01004254 mbedtls_pk_context *peer_pk = NULL;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004257
Hanno Beckercd901262019-02-07 13:17:25 +00004258 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004261 ssl->state++;
4262 return( 0 );
4263 }
4264
Hanno Becker5882dd02019-06-06 16:25:57 +01004265 /* Skip if we haven't received a certificate from the client.
4266 * If MBEDTLS_SSL_KEEP_PEER_CERTIFICATE is set, this can be
4267 * inferred from the setting of mbedtls_ssl_session::peer_cert.
4268 * If MBEDTLS_SSL_KEEP_PEER_CERTIFICATE is not set, it can
4269 * be inferred from whether we've held back the peer CRT's
4270 * public key in mbedtls_ssl_handshake_params::peer_pubkey. */
4271#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4272 /* Because the peer CRT pubkey is embedded into the handshake
4273 * params currently, and there's no 'is_init' functions for PK
4274 * contexts, we need to break the abstraction and peek into
4275 * the PK context to see if it has been initialized. */
4276 if( ssl->handshake->peer_pubkey.pk_info != NULL )
4277 peer_pk = &ssl->handshake->peer_pubkey;
4278#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4279 if( ssl->session_negotiate->peer_cert != NULL )
Hanno Becker73cd8d82019-02-28 14:04:16 +00004280 {
4281 ret = mbedtls_x509_crt_pk_acquire( ssl->session_negotiate->peer_cert,
4282 &peer_pk );
4283 if( ret != 0 )
4284 {
Hanno Becker2224ccf2019-06-28 10:52:45 +01004285 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
4286 return( ret );
Hanno Becker73cd8d82019-02-28 14:04:16 +00004287 }
4288 }
Hanno Becker5882dd02019-06-06 16:25:57 +01004289#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4290
4291 if( peer_pk == NULL )
Hanno Beckercd901262019-02-07 13:17:25 +00004292 {
4293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4294 ssl->state++;
4295 return( 0 );
4296 }
Hanno Beckercd901262019-02-07 13:17:25 +00004297
Simon Butcher99000142016-10-13 17:21:01 +01004298 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004299 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004300 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004301 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004302 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Hanno Beckerbc6b5982019-07-02 15:36:44 +01004303 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00004304 }
4305
4306 ssl->state++;
4307
Simon Butcher99000142016-10-13 17:21:01 +01004308 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4310 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerbc6b5982019-07-02 15:36:44 +01004313 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
4314 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00004315 }
4316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004318
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004319 /*
4320 * struct {
4321 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4322 * opaque signature<0..2^16-1>;
4323 * } DigitallySigned;
4324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4326 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4327 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01004328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004330 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004331
4332 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Hanno Becker0833c102019-02-06 18:31:04 +00004333 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004334 {
4335 hash_start += 16;
4336 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004338 }
Paul Bakker926af752012-11-23 13:38:07 +01004339 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004340 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4342 MBEDTLS_SSL_PROTO_TLS1_1 */
4343#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4344 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004345 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004346 if( i + 2 > ssl->in_hslen )
4347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Becker73cd8d82019-02-28 14:04:16 +00004349 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
4350 goto exit;
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004351 }
4352
Paul Bakker5121ce52009-01-03 21:22:43 +00004353 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004354 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 */
Simon Butcher99000142016-10-13 17:21:01 +01004356 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4357
4358 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004361 " for verify message" ) );
Hanno Becker73cd8d82019-02-28 14:04:16 +00004362 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
4363 goto exit;
Paul Bakker926af752012-11-23 13:38:07 +01004364 }
4365
Simon Butcher99000142016-10-13 17:21:01 +01004366#if !defined(MBEDTLS_MD_SHA1)
4367 if( MBEDTLS_MD_SHA1 == md_alg )
4368 hash_start += 16;
4369#endif
Paul Bakker926af752012-11-23 13:38:07 +01004370
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004371 /* Info from md_alg will be used instead */
4372 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004373
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004374 i++;
4375
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004376 /*
4377 * Signature
4378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4380 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004383 " for verify message" ) );
Hanno Becker73cd8d82019-02-28 14:04:16 +00004384 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
4385 goto exit;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004386 }
4387
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004388 /*
4389 * Check the certificate's key type matches the signature alg
4390 */
Hanno Becker0833c102019-02-06 18:31:04 +00004391 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
Hanno Becker73cd8d82019-02-28 14:04:16 +00004394 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
4395 goto exit;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004396 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004397
4398 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004399 }
4400 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004405 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004406
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004407 if( i + 2 > ssl->in_hslen )
4408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Becker73cd8d82019-02-28 14:04:16 +00004410 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
4411 goto exit;
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004412 }
4413
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004414 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4415 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004416
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004417 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Becker73cd8d82019-02-28 14:04:16 +00004420 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
4421 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00004422 }
4423
Simon Butcher99000142016-10-13 17:21:01 +01004424 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02004425 {
4426 size_t dummy_hlen;
4427 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4428 }
Simon Butcher99000142016-10-13 17:21:01 +01004429
Hanno Becker0833c102019-02-06 18:31:04 +00004430 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004431 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004432 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Hanno Becker73cd8d82019-02-28 14:04:16 +00004435 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00004436 }
4437
Simon Butcher99000142016-10-13 17:21:01 +01004438 mbedtls_ssl_update_handshake_status( ssl );
4439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004441
Hanno Becker73cd8d82019-02-28 14:04:16 +00004442exit:
4443
4444#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00004445 mbedtls_x509_crt_pk_release( ssl->session_negotiate->peer_cert );
Hanno Becker73cd8d82019-02-28 14:04:16 +00004446#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4447
Paul Bakkered27a042013-04-18 22:46:23 +02004448 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004449}
Hanno Beckerae39b9e2019-02-07 12:32:43 +00004450#endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4453static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004454{
4455 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004456 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004457 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4462 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004463
4464 /*
4465 * struct {
4466 * uint32 ticket_lifetime_hint;
4467 * opaque ticket<0..2^16-1>;
4468 * } NewSessionTicket;
4469 *
4470 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4471 * 8 . 9 ticket_len (n)
4472 * 10 . 9+n ticket content
4473 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004474
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004475 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004476 ssl->session_negotiate,
4477 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004478 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004479 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004480 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004482 tlen = 0;
4483 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004484
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004485 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4486 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4487 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4488 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4489
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004490 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4491 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004492
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004493 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004494
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004495 /*
4496 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4497 * ChangeCipherSpec share the same state.
4498 */
4499 ssl->handshake->new_session_ticket = 0;
4500
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004501 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004502 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004504 return( ret );
4505 }
4506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004508
4509 return( 0 );
4510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004512
Paul Bakker5121ce52009-01-03 21:22:43 +00004513/*
Paul Bakker1961b702013-01-25 14:49:24 +01004514 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004517{
4518 int ret = 0;
4519
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004520 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004526 return( ret );
4527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004529 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004531 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004532 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004533 return( ret );
4534 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004535#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004536
Paul Bakker1961b702013-01-25 14:49:24 +01004537 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 case MBEDTLS_SSL_HELLO_REQUEST:
4540 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004541 break;
4542
Paul Bakker1961b702013-01-25 14:49:24 +01004543 /*
4544 * <== ClientHello
4545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004547 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550#if defined(MBEDTLS_SSL_PROTO_DTLS)
4551 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4552 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004553#endif
4554
Paul Bakker1961b702013-01-25 14:49:24 +01004555 /*
4556 * ==> ServerHello
4557 * Certificate
4558 * ( ServerKeyExchange )
4559 * ( CertificateRequest )
4560 * ServerHelloDone
4561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004563 ret = ssl_write_server_hello( ssl );
4564 break;
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4567 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004568 break;
4569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004571 ret = ssl_write_server_key_exchange( ssl );
4572 break;
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004575 ret = ssl_write_certificate_request( ssl );
4576 break;
4577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004579 ret = ssl_write_server_hello_done( ssl );
4580 break;
4581
4582 /*
4583 * <== ( Certificate/Alert )
4584 * ClientKeyExchange
4585 * ( CertificateVerify )
4586 * ChangeCipherSpec
4587 * Finished
4588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4590 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004591 break;
4592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004594 ret = ssl_parse_client_key_exchange( ssl );
4595 break;
4596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004598 ret = ssl_parse_certificate_verify( ssl );
4599 break;
4600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4602 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004603 break;
4604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605 case MBEDTLS_SSL_CLIENT_FINISHED:
4606 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004607 break;
4608
4609 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004610 * ==> ( NewSessionTicket )
4611 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004612 * Finished
4613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004614 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4615#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004616 if( ssl->handshake->new_session_ticket != 0 )
4617 ret = ssl_write_new_session_ticket( ssl );
4618 else
Paul Bakkera503a632013-08-14 13:48:06 +02004619#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004621 break;
4622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 case MBEDTLS_SSL_SERVER_FINISHED:
4624 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004625 break;
4626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 case MBEDTLS_SSL_FLUSH_BUFFERS:
4628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4629 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004630 break;
4631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4633 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004634 break;
4635
4636 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 }
4640
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 return( ret );
4642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643#endif /* MBEDTLS_SSL_SRV_C */