blob: d871dba66bec9f6b8456e5f8ebddd7a2f7a72808 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS client-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020026#else
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020028#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010029#define mbedtls_free free
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020030#endif
31
SimonBd5800b72016-04-26 07:43:27 +010032#include "mbedtls/ssl.h"
Chris Jones84a773f2021-03-05 18:38:47 +000033#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000034#include "mbedtls/debug.h"
35#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010036
Hanno Beckerbb89e272019-01-08 12:54:37 +000037#if defined(MBEDTLS_USE_PSA_CRYPTO)
38#include "mbedtls/psa_util.h"
39#endif /* MBEDTLS_USE_PSA_CRYPTO */
40
SimonBd5800b72016-04-26 07:43:27 +010041#include <string.h>
42
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020043#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010046#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050050#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020051#endif
52
Gilles Peskineeccd8882020-03-10 12:19:08 +010053#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker520224e2018-10-26 11:38:07 +010054static int ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2e4f6162018-10-23 11:54:44 +010055{
56 if( conf->psk_identity == NULL ||
57 conf->psk_identity_len == 0 )
58 {
59 return( 0 );
60 }
61
62 if( conf->psk != NULL && conf->psk_len != 0 )
63 return( 1 );
64
65#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020066 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010067 return( 1 );
68#endif /* MBEDTLS_USE_PSA_CRYPTO */
69
70 return( 0 );
71}
Hanno Beckerdfab8e22018-10-23 11:59:34 +010072
73#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker520224e2018-10-26 11:38:07 +010074static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
Hanno Beckerdfab8e22018-10-23 11:59:34 +010075{
76 if( conf->psk_identity == NULL ||
77 conf->psk_identity_len == 0 )
78 {
79 return( 0 );
80 }
81
82 if( conf->psk != NULL && conf->psk_len != 0 )
83 return( 1 );
84
85 return( 0 );
86}
87#endif /* MBEDTLS_USE_PSA_CRYPTO */
88
Gilles Peskineeccd8882020-03-10 12:19:08 +010089#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +000092int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
93 unsigned char *buf,
94 const unsigned char *end,
95 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010096{
97 unsigned char *p = buf;
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +010098 size_t hostname_len;
Paul Bakkerd3edc862013-03-20 16:07:17 +010099
100 *olen = 0;
101
Paul Bakker66d5d072014-06-17 16:39:18 +0200102 if( ssl->hostname == NULL )
Hanno Becker261602c2017-04-12 14:54:42 +0100103 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100104
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100105 MBEDTLS_SSL_DEBUG_MSG( 3,
106 ( "client hello, adding server name extension: %s",
107 ssl->hostname ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100108
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +0100109 hostname_len = strlen( ssl->hostname );
110
Hanno Becker261602c2017-04-12 14:54:42 +0100111 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
Simon Butchered997662015-09-28 02:14:30 +0100112
Paul Bakkerd3edc862013-03-20 16:07:17 +0100113 /*
Hanno Becker1a9a51c2017-04-07 13:02:16 +0100114 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
115 *
116 * In order to provide any of the server names, clients MAY include an
117 * extension of type "server_name" in the (extended) client hello. The
118 * "extension_data" field of this extension SHALL contain
119 * "ServerNameList" where:
120 *
Paul Bakkerd3edc862013-03-20 16:07:17 +0100121 * struct {
122 * NameType name_type;
123 * select (name_type) {
124 * case host_name: HostName;
125 * } name;
126 * } ServerName;
127 *
128 * enum {
129 * host_name(0), (255)
130 * } NameType;
131 *
132 * opaque HostName<1..2^16-1>;
133 *
134 * struct {
135 * ServerName server_name_list<1..2^16-1>
136 * } ServerNameList;
Hanno Becker1a9a51c2017-04-07 13:02:16 +0100137 *
Paul Bakkerd3edc862013-03-20 16:07:17 +0100138 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100139 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
140 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100141
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100142 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
143 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100144
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100145 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
146 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100147
Joe Subbiani2194dc42021-07-14 12:31:31 +0100148 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100149
150 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
151 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100152
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +0100153 memcpy( p, ssl->hostname, hostname_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100154
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +0100155 *olen = hostname_len + 9;
Hanno Becker261602c2017-04-12 14:54:42 +0100156
157 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +0100162static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
163 unsigned char *buf,
164 const unsigned char *end,
165 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100166{
167 unsigned char *p = buf;
168
169 *olen = 0;
170
Hanno Becker40f8b512017-10-12 14:58:55 +0100171 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
172 * initial ClientHello, in which case also adding the renegotiation
173 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +0100175 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100176
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100177 MBEDTLS_SSL_DEBUG_MSG( 3,
178 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100179
Hanno Becker261602c2017-04-12 14:54:42 +0100180 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +0100181
Paul Bakkerd3edc862013-03-20 16:07:17 +0100182 /*
183 * Secure renegotiation
184 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100185 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
186 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100187
188 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100189 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
190 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100191
192 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
193
194 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100195
196 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100199
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100200/*
201 * Only if we handle at least one key exchange that needs signatures.
202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100204 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100205static int ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl,
206 unsigned char *buf,
207 const unsigned char *end,
208 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100209{
210 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100211 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200212 const int *md;
Hanno Becker261602c2017-04-12 14:54:42 +0100213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200215 unsigned char *sig_alg_list = buf + 6;
216#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100217
218 *olen = 0;
219
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200220 if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker261602c2017-04-12 14:54:42 +0100221 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100222
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100223 MBEDTLS_SSL_DEBUG_MSG( 3,
224 ( "client hello, adding signature_algorithms extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100225
Hanno Beckere131bfe2017-04-12 14:54:42 +0100226 if( ssl->conf->sig_hashes == NULL )
227 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
228
Simon Butchered997662015-09-28 02:14:30 +0100229 for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
230 {
231#if defined(MBEDTLS_ECDSA_C)
232 sig_alg_len += 2;
233#endif
234#if defined(MBEDTLS_RSA_C)
235 sig_alg_len += 2;
236#endif
Hanno Beckere131bfe2017-04-12 14:54:42 +0100237 if( sig_alg_len > MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN )
238 {
239 MBEDTLS_SSL_DEBUG_MSG( 3,
240 ( "length in bytes of sig-hash-alg extension too big" ) );
241 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
242 }
Simon Butchered997662015-09-28 02:14:30 +0100243 }
244
Hanno Beckere131bfe2017-04-12 14:54:42 +0100245 /* Empty signature algorithms list, this is a configuration error. */
246 if( sig_alg_len == 0 )
247 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
248
Hanno Becker261602c2017-04-12 14:54:42 +0100249 MBEDTLS_SSL_CHK_BUF_PTR( p, end, sig_alg_len + 6 );
Simon Butchered997662015-09-28 02:14:30 +0100250
Paul Bakkerd3edc862013-03-20 16:07:17 +0100251 /*
252 * Prepare signature_algorithms extension (TLS 1.2)
253 */
Simon Butchered997662015-09-28 02:14:30 +0100254 sig_alg_len = 0;
255
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200256 for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200259 sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
260 sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200261#endif
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200262#if defined(MBEDTLS_RSA_C)
263 sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
264 sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200265#endif
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200266 }
Paul Bakkerd3edc862013-03-20 16:07:17 +0100267
268 /*
269 * enum {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200270 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
271 * sha512(6), (255)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100272 * } HashAlgorithm;
273 *
274 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
275 * SignatureAlgorithm;
276 *
277 * struct {
278 * HashAlgorithm hash;
279 * SignatureAlgorithm signature;
280 * } SignatureAndHashAlgorithm;
281 *
282 * SignatureAndHashAlgorithm
283 * supported_signature_algorithms<2..2^16-2>;
284 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100285 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, p, 0 );
286 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100287
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100288 MBEDTLS_PUT_UINT16_BE( sig_alg_len + 2, p, 0 );
289 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100290
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100291 MBEDTLS_PUT_UINT16_BE( sig_alg_len, p, 0 );
292 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100293
Paul Bakkerd3edc862013-03-20 16:07:17 +0100294 *olen = 6 + sig_alg_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100295
296 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100299 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100300
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200301#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100302 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100303static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl,
304 unsigned char *buf,
305 const unsigned char *end,
306 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100307{
308 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100309 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100310 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 const mbedtls_ecp_curve_info *info;
Brett Warren01f3dae2021-08-17 13:50:51 +0100312 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313 *olen = 0;
314
Brett Warren01f3dae2021-08-17 13:50:51 +0100315 /* Check there is room for header */
316 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
317
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100318 MBEDTLS_SSL_DEBUG_MSG( 3,
319 ( "client hello, adding supported_elliptic_curves extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100320
Brett Warren01f3dae2021-08-17 13:50:51 +0100321 if( group_list == NULL )
Hanno Beckere131bfe2017-04-12 14:54:42 +0100322 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
323
Brett Warren01f3dae2021-08-17 13:50:51 +0100324 for( ; *group_list != 0; group_list++ )
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100325 {
Brett Warren01f3dae2021-08-17 13:50:51 +0100326 info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
Janos Follath8a317052016-04-21 23:37:09 +0100327 if( info == NULL )
328 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100329 MBEDTLS_SSL_DEBUG_MSG( 1,
330 ( "invalid curve in ssl configuration" ) );
Hanno Beckere131bfe2017-04-12 14:54:42 +0100331 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Janos Follath8a317052016-04-21 23:37:09 +0100332 }
Brett Warren01f3dae2021-08-17 13:50:51 +0100333
334 /* Check there is room for another curve */
335 MBEDTLS_SSL_CHK_BUF_PTR( elliptic_curve_list, end, elliptic_curve_len + 2 );
336
337 MBEDTLS_PUT_UINT16_BE( *group_list, elliptic_curve_list, elliptic_curve_len );
Simon Butchered997662015-09-28 02:14:30 +0100338 elliptic_curve_len += 2;
Hanno Beckere131bfe2017-04-12 14:54:42 +0100339
340 if( elliptic_curve_len > MBEDTLS_SSL_MAX_CURVE_LIST_LEN )
341 {
342 MBEDTLS_SSL_DEBUG_MSG( 3,
343 ( "malformed supported_elliptic_curves extension in config" ) );
344 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
345 }
Simon Butchered997662015-09-28 02:14:30 +0100346 }
347
Hanno Beckere131bfe2017-04-12 14:54:42 +0100348 /* Empty elliptic curve list, this is a configuration error. */
Hanno Becker261602c2017-04-12 14:54:42 +0100349 if( elliptic_curve_len == 0 )
Hanno Beckere131bfe2017-04-12 14:54:42 +0100350 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Hanno Becker261602c2017-04-12 14:54:42 +0100351
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100352 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES, p, 0 );
353 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100354
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100355 MBEDTLS_PUT_UINT16_BE( elliptic_curve_len + 2, p, 0 );
356 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100357
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100358 MBEDTLS_PUT_UINT16_BE( elliptic_curve_len, p, 0 );
359 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100360
Paul Bakkerd3edc862013-03-20 16:07:17 +0100361 *olen = 6 + elliptic_curve_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100362
363 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100364}
365
Hanno Becker261602c2017-04-12 14:54:42 +0100366static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
367 unsigned char *buf,
368 const unsigned char *end,
369 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100370{
371 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100372 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100373
374 *olen = 0;
375
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100376 MBEDTLS_SSL_DEBUG_MSG( 3,
377 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100378 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100379
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100380 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
381 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100382
383 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100384 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200385
386 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100388
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200389 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100390
391 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100392}
Darryl Green11999bb2018-03-13 15:22:58 +0000393#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100394 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100395
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200396#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100397static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
398 unsigned char *buf,
399 const unsigned char *end,
400 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200401{
Janos Follath865b3eb2019-12-16 11:46:15 +0000402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200403 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200404 size_t kkpp_len;
405
406 *olen = 0;
407
408 /* Skip costly extension if we can't use EC J-PAKE anyway */
409 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100410 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200411
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100412 MBEDTLS_SSL_DEBUG_MSG( 3,
413 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200414
Hanno Becker261602c2017-04-12 14:54:42 +0100415 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200416
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100417 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
418 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200419
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200420 /*
421 * We may need to send ClientHello multiple times for Hello verification.
422 * We don't want to compute fresh values every time (both for performance
423 * and consistency reasons), so cache the extension content.
424 */
425 if( ssl->handshake->ecjpake_cache == NULL ||
426 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200427 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200428 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
429
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200430 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100431 p + 2, end - p - 2, &kkpp_len,
432 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200433 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200434 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100435 MBEDTLS_SSL_DEBUG_RET( 1 ,
436 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100437 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200438 }
439
440 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
441 if( ssl->handshake->ecjpake_cache == NULL )
442 {
443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100444 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200445 }
446
447 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
448 ssl->handshake->ecjpake_cache_len = kkpp_len;
449 }
450 else
451 {
452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
453
454 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100455 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200456
457 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200458 }
459
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100460 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
461 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200462
463 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100464
465 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200466}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200467#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000468
Hanno Beckera0e20d02019-05-15 14:03:01 +0100469#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +0100470static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
471 unsigned char *buf,
472 const unsigned char *end,
473 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100474{
475 unsigned char *p = buf;
476 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100477
478 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100479 * Quoting draft-ietf-tls-dtls-connection-id-05
480 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100481 *
482 * struct {
483 * opaque cid<0..2^8-1>;
484 * } ConnectionId;
485 */
486
487 *olen = 0;
488 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
489 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
490 {
Hanno Becker261602c2017-04-12 14:54:42 +0100491 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100492 }
493 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
494
495 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
496 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100497 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100498
499 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100500 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
501 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100502 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100503 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
504 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100505
506 *p++ = (uint8_t) ssl->own_cid_len;
507 memcpy( p, ssl->own_cid, ssl->own_cid_len );
508
509 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100510
511 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100512}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100513#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +0100516static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
517 unsigned char *buf,
518 const unsigned char *end,
519 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200520{
521 unsigned char *p = buf;
522
Simon Butcher0fc94e92015-09-28 20:52:04 +0100523 *olen = 0;
524
Hanno Becker261602c2017-04-12 14:54:42 +0100525 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
526 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200527
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100528 MBEDTLS_SSL_DEBUG_MSG( 3,
529 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200530
Hanno Becker261602c2017-04-12 14:54:42 +0100531 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100532
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100533 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
534 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200535
536 *p++ = 0x00;
537 *p++ = 1;
538
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200539 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200540
541 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100542
543 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200544}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +0100548static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
549 unsigned char *buf,
550 const unsigned char *end,
551 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100552{
553 unsigned char *p = buf;
554
Simon Butcher0fc94e92015-09-28 20:52:04 +0100555 *olen = 0;
556
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100557 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100558 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100559
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100560 MBEDTLS_SSL_DEBUG_MSG( 3,
561 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100562
Hanno Becker261602c2017-04-12 14:54:42 +0100563 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100564
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100565 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
566 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100567
568 *p++ = 0x00;
569 *p++ = 0x00;
570
571 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100572
573 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +0100578static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
579 unsigned char *buf,
580 const unsigned char *end,
581 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200582{
583 unsigned char *p = buf;
584
Simon Butcher0fc94e92015-09-28 20:52:04 +0100585 *olen = 0;
586
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100587 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100588 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200589
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100590 MBEDTLS_SSL_DEBUG_MSG( 3,
591 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200592
Hanno Becker261602c2017-04-12 14:54:42 +0100593 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100594
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100595 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
596 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200597
598 *p++ = 0x00;
599 *p++ = 0x00;
600
601 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100602
603 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +0100608static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
609 unsigned char *buf,
610 const unsigned char *end,
611 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200612{
613 unsigned char *p = buf;
614 size_t tlen = ssl->session_negotiate->ticket_len;
615
Simon Butcher0fc94e92015-09-28 20:52:04 +0100616 *olen = 0;
617
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200618 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100619 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200620
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100621 MBEDTLS_SSL_DEBUG_MSG( 3,
622 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200623
Hanno Becker261602c2017-04-12 14:54:42 +0100624 /* The addition is safe here since the ticket length is 16 bit. */
625 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100626
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100627 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
628 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200629
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100630 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
631 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200632
633 *olen = 4;
634
Simon Butchered997662015-09-28 02:14:30 +0100635 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100636 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200637
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100638 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000639 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200640
641 memcpy( p, ssl->session_negotiate->ticket, tlen );
642
643 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100644
645 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker261602c2017-04-12 14:54:42 +0100650static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
651 unsigned char *buf,
652 const unsigned char *end,
653 size_t *olen )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200654{
655 unsigned char *p = buf;
Simon Butchered997662015-09-28 02:14:30 +0100656 size_t alpnlen = 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200657 const char **cur;
658
Simon Butcher0fc94e92015-09-28 20:52:04 +0100659 *olen = 0;
660
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200661 if( ssl->conf->alpn_list == NULL )
Hanno Becker261602c2017-04-12 14:54:42 +0100662 return( 0 );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200665
Simon Butchered997662015-09-28 02:14:30 +0100666 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
Hanno Beckere131bfe2017-04-12 14:54:42 +0100667 alpnlen += strlen( *cur ) + 1;
Simon Butchered997662015-09-28 02:14:30 +0100668
Hanno Becker261602c2017-04-12 14:54:42 +0100669 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen );
Simon Butchered997662015-09-28 02:14:30 +0100670
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100671 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
672 p += 2;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200673
674 /*
675 * opaque ProtocolName<1..2^8-1>;
676 *
677 * struct {
678 * ProtocolName protocol_name_list<2..2^16-1>
679 * } ProtocolNameList;
680 */
681
682 /* Skip writing extension and list length for now */
683 p += 4;
684
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200685 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200686 {
Hanno Beckere131bfe2017-04-12 14:54:42 +0100687 /*
688 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
689 * protocol names is less than 255.
690 */
691 *p = (unsigned char)strlen( *cur );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200692 memcpy( p + 1, *cur, *p );
693 p += 1 + *p;
694 }
695
696 *olen = p - buf;
697
698 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
Joe Subbiani6dd73642021-07-19 11:56:54 +0100699 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200700
701 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
Joe Subbiani6dd73642021-07-19 11:56:54 +0100702 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Hanno Becker261602c2017-04-12 14:54:42 +0100703
704 return( 0 );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200707
Ron Eldora9788042018-12-05 11:04:31 +0200708#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal77696ee2020-09-22 21:49:40 +0200709static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200710 unsigned char *buf,
711 const unsigned char *end,
712 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100713{
714 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200715 size_t protection_profiles_index = 0, ext_len = 0;
716 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100717
718 *olen = 0;
719
Johan Pascalc3ccd982020-10-28 17:18:18 +0100720 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
721 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
722 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100723 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200724 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100725 }
726
Ron Eldora9788042018-12-05 11:04:31 +0200727 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100728 * uint8 SRTPProtectionProfile[2];
729 *
730 * struct {
731 * SRTPProtectionProfiles SRTPProtectionProfiles;
732 * opaque srtp_mki<0..255>;
733 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100734 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100735 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200736 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200737 {
738 mki_len = ssl->dtls_srtp_info.mki_len;
739 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300740 /* Extension length = 2 bytes for profiles length,
741 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
742 * 1 byte for srtp_mki vector length and the mki_len value
743 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200744 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
745
Johan Pascal77696ee2020-09-22 21:49:40 +0200746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
747
748 /* Check there is room in the buffer for the extension + 4 bytes
749 * - the extension tag (2 bytes)
750 * - the extension length (2 bytes)
751 */
752 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
753
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100754 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
755 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200756
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100757 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
758 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100759
Ron Eldor3adb9922017-12-21 10:15:08 +0200760 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200761 /* micro-optimization:
762 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
763 * which is lower than 127, so the upper byte of the length is always 0
764 * For the documentation, the more generic code is left in comments
765 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
766 * >> 8 ) & 0xFF );
767 */
768 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100769 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100770
Ron Eldoref72faf2018-07-12 11:54:20 +0300771 for( protection_profiles_index=0;
772 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
773 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100774 {
Johan Pascal43f94902020-09-22 12:25:52 +0200775 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200776 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200777 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200778 {
779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
780 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100781 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
782 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200783 }
784 else
785 {
786 /*
787 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200788 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200789 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200790 MBEDTLS_SSL_DEBUG_MSG( 3,
791 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200792 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200793 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
794 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200795 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100796 }
797 }
798
Ron Eldor591f1622018-01-22 12:30:04 +0200799 *p++ = mki_len & 0xFF;
800
801 if( mki_len != 0 )
802 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200803 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200804 /*
805 * Increment p to point to the current position.
806 */
807 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300808 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
809 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200810 }
811
Ron Eldoref72faf2018-07-12 11:54:20 +0300812 /*
813 * total extension length: extension type (2 bytes)
814 * + extension length (2 bytes)
815 * + protection profile length (2 bytes)
816 * + 2 * number of protection profiles
817 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200818 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300819 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200820 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200821
822 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100823}
824#endif /* MBEDTLS_SSL_DTLS_SRTP */
825
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200826/*
827 * Generate random bytes for ClientHello
828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829static int ssl_generate_random( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200830{
Janos Follath865b3eb2019-12-16 11:46:15 +0000831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200832 unsigned char *p = ssl->handshake->randbytes;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100834 mbedtls_time_t t;
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200835#endif
836
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200837 /*
838 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200841 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200842 ssl->handshake->verify_cookie != NULL )
843 {
844 return( 0 );
845 }
846#endif
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100849 t = mbedtls_time( NULL );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100850 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
851 p += 4;
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200852
Paul Elliottd48d5c62021-01-07 14:47:05 +0000853 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
854 (long long) t ) );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200855#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +0100856 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200857 return( ret );
858
859 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#endif /* MBEDTLS_HAVE_TIME */
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200861
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +0100862 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200863 return( ret );
864
865 return( 0 );
866}
867
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100868/**
869 * \brief Validate cipher suite against config in SSL context.
870 *
871 * \param suite_info cipher suite to validate
872 * \param ssl SSL context
Andrzej Kurek03bac442018-04-25 05:06:07 -0400873 * \param min_minor_ver Minimal minor version to accept a cipher suite
874 * \param max_minor_ver Maximal minor version to accept a cipher suite
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100875 *
876 * \return 0 if valid, else 1
877 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100878static int ssl_validate_ciphersuite(
879 const mbedtls_ssl_ciphersuite_t * suite_info,
880 const mbedtls_ssl_context * ssl,
881 int min_minor_ver, int max_minor_ver )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100882{
Andrzej Kurek03bac442018-04-25 05:06:07 -0400883 (void) ssl;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100884 if( suite_info == NULL )
885 return( 1 );
886
Andrzej Kurek03bac442018-04-25 05:06:07 -0400887 if( suite_info->min_minor_ver > max_minor_ver ||
888 suite_info->max_minor_ver < min_minor_ver )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100889 return( 1 );
890
891#if defined(MBEDTLS_SSL_PROTO_DTLS)
892 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
893 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
894 return( 1 );
895#endif
896
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100897#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
898 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
899 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
900 return( 1 );
901#endif
902
Hanno Becker2e4f6162018-10-23 11:54:44 +0100903 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100904#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker2e4f6162018-10-23 11:54:44 +0100905 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker520224e2018-10-26 11:38:07 +0100906 ssl_conf_has_static_psk( ssl->conf ) == 0 )
Hanno Becker2e4f6162018-10-23 11:54:44 +0100907 {
908 return( 1 );
909 }
Gilles Peskineeccd8882020-03-10 12:19:08 +0100910#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +0100911
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100912 return( 0 );
913}
914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000916{
Janos Follath865b3eb2019-12-16 11:46:15 +0000917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100918 size_t i, n, olen, ext_len = 0;
Hanno Becker261602c2017-04-12 14:54:42 +0100919
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200921 unsigned char *p, *q;
Hanno Becker261602c2017-04-12 14:54:42 +0100922 const unsigned char *end;
923
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200924 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ron Eldor755bb6a2018-02-14 19:30:48 +0200926#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
927 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
928 int uses_ec = 0;
929#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +0100933 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +0100934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
936 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +0100937 }
938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939#if defined(MBEDTLS_SSL_RENEGOTIATION)
940 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100941#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000942 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200943 ssl->major_ver = ssl->conf->min_major_ver;
944 ssl->minor_ver = ssl->conf->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000945 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200947 if( ssl->conf->max_major_ver == 0 )
Paul Bakker490ecc82011-10-06 13:04:09 +0000948 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100949 MBEDTLS_SSL_DEBUG_MSG( 1,
950 ( "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()" ) );
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker490ecc82011-10-06 13:04:09 +0000952 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Hanno Becker261602c2017-04-12 14:54:42 +0100954 buf = ssl->out_msg;
955 end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN;
956
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 /*
Hanno Becker261602c2017-04-12 14:54:42 +0100958 * Check if there's enough space for the first part of the ClientHello
959 * consisting of the 38 bytes described below, the session identifier (at
960 * most 32 bytes) and its length (1 byte).
961 *
962 * Use static upper bounds instead of the actual values
963 * to allow the compiler to optimize this away.
964 */
965 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );
966
967 /*
968 * The 38 first bytes of the ClientHello:
969 * 0 . 0 handshake type (written later)
970 * 1 . 3 handshake length (written later)
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 * 4 . 5 highest version supported
972 * 6 . 9 current UNIX time
973 * 10 . 37 random bytes
Hanno Becker261602c2017-04-12 14:54:42 +0100974 *
975 * The current UNIX time (4 bytes) and following 28 random bytes are written
976 * by ssl_generate_random() into ssl->handshake->randbytes buffer and then
977 * copied from there into the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Hanno Becker261602c2017-04-12 14:54:42 +0100980 p = buf + 4;
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100981 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
982 ssl->conf->max_minor_ver,
983 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100984 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +0000987 buf[4], buf[5] ) );
988
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200989 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200992 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200993 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200994
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200995 memcpy( p, ssl->handshake->randbytes, 32 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200997 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
999 /*
1000 * 38 . 38 session id length
1001 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001002 * 39+n . 39+n DTLS only: cookie length (1 byte)
Hanno Becker261602c2017-04-12 14:54:42 +01001003 * 40+n . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001004 * .. . .. ciphersuitelist length (2 bytes)
1005 * .. . .. ciphersuitelist
1006 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001007 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001008 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001009 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001011 n = ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001013 if( n < 16 || n > 32 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#if defined(MBEDTLS_SSL_RENEGOTIATION)
1015 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001016#endif
Paul Bakker0a597072012-09-25 21:55:46 +00001017 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +02001018 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +02001020 }
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +02001023 /*
1024 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
1025 * generate and include a Session ID in the TLS ClientHello."
1026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#if defined(MBEDTLS_SSL_RENEGOTIATION)
1028 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001029#endif
Manuel Pégourié-Gonnardd2b35ec2015-03-10 11:40:43 +00001030 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001031 if( ssl->session_negotiate->ticket != NULL &&
1032 ssl->session_negotiate->ticket_len != 0 )
1033 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001034 ret = ssl->conf->f_rng( ssl->conf->p_rng,
1035 ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +02001036
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001037 if( ret != 0 )
1038 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +02001039
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001040 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001041 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +02001042 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Hanno Becker261602c2017-04-12 14:54:42 +01001045 /*
1046 * The first check of the output buffer size above (
1047 * MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );)
1048 * has checked that there is enough space in the output buffer for the
1049 * session identifier length byte and the session identifier (n <= 32).
1050 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 *p++ = (unsigned char) n;
1052
1053 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +00001054 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Paul Elliottd48d5c62021-01-07 14:47:05 +00001056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001059 /*
Hanno Becker261602c2017-04-12 14:54:42 +01001060 * With 'n' being the length of the session identifier
1061 *
1062 * 39+n . 39+n DTLS only: cookie length (1 byte)
1063 * 40+n . .. DTLS only: cookie
1064 * .. . .. ciphersuitelist length (2 bytes)
1065 * .. . .. ciphersuitelist
1066 * .. . .. compression methods length (1 byte)
1067 * .. . .. compression methods
1068 * .. . .. extensions length (2 bytes)
1069 * .. . .. extensions
1070 */
1071
1072 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001073 * DTLS cookie
1074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001077 {
Hanno Becker261602c2017-04-12 14:54:42 +01001078 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1079
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001080 if( ssl->handshake->verify_cookie == NULL )
1081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001083 *p++ = 0;
1084 }
1085 else
1086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001088 ssl->handshake->verify_cookie,
1089 ssl->handshake->verify_cookie_len );
1090
1091 *p++ = ssl->handshake->verify_cookie_len;
Hanno Becker261602c2017-04-12 14:54:42 +01001092
1093 MBEDTLS_SSL_CHK_BUF_PTR( p, end,
1094 ssl->handshake->verify_cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001095 memcpy( p, ssl->handshake->verify_cookie,
1096 ssl->handshake->verify_cookie_len );
1097 p += ssl->handshake->verify_cookie_len;
1098 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001099 }
1100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Paul Bakker48916f92012-09-16 19:57:18 +00001102 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001103 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +00001104 */
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001105 ciphersuites = ssl->conf->ciphersuite_list;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001106
1107 /* Skip writing ciphersuite length for now */
1108 n = 0;
1109 q = p;
Hanno Becker261602c2017-04-12 14:54:42 +01001110
1111 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001112 p += 2;
1113
Paul Bakker2fbefde2013-06-29 16:01:15 +02001114 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker2fbefde2013-06-29 16:01:15 +02001117
Andrzej Kurek03bac442018-04-25 05:06:07 -04001118 if( ssl_validate_ciphersuite( ciphersuite_info, ssl,
1119 ssl->conf->min_minor_ver,
1120 ssl->conf->max_minor_ver ) != 0 )
Paul Bakker2fbefde2013-06-29 16:01:15 +02001121 continue;
1122
Hanno Becker3c88c652019-01-02 11:17:25 +00001123 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)",
Paul Elliott9f352112020-12-09 14:55:45 +00001124 (unsigned int)ciphersuites[i], ciphersuite_info->name ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Ron Eldor755bb6a2018-02-14 19:30:48 +02001126#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1127 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1128 uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
1129#endif
1130
Hanno Becker261602c2017-04-12 14:54:42 +01001131 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1132
Paul Bakker2fbefde2013-06-29 16:01:15 +02001133 n++;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001134 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], p, 0 );
1135 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 }
1137
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001138 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001139 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites (excluding SCSVs)", n ) );
Ron Eldor714785d2017-08-28 13:55:55 +03001140
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +00001141 /*
1142 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_RENEGOTIATION)
1145 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +00001146#endif
1147 {
Ron Eldor4a2fb4c2017-09-10 17:03:50 +03001148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
Hanno Becker261602c2017-04-12 14:54:42 +01001149 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001150 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
1151 p += 2;
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +00001152 n++;
1153 }
1154
Paul Bakker2fbefde2013-06-29 16:01:15 +02001155 *q++ = (unsigned char)( n >> 7 );
1156 *q++ = (unsigned char)( n << 1 );
1157
Mateusz Starzyka3a99842021-02-19 14:27:22 +01001158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
1159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
1160 MBEDTLS_SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Mateusz Starzyka3a99842021-02-19 14:27:22 +01001162 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1163 *p++ = 1;
1164 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
Hanno Becker261602c2017-04-12 14:54:42 +01001166 /* First write extensions, then the total length */
1167
1168 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Hanno Becker261602c2017-04-12 14:54:42 +01001171 if( ( ret = ssl_write_hostname_ext( ssl, p + 2 + ext_len,
1172 end, &olen ) ) != 0 )
1173 {
1174 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_hostname_ext", ret );
1175 return( ret );
1176 }
Paul Bakkerd3edc862013-03-20 16:07:17 +01001177 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +02001178#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
Hanno Becker40f8b512017-10-12 14:58:55 +01001180 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
1181 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +01001183 if( ( ret = ssl_write_renegotiation_ext( ssl, p + 2 + ext_len,
1184 end, &olen ) ) != 0 )
1185 {
1186 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
1187 return( ret );
1188 }
Paul Bakkerd3edc862013-03-20 16:07:17 +01001189 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001190#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001193 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +01001194 if( ( ret = ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len,
1195 end, &olen ) ) != 0 )
1196 {
1197 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_signature_algorithms_ext", ret );
1198 return( ret );
1199 }
Paul Bakkerd3edc862013-03-20 16:07:17 +01001200 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001201#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001202
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02001203#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001204 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02001205 if( uses_ec )
1206 {
Hanno Becker261602c2017-04-12 14:54:42 +01001207 if( ( ret = ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len,
1208 end, &olen ) ) != 0 )
1209 {
1210 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_elliptic_curves_ext", ret );
1211 return( ret );
1212 }
Ron Eldor755bb6a2018-02-14 19:30:48 +02001213 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +01001214
Hanno Becker261602c2017-04-12 14:54:42 +01001215 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len,
1216 end, &olen ) ) != 0 )
1217 {
1218 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
1219 return( ret );
1220 }
Ron Eldor755bb6a2018-02-14 19:30:48 +02001221 ext_len += olen;
1222 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001223#endif
1224
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001225#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +01001226 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len,
1227 end, &olen ) ) != 0 )
1228 {
1229 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
1230 return( ret );
1231 }
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +02001232 ext_len += olen;
1233#endif
1234
Hanno Beckera0e20d02019-05-15 14:03:01 +01001235#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +01001236 if( ( ret = ssl_write_cid_ext( ssl, p + 2 + ext_len, end, &olen ) ) != 0 )
1237 {
1238 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
1239 return( ret );
1240 }
Hanno Becker49770ff2019-04-25 16:55:15 +01001241 ext_len += olen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001242#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +01001243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +01001245 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len,
1246 end, &olen ) ) != 0 )
1247 {
1248 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
1249 return( ret );
1250 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001251 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001252#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +01001255 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len,
1256 end, &olen ) ) != 0 )
1257 {
1258 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
1259 return( ret );
1260 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001261 ext_len += olen;
1262#endif
1263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +01001265 if( ( ret = ssl_write_extended_ms_ext( ssl, p + 2 + ext_len,
1266 end, &olen ) ) != 0 )
1267 {
1268 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
1269 return( ret );
1270 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001271 ext_len += olen;
1272#endif
1273
Simon Butcher5624ec82015-09-29 01:06:06 +01001274#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker261602c2017-04-12 14:54:42 +01001275 if( ( ret = ssl_write_alpn_ext( ssl, p + 2 + ext_len,
1276 end, &olen ) ) != 0 )
1277 {
1278 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_alpn_ext", ret );
1279 return( ret );
1280 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001281 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001282#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001283
Johan Pascalb62bb512015-12-03 21:56:45 +01001284#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01001285 if( ( ret = ssl_write_use_srtp_ext( ssl, p + 2 + ext_len,
1286 end, &olen ) ) != 0 )
Ron Eldor3adb9922017-12-21 10:15:08 +02001287 {
Johan Pascalc3ccd982020-10-28 17:18:18 +01001288 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
1289 return( ret );
Ron Eldor3adb9922017-12-21 10:15:08 +02001290 }
Johan Pascalc3ccd982020-10-28 17:18:18 +01001291 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01001292#endif
1293
Simon Butcher5624ec82015-09-29 01:06:06 +01001294#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +01001295 if( ( ret = ssl_write_session_ticket_ext( ssl, p + 2 + ext_len,
1296 end, &olen ) ) != 0 )
1297 {
1298 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
1299 return( ret );
1300 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001301 ext_len += olen;
1302#endif
1303
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001304 /* olen unused if all extensions are disabled */
1305 ((void) olen);
1306
Paul Elliottd48d5c62021-01-07 14:47:05 +00001307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker261602c2017-04-12 14:54:42 +01001308 ext_len ) );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001309
Paul Bakkera7036632014-04-30 10:15:38 +02001310 if( ext_len > 0 )
1311 {
Hanno Becker261602c2017-04-12 14:54:42 +01001312 /* No need to check for space here, because the extension
1313 * writing functions already took care of that. */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001314 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
Joe Subbiani94180e72021-08-20 16:20:44 +01001315 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02001316 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001317
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
1320 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
1322 ssl->state++;
1323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001325 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02001327#endif
1328
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02001329 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02001331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 return( ret );
1333 }
1334
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001335#if defined(MBEDTLS_SSL_PROTO_DTLS)
1336 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1337 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
1338 {
1339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
1340 return( ret );
1341 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01001342#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
1346 return( 0 );
1347}
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001350 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +00001351 size_t len )
1352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#if defined(MBEDTLS_SSL_RENEGOTIATION)
1354 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001355 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001356 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +00001357 if( len != 1 + ssl->verify_data_len * 2 ||
1358 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 mbedtls_ssl_safer_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001360 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001362 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001365 mbedtls_ssl_send_alert_message(
1366 ssl,
1367 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1368 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001369 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001370 }
1371 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001372 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001374 {
1375 if( len != 1 || buf[0] != 0x00 )
1376 {
Ronald Cron5ee57072020-06-11 09:34:06 +02001377 MBEDTLS_SSL_DEBUG_MSG( 1,
1378 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001379 mbedtls_ssl_send_alert_message(
1380 ssl,
1381 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1382 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001383 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001384 }
1385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001387 }
Paul Bakker48916f92012-09-16 19:57:18 +00001388
1389 return( 0 );
1390}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1393static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001394 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001395 size_t len )
1396{
1397 /*
1398 * server should use the extension only if we did,
1399 * and if so the server's value should match ours (and len is always 1)
1400 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001401 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001402 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001403 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001404 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001405 MBEDTLS_SSL_DEBUG_MSG( 1,
1406 ( "non-matching max fragment length extension" ) );
1407 mbedtls_ssl_send_alert_message(
1408 ssl,
1409 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001410 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1411 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001412 }
1413
1414 return( 0 );
1415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +00001417
Hanno Beckera0e20d02019-05-15 14:03:01 +01001418#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001419static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
1420 const unsigned char *buf,
1421 size_t len )
1422{
1423 size_t peer_cid_len;
1424
1425 if( /* CID extension only makes sense in DTLS */
1426 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
1427 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +01001428 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +01001429 {
Hanno Becker22626482019-05-03 12:46:59 +01001430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +01001431 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001432 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +01001433 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +01001434 }
1435
1436 if( len == 0 )
1437 {
1438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
1439 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001440 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1441 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +01001442 }
1443
1444 peer_cid_len = *buf++;
1445 len--;
1446
1447 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
1448 {
Hanno Becker22626482019-05-03 12:46:59 +01001449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +01001450 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001451 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1452 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +01001453 }
1454
1455 if( len != peer_cid_len )
1456 {
Hanno Becker22626482019-05-03 12:46:59 +01001457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +01001458 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001459 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1460 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +01001461 }
1462
Hanno Becker5a299902019-05-03 12:47:49 +01001463 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +01001464 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
1465 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
1466
1467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
1468 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
1469
Hanno Beckera8373a12019-04-26 15:37:26 +01001470 return( 0 );
1471}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001472#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1475static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001476 const unsigned char *buf,
1477 size_t len )
1478{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001479 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001480 len != 0 )
1481 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001482 MBEDTLS_SSL_DEBUG_MSG( 1,
1483 ( "non-matching encrypt-then-MAC extension" ) );
1484 mbedtls_ssl_send_alert_message(
1485 ssl,
1486 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001487 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +01001488 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001489 }
1490
1491 ((void) buf);
1492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001494
1495 return( 0 );
1496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1500static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001501 const unsigned char *buf,
1502 size_t len )
1503{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001504 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001505 len != 0 )
1506 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001507 MBEDTLS_SSL_DEBUG_MSG( 1,
1508 ( "non-matching extended master secret extension" ) );
1509 mbedtls_ssl_send_alert_message(
1510 ssl,
1511 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +01001512 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
1513 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001514 }
1515
1516 ((void) buf);
1517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001519
1520 return( 0 );
1521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1525static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001526 const unsigned char *buf,
1527 size_t len )
1528{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001529 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001530 len != 0 )
1531 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001532 MBEDTLS_SSL_DEBUG_MSG( 1,
1533 ( "non-matching session ticket extension" ) );
1534 mbedtls_ssl_send_alert_message(
1535 ssl,
1536 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +01001537 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
1538 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001539 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001540
1541 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001542
1543 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001544
1545 return( 0 );
1546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001548
Robert Cragie136884c2015-10-02 13:34:31 +01001549#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001550 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001552 const unsigned char *buf,
1553 size_t len )
1554{
1555 size_t list_size;
1556 const unsigned char *p;
1557
Philippe Antoine747fd532018-05-30 09:13:21 +02001558 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001561 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1562 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001563 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001564 }
Philippe Antoine747fd532018-05-30 09:13:21 +02001565 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001566
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001567 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001568 while( list_size > 0 )
1569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1571 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001572 {
Robert Cragie136884c2015-10-02 13:34:31 +01001573#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001574 ssl->handshake->ecdh_ctx.point_format = p[0];
Gilles Peskine064a85c2017-05-10 10:46:40 +02001575#endif
Robert Cragieae8535d2015-10-06 17:11:18 +01001576#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +02001577 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
1578 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +01001579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001581 return( 0 );
1582 }
1583
1584 list_size--;
1585 p++;
1586 }
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001589 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1590 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001591 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001592}
Darryl Green11999bb2018-03-13 15:22:58 +00001593#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001594 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001595
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001596#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1597static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
1598 const unsigned char *buf,
1599 size_t len )
1600{
Janos Follath865b3eb2019-12-16 11:46:15 +00001601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001602
Hanno Beckere694c3e2017-12-27 21:34:08 +00001603 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001604 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
1605 {
1606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
1607 return( 0 );
1608 }
1609
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +02001610 /* If we got here, we no longer need our cached extension */
1611 mbedtls_free( ssl->handshake->ecjpake_cache );
1612 ssl->handshake->ecjpake_cache = NULL;
1613 ssl->handshake->ecjpake_cache_len = 0;
1614
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001615 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
1616 buf, len ) ) != 0 )
1617 {
1618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001619 mbedtls_ssl_send_alert_message(
1620 ssl,
1621 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1622 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001623 return( ret );
1624 }
1625
1626 return( 0 );
1627}
1628#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#if defined(MBEDTLS_SSL_ALPN)
1631static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001632 const unsigned char *buf, size_t len )
1633{
1634 size_t list_len, name_len;
1635 const char **p;
1636
1637 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001638 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001639 {
1640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001641 mbedtls_ssl_send_alert_message(
1642 ssl,
1643 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +01001644 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
1645 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001646 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001647
1648 /*
1649 * opaque ProtocolName<1..2^8-1>;
1650 *
1651 * struct {
1652 * ProtocolName protocol_name_list<2..2^16-1>
1653 * } ProtocolNameList;
1654 *
1655 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1656 */
1657
1658 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1659 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001660 {
1661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1662 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001663 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001664 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001665
1666 list_len = ( buf[0] << 8 ) | buf[1];
1667 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001668 {
1669 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1670 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001671 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001672 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001673
1674 name_len = buf[2];
1675 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001676 {
1677 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1678 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001679 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001680 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001681
1682 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001683 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001684 {
1685 if( name_len == strlen( *p ) &&
1686 memcmp( buf + 3, *p, name_len ) == 0 )
1687 {
1688 ssl->alpn_chosen = *p;
1689 return( 0 );
1690 }
1691 }
1692
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001694 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1695 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001696 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001699
Johan Pascalb62bb512015-12-03 21:56:45 +01001700#if defined(MBEDTLS_SSL_DTLS_SRTP)
1701static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001702 const unsigned char *buf,
1703 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001704{
Johan Pascal43f94902020-09-22 12:25:52 +02001705 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001706 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001707 uint16_t server_protection_profile_value = 0;
1708
1709 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001710 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1711 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1712 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001713 return( 0 );
1714
Ron Eldora9788042018-12-05 11:04:31 +02001715 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001716 * uint8 SRTPProtectionProfile[2];
1717 *
1718 * struct {
1719 * SRTPProtectionProfiles SRTPProtectionProfiles;
1720 * opaque srtp_mki<0..255>;
1721 * } UseSRTPData;
1722
1723 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1724 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001725 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001726 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001727 {
1728 mki_len = ssl->dtls_srtp_info.mki_len;
1729 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001730
Ron Eldoref72faf2018-07-12 11:54:20 +03001731 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001732 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1733 * + protection profile (2 bytes)
1734 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001735 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001736 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001737 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001738 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001739
1740 /*
1741 * get the server protection profile
1742 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001743
1744 /*
1745 * protection profile length must be 0x0002 as we must have only
1746 * one protection profile in server Hello
1747 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001748 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001749 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001750
1751 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001752 server_protection = mbedtls_ssl_check_srtp_profile_value(
1753 server_protection_profile_value );
1754 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001755 {
Johan Pascal43f94902020-09-22 12:25:52 +02001756 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1757 mbedtls_ssl_get_srtp_profile_as_string(
1758 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001759 }
1760
Johan Pascal43f94902020-09-22 12:25:52 +02001761 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001762
Johan Pascalb62bb512015-12-03 21:56:45 +01001763 /*
1764 * Check we have the server profile in our list
1765 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001766 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001767 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001768 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1769 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001770 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1772 mbedtls_ssl_get_srtp_profile_as_string(
1773 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001774 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001775 }
1776 }
1777
Ron Eldor591f1622018-01-22 12:30:04 +02001778 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001779 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001780 {
1781 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001782 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001783 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001784 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001785
1786 /* If server does not use mki in its reply, make sure the client won't keep
1787 * one as negotiated */
1788 if( len == 5 )
1789 {
1790 ssl->dtls_srtp_info.mki_len = 0;
1791 }
1792
Ron Eldoref72faf2018-07-12 11:54:20 +03001793 /*
1794 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001795 * If the client detects a nonzero-length MKI in the server's response
1796 * that is different than the one the client offered, then the client
1797 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1798 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001799 if( len > 5 && ( buf[4] != mki_len ||
1800 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001801 {
1802 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1803 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001804 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001805 }
Ron Eldorb4655392018-07-05 18:25:39 +03001806#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001807 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001808 {
Ron Eldora9788042018-12-05 11:04:31 +02001809 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1810 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001811 }
1812#endif
Ron Eldora9788042018-12-05 11:04:31 +02001813 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001814}
1815#endif /* MBEDTLS_SSL_DTLS_SRTP */
1816
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001817/*
1818 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#if defined(MBEDTLS_SSL_PROTO_DTLS)
1821static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001822{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001824 int major_ver, minor_ver;
1825 unsigned char cookie_len;
1826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001828
Gilles Peskineb64bf062019-09-27 14:02:44 +02001829 /* Check that there is enough room for:
1830 * - 2 bytes of version
1831 * - 1 byte of cookie_len
1832 */
1833 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1834 {
1835 MBEDTLS_SSL_DEBUG_MSG( 1,
1836 ( "incoming HelloVerifyRequest message is too short" ) );
1837 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1838 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001839 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001840 }
1841
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001842 /*
1843 * struct {
1844 * ProtocolVersion server_version;
1845 * opaque cookie<0..2^8-1>;
1846 * } HelloVerifyRequest;
1847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001849 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001850 p += 2;
1851
TRodziewicz2d8800e2021-05-13 19:14:19 +02001852 /*
1853 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1854 * even is lower than our min version.
1855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
TRodziewiczb5850c52021-05-13 17:11:23 +02001857 minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001858 major_ver > ssl->conf->max_major_ver ||
1859 minor_ver > ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1864 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001865
Hanno Beckerbc000442021-06-24 09:18:19 +01001866 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001867 }
1868
1869 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001870 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1871 {
1872 MBEDTLS_SSL_DEBUG_MSG( 1,
1873 ( "cookie length does not match incoming message size" ) );
1874 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1875 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001876 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001877 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001878 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 mbedtls_free( ssl->handshake->verify_cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001881
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001882 ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001883 if( ssl->handshake->verify_cookie == NULL )
1884 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001886 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001887 }
1888
1889 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1890 ssl->handshake->verify_cookie_len = cookie_len;
1891
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001892 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1894 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001899
1900 return( 0 );
1901}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001905{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001906 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001907 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001908 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001909 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001910 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001912 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001913#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001914 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Hanno Becker327c93b2018-08-15 13:56:18 +01001919 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001921 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 return( ret );
1924 }
1925
Hanno Becker79594fd2019-05-08 09:38:41 +01001926 buf = ssl->in_msg;
1927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930#if defined(MBEDTLS_SSL_RENEGOTIATION)
1931 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001932 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001933 ssl->renego_records_seen++;
1934
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001935 if( ssl->conf->renego_max_records >= 0 &&
1936 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001937 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001938 MBEDTLS_SSL_DEBUG_MSG( 1,
1939 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001941 }
1942
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001943 MBEDTLS_SSL_DEBUG_MSG( 1,
1944 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001945
1946 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001948 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001952 mbedtls_ssl_send_alert_message(
1953 ssl,
1954 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1955 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 }
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001960 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001966 return( ssl_parse_hello_verify_request( ssl ) );
1967 }
1968 else
1969 {
1970 /* We made it through the verification process */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 mbedtls_free( ssl->handshake->verify_cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001972 ssl->handshake->verify_cookie = NULL;
1973 ssl->handshake->verify_cookie_len = 0;
1974 }
1975 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1979 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001982 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1983 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001984 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 }
1986
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001987 /*
1988 * 0 . 1 server_version
1989 * 2 . 33 random (maybe including 4 bytes of Unix time)
1990 * 34 . 34 session_id length = n
1991 * 35 . 34+n session_id
1992 * 35+n . 36+n cipher_suite
1993 * 37+n . 37+n compression_method
1994 *
1995 * 38+n . 39+n extensions length (optional)
1996 * 40+n . .. extensions
1997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
2001 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002002 ssl->conf->transport, buf + 0 );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002003 ssl->session_negotiate->minor_ver = ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002004
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002005 if( ssl->major_ver < ssl->conf->min_major_ver ||
2006 ssl->minor_ver < ssl->conf->min_minor_ver ||
2007 ssl->major_ver > ssl->conf->max_major_ver ||
2008 ssl->minor_ver > ssl->conf->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002009 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002010 MBEDTLS_SSL_DEBUG_MSG( 1,
2011 ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
2012 ssl->conf->min_major_ver,
2013 ssl->conf->min_minor_ver,
2014 ssl->major_ver, ssl->minor_ver,
2015 ssl->conf->max_major_ver,
2016 ssl->conf->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2019 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002020
Hanno Beckerbc000442021-06-24 09:18:19 +01002021 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002022 }
2023
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01002024 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00002025 ( (unsigned long) buf[2] << 24 ) |
2026 ( (unsigned long) buf[3] << 16 ) |
2027 ( (unsigned long) buf[4] << 8 ) |
2028 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002030 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002032 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035
Paul Bakker48916f92012-09-16 19:57:18 +00002036 if( n > 32 )
2037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002039 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2040 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002041 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00002042 }
2043
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02002044 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002046 ext_len = ( ( buf[38 + n] << 8 )
2047 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048
Paul Bakker48916f92012-09-16 19:57:18 +00002049 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00002051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002053 mbedtls_ssl_send_alert_message(
2054 ssl,
2055 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2056 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002057 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00002058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02002060 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02002061 {
2062 ext_len = 0;
2063 }
2064 else
2065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002067 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2068 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002069 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02002070 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02002072 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002073 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02002074
2075 /*
2076 * Read and check compression
2077 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002078 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02002080 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02002081 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002082 MBEDTLS_SSL_DEBUG_MSG( 1,
2083 ( "server hello, bad compression: %d", comp ) );
2084 mbedtls_ssl_send_alert_message(
2085 ssl,
2086 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2087 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02002089 }
2090
Paul Bakker380da532012-04-18 16:10:25 +00002091 /*
2092 * Initialize update checksum functions
2093 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002094 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
2095 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01002096 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002097 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00002098 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002099 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2100 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01002102 }
Paul Bakker380da532012-04-18 16:10:25 +00002103
Hanno Beckere694c3e2017-12-27 21:34:08 +00002104 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01002105
Paul Elliottd48d5c62021-01-07 14:47:05 +00002106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108
2109 /*
2110 * Check if the session can be resumed
2111 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002112 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#if defined(MBEDTLS_SSL_RENEGOTIATION)
2114 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002115#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002116 ssl->session_negotiate->ciphersuite != i ||
2117 ssl->session_negotiate->compression != comp ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002118 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002119 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 {
2121 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00002122 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002124 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002125#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002126 ssl->session_negotiate->ciphersuite = i;
2127 ssl->session_negotiate->compression = comp;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002128 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002129 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 }
2131 else
2132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002138 mbedtls_ssl_send_alert_message(
2139 ssl,
2140 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2141 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002142 return( ret );
2143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 }
2145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002147 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Paul Elliott3891caf2020-12-17 18:42:40 +00002149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
2151 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Andrzej Kurek03bac442018-04-25 05:06:07 -04002153 /*
2154 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01002155 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 i = 0;
2157 while( 1 )
2158 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01002159 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002162 mbedtls_ssl_send_alert_message(
2163 ssl,
2164 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2165 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002166 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 }
2168
Hanno Beckerd60b6c62021-04-29 12:04:11 +01002169 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002170 ssl->session_negotiate->ciphersuite )
2171 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 }
2175
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002176 suite_info = mbedtls_ssl_ciphersuite_from_id(
2177 ssl->session_negotiate->ciphersuite );
2178 if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver,
2179 ssl->minor_ver ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01002180 {
2181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002182 mbedtls_ssl_send_alert_message(
2183 ssl,
2184 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01002185 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2186 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01002187 }
2188
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002189 MBEDTLS_SSL_DEBUG_MSG( 3,
2190 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01002191
Gilles Peskineeccd8882020-03-10 12:19:08 +01002192#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002193 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
2194 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2195 {
2196 ssl->handshake->ecrs_enabled = 1;
2197 }
2198#endif
2199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 if( comp != MBEDTLS_SSL_COMPRESS_NULL
Paul Bakker2770fbd2012-07-03 13:30:23 +00002201 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002204 mbedtls_ssl_send_alert_message(
2205 ssl,
2206 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2207 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002208 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 }
Paul Bakker48916f92012-09-16 19:57:18 +00002210 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002212 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00002213
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002214 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002215 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02002216
Paul Bakker48916f92012-09-16 19:57:18 +00002217 while( ext_len )
2218 {
2219 unsigned int ext_id = ( ( ext[0] << 8 )
2220 | ( ext[1] ) );
2221 unsigned int ext_size = ( ( ext[2] << 8 )
2222 | ( ext[3] ) );
2223
2224 if( ext_size + 4 > ext_len )
2225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002227 mbedtls_ssl_send_alert_message(
2228 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2229 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002230 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00002231 }
2232
2233 switch( ext_id )
2234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
2236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
2237#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00002238 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01002239#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002240
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002241 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
2242 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002243 return( ret );
2244
2245 break;
2246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2248 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002249 MBEDTLS_SSL_DEBUG_MSG( 3,
2250 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02002251
2252 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
2253 ext + 4, ext_size ) ) != 0 )
2254 {
2255 return( ret );
2256 }
2257
2258 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02002260
Hanno Beckera0e20d02019-05-15 14:03:01 +01002261#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01002262 case MBEDTLS_TLS_EXT_CID:
2263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2264
2265 if( ( ret = ssl_parse_cid_ext( ssl,
2266 ext + 4,
2267 ext_size ) ) != 0 )
2268 {
2269 return( ret );
2270 }
2271
2272 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2276 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002278
2279 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
2280 ext + 4, ext_size ) ) != 0 )
2281 {
2282 return( ret );
2283 }
2284
2285 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2289 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002290 MBEDTLS_SSL_DEBUG_MSG( 3,
2291 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002292
2293 if( ( ret = ssl_parse_extended_ms_ext( ssl,
2294 ext + 4, ext_size ) ) != 0 )
2295 {
2296 return( ret );
2297 }
2298
2299 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2303 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02002305
2306 if( ( ret = ssl_parse_session_ticket_ext( ssl,
2307 ext + 4, ext_size ) ) != 0 )
2308 {
2309 return( ret );
2310 }
2311
2312 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02002314
Robert Cragie136884c2015-10-02 13:34:31 +01002315#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002316 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002318 MBEDTLS_SSL_DEBUG_MSG( 3,
2319 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002320
2321 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
2322 ext + 4, ext_size ) ) != 0 )
2323 {
2324 return( ret );
2325 }
2326
2327 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01002328#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
2329 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002330
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02002331#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2332 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
2333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
2334
2335 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
2336 ext + 4, ext_size ) ) != 0 )
2337 {
2338 return( ret );
2339 }
2340
2341 break;
2342#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#if defined(MBEDTLS_SSL_ALPN)
2345 case MBEDTLS_TLS_EXT_ALPN:
2346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002347
Johan Pascal275874b2020-10-27 10:43:53 +01002348 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
2349 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002350
2351 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002353
Johan Pascalb62bb512015-12-03 21:56:45 +01002354#if defined(MBEDTLS_SSL_DTLS_SRTP)
2355 case MBEDTLS_TLS_EXT_USE_SRTP:
2356 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2357
Johan Pascalc3ccd982020-10-28 17:18:18 +01002358 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
2359 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01002360
2361 break;
2362#endif /* MBEDTLS_SSL_DTLS_SRTP */
2363
Paul Bakker48916f92012-09-16 19:57:18 +00002364 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002365 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00002366 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002367 }
2368
2369 ext_len -= 4 + ext_size;
2370 ext += 4 + ext_size;
2371
2372 if( ext_len > 0 && ext_len < 4 )
2373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002375 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00002376 }
2377 }
2378
2379 /*
2380 * Renegotiation security checks
2381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002383 ssl->conf->allow_legacy_renegotiation ==
2384 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00002385 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002386 MBEDTLS_SSL_DEBUG_MSG( 1,
2387 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002388 handshake_failure = 1;
2389 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_SSL_RENEGOTIATION)
2391 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2392 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00002393 renegotiation_info_seen == 0 )
2394 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002395 MBEDTLS_SSL_DEBUG_MSG( 1,
2396 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002397 handshake_failure = 1;
2398 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2400 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002401 ssl->conf->allow_legacy_renegotiation ==
2402 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002405 handshake_failure = 1;
2406 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2408 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002409 renegotiation_info_seen == 1 )
2410 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002411 MBEDTLS_SSL_DEBUG_MSG( 1,
2412 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002413 handshake_failure = 1;
2414 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002416
2417 if( handshake_failure == 1 )
2418 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002419 mbedtls_ssl_send_alert_message(
2420 ssl,
2421 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2422 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002423 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00002424 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
2428 return( 0 );
2429}
2430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2432 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002433static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
2434 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02002435 unsigned char *end )
2436{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01002438 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02002439
Paul Bakker29e1f122013-04-16 13:07:56 +02002440 /*
2441 * Ephemeral DH parameters:
2442 *
2443 * struct {
2444 * opaque dh_p<1..2^16-1>;
2445 * opaque dh_g<1..2^16-1>;
2446 * opaque dh_Ys<1..2^16-1>;
2447 * } ServerDHParams;
2448 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002449 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
2450 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002453 return( ret );
2454 }
2455
Gilles Peskine487bbf62021-05-27 22:17:07 +02002456 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01002457 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02002458 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01002460 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002461 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002462 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002463 }
2464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2466 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2467 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02002468
2469 return( ret );
2470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2472 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2475 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2476 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2477 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2478 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2479static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002482 mbedtls_ecp_group_id grp_id;
2483#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
2484 grp_id = ssl->handshake->ecdh_ctx.grp.id;
2485#else
2486 grp_id = ssl->handshake->ecdh_ctx.grp_id;
2487#endif
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002488
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002489 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002490 if( curve_info == NULL )
2491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2493 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002494 }
2495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002497
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002498#if defined(MBEDTLS_ECP_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002499 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002500#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002501 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
2502 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002503#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002504 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002505
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002506 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2507 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002508
2509 return( 0 );
2510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2512 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2513 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2514 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2515 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002516
Hanno Beckerbb89e272019-01-08 12:54:37 +00002517#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2518 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2519 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
2520static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl,
2521 unsigned char **p,
2522 unsigned char *end )
2523{
2524 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01002525 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00002526 uint8_t ecpoint_len;
2527 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2528
2529 /*
2530 * Parse ECC group
2531 */
2532
2533 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002534 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002535
2536 /* First byte is curve_type; only named_curve is handled */
2537 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01002538 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002539
2540 /* Next two bytes are the namedcurve value */
2541 tls_id = *(*p)++;
2542 tls_id <<= 8;
2543 tls_id |= *(*p)++;
2544
2545 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01002546 if( ( handshake->ecdh_psa_type =
2547 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00002548 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01002549 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002550 }
Gilles Peskine42459802019-12-19 13:31:53 +01002551 if( ecdh_bits > 0xffff )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002552 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Gilles Peskine42459802019-12-19 13:31:53 +01002553 handshake->ecdh_bits = (uint16_t) ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00002554
2555 /*
2556 * Put peer's ECDH public key in the format understood by PSA.
2557 */
2558
2559 ecpoint_len = *(*p)++;
2560 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002561 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002562
Gilles Peskine42459802019-12-19 13:31:53 +01002563 if( mbedtls_psa_tls_ecpoint_to_psa_ec(
Hanno Beckerbb89e272019-01-08 12:54:37 +00002564 *p, ecpoint_len,
2565 handshake->ecdh_psa_peerkey,
2566 sizeof( handshake->ecdh_psa_peerkey ),
2567 &handshake->ecdh_psa_peerkey_len ) != 0 )
2568 {
2569 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2570 }
2571
2572 *p += ecpoint_len;
2573 return( 0 );
2574}
2575#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2576 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2577 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
2578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2580 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2581 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2582static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002583 unsigned char **p,
2584 unsigned char *end )
2585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02002587
Paul Bakker29e1f122013-04-16 13:07:56 +02002588 /*
2589 * Ephemeral ECDH parameters:
2590 *
2591 * struct {
2592 * ECParameters curve_params;
2593 * ECPoint public;
2594 * } ServerECDHParams;
2595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02002597 (const unsigned char **) p, end ) ) != 0 )
2598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002600#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02002601 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2602 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002603#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02002604 return( ret );
2605 }
2606
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002607 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002608 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002609 MBEDTLS_SSL_DEBUG_MSG( 1,
2610 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002611 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002612 }
2613
Paul Bakker29e1f122013-04-16 13:07:56 +02002614 return( ret );
2615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2617 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2618 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002619
Gilles Peskineeccd8882020-03-10 12:19:08 +01002620#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002622 unsigned char **p,
2623 unsigned char *end )
2624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03002626 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02002627 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002628
2629 /*
2630 * PSK parameters:
2631 *
2632 * opaque psk_identity_hint<0..2^16-1>;
2633 */
Hanno Becker0c161d12018-10-08 13:40:50 +01002634 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01002635 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002636 MBEDTLS_SSL_DEBUG_MSG( 1,
2637 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002638 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01002639 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02002640 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002641 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002642
irwir6527bd62019-09-21 18:51:25 +03002643 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002644 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002645 MBEDTLS_SSL_DEBUG_MSG( 1,
2646 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002647 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002648 }
2649
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01002650 /*
2651 * Note: we currently ignore the PKS identity hint, as we only allow one
2652 * PSK to be provisionned on the client. This could be changed later if
2653 * someone needs that feature.
2654 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002655 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002656 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002657
2658 return( ret );
2659}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002660#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2663 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002664/*
2665 * Generate a pre-master secret and encrypt it with the server's RSA key
2666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002668 size_t offset, size_t *olen,
2669 size_t pms_offset )
2670{
Janos Follath865b3eb2019-12-16 11:46:15 +00002671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002672 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002673 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002674 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002675
Angus Grattond8213d02016-05-25 20:56:48 +10002676 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002677 {
2678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2679 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2680 }
2681
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002682 /*
2683 * Generate (part of) the pre-master as
2684 * struct {
2685 * ProtocolVersion client_version;
2686 * opaque random[46];
2687 * } PreMasterSecret;
2688 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002689 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
2690 ssl->conf->max_minor_ver,
2691 ssl->conf->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002692
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002693 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002696 return( ret );
2697 }
2698
2699 ssl->handshake->pmslen = 48;
2700
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002701#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2702 peer_pk = &ssl->handshake->peer_pubkey;
2703#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002704 if( ssl->session_negotiate->peer_cert == NULL )
2705 {
Hanno Becker8273df82019-02-06 17:37:32 +00002706 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002708 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002709 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002710 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2711#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002712
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002713 /*
2714 * Now write it out, encrypted
2715 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002716 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2719 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002720 }
2721
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002722 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002723 p, ssl->handshake->pmslen,
2724 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002725 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002726 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002729 return( ret );
2730 }
2731
TRodziewicz0f82ec62021-05-12 17:49:18 +02002732#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002733 if( len_bytes == 2 )
2734 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002735 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002736 *olen += 2;
2737 }
2738#endif
2739
Hanno Beckerae553dd2019-02-08 14:06:00 +00002740#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2741 /* We don't need the peer's public key anymore. Free it. */
2742 mbedtls_pk_free( peer_pk );
2743#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002744 return( 0 );
2745}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2747 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002750#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2751 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2752 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002754 unsigned char **p,
2755 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 mbedtls_md_type_t *md_alg,
2757 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002758{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02002759 ((void) ssl);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 *md_alg = MBEDTLS_MD_NONE;
2761 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002762
2763 /* Only in TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002765 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002766 return( 0 );
2767 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002768
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002769 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002770 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002771
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002772 /*
2773 * Get hash algorithm
2774 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002775 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2776 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002777 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002778 MBEDTLS_SSL_DEBUG_MSG( 1,
2779 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002780 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002781 }
2782
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002783 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002784 * Get signature algorithm
2785 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002786 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2787 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002788 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002789 MBEDTLS_SSL_DEBUG_MSG( 1,
2790 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002791 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002792 }
2793
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002794 /*
2795 * Check if the hash is acceptable
2796 */
2797 if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
2798 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002799 MBEDTLS_SSL_DEBUG_MSG( 1,
2800 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002801 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002802 }
2803
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2805 (*p)[1] ) );
2806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2807 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002808 *p += 2;
2809
2810 return( 0 );
2811}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002812#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2813 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2814 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2818 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2819static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002820{
Janos Follath865b3eb2019-12-16 11:46:15 +00002821 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002823 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002824
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002825#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2826 peer_pk = &ssl->handshake->peer_pubkey;
2827#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002828 if( ssl->session_negotiate->peer_cert == NULL )
2829 {
Hanno Becker8273df82019-02-06 17:37:32 +00002830 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002832 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002833 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002834 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2835#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002836
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002837 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2840 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002841 }
2842
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002843 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2846 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002849 return( ret );
2850 }
2851
2852 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002855 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002856 }
2857
Hanno Beckerae553dd2019-02-08 14:06:00 +00002858#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2859 /* We don't need the peer's public key anymore. Free it,
2860 * so that more RAM is available for upcoming expensive
2861 * operations like ECDHE. */
2862 mbedtls_pk_free( peer_pk );
2863#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2864
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002865 return( ret );
2866}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2868 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002871{
Janos Follath865b3eb2019-12-16 11:46:15 +00002872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002873 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002874 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002875 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2880 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 ssl->state++;
2884 return( 0 );
2885 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002886 ((void) p);
2887 ((void) end);
2888#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2891 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2892 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2893 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002894 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002895 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002898 mbedtls_ssl_send_alert_message(
2899 ssl,
2900 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2901 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002902 return( ret );
2903 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002906 ssl->state++;
2907 return( 0 );
2908 }
2909 ((void) p);
2910 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2912 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002913
Gilles Peskineeccd8882020-03-10 12:19:08 +01002914#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002915 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002916 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002917 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002918 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002919 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002920#endif
2921
Hanno Becker327c93b2018-08-15 13:56:18 +01002922 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 return( ret );
2926 }
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002931 mbedtls_ssl_send_alert_message(
2932 ssl,
2933 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2934 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 }
2937
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002938 /*
2939 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2940 * doesn't use a psk_identity_hint
2941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2945 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002946 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002947 /* Current message is probably either
2948 * CertificateRequest or ServerHelloDone */
2949 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002950 goto exit;
2951 }
2952
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002953 MBEDTLS_SSL_DEBUG_MSG( 1,
2954 ( "server key exchange message must not be skipped" ) );
2955 mbedtls_ssl_send_alert_message(
2956 ssl,
2957 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2958 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 }
2962
Gilles Peskineeccd8882020-03-10 12:19:08 +01002963#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002964 if( ssl->handshake->ecrs_enabled )
2965 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2966
2967start_processing:
2968#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002970 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002972
Gilles Peskineeccd8882020-03-10 12:19:08 +01002973#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2975 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2976 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2977 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002978 {
2979 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002982 mbedtls_ssl_send_alert_message(
2983 ssl,
2984 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002985 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002986 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002987 }
2988 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002989#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2992 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2993 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2994 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002995 ; /* nothing more to do */
2996 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2998 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2999#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3000 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3001 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
3002 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 {
Paul Bakker29e1f122013-04-16 13:07:56 +02003004 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003007 mbedtls_ssl_send_alert_message(
3008 ssl,
3009 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3010 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01003011 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003012 }
3013 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3016 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +00003017#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3018 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3019 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
3020 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3021 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3022 {
3023 if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 )
3024 {
3025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003026 mbedtls_ssl_send_alert_message(
3027 ssl,
3028 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3029 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanc50b7172021-06-29 14:40:23 +01003030 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckerbb89e272019-01-08 12:54:37 +00003031 }
3032 }
3033 else
3034#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3035 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3036 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3038 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
3039 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
3040 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3041 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
3042 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003043 {
3044 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
3045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003047 mbedtls_ssl_send_alert_message(
3048 ssl,
3049 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3050 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01003051 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01003052 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003053 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3056 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
3057 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003058#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3059 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3060 {
3061 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
3062 p, end - p );
3063 if( ret != 0 )
3064 {
3065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003066 mbedtls_ssl_send_alert_message(
3067 ssl,
3068 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01003069 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
3070 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003071 }
3072 }
3073 else
3074#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3077 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003078 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079
Gilles Peskineeccd8882020-03-10 12:19:08 +01003080#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003081 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00003083 size_t sig_len, hashlen;
3084 unsigned char hash[64];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
3086 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
3087 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00003088 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003089 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003090
Hanno Beckera6899bb2019-02-06 18:26:03 +00003091 mbedtls_pk_context * peer_pk;
3092
Paul Bakker29e1f122013-04-16 13:07:56 +02003093 /*
3094 * Handle the digitally-signed structure
3095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3097 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098 {
Paul Bakker9659dae2013-08-28 16:21:34 +02003099 if( ssl_parse_signature_algorithm( ssl, &p, end,
3100 &md_alg, &pk_alg ) != 0 )
3101 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003102 MBEDTLS_SSL_DEBUG_MSG( 1,
3103 ( "bad server key exchange message" ) );
3104 mbedtls_ssl_send_alert_message(
3105 ssl,
3106 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3107 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02003109 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003110
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003111 if( pk_alg !=
3112 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003113 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003114 MBEDTLS_SSL_DEBUG_MSG( 1,
3115 ( "bad server key exchange message" ) );
3116 mbedtls_ssl_send_alert_message(
3117 ssl,
3118 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3119 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003120 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121 }
3122 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02003123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02003125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02003128 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003129
3130 /*
3131 * Read signature
3132 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01003133
3134 if( p > end - 2 )
3135 {
3136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003137 mbedtls_ssl_send_alert_message(
3138 ssl,
3139 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3140 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003141 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01003142 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003143 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00003144 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003145
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01003146 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003149 mbedtls_ssl_send_alert_message(
3150 ssl,
3151 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3152 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003153 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01003154 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003157
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003158 /*
3159 * Compute the hash that has been signed
3160 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003161#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02003163 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003164 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
3165 params, params_len,
3166 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003167 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02003168 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02003169 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003170 else
TRodziewicz0f82ec62021-05-12 17:49:18 +02003171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02003172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003175 }
Paul Bakker29e1f122013-04-16 13:07:56 +02003176
Gilles Peskineca1d7422018-04-24 11:53:22 +02003177 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02003178
Hanno Beckera6899bb2019-02-06 18:26:03 +00003179#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3180 peer_pk = &ssl->handshake->peer_pubkey;
3181#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003182 if( ssl->session_negotiate->peer_cert == NULL )
3183 {
Hanno Becker8273df82019-02-06 17:37:32 +00003184 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00003185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00003186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003187 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00003188 peer_pk = &ssl->session_negotiate->peer_cert->pk;
3189#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003190
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003191 /*
3192 * Verify signature
3193 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00003194 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003197 mbedtls_ssl_send_alert_message(
3198 ssl,
3199 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3200 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003202 }
3203
Gilles Peskineeccd8882020-03-10 12:19:08 +01003204#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003205 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003206 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003207#endif
3208
Hanno Beckera6899bb2019-02-06 18:26:03 +00003209 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003210 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003211 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01003212#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003213 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3214#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003215 mbedtls_ssl_send_alert_message(
3216 ssl,
3217 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3218 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003220#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003221 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3222 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3223#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02003224 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00003225 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00003226
3227#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3228 /* We don't need the peer's public key anymore. Free it,
3229 * so that more RAM is available for upcoming expensive
3230 * operations like ECDHE. */
3231 mbedtls_pk_free( peer_pk );
3232#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003234#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003236exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00003237 ssl->state++;
3238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
3241 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003242}
3243
Gilles Peskineeccd8882020-03-10 12:19:08 +01003244#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003246{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003247 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003248 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003251
Hanno Becker1aa267c2017-04-28 17:08:27 +01003252 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003255 ssl->state++;
3256 return( 0 );
3257 }
3258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3260 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003261}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003262#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003264{
Janos Follath865b3eb2019-12-16 11:46:15 +00003265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003266 unsigned char *buf;
3267 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003268 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003269 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003270 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
Hanno Becker1aa267c2017-04-28 17:08:27 +01003274 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003277 ssl->state++;
3278 return( 0 );
3279 }
3280
Hanno Becker327c93b2018-08-15 13:56:18 +01003281 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3284 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 }
3286
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003287 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3288 {
3289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003290 mbedtls_ssl_send_alert_message(
3291 ssl,
3292 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3293 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003294 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3295 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003297 ssl->state++;
3298 ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00003299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 ssl->client_auth ? "a" : "no" ) );
3302
Paul Bakker926af752012-11-23 13:38:07 +01003303 if( ssl->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003304 {
Johan Pascala89ca862020-08-25 10:03:19 +02003305 /* Current message is probably the ServerHelloDone */
3306 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01003307 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003308 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003309
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02003310 /*
3311 * struct {
3312 * ClientCertificateType certificate_types<1..2^8-1>;
3313 * SignatureAndHashAlgorithm
3314 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
3315 * DistinguishedName certificate_authorities<0..2^16-1>;
3316 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003317 *
3318 * Since we only support a single certificate on clients, let's just
3319 * ignore all the information that's supposed to help us pick a
3320 * certificate.
3321 *
3322 * We could check that our certificate matches the request, and bail out
3323 * if it doesn't, but it's simpler to just send the certificate anyway,
3324 * and give the server the opportunity to decide if it should terminate
3325 * the connection when it doesn't like our certificate.
3326 *
3327 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
3328 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00003329 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003330 *
3331 * However, we still minimally parse the message to check it is at least
3332 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02003333 */
Paul Bakker926af752012-11-23 13:38:07 +01003334 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003335
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003336 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02003337 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
3338 {
3339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3340 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3341 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003342 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02003343 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01003345 n = cert_type_len;
3346
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01003347 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02003348 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01003349 * * the length of the signature algorithms field (if minor version of
3350 * SSL is 3),
3351 * * distinguished name length otherwise.
3352 * Both reach at most the index:
3353 * ...hdr_len + 2 + n,
3354 * therefore the buffer length at this point must be greater than that
3355 * regardless of the actual code path.
3356 */
3357 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01003358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003360 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3361 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003362 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01003363 }
3364
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003365 /* supported_signature_algorithms */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3367 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003368 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003369 size_t sig_alg_len =
3370 ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3371 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Simon Butcher99000142016-10-13 17:21:01 +01003372#if defined(MBEDTLS_DEBUG_C)
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003373 unsigned char* sig_alg;
Simon Butcher99000142016-10-13 17:21:01 +01003374 size_t i;
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003375#endif
Simon Butcher99000142016-10-13 17:21:01 +01003376
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003377 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02003378 * The furthest access in buf is in the loop few lines below:
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003379 * sig_alg[i + 1],
3380 * where:
3381 * sig_alg = buf + ...hdr_len + 3 + n,
3382 * max(i) = sig_alg_len - 1.
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02003383 * Therefore the furthest access is:
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003384 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
3385 * which reduces to:
3386 * buf[...hdr_len + 3 + n + sig_alg_len],
3387 * which is one less than we need the buf to be.
3388 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003389 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl )
3390 + 3 + n + sig_alg_len )
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003391 {
3392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003393 mbedtls_ssl_send_alert_message(
3394 ssl,
3395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3396 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003397 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003398 }
3399
3400#if defined(MBEDTLS_DEBUG_C)
3401 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
Simon Butcher99000142016-10-13 17:21:01 +01003402 for( i = 0; i < sig_alg_len; i += 2 )
3403 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003404 MBEDTLS_SSL_DEBUG_MSG( 3,
3405 ( "Supported Signature Algorithm found: %d,%d",
3406 sig_alg[i], sig_alg[i + 1] ) );
Simon Butcher99000142016-10-13 17:21:01 +01003407 }
3408#endif
Paul Bakker926af752012-11-23 13:38:07 +01003409
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003410 n += 2 + sig_alg_len;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003411 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01003413
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003414 /* certificate_authorities */
3415 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3416 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01003417
3418 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003419 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01003420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003422 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3423 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003424 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01003425 }
3426
3427exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429
3430 return( 0 );
3431}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003432#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003435{
Janos Follath865b3eb2019-12-16 11:46:15 +00003436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003439
Hanno Becker327c93b2018-08-15 13:56:18 +01003440 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3443 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003445
3446 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3447 {
3448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
3449 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3450 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
3453 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003456 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3457 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01003458 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 }
3460
3461 ssl->state++;
3462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003464 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003466#endif
3467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003469
3470 return( 0 );
3471}
3472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003474{
Janos Follath865b3eb2019-12-16 11:46:15 +00003475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003476
3477 size_t header_len;
3478 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003479 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003480 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3485 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003486 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 /*
3488 * DHM key exchange -- send G^X mod P
3489 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003490 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00003491
Joe Subbiani6dd73642021-07-19 11:56:54 +01003492 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003493 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00003494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003496 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003497 &ssl->out_msg[header_len], content_len,
3498 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 if( ret != 0 )
3500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 return( ret );
3503 }
3504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3506 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003509 ssl->handshake->premaster,
3510 MBEDTLS_PREMASTER_SIZE,
3511 &ssl->handshake->pmslen,
3512 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 return( ret );
3516 }
3517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00003519 }
3520 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00003522#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3523 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3524 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
3525 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3526 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3527 {
3528 psa_status_t status;
Janos Follath53b8ec22019-08-08 10:28:27 +01003529 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003530
3531 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3532
3533 unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
3534 size_t own_pubkey_len;
3535 unsigned char *own_pubkey_ecpoint;
3536 size_t own_pubkey_ecpoint_len;
3537
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003538 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003539
Hanno Becker0a94a642019-01-11 14:35:30 +00003540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3541
Hanno Becker4a63ed42019-01-08 11:39:35 +00003542 /*
3543 * Generate EC private key for ECDHE exchange.
3544 */
3545
Hanno Becker4a63ed42019-01-08 11:39:35 +00003546 /* The master secret is obtained from the shared ECDH secret by
3547 * applying the TLS 1.2 PRF with a specific salt and label. While
3548 * the PSA Crypto API encourages combining key agreement schemes
3549 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3550 * yet support the provisioning of salt + label to the KDF.
3551 * For the time being, we therefore need to split the computation
3552 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01003553 key_attributes = psa_key_attributes_init();
3554 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01003555 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01003556 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3557 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003558
3559 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01003560 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01003561 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003562 if( status != PSA_SUCCESS )
3563 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3564
3565 /* Export the public part of the ECDH private key from PSA
3566 * and convert it to ECPoint format used in ClientKeyExchange. */
3567 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3568 own_pubkey, sizeof( own_pubkey ),
3569 &own_pubkey_len );
3570 if( status != PSA_SUCCESS )
3571 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3572
3573 if( mbedtls_psa_tls_psa_ec_to_ecpoint( own_pubkey,
3574 own_pubkey_len,
3575 &own_pubkey_ecpoint,
3576 &own_pubkey_ecpoint_len ) != 0 )
3577 {
3578 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3579 }
3580
3581 /* Copy ECPoint structure to outgoing message buffer. */
Andrzej Kurekade9e282019-05-07 08:19:33 -04003582 ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003583 memcpy( ssl->out_msg + header_len + 1,
3584 own_pubkey_ecpoint, own_pubkey_ecpoint_len );
3585 content_len = own_pubkey_ecpoint_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003586
Hanno Becker4a63ed42019-01-08 11:39:35 +00003587 /* The ECDH secret is the premaster secret used for key derivation. */
3588
Janos Follathdf3b0892019-08-08 11:12:24 +01003589 /* Compute ECDH shared secret. */
3590 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3591 handshake->ecdh_psa_privkey,
3592 handshake->ecdh_psa_peerkey,
3593 handshake->ecdh_psa_peerkey_len,
3594 ssl->handshake->premaster,
3595 sizeof( ssl->handshake->premaster ),
3596 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003597 if( status != PSA_SUCCESS )
3598 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3599
3600 status = psa_destroy_key( handshake->ecdh_psa_privkey );
3601 if( status != PSA_SUCCESS )
3602 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Ronald Croncf56a0a2020-08-04 09:51:30 +02003603 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003604 }
3605 else
3606#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3607 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3608 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3610 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3611 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3612 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3613 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3614 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3615 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3616 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003617 {
3618 /*
3619 * ECDH key exchange -- send client public value
3620 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003621 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01003622
Gilles Peskineeccd8882020-03-10 12:19:08 +01003623#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003624 if( ssl->handshake->ecrs_enabled )
3625 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003626 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003627 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02003628
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003629 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
3630 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003631#endif
3632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003634 &content_len,
3635 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003636 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01003637 if( ret != 0 )
3638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003640#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003641 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3642 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3643#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003644 return( ret );
3645 }
3646
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003647 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3648 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003649
Gilles Peskineeccd8882020-03-10 12:19:08 +01003650#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003651 if( ssl->handshake->ecrs_enabled )
3652 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003653 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003654 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003655 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003656
3657ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003658 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003659 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003660#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003662 &ssl->handshake->pmslen,
3663 ssl->handshake->premaster,
3664 MBEDTLS_MPI_MAX_SIZE,
3665 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003668#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003669 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3670 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3671#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003672 return( ret );
3673 }
3674
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003675 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3676 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker41c83d32013-03-20 14:39:14 +01003677 }
3678 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3680 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3681 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3682 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003683#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003684 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003685 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003686 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003687 * opaque psk_identity<0..2^16-1>;
3688 */
Hanno Becker520224e2018-10-26 11:38:07 +01003689 if( ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003690 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003691 /* We don't offer PSK suites if we don't have a PSK,
3692 * and we check that the server's choice is among the
3693 * ciphersuites we offered, so this should never happen. */
3694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003695 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003696
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003697 header_len = 4;
3698 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003699
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003700 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003701 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003702 MBEDTLS_SSL_DEBUG_MSG( 1,
3703 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003704 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3705 }
3706
Joe Subbianifbeb6922021-07-16 14:27:50 +01003707 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3708 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003709
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003710 memcpy( ssl->out_msg + header_len,
3711 ssl->conf->psk_identity,
3712 ssl->conf->psk_identity_len );
3713 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3716 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003717 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003718 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003719 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003720 else
3721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3723 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003724 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003725#if defined(MBEDTLS_USE_PSA_CRYPTO)
3726 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003727 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003728 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3729#endif /* MBEDTLS_USE_PSA_CRYPTO */
3730
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003731 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3732 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003733 return( ret );
3734 }
3735 else
3736#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3738 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003739 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003740#if defined(MBEDTLS_USE_PSA_CRYPTO)
3741 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003742 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003743 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3744#endif /* MBEDTLS_USE_PSA_CRYPTO */
3745
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003746 /*
3747 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3748 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003749 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003750
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003751 if( header_len + 2 + content_len >
3752 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003753 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003754 MBEDTLS_SSL_DEBUG_MSG( 1,
3755 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003756 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3757 }
3758
Joe Subbianifbeb6922021-07-16 14:27:50 +01003759 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3760 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003763 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003764 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003765 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003766 if( ret != 0 )
3767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003769 return( ret );
3770 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003771 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003772 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3774#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3775 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003776 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003777#if defined(MBEDTLS_USE_PSA_CRYPTO)
3778 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003779 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003780 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3781#endif /* MBEDTLS_USE_PSA_CRYPTO */
3782
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003783 /*
3784 * ClientECDiffieHellmanPublic public;
3785 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003786 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3787 &content_len,
3788 &ssl->out_msg[header_len],
3789 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003790 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003791 if( ret != 0 )
3792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003794 return( ret );
3795 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003796
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003797 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3798 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003799 }
3800 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003805 }
3806
Hanno Beckerafd311e2018-10-23 15:26:40 +01003807#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3808 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3809 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
3810 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Becker520224e2018-10-26 11:38:07 +01003811 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003812 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003813 MBEDTLS_SSL_DEBUG_MSG( 1,
3814 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003815 }
3816 else
3817#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3818 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003820 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003821 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003822 MBEDTLS_SSL_DEBUG_RET( 1,
3823 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003824 return( ret );
3825 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003826 }
3827 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003828#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3830 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003831 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003832 header_len = 4;
3833 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3834 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003835 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003836 }
Paul Bakkered27a042013-04-18 22:46:23 +02003837 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003839#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3840 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3841 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003842 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003843
3844 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003845 ssl->out_msg + header_len,
3846 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3847 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003848 ssl->conf->f_rng, ssl->conf->p_rng );
3849 if( ret != 0 )
3850 {
3851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3852 return( ret );
3853 }
3854
3855 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3856 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3857 ssl->conf->f_rng, ssl->conf->p_rng );
3858 if( ret != 0 )
3859 {
3860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3861 return( ret );
3862 }
3863 }
3864 else
3865#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003866 {
3867 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3869 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003871
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003872 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3874 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003875
3876 ssl->state++;
3877
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003878 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003879 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003881 return( ret );
3882 }
3883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003885
3886 return( 0 );
3887}
3888
Gilles Peskineeccd8882020-03-10 12:19:08 +01003889#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003891{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003892 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003893 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003901 return( ret );
3902 }
3903
Hanno Becker77adddc2019-02-07 12:32:43 +00003904 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003907 ssl->state++;
3908 return( 0 );
3909 }
3910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3912 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003913}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003914#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003916{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003918 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003919 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003920 size_t n = 0, offset = 0;
3921 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003922 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003924 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003925 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003926#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3927 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3928#else
3929 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3930#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003933
Gilles Peskineeccd8882020-03-10 12:19:08 +01003934#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003935 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003936 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003937 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003938 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003939 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003940#endif
3941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003945 return( ret );
3946 }
3947
Hanno Becker77adddc2019-02-07 12:32:43 +00003948 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003951 ssl->state++;
3952 return( 0 );
3953 }
3954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003956 {
Johan Pascala89ca862020-08-25 10:03:19 +02003957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3958 ssl->state++;
3959 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003960 }
3961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003966 }
3967
3968 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003969 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003970 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003971#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003972 if( ssl->handshake->ecrs_enabled )
3973 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3974
3975sign:
3976#endif
3977
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003978 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3981 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003982 {
3983 /*
3984 * digitally-signed struct {
3985 * opaque handshake_messages[handshake_messages_length];
3986 * };
3987 *
3988 * Taking shortcut here. We assume that the server always allows the
3989 * PRF Hash function and has sent it in the allowed signature
3990 * algorithms list received in the Certificate Request message.
3991 *
3992 * Until we encounter a server that does not, we will take this
3993 * shortcut.
3994 *
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003995 * Reason: Otherwise we should have running hashes for SHA512 and
3996 * SHA224 in order to satisfy 'weird' needs from the server
3997 * side.
Paul Bakker926af752012-11-23 13:38:07 +01003998 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00003999 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00004000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 md_alg = MBEDTLS_MD_SHA384;
4002 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004003 }
4004 else
4005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 md_alg = MBEDTLS_MD_SHA256;
4007 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004008 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004010
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004011 /* Info from md_alg will be used instead */
4012 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004013 offset = 2;
4014 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004015 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004020 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00004021
Gilles Peskineeccd8882020-03-10 12:19:08 +01004022#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02004023 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02004024 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004025#endif
4026
4027 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
4028 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02004029 ssl->out_msg + 6 + offset,
4030 out_buf_len - 6 - offset,
4031 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004032 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02004033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01004035#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02004036 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
4037 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
4038#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004039 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02004040 }
Paul Bakker926af752012-11-23 13:38:07 +01004041
Joe Subbiani6dd73642021-07-19 11:56:54 +01004042 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004043
Paul Bakker1ef83d62012-04-11 12:09:53 +00004044 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4046 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
4048 ssl->state++;
4049
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004050 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004051 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 return( ret );
4054 }
4055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004057
Paul Bakkered27a042013-04-18 22:46:23 +02004058 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004059}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004060#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4063static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004064{
Janos Follath865b3eb2019-12-16 11:46:15 +00004065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004066 uint32_t lifetime;
4067 size_t ticket_len;
4068 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02004069 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004072
Hanno Becker327c93b2018-08-15 13:56:18 +01004073 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004076 return( ret );
4077 }
4078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01004082 mbedtls_ssl_send_alert_message(
4083 ssl,
4084 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4085 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004087 }
4088
4089 /*
4090 * struct {
4091 * uint32 ticket_lifetime_hint;
4092 * opaque ticket<0..2^16-1>;
4093 * } NewSessionTicket;
4094 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02004095 * 0 . 3 ticket_lifetime_hint
4096 * 4 . 5 ticket_len (n)
4097 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
4100 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004103 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4104 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01004105 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004106 }
4107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004109
Philippe Antoineb5b25432018-05-11 11:06:29 +02004110 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
4111 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004112
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02004113 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
4114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004118 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4119 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01004120 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004121 }
4122
Paul Elliottd48d5c62021-01-07 14:47:05 +00004123 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004124
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004125 /* We're not waiting for a NewSessionTicket message any more */
4126 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004128
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004129 /*
4130 * Zero-length ticket means the server changed his mind and doesn't want
4131 * to send a ticket after all, so just forget it
4132 */
Paul Bakker66d5d072014-06-17 16:39:18 +02004133 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004134 return( 0 );
4135
Hanno Beckerb2964cb2019-01-30 14:46:35 +00004136 if( ssl->session != NULL && ssl->session->ticket != NULL )
4137 {
4138 mbedtls_platform_zeroize( ssl->session->ticket,
4139 ssl->session->ticket_len );
4140 mbedtls_free( ssl->session->ticket );
4141 ssl->session->ticket = NULL;
4142 ssl->session->ticket_len = 0;
4143 }
4144
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004145 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
4146 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004148 ssl->session_negotiate->ticket = NULL;
4149 ssl->session_negotiate->ticket_len = 0;
4150
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004151 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004152 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004154 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4155 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004156 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004157 }
4158
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02004159 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004160
4161 ssl->session_negotiate->ticket = ticket;
4162 ssl->session_negotiate->ticket_len = ticket_len;
4163 ssl->session_negotiate->ticket_lifetime = lifetime;
4164
4165 /*
4166 * RFC 5077 section 3.4:
4167 * "If the client receives a session ticket from the server, then it
4168 * discards any Session ID that was sent in the ServerHello."
4169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02004171 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004174
4175 return( 0 );
4176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004178
Paul Bakker5121ce52009-01-03 21:22:43 +00004179/*
Paul Bakker1961b702013-01-25 14:49:24 +01004180 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004183{
4184 int ret = 0;
4185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004188 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004189 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4191 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004192 ssl->handshake->new_session_ticket != 0 )
4193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004195 }
4196#endif
4197
Paul Bakker1961b702013-01-25 14:49:24 +01004198 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 case MBEDTLS_SSL_HELLO_REQUEST:
4201 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004202 break;
4203
Paul Bakker1961b702013-01-25 14:49:24 +01004204 /*
4205 * ==> ClientHello
4206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004208 ret = ssl_write_client_hello( ssl );
4209 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004210
Paul Bakker1961b702013-01-25 14:49:24 +01004211 /*
4212 * <== ServerHello
4213 * Certificate
4214 * ( ServerKeyExchange )
4215 * ( CertificateRequest )
4216 * ServerHelloDone
4217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004219 ret = ssl_parse_server_hello( ssl );
4220 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4223 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004224 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004227 ret = ssl_parse_server_key_exchange( ssl );
4228 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004231 ret = ssl_parse_certificate_request( ssl );
4232 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004235 ret = ssl_parse_server_hello_done( ssl );
4236 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004237
Paul Bakker1961b702013-01-25 14:49:24 +01004238 /*
4239 * ==> ( Certificate/Alert )
4240 * ClientKeyExchange
4241 * ( CertificateVerify )
4242 * ChangeCipherSpec
4243 * Finished
4244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4246 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004247 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004250 ret = ssl_write_client_key_exchange( ssl );
4251 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004254 ret = ssl_write_certificate_verify( ssl );
4255 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4258 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004259 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 case MBEDTLS_SSL_CLIENT_FINISHED:
4262 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004263 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004264
Paul Bakker1961b702013-01-25 14:49:24 +01004265 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004266 * <== ( NewSessionTicket )
4267 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004268 * Finished
4269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4271 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004272 ret = ssl_parse_new_session_ticket( ssl );
4273 break;
Paul Bakkera503a632013-08-14 13:48:06 +02004274#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4277 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004278 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 case MBEDTLS_SSL_SERVER_FINISHED:
4281 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004282 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 case MBEDTLS_SSL_FLUSH_BUFFERS:
4285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4286 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004287 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4290 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004291 break;
Paul Bakker48916f92012-09-16 19:57:18 +00004292
Paul Bakker1961b702013-01-25 14:49:24 +01004293 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01004296 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004297
4298 return( ret );
4299}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300#endif /* MBEDTLS_SSL_CLI_C */