blob: 023fac6af8a0015dc373c85d228c5907e2ac02bd [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"
Gabor Mezei765862c2021-10-19 12:22:25 +020036#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010037
Hanno Beckerbb89e272019-01-08 12:54:37 +000038#if defined(MBEDTLS_USE_PSA_CRYPTO)
39#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020040#include "psa/crypto.h"
Hanno Beckerbb89e272019-01-08 12:54:37 +000041#endif /* MBEDTLS_USE_PSA_CRYPTO */
42
SimonBd5800b72016-04-26 07:43:27 +010043#include <string.h>
44
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020045#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010048#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050052#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020053#endif
54
Gilles Peskineeccd8882020-03-10 12:19:08 +010055#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker520224e2018-10-26 11:38:07 +010056static int ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2e4f6162018-10-23 11:54:44 +010057{
58 if( conf->psk_identity == NULL ||
59 conf->psk_identity_len == 0 )
60 {
61 return( 0 );
62 }
63
64 if( conf->psk != NULL && conf->psk_len != 0 )
65 return( 1 );
66
67#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020068 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010069 return( 1 );
70#endif /* MBEDTLS_USE_PSA_CRYPTO */
71
72 return( 0 );
73}
Hanno Beckerdfab8e22018-10-23 11:59:34 +010074
75#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker520224e2018-10-26 11:38:07 +010076static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
Hanno Beckerdfab8e22018-10-23 11:59:34 +010077{
78 if( conf->psk_identity == NULL ||
79 conf->psk_identity_len == 0 )
80 {
81 return( 0 );
82 }
83
84 if( conf->psk != NULL && conf->psk_len != 0 )
85 return( 1 );
86
87 return( 0 );
88}
89#endif /* MBEDTLS_USE_PSA_CRYPTO */
90
Gilles Peskineeccd8882020-03-10 12:19:08 +010091#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +000094int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
95 unsigned char *buf,
96 const unsigned char *end,
97 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010098{
99 unsigned char *p = buf;
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +0100100 size_t hostname_len;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100101
102 *olen = 0;
103
Paul Bakker66d5d072014-06-17 16:39:18 +0200104 if( ssl->hostname == NULL )
Hanno Becker261602c2017-04-12 14:54:42 +0100105 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100106
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100107 MBEDTLS_SSL_DEBUG_MSG( 3,
108 ( "client hello, adding server name extension: %s",
109 ssl->hostname ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100110
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +0100111 hostname_len = strlen( ssl->hostname );
112
Hanno Becker261602c2017-04-12 14:54:42 +0100113 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
Simon Butchered997662015-09-28 02:14:30 +0100114
Paul Bakkerd3edc862013-03-20 16:07:17 +0100115 /*
Hanno Becker1a9a51c2017-04-07 13:02:16 +0100116 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
117 *
118 * In order to provide any of the server names, clients MAY include an
119 * extension of type "server_name" in the (extended) client hello. The
120 * "extension_data" field of this extension SHALL contain
121 * "ServerNameList" where:
122 *
Paul Bakkerd3edc862013-03-20 16:07:17 +0100123 * struct {
124 * NameType name_type;
125 * select (name_type) {
126 * case host_name: HostName;
127 * } name;
128 * } ServerName;
129 *
130 * enum {
131 * host_name(0), (255)
132 * } NameType;
133 *
134 * opaque HostName<1..2^16-1>;
135 *
136 * struct {
137 * ServerName server_name_list<1..2^16-1>
138 * } ServerNameList;
Hanno Becker1a9a51c2017-04-07 13:02:16 +0100139 *
Paul Bakkerd3edc862013-03-20 16:07:17 +0100140 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100141 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
142 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100143
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100144 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
145 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100147 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
148 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100149
Joe Subbiani2194dc42021-07-14 12:31:31 +0100150 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100151
152 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
153 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100154
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +0100155 memcpy( p, ssl->hostname, hostname_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100156
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +0100157 *olen = hostname_len + 9;
Hanno Becker261602c2017-04-12 14:54:42 +0100158
159 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +0100164static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
165 unsigned char *buf,
166 const unsigned char *end,
167 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100168{
169 unsigned char *p = buf;
170
171 *olen = 0;
172
Hanno Becker40f8b512017-10-12 14:58:55 +0100173 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
174 * initial ClientHello, in which case also adding the renegotiation
175 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +0100177 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100178
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100179 MBEDTLS_SSL_DEBUG_MSG( 3,
180 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100181
Hanno Becker261602c2017-04-12 14:54:42 +0100182 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +0100183
Paul Bakkerd3edc862013-03-20 16:07:17 +0100184 /*
185 * Secure renegotiation
186 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100187 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
188 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100189
190 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100191 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
192 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100193
194 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
195
196 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100197
198 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100201
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100202/*
203 * Only if we handle at least one key exchange that needs signatures.
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100206 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu1abd1bc2021-12-21 21:27:48 +0800207
Jerry Yu18cd4392021-12-17 17:44:24 +0800208static int ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
Jerry Yu1abd1bc2021-12-21 21:27:48 +0800209 const unsigned char *end, size_t *out_len )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100210{
211 unsigned char *p = buf;
Jerry Yu1abd1bc2021-12-21 21:27:48 +0800212 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
213 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
Hanno Becker261602c2017-04-12 14:54:42 +0100214
Jerry Yu1abd1bc2021-12-21 21:27:48 +0800215 *out_len = 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100216
217 /*
218 * enum {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200219 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
220 * sha512(6), (255)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100221 * } HashAlgorithm;
222 *
223 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
224 * SignatureAlgorithm;
225 *
226 * struct {
227 * HashAlgorithm hash;
228 * SignatureAlgorithm signature;
229 * } SignatureAndHashAlgorithm;
230 *
231 * SignatureAndHashAlgorithm
232 * supported_signature_algorithms<2..2^16-2>;
233 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100234
Jerry Yu1abd1bc2021-12-21 21:27:48 +0800235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100236
Jerry Yu1abd1bc2021-12-21 21:27:48 +0800237 /* Check if we have space for header and length field:
238 * - extension_type (2 bytes)
239 * - extension_data_length (2 bytes)
240 * - supported_signature_algorithms_length (2 bytes)
241 */
242 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
243 p += 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100244
Jerry Yu1abd1bc2021-12-21 21:27:48 +0800245 /*
246 * Write supported_signature_algorithms
247 */
248 supported_sig_alg = p;
249 for( const uint16_t *sig_alg = mbedtls_ssl_conf_get_sig_algs( ssl->conf );
250 *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
251 {
252 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
253 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
254 p += 2;
255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
256 }
257
258 /* Length of supported_signature_algorithms */
259 supported_sig_alg_len = p - supported_sig_alg;
260 if( supported_sig_alg_len == 0 )
261 {
262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
264 }
265
266 /* Write extension_type */
267 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
268 /* Write extension_data_length */
269 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
270 /* Write length of supported_signature_algorithms */
271 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
272
273 /* Output the total length of signature algorithms extension. */
274 *out_len = p - buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100275
276 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100279 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100280
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200281#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100282 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100283
Hanno Becker261602c2017-04-12 14:54:42 +0100284static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
285 unsigned char *buf,
286 const unsigned char *end,
287 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100288{
289 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100290 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100291
292 *olen = 0;
293
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100294 MBEDTLS_SSL_DEBUG_MSG( 3,
295 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100296 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100297
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100298 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
299 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100300
301 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100302 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200303
304 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100306
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200307 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100308
309 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100310}
Darryl Green11999bb2018-03-13 15:22:58 +0000311#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100312 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200314#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100315static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
316 unsigned char *buf,
317 const unsigned char *end,
318 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200319{
Janos Follath865b3eb2019-12-16 11:46:15 +0000320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200321 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200322 size_t kkpp_len;
323
324 *olen = 0;
325
326 /* Skip costly extension if we can't use EC J-PAKE anyway */
327 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100328 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200329
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100330 MBEDTLS_SSL_DEBUG_MSG( 3,
331 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200332
Hanno Becker261602c2017-04-12 14:54:42 +0100333 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200334
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100335 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
336 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200337
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200338 /*
339 * We may need to send ClientHello multiple times for Hello verification.
340 * We don't want to compute fresh values every time (both for performance
341 * and consistency reasons), so cache the extension content.
342 */
343 if( ssl->handshake->ecjpake_cache == NULL ||
344 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200345 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
347
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200348 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100349 p + 2, end - p - 2, &kkpp_len,
350 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200351 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200352 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100353 MBEDTLS_SSL_DEBUG_RET( 1 ,
354 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100355 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200356 }
357
358 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
359 if( ssl->handshake->ecjpake_cache == NULL )
360 {
361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100362 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200363 }
364
365 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
366 ssl->handshake->ecjpake_cache_len = kkpp_len;
367 }
368 else
369 {
370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
371
372 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100373 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200374
375 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200376 }
377
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100378 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
379 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200380
381 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100382
383 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200384}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200385#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000386
Hanno Beckera0e20d02019-05-15 14:03:01 +0100387#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +0100388static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
389 unsigned char *buf,
390 const unsigned char *end,
391 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100392{
393 unsigned char *p = buf;
394 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100395
396 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100397 * Quoting draft-ietf-tls-dtls-connection-id-05
398 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100399 *
400 * struct {
401 * opaque cid<0..2^8-1>;
402 * } ConnectionId;
403 */
404
405 *olen = 0;
406 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
407 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
408 {
Hanno Becker261602c2017-04-12 14:54:42 +0100409 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100410 }
411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
412
413 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
414 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100415 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100416
417 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100418 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
419 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100420 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100421 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
422 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100423
424 *p++ = (uint8_t) ssl->own_cid_len;
425 memcpy( p, ssl->own_cid, ssl->own_cid_len );
426
427 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100428
429 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100430}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100431#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +0100434static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
435 unsigned char *buf,
436 const unsigned char *end,
437 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200438{
439 unsigned char *p = buf;
440
Simon Butcher0fc94e92015-09-28 20:52:04 +0100441 *olen = 0;
442
Hanno Becker261602c2017-04-12 14:54:42 +0100443 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
444 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200445
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100446 MBEDTLS_SSL_DEBUG_MSG( 3,
447 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200448
Hanno Becker261602c2017-04-12 14:54:42 +0100449 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100450
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100451 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
452 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200453
454 *p++ = 0x00;
455 *p++ = 1;
456
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200457 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200458
459 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100460
461 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +0100466static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
467 unsigned char *buf,
468 const unsigned char *end,
469 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100470{
471 unsigned char *p = buf;
472
Simon Butcher0fc94e92015-09-28 20:52:04 +0100473 *olen = 0;
474
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100475 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100476 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100477
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100478 MBEDTLS_SSL_DEBUG_MSG( 3,
479 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100480
Hanno Becker261602c2017-04-12 14:54:42 +0100481 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100482
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100483 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
484 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100485
486 *p++ = 0x00;
487 *p++ = 0x00;
488
489 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100490
491 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +0100496static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
497 unsigned char *buf,
498 const unsigned char *end,
499 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200500{
501 unsigned char *p = buf;
502
Simon Butcher0fc94e92015-09-28 20:52:04 +0100503 *olen = 0;
504
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100505 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100506 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200507
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100508 MBEDTLS_SSL_DEBUG_MSG( 3,
509 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200510
Hanno Becker261602c2017-04-12 14:54:42 +0100511 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100512
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100513 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
514 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200515
516 *p++ = 0x00;
517 *p++ = 0x00;
518
519 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100520
521 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200522}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +0100526static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
527 unsigned char *buf,
528 const unsigned char *end,
529 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200530{
531 unsigned char *p = buf;
532 size_t tlen = ssl->session_negotiate->ticket_len;
533
Simon Butcher0fc94e92015-09-28 20:52:04 +0100534 *olen = 0;
535
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200536 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100537 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200538
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100539 MBEDTLS_SSL_DEBUG_MSG( 3,
540 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200541
Hanno Becker261602c2017-04-12 14:54:42 +0100542 /* The addition is safe here since the ticket length is 16 bit. */
543 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100544
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100545 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
546 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200547
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100548 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
549 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200550
551 *olen = 4;
552
Simon Butchered997662015-09-28 02:14:30 +0100553 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100554 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200555
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100556 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000557 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200558
559 memcpy( p, ssl->session_negotiate->ticket, tlen );
560
561 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100562
563 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker261602c2017-04-12 14:54:42 +0100568static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
569 unsigned char *buf,
570 const unsigned char *end,
571 size_t *olen )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200572{
573 unsigned char *p = buf;
Simon Butchered997662015-09-28 02:14:30 +0100574 size_t alpnlen = 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200575 const char **cur;
576
Simon Butcher0fc94e92015-09-28 20:52:04 +0100577 *olen = 0;
578
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200579 if( ssl->conf->alpn_list == NULL )
Hanno Becker261602c2017-04-12 14:54:42 +0100580 return( 0 );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200583
Simon Butchered997662015-09-28 02:14:30 +0100584 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
Hanno Beckere131bfe2017-04-12 14:54:42 +0100585 alpnlen += strlen( *cur ) + 1;
Simon Butchered997662015-09-28 02:14:30 +0100586
Hanno Becker261602c2017-04-12 14:54:42 +0100587 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen );
Simon Butchered997662015-09-28 02:14:30 +0100588
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100589 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
590 p += 2;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200591
592 /*
593 * opaque ProtocolName<1..2^8-1>;
594 *
595 * struct {
596 * ProtocolName protocol_name_list<2..2^16-1>
597 * } ProtocolNameList;
598 */
599
600 /* Skip writing extension and list length for now */
601 p += 4;
602
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200603 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200604 {
Hanno Beckere131bfe2017-04-12 14:54:42 +0100605 /*
606 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
607 * protocol names is less than 255.
608 */
609 *p = (unsigned char)strlen( *cur );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200610 memcpy( p + 1, *cur, *p );
611 p += 1 + *p;
612 }
613
614 *olen = p - buf;
615
616 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
Joe Subbiani6dd73642021-07-19 11:56:54 +0100617 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200618
619 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
Joe Subbiani6dd73642021-07-19 11:56:54 +0100620 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Hanno Becker261602c2017-04-12 14:54:42 +0100621
622 return( 0 );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200625
Ron Eldora9788042018-12-05 11:04:31 +0200626#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal77696ee2020-09-22 21:49:40 +0200627static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200628 unsigned char *buf,
629 const unsigned char *end,
630 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100631{
632 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200633 size_t protection_profiles_index = 0, ext_len = 0;
634 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100635
636 *olen = 0;
637
Johan Pascalc3ccd982020-10-28 17:18:18 +0100638 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
639 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
640 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100641 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200642 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100643 }
644
Ron Eldora9788042018-12-05 11:04:31 +0200645 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100646 * uint8 SRTPProtectionProfile[2];
647 *
648 * struct {
649 * SRTPProtectionProfiles SRTPProtectionProfiles;
650 * opaque srtp_mki<0..255>;
651 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100652 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100653 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200654 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200655 {
656 mki_len = ssl->dtls_srtp_info.mki_len;
657 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300658 /* Extension length = 2 bytes for profiles length,
659 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
660 * 1 byte for srtp_mki vector length and the mki_len value
661 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200662 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
663
Johan Pascal77696ee2020-09-22 21:49:40 +0200664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
665
666 /* Check there is room in the buffer for the extension + 4 bytes
667 * - the extension tag (2 bytes)
668 * - the extension length (2 bytes)
669 */
670 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
671
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100672 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
673 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200674
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100675 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
676 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100677
Ron Eldor3adb9922017-12-21 10:15:08 +0200678 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200679 /* micro-optimization:
680 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
681 * which is lower than 127, so the upper byte of the length is always 0
682 * For the documentation, the more generic code is left in comments
683 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
684 * >> 8 ) & 0xFF );
685 */
686 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100687 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100688
Ron Eldoref72faf2018-07-12 11:54:20 +0300689 for( protection_profiles_index=0;
690 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
691 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100692 {
Johan Pascal43f94902020-09-22 12:25:52 +0200693 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200694 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200695 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200696 {
697 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
698 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100699 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
700 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200701 }
702 else
703 {
704 /*
705 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200706 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200707 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200708 MBEDTLS_SSL_DEBUG_MSG( 3,
709 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200710 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200711 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
712 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200713 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100714 }
715 }
716
Ron Eldor591f1622018-01-22 12:30:04 +0200717 *p++ = mki_len & 0xFF;
718
719 if( mki_len != 0 )
720 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200721 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200722 /*
723 * Increment p to point to the current position.
724 */
725 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300726 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
727 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200728 }
729
Ron Eldoref72faf2018-07-12 11:54:20 +0300730 /*
731 * total extension length: extension type (2 bytes)
732 * + extension length (2 bytes)
733 * + protection profile length (2 bytes)
734 * + 2 * number of protection profiles
735 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200736 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300737 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200738 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200739
740 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100741}
742#endif /* MBEDTLS_SSL_DTLS_SRTP */
743
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200744/*
745 * Generate random bytes for ClientHello
746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747static int ssl_generate_random( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200748{
Janos Follath865b3eb2019-12-16 11:46:15 +0000749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200750 unsigned char *p = ssl->handshake->randbytes;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100752 mbedtls_time_t t;
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200753#endif
754
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200755 /*
756 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200759 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200760 ssl->handshake->verify_cookie != NULL )
761 {
762 return( 0 );
763 }
764#endif
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100767 t = mbedtls_time( NULL );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100768 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
769 p += 4;
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200770
Paul Elliottd48d5c62021-01-07 14:47:05 +0000771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
772 (long long) t ) );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200773#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +0100774 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200775 return( ret );
776
777 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#endif /* MBEDTLS_HAVE_TIME */
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200779
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +0100780 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200781 return( ret );
782
783 return( 0 );
784}
785
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100786/**
787 * \brief Validate cipher suite against config in SSL context.
788 *
789 * \param suite_info cipher suite to validate
790 * \param ssl SSL context
Andrzej Kurek03bac442018-04-25 05:06:07 -0400791 * \param min_minor_ver Minimal minor version to accept a cipher suite
792 * \param max_minor_ver Maximal minor version to accept a cipher suite
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100793 *
794 * \return 0 if valid, else 1
795 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100796static int ssl_validate_ciphersuite(
797 const mbedtls_ssl_ciphersuite_t * suite_info,
798 const mbedtls_ssl_context * ssl,
799 int min_minor_ver, int max_minor_ver )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100800{
Andrzej Kurek03bac442018-04-25 05:06:07 -0400801 (void) ssl;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100802 if( suite_info == NULL )
803 return( 1 );
804
Andrzej Kurek03bac442018-04-25 05:06:07 -0400805 if( suite_info->min_minor_ver > max_minor_ver ||
806 suite_info->max_minor_ver < min_minor_ver )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100807 return( 1 );
808
809#if defined(MBEDTLS_SSL_PROTO_DTLS)
810 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
811 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
812 return( 1 );
813#endif
814
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100815#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
816 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
817 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
818 return( 1 );
819#endif
820
Hanno Becker2e4f6162018-10-23 11:54:44 +0100821 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100822#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker2e4f6162018-10-23 11:54:44 +0100823 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker520224e2018-10-26 11:38:07 +0100824 ssl_conf_has_static_psk( ssl->conf ) == 0 )
Hanno Becker2e4f6162018-10-23 11:54:44 +0100825 {
826 return( 1 );
827 }
Gilles Peskineeccd8882020-03-10 12:19:08 +0100828#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +0100829
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100830 return( 0 );
831}
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000834{
Janos Follath865b3eb2019-12-16 11:46:15 +0000835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100836 size_t i, n, olen, ext_len = 0;
Hanno Becker261602c2017-04-12 14:54:42 +0100837
Paul Bakker5121ce52009-01-03 21:22:43 +0000838 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200839 unsigned char *p, *q;
Hanno Becker261602c2017-04-12 14:54:42 +0100840 const unsigned char *end;
841
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200842 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ron Eldor755bb6a2018-02-14 19:30:48 +0200844#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
845 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
846 int uses_ec = 0;
847#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +0100851 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +0100852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
854 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +0100855 }
856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_RENEGOTIATION)
858 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100859#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000860 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200861 ssl->major_ver = ssl->conf->min_major_ver;
862 ssl->minor_ver = ssl->conf->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000863 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200865 if( ssl->conf->max_major_ver == 0 )
Paul Bakker490ecc82011-10-06 13:04:09 +0000866 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100867 MBEDTLS_SSL_DEBUG_MSG( 1,
868 ( "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()" ) );
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker490ecc82011-10-06 13:04:09 +0000870 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Hanno Becker261602c2017-04-12 14:54:42 +0100872 buf = ssl->out_msg;
873 end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN;
874
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 /*
Hanno Becker261602c2017-04-12 14:54:42 +0100876 * Check if there's enough space for the first part of the ClientHello
877 * consisting of the 38 bytes described below, the session identifier (at
878 * most 32 bytes) and its length (1 byte).
879 *
880 * Use static upper bounds instead of the actual values
881 * to allow the compiler to optimize this away.
882 */
883 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );
884
885 /*
886 * The 38 first bytes of the ClientHello:
887 * 0 . 0 handshake type (written later)
888 * 1 . 3 handshake length (written later)
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 * 4 . 5 highest version supported
890 * 6 . 9 current UNIX time
891 * 10 . 37 random bytes
Hanno Becker261602c2017-04-12 14:54:42 +0100892 *
893 * The current UNIX time (4 bytes) and following 28 random bytes are written
894 * by ssl_generate_random() into ssl->handshake->randbytes buffer and then
895 * copied from there into the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000897
Hanno Becker261602c2017-04-12 14:54:42 +0100898 p = buf + 4;
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100899 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
900 ssl->conf->max_minor_ver,
901 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100902 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 buf[4], buf[5] ) );
906
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200907 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200910 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200911 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200912
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200913 memcpy( p, ssl->handshake->randbytes, 32 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200915 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
917 /*
918 * 38 . 38 session id length
919 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100920 * 39+n . 39+n DTLS only: cookie length (1 byte)
Hanno Becker261602c2017-04-12 14:54:42 +0100921 * 40+n . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100922 * .. . .. ciphersuitelist length (2 bytes)
923 * .. . .. ciphersuitelist
924 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000925 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100926 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000927 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200929 n = ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100931 if( n < 16 || n > 32 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_SSL_RENEGOTIATION)
933 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100934#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000935 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200936 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200938 }
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200941 /*
942 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
943 * generate and include a Session ID in the TLS ClientHello."
944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#if defined(MBEDTLS_SSL_RENEGOTIATION)
946 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000947#endif
Manuel Pégourié-Gonnardd2b35ec2015-03-10 11:40:43 +0000948 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000949 if( ssl->session_negotiate->ticket != NULL &&
950 ssl->session_negotiate->ticket_len != 0 )
951 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100952 ret = ssl->conf->f_rng( ssl->conf->p_rng,
953 ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200954
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000955 if( ret != 0 )
956 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200957
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200958 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000959 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200960 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Hanno Becker261602c2017-04-12 14:54:42 +0100963 /*
964 * The first check of the output buffer size above (
965 * MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );)
966 * has checked that there is enough space in the output buffer for the
967 * session identifier length byte and the session identifier (n <= 32).
968 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 *p++ = (unsigned char) n;
970
971 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000972 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Paul Elliottd48d5c62021-01-07 14:47:05 +0000974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100977 /*
Hanno Becker261602c2017-04-12 14:54:42 +0100978 * With 'n' being the length of the session identifier
979 *
980 * 39+n . 39+n DTLS only: cookie length (1 byte)
981 * 40+n . .. DTLS only: cookie
982 * .. . .. ciphersuitelist length (2 bytes)
983 * .. . .. ciphersuitelist
984 * .. . .. compression methods length (1 byte)
985 * .. . .. compression methods
986 * .. . .. extensions length (2 bytes)
987 * .. . .. extensions
988 */
989
990 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100991 * DTLS cookie
992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200994 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100995 {
Hanno Becker261602c2017-04-12 14:54:42 +0100996 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
997
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200998 if( ssl->handshake->verify_cookie == NULL )
999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001001 *p++ = 0;
1002 }
1003 else
1004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001006 ssl->handshake->verify_cookie,
1007 ssl->handshake->verify_cookie_len );
1008
1009 *p++ = ssl->handshake->verify_cookie_len;
Hanno Becker261602c2017-04-12 14:54:42 +01001010
1011 MBEDTLS_SSL_CHK_BUF_PTR( p, end,
1012 ssl->handshake->verify_cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001013 memcpy( p, ssl->handshake->verify_cookie,
1014 ssl->handshake->verify_cookie_len );
1015 p += ssl->handshake->verify_cookie_len;
1016 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001017 }
1018#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Paul Bakker48916f92012-09-16 19:57:18 +00001020 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001021 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +00001022 */
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001023 ciphersuites = ssl->conf->ciphersuite_list;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001024
1025 /* Skip writing ciphersuite length for now */
1026 n = 0;
1027 q = p;
Hanno Becker261602c2017-04-12 14:54:42 +01001028
1029 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001030 p += 2;
1031
Paul Bakker2fbefde2013-06-29 16:01:15 +02001032 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker2fbefde2013-06-29 16:01:15 +02001035
Andrzej Kurek03bac442018-04-25 05:06:07 -04001036 if( ssl_validate_ciphersuite( ciphersuite_info, ssl,
1037 ssl->conf->min_minor_ver,
1038 ssl->conf->max_minor_ver ) != 0 )
Paul Bakker2fbefde2013-06-29 16:01:15 +02001039 continue;
1040
Hanno Becker3c88c652019-01-02 11:17:25 +00001041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)",
Paul Elliott9f352112020-12-09 14:55:45 +00001042 (unsigned int)ciphersuites[i], ciphersuite_info->name ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Ron Eldor755bb6a2018-02-14 19:30:48 +02001044#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1045 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1046 uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
1047#endif
1048
Hanno Becker261602c2017-04-12 14:54:42 +01001049 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1050
Paul Bakker2fbefde2013-06-29 16:01:15 +02001051 n++;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001052 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], p, 0 );
1053 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 }
1055
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001056 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001057 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites (excluding SCSVs)", n ) );
Ron Eldor714785d2017-08-28 13:55:55 +03001058
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +00001059 /*
1060 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_SSL_RENEGOTIATION)
1063 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +00001064#endif
1065 {
Ron Eldor4a2fb4c2017-09-10 17:03:50 +03001066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
Hanno Becker261602c2017-04-12 14:54:42 +01001067 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001068 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
1069 p += 2;
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +00001070 n++;
1071 }
1072
Paul Bakker2fbefde2013-06-29 16:01:15 +02001073 *q++ = (unsigned char)( n >> 7 );
1074 *q++ = (unsigned char)( n << 1 );
1075
Mateusz Starzyka3a99842021-02-19 14:27:22 +01001076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
1077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
1078 MBEDTLS_SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Mateusz Starzyka3a99842021-02-19 14:27:22 +01001080 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1081 *p++ = 1;
1082 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
Hanno Becker261602c2017-04-12 14:54:42 +01001084 /* First write extensions, then the total length */
1085
1086 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai6f435f02021-11-09 04:08:32 +00001089 if( ( ret = mbedtls_ssl_write_hostname_ext( ssl, p + 2 + ext_len,
Xiaofei Baif36e1672021-11-09 09:28:25 +00001090 end, &olen ) ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +01001091 {
Xiaofei Bai6f435f02021-11-09 04:08:32 +00001092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_hostname_ext", ret );
Hanno Becker261602c2017-04-12 14:54:42 +01001093 return( ret );
1094 }
Paul Bakkerd3edc862013-03-20 16:07:17 +01001095 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +02001096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Hanno Becker40f8b512017-10-12 14:58:55 +01001098 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
1099 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +01001101 if( ( ret = ssl_write_renegotiation_ext( ssl, p + 2 + ext_len,
1102 end, &olen ) ) != 0 )
1103 {
1104 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
1105 return( ret );
1106 }
Paul Bakkerd3edc862013-03-20 16:07:17 +01001107 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001108#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001111 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu18cd4392021-12-17 17:44:24 +08001112 if( ( ret = ssl_write_sig_alg_ext( ssl, p + 2 + ext_len,
1113 end, &olen ) ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +01001114 {
Jerry Yu2d0bd322022-01-12 12:58:00 +08001115 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_sig_alg_ext", ret );
Hanno Becker261602c2017-04-12 14:54:42 +01001116 return( ret );
1117 }
Paul Bakkerd3edc862013-03-20 16:07:17 +01001118 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001119#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001120
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02001121#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001122 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02001123 if( uses_ec )
1124 {
Jerry Yu9d555ac2021-12-20 22:27:58 +08001125 if( ( ret = mbedtls_ssl_write_supported_groups_ext( ssl, p + 2 + ext_len,
1126 end, &olen ) ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +01001127 {
Jerry Yub47d0f82021-12-20 17:34:40 +08001128 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_groups_ext", ret );
Hanno Becker261602c2017-04-12 14:54:42 +01001129 return( ret );
1130 }
Ron Eldor755bb6a2018-02-14 19:30:48 +02001131 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +01001132
Hanno Becker261602c2017-04-12 14:54:42 +01001133 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len,
1134 end, &olen ) ) != 0 )
1135 {
1136 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
1137 return( ret );
1138 }
Ron Eldor755bb6a2018-02-14 19:30:48 +02001139 ext_len += olen;
1140 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001141#endif
1142
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001143#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +01001144 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len,
1145 end, &olen ) ) != 0 )
1146 {
1147 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
1148 return( ret );
1149 }
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +02001150 ext_len += olen;
1151#endif
1152
Hanno Beckera0e20d02019-05-15 14:03:01 +01001153#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +01001154 if( ( ret = ssl_write_cid_ext( ssl, p + 2 + ext_len, end, &olen ) ) != 0 )
1155 {
1156 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
1157 return( ret );
1158 }
Hanno Becker49770ff2019-04-25 16:55:15 +01001159 ext_len += olen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001160#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +01001161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +01001163 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len,
1164 end, &olen ) ) != 0 )
1165 {
1166 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
1167 return( ret );
1168 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001169 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001170#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +01001173 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len,
1174 end, &olen ) ) != 0 )
1175 {
1176 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
1177 return( ret );
1178 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001179 ext_len += olen;
1180#endif
1181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +01001183 if( ( ret = ssl_write_extended_ms_ext( ssl, p + 2 + ext_len,
1184 end, &olen ) ) != 0 )
1185 {
1186 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
1187 return( ret );
1188 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001189 ext_len += olen;
1190#endif
1191
Simon Butcher5624ec82015-09-29 01:06:06 +01001192#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker261602c2017-04-12 14:54:42 +01001193 if( ( ret = ssl_write_alpn_ext( ssl, p + 2 + ext_len,
1194 end, &olen ) ) != 0 )
1195 {
1196 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_alpn_ext", ret );
1197 return( ret );
1198 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001199 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001200#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001201
Johan Pascalb62bb512015-12-03 21:56:45 +01001202#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01001203 if( ( ret = ssl_write_use_srtp_ext( ssl, p + 2 + ext_len,
1204 end, &olen ) ) != 0 )
Ron Eldor3adb9922017-12-21 10:15:08 +02001205 {
Johan Pascalc3ccd982020-10-28 17:18:18 +01001206 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
1207 return( ret );
Ron Eldor3adb9922017-12-21 10:15:08 +02001208 }
Johan Pascalc3ccd982020-10-28 17:18:18 +01001209 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01001210#endif
1211
Simon Butcher5624ec82015-09-29 01:06:06 +01001212#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +01001213 if( ( ret = ssl_write_session_ticket_ext( ssl, p + 2 + ext_len,
1214 end, &olen ) ) != 0 )
1215 {
1216 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
1217 return( ret );
1218 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001219 ext_len += olen;
1220#endif
1221
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001222 /* olen unused if all extensions are disabled */
1223 ((void) olen);
1224
Paul Elliottd48d5c62021-01-07 14:47:05 +00001225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker261602c2017-04-12 14:54:42 +01001226 ext_len ) );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001227
Paul Bakkera7036632014-04-30 10:15:38 +02001228 if( ext_len > 0 )
1229 {
Hanno Becker261602c2017-04-12 14:54:42 +01001230 /* No need to check for space here, because the extension
1231 * writing functions already took care of that. */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001232 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
Joe Subbiani94180e72021-08-20 16:20:44 +01001233 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02001234 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001235
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
1238 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
1240 ssl->state++;
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001243 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02001245#endif
1246
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02001247 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02001249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 return( ret );
1251 }
1252
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001253#if defined(MBEDTLS_SSL_PROTO_DTLS)
1254 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1255 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
1256 {
1257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
1258 return( ret );
1259 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01001260#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
1264 return( 0 );
1265}
1266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001268 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +00001269 size_t len )
1270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_SSL_RENEGOTIATION)
1272 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001273 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001274 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +00001275 if( len != 1 + ssl->verify_data_len * 2 ||
1276 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +02001277 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001278 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +02001279 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001280 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001283 mbedtls_ssl_send_alert_message(
1284 ssl,
1285 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1286 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001287 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001288 }
1289 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001290 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001292 {
1293 if( len != 1 || buf[0] != 0x00 )
1294 {
Ronald Cron5ee57072020-06-11 09:34:06 +02001295 MBEDTLS_SSL_DEBUG_MSG( 1,
1296 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001297 mbedtls_ssl_send_alert_message(
1298 ssl,
1299 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1300 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001301 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001302 }
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001305 }
Paul Bakker48916f92012-09-16 19:57:18 +00001306
1307 return( 0 );
1308}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1311static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001312 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001313 size_t len )
1314{
1315 /*
1316 * server should use the extension only if we did,
1317 * and if so the server's value should match ours (and len is always 1)
1318 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001319 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001320 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001321 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001322 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001323 MBEDTLS_SSL_DEBUG_MSG( 1,
1324 ( "non-matching max fragment length extension" ) );
1325 mbedtls_ssl_send_alert_message(
1326 ssl,
1327 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001328 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1329 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001330 }
1331
1332 return( 0 );
1333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +00001335
Hanno Beckera0e20d02019-05-15 14:03:01 +01001336#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001337static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
1338 const unsigned char *buf,
1339 size_t len )
1340{
1341 size_t peer_cid_len;
1342
1343 if( /* CID extension only makes sense in DTLS */
1344 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
1345 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +01001346 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +01001347 {
Hanno Becker22626482019-05-03 12:46:59 +01001348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +01001349 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001350 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +01001351 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +01001352 }
1353
1354 if( len == 0 )
1355 {
1356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
1357 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001358 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1359 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +01001360 }
1361
1362 peer_cid_len = *buf++;
1363 len--;
1364
1365 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
1366 {
Hanno Becker22626482019-05-03 12:46:59 +01001367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +01001368 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001369 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1370 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +01001371 }
1372
1373 if( len != peer_cid_len )
1374 {
Hanno Becker22626482019-05-03 12:46:59 +01001375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +01001376 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001377 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1378 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +01001379 }
1380
Hanno Becker5a299902019-05-03 12:47:49 +01001381 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +01001382 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
1383 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
1384
1385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
1386 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
1387
Hanno Beckera8373a12019-04-26 15:37:26 +01001388 return( 0 );
1389}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001390#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1393static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001394 const unsigned char *buf,
1395 size_t len )
1396{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001397 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001398 len != 0 )
1399 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001400 MBEDTLS_SSL_DEBUG_MSG( 1,
1401 ( "non-matching encrypt-then-MAC extension" ) );
1402 mbedtls_ssl_send_alert_message(
1403 ssl,
1404 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001405 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +01001406 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001407 }
1408
1409 ((void) buf);
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001412
1413 return( 0 );
1414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1418static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001419 const unsigned char *buf,
1420 size_t len )
1421{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001422 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001423 len != 0 )
1424 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001425 MBEDTLS_SSL_DEBUG_MSG( 1,
1426 ( "non-matching extended master secret extension" ) );
1427 mbedtls_ssl_send_alert_message(
1428 ssl,
1429 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +01001430 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
1431 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001432 }
1433
1434 ((void) buf);
1435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001437
1438 return( 0 );
1439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1443static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001444 const unsigned char *buf,
1445 size_t len )
1446{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001447 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001448 len != 0 )
1449 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001450 MBEDTLS_SSL_DEBUG_MSG( 1,
1451 ( "non-matching session ticket extension" ) );
1452 mbedtls_ssl_send_alert_message(
1453 ssl,
1454 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +01001455 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
1456 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001457 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001458
1459 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001460
1461 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001462
1463 return( 0 );
1464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001466
Robert Cragie136884c2015-10-02 13:34:31 +01001467#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001468 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001470 const unsigned char *buf,
1471 size_t len )
1472{
1473 size_t list_size;
1474 const unsigned char *p;
1475
Philippe Antoine747fd532018-05-30 09:13:21 +02001476 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001479 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1480 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001481 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001482 }
Philippe Antoine747fd532018-05-30 09:13:21 +02001483 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001484
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001485 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001486 while( list_size > 0 )
1487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1489 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001490 {
Robert Cragie136884c2015-10-02 13:34:31 +01001491#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001492 ssl->handshake->ecdh_ctx.point_format = p[0];
Gilles Peskine064a85c2017-05-10 10:46:40 +02001493#endif
Robert Cragieae8535d2015-10-06 17:11:18 +01001494#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +02001495 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
1496 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +01001497#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001499 return( 0 );
1500 }
1501
1502 list_size--;
1503 p++;
1504 }
1505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1508 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001509 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001510}
Darryl Green11999bb2018-03-13 15:22:58 +00001511#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001512 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001513
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001514#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1515static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
1516 const unsigned char *buf,
1517 size_t len )
1518{
Janos Follath865b3eb2019-12-16 11:46:15 +00001519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001520
Hanno Beckere694c3e2017-12-27 21:34:08 +00001521 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001522 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
1523 {
1524 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
1525 return( 0 );
1526 }
1527
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +02001528 /* If we got here, we no longer need our cached extension */
1529 mbedtls_free( ssl->handshake->ecjpake_cache );
1530 ssl->handshake->ecjpake_cache = NULL;
1531 ssl->handshake->ecjpake_cache_len = 0;
1532
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001533 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
1534 buf, len ) ) != 0 )
1535 {
1536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001537 mbedtls_ssl_send_alert_message(
1538 ssl,
1539 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1540 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001541 return( ret );
1542 }
1543
1544 return( 0 );
1545}
1546#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_SSL_ALPN)
1549static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001550 const unsigned char *buf, size_t len )
1551{
1552 size_t list_len, name_len;
1553 const char **p;
1554
1555 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001556 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001557 {
1558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001559 mbedtls_ssl_send_alert_message(
1560 ssl,
1561 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +01001562 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
1563 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001564 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001565
1566 /*
1567 * opaque ProtocolName<1..2^8-1>;
1568 *
1569 * struct {
1570 * ProtocolName protocol_name_list<2..2^16-1>
1571 * } ProtocolNameList;
1572 *
1573 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1574 */
1575
1576 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1577 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001578 {
1579 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1580 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001581 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001582 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001583
1584 list_len = ( buf[0] << 8 ) | buf[1];
1585 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001586 {
1587 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1588 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001589 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001590 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001591
1592 name_len = buf[2];
1593 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001594 {
1595 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1596 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001597 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001598 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001599
1600 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001601 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001602 {
1603 if( name_len == strlen( *p ) &&
1604 memcmp( buf + 3, *p, name_len ) == 0 )
1605 {
1606 ssl->alpn_chosen = *p;
1607 return( 0 );
1608 }
1609 }
1610
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001612 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1613 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001614 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001617
Johan Pascalb62bb512015-12-03 21:56:45 +01001618#if defined(MBEDTLS_SSL_DTLS_SRTP)
1619static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001620 const unsigned char *buf,
1621 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001622{
Johan Pascal43f94902020-09-22 12:25:52 +02001623 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001624 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001625 uint16_t server_protection_profile_value = 0;
1626
1627 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001628 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1629 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1630 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001631 return( 0 );
1632
Ron Eldora9788042018-12-05 11:04:31 +02001633 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001634 * uint8 SRTPProtectionProfile[2];
1635 *
1636 * struct {
1637 * SRTPProtectionProfiles SRTPProtectionProfiles;
1638 * opaque srtp_mki<0..255>;
1639 * } UseSRTPData;
1640
1641 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1642 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001643 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001644 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001645 {
1646 mki_len = ssl->dtls_srtp_info.mki_len;
1647 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001648
Ron Eldoref72faf2018-07-12 11:54:20 +03001649 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001650 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1651 * + protection profile (2 bytes)
1652 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001653 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001654 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001655 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001656 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001657
1658 /*
1659 * get the server protection profile
1660 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001661
1662 /*
1663 * protection profile length must be 0x0002 as we must have only
1664 * one protection profile in server Hello
1665 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001666 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001667 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001668
1669 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001670 server_protection = mbedtls_ssl_check_srtp_profile_value(
1671 server_protection_profile_value );
1672 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001673 {
Johan Pascal43f94902020-09-22 12:25:52 +02001674 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1675 mbedtls_ssl_get_srtp_profile_as_string(
1676 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001677 }
1678
Johan Pascal43f94902020-09-22 12:25:52 +02001679 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001680
Johan Pascalb62bb512015-12-03 21:56:45 +01001681 /*
1682 * Check we have the server profile in our list
1683 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001684 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001685 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001686 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1687 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001688 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1690 mbedtls_ssl_get_srtp_profile_as_string(
1691 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001692 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001693 }
1694 }
1695
Ron Eldor591f1622018-01-22 12:30:04 +02001696 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001697 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001698 {
1699 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001700 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001701 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001702 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001703
1704 /* If server does not use mki in its reply, make sure the client won't keep
1705 * one as negotiated */
1706 if( len == 5 )
1707 {
1708 ssl->dtls_srtp_info.mki_len = 0;
1709 }
1710
Ron Eldoref72faf2018-07-12 11:54:20 +03001711 /*
1712 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001713 * If the client detects a nonzero-length MKI in the server's response
1714 * that is different than the one the client offered, then the client
1715 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1716 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001717 if( len > 5 && ( buf[4] != mki_len ||
1718 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001719 {
1720 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1721 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001722 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001723 }
Ron Eldorb4655392018-07-05 18:25:39 +03001724#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001725 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001726 {
Ron Eldora9788042018-12-05 11:04:31 +02001727 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1728 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001729 }
1730#endif
Ron Eldora9788042018-12-05 11:04:31 +02001731 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001732}
1733#endif /* MBEDTLS_SSL_DTLS_SRTP */
1734
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001735/*
1736 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738#if defined(MBEDTLS_SSL_PROTO_DTLS)
1739static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001742 int major_ver, minor_ver;
1743 unsigned char cookie_len;
1744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001746
Gilles Peskineb64bf062019-09-27 14:02:44 +02001747 /* Check that there is enough room for:
1748 * - 2 bytes of version
1749 * - 1 byte of cookie_len
1750 */
1751 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1752 {
1753 MBEDTLS_SSL_DEBUG_MSG( 1,
1754 ( "incoming HelloVerifyRequest message is too short" ) );
1755 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1756 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001757 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001758 }
1759
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001760 /*
1761 * struct {
1762 * ProtocolVersion server_version;
1763 * opaque cookie<0..2^8-1>;
1764 * } HelloVerifyRequest;
1765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001767 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001768 p += 2;
1769
TRodziewicz2d8800e2021-05-13 19:14:19 +02001770 /*
1771 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1772 * even is lower than our min version.
1773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
TRodziewiczb5850c52021-05-13 17:11:23 +02001775 minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001776 major_ver > ssl->conf->max_major_ver ||
1777 minor_ver > ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1782 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001783
Hanno Beckerbc000442021-06-24 09:18:19 +01001784 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001785 }
1786
1787 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001788 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1789 {
1790 MBEDTLS_SSL_DEBUG_MSG( 1,
1791 ( "cookie length does not match incoming message size" ) );
1792 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1793 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001794 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001795 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001796 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 mbedtls_free( ssl->handshake->verify_cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001799
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001800 ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001801 if( ssl->handshake->verify_cookie == NULL )
1802 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001804 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001805 }
1806
1807 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1808 ssl->handshake->verify_cookie_len = cookie_len;
1809
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001810 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1812 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001817
1818 return( 0 );
1819}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001823{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001824 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001825 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001826 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001827 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001828 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001830 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001831#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001832 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
Hanno Becker327c93b2018-08-15 13:56:18 +01001837 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001839 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 return( ret );
1842 }
1843
Hanno Becker79594fd2019-05-08 09:38:41 +01001844 buf = ssl->in_msg;
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848#if defined(MBEDTLS_SSL_RENEGOTIATION)
1849 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001850 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001851 ssl->renego_records_seen++;
1852
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001853 if( ssl->conf->renego_max_records >= 0 &&
1854 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001855 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001856 MBEDTLS_SSL_DEBUG_MSG( 1,
1857 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001859 }
1860
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001861 MBEDTLS_SSL_DEBUG_MSG( 1,
1862 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001863
1864 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001866 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001870 mbedtls_ssl_send_alert_message(
1871 ssl,
1872 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1873 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 }
1876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001878 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001884 return( ssl_parse_hello_verify_request( ssl ) );
1885 }
1886 else
1887 {
1888 /* We made it through the verification process */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 mbedtls_free( ssl->handshake->verify_cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001890 ssl->handshake->verify_cookie = NULL;
1891 ssl->handshake->verify_cookie_len = 0;
1892 }
1893 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1897 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001900 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1901 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001902 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 }
1904
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001905 /*
1906 * 0 . 1 server_version
1907 * 2 . 33 random (maybe including 4 bytes of Unix time)
1908 * 34 . 34 session_id length = n
1909 * 35 . 34+n session_id
1910 * 35+n . 36+n cipher_suite
1911 * 37+n . 37+n compression_method
1912 *
1913 * 38+n . 39+n extensions length (optional)
1914 * 40+n . .. extensions
1915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
1919 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001920 ssl->conf->transport, buf + 0 );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001921 ssl->session_negotiate->minor_ver = ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001923 if( ssl->major_ver < ssl->conf->min_major_ver ||
1924 ssl->minor_ver < ssl->conf->min_minor_ver ||
1925 ssl->major_ver > ssl->conf->max_major_ver ||
1926 ssl->minor_ver > ssl->conf->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001927 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001928 MBEDTLS_SSL_DEBUG_MSG( 1,
1929 ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1930 ssl->conf->min_major_ver,
1931 ssl->conf->min_minor_ver,
1932 ssl->major_ver, ssl->minor_ver,
1933 ssl->conf->max_major_ver,
1934 ssl->conf->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1937 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001938
Hanno Beckerbc000442021-06-24 09:18:19 +01001939 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001940 }
1941
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001943 ( (unsigned long) buf[2] << 24 ) |
1944 ( (unsigned long) buf[3] << 16 ) |
1945 ( (unsigned long) buf[4] << 8 ) |
1946 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001948 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001950 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001953
Paul Bakker48916f92012-09-16 19:57:18 +00001954 if( n > 32 )
1955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001957 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1958 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001959 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001960 }
1961
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001962 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001964 ext_len = ( ( buf[38 + n] << 8 )
1965 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001966
Paul Bakker48916f92012-09-16 19:57:18 +00001967 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001971 mbedtls_ssl_send_alert_message(
1972 ssl,
1973 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1974 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001975 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001976 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001978 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001979 {
1980 ext_len = 0;
1981 }
1982 else
1983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001985 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1986 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001987 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001988 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001990 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001991 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001992
1993 /*
1994 * Read and check compression
1995 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001996 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001998 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001999 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002000 MBEDTLS_SSL_DEBUG_MSG( 1,
2001 ( "server hello, bad compression: %d", comp ) );
2002 mbedtls_ssl_send_alert_message(
2003 ssl,
2004 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2005 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02002007 }
2008
Paul Bakker380da532012-04-18 16:10:25 +00002009 /*
2010 * Initialize update checksum functions
2011 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002012 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
2013 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01002014 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002015 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00002016 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002017 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2018 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01002020 }
Paul Bakker380da532012-04-18 16:10:25 +00002021
Hanno Beckere694c3e2017-12-27 21:34:08 +00002022 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01002023
Paul Elliottd48d5c62021-01-07 14:47:05 +00002024 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
2027 /*
2028 * Check if the session can be resumed
2029 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002030 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#if defined(MBEDTLS_SSL_RENEGOTIATION)
2032 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002033#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002034 ssl->session_negotiate->ciphersuite != i ||
2035 ssl->session_negotiate->compression != comp ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002036 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002037 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 {
2039 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00002040 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002042 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002043#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002044 ssl->session_negotiate->ciphersuite = i;
2045 ssl->session_negotiate->compression = comp;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002046 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002047 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 }
2049 else
2050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002056 mbedtls_ssl_send_alert_message(
2057 ssl,
2058 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2059 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002060 return( ret );
2061 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 }
2063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002065 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
Paul Elliott3891caf2020-12-17 18:42:40 +00002067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
2069 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
Andrzej Kurek03bac442018-04-25 05:06:07 -04002071 /*
2072 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01002073 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 i = 0;
2075 while( 1 )
2076 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01002077 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002080 mbedtls_ssl_send_alert_message(
2081 ssl,
2082 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2083 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002084 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 }
2086
Hanno Beckerd60b6c62021-04-29 12:04:11 +01002087 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002088 ssl->session_negotiate->ciphersuite )
2089 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002091 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002094 suite_info = mbedtls_ssl_ciphersuite_from_id(
2095 ssl->session_negotiate->ciphersuite );
2096 if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver,
2097 ssl->minor_ver ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01002098 {
2099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002100 mbedtls_ssl_send_alert_message(
2101 ssl,
2102 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01002103 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2104 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01002105 }
2106
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002107 MBEDTLS_SSL_DEBUG_MSG( 3,
2108 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01002109
Gilles Peskineeccd8882020-03-10 12:19:08 +01002110#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002111 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
2112 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2113 {
2114 ssl->handshake->ecrs_enabled = 1;
2115 }
2116#endif
2117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 if( comp != MBEDTLS_SSL_COMPRESS_NULL
Paul Bakker2770fbd2012-07-03 13:30:23 +00002119 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002122 mbedtls_ssl_send_alert_message(
2123 ssl,
2124 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2125 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002126 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 }
Paul Bakker48916f92012-09-16 19:57:18 +00002128 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00002129
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002130 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00002131
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002132 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002133 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02002134
Paul Bakker48916f92012-09-16 19:57:18 +00002135 while( ext_len )
2136 {
2137 unsigned int ext_id = ( ( ext[0] << 8 )
2138 | ( ext[1] ) );
2139 unsigned int ext_size = ( ( ext[2] << 8 )
2140 | ( ext[3] ) );
2141
2142 if( ext_size + 4 > ext_len )
2143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002145 mbedtls_ssl_send_alert_message(
2146 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2147 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002148 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00002149 }
2150
2151 switch( ext_id )
2152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
2154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
2155#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00002156 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01002157#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002158
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002159 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
2160 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002161 return( ret );
2162
2163 break;
2164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2166 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002167 MBEDTLS_SSL_DEBUG_MSG( 3,
2168 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02002169
2170 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
2171 ext + 4, ext_size ) ) != 0 )
2172 {
2173 return( ret );
2174 }
2175
2176 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02002178
Hanno Beckera0e20d02019-05-15 14:03:01 +01002179#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01002180 case MBEDTLS_TLS_EXT_CID:
2181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2182
2183 if( ( ret = ssl_parse_cid_ext( ssl,
2184 ext + 4,
2185 ext_size ) ) != 0 )
2186 {
2187 return( ret );
2188 }
2189
2190 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002191#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2194 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002196
2197 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
2198 ext + 4, ext_size ) ) != 0 )
2199 {
2200 return( ret );
2201 }
2202
2203 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2207 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002208 MBEDTLS_SSL_DEBUG_MSG( 3,
2209 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002210
2211 if( ( ret = ssl_parse_extended_ms_ext( ssl,
2212 ext + 4, ext_size ) ) != 0 )
2213 {
2214 return( ret );
2215 }
2216
2217 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2221 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02002223
2224 if( ( ret = ssl_parse_session_ticket_ext( ssl,
2225 ext + 4, ext_size ) ) != 0 )
2226 {
2227 return( ret );
2228 }
2229
2230 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02002232
Robert Cragie136884c2015-10-02 13:34:31 +01002233#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002234 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002236 MBEDTLS_SSL_DEBUG_MSG( 3,
2237 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002238
2239 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
2240 ext + 4, ext_size ) ) != 0 )
2241 {
2242 return( ret );
2243 }
2244
2245 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01002246#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
2247 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002248
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02002249#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2250 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
2251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
2252
2253 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
2254 ext + 4, ext_size ) ) != 0 )
2255 {
2256 return( ret );
2257 }
2258
2259 break;
2260#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#if defined(MBEDTLS_SSL_ALPN)
2263 case MBEDTLS_TLS_EXT_ALPN:
2264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002265
Johan Pascal275874b2020-10-27 10:43:53 +01002266 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
2267 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002268
2269 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002271
Johan Pascalb62bb512015-12-03 21:56:45 +01002272#if defined(MBEDTLS_SSL_DTLS_SRTP)
2273 case MBEDTLS_TLS_EXT_USE_SRTP:
2274 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2275
Johan Pascalc3ccd982020-10-28 17:18:18 +01002276 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
2277 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01002278
2279 break;
2280#endif /* MBEDTLS_SSL_DTLS_SRTP */
2281
Paul Bakker48916f92012-09-16 19:57:18 +00002282 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002283 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00002284 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002285 }
2286
2287 ext_len -= 4 + ext_size;
2288 ext += 4 + ext_size;
2289
2290 if( ext_len > 0 && ext_len < 4 )
2291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002293 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00002294 }
2295 }
2296
2297 /*
2298 * Renegotiation security checks
2299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002301 ssl->conf->allow_legacy_renegotiation ==
2302 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00002303 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002304 MBEDTLS_SSL_DEBUG_MSG( 1,
2305 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002306 handshake_failure = 1;
2307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#if defined(MBEDTLS_SSL_RENEGOTIATION)
2309 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2310 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00002311 renegotiation_info_seen == 0 )
2312 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002313 MBEDTLS_SSL_DEBUG_MSG( 1,
2314 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002315 handshake_failure = 1;
2316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2318 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002319 ssl->conf->allow_legacy_renegotiation ==
2320 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002323 handshake_failure = 1;
2324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2326 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002327 renegotiation_info_seen == 1 )
2328 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002329 MBEDTLS_SSL_DEBUG_MSG( 1,
2330 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002331 handshake_failure = 1;
2332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002334
2335 if( handshake_failure == 1 )
2336 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002337 mbedtls_ssl_send_alert_message(
2338 ssl,
2339 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2340 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002341 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00002342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
2346 return( 0 );
2347}
2348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2350 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002351static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
2352 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02002353 unsigned char *end )
2354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01002356 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02002357
Paul Bakker29e1f122013-04-16 13:07:56 +02002358 /*
2359 * Ephemeral DH parameters:
2360 *
2361 * struct {
2362 * opaque dh_p<1..2^16-1>;
2363 * opaque dh_g<1..2^16-1>;
2364 * opaque dh_Ys<1..2^16-1>;
2365 * } ServerDHParams;
2366 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002367 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
2368 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002371 return( ret );
2372 }
2373
Gilles Peskine487bbf62021-05-27 22:17:07 +02002374 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01002375 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02002376 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01002378 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002379 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002380 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002381 }
2382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2384 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2385 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02002386
2387 return( ret );
2388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2390 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2393 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2394 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2395 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2396 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2397static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002400 mbedtls_ecp_group_id grp_id;
2401#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
2402 grp_id = ssl->handshake->ecdh_ctx.grp.id;
2403#else
2404 grp_id = ssl->handshake->ecdh_ctx.grp_id;
2405#endif
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002406
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002407 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002408 if( curve_info == NULL )
2409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2411 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002412 }
2413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002415
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002416#if defined(MBEDTLS_ECP_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002417 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002418#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002419 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
2420 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002421#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002422 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002423
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002424 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2425 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002426
2427 return( 0 );
2428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2430 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2431 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2432 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2433 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002434
Hanno Beckerbb89e272019-01-08 12:54:37 +00002435#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2436 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2437 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
2438static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl,
2439 unsigned char **p,
2440 unsigned char *end )
2441{
2442 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01002443 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00002444 uint8_t ecpoint_len;
2445 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2446
2447 /*
2448 * Parse ECC group
2449 */
2450
2451 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002452 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002453
2454 /* First byte is curve_type; only named_curve is handled */
2455 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01002456 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002457
2458 /* Next two bytes are the namedcurve value */
2459 tls_id = *(*p)++;
2460 tls_id <<= 8;
2461 tls_id |= *(*p)++;
2462
2463 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01002464 if( ( handshake->ecdh_psa_type =
2465 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00002466 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01002467 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002468 }
Gilles Peskine42459802019-12-19 13:31:53 +01002469 if( ecdh_bits > 0xffff )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002470 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Gilles Peskine42459802019-12-19 13:31:53 +01002471 handshake->ecdh_bits = (uint16_t) ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00002472
2473 /*
2474 * Put peer's ECDH public key in the format understood by PSA.
2475 */
2476
2477 ecpoint_len = *(*p)++;
2478 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002479 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002480
Gilles Peskine42459802019-12-19 13:31:53 +01002481 if( mbedtls_psa_tls_ecpoint_to_psa_ec(
Hanno Beckerbb89e272019-01-08 12:54:37 +00002482 *p, ecpoint_len,
2483 handshake->ecdh_psa_peerkey,
2484 sizeof( handshake->ecdh_psa_peerkey ),
2485 &handshake->ecdh_psa_peerkey_len ) != 0 )
2486 {
2487 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2488 }
2489
2490 *p += ecpoint_len;
2491 return( 0 );
2492}
2493#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2494 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2495 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
2496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2498 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2499 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2500static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002501 unsigned char **p,
2502 unsigned char *end )
2503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02002505
Paul Bakker29e1f122013-04-16 13:07:56 +02002506 /*
2507 * Ephemeral ECDH parameters:
2508 *
2509 * struct {
2510 * ECParameters curve_params;
2511 * ECPoint public;
2512 * } ServerECDHParams;
2513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02002515 (const unsigned char **) p, end ) ) != 0 )
2516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002518#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02002519 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2520 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002521#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02002522 return( ret );
2523 }
2524
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002525 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002526 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002527 MBEDTLS_SSL_DEBUG_MSG( 1,
2528 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002529 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002530 }
2531
Paul Bakker29e1f122013-04-16 13:07:56 +02002532 return( ret );
2533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2535 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2536 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002537
Gilles Peskineeccd8882020-03-10 12:19:08 +01002538#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002540 unsigned char **p,
2541 unsigned char *end )
2542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03002544 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02002545 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002546
2547 /*
2548 * PSK parameters:
2549 *
2550 * opaque psk_identity_hint<0..2^16-1>;
2551 */
Hanno Becker0c161d12018-10-08 13:40:50 +01002552 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01002553 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002554 MBEDTLS_SSL_DEBUG_MSG( 1,
2555 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002556 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01002557 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02002558 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002559 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002560
irwir6527bd62019-09-21 18:51:25 +03002561 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002562 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002563 MBEDTLS_SSL_DEBUG_MSG( 1,
2564 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002565 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002566 }
2567
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01002568 /*
2569 * Note: we currently ignore the PKS identity hint, as we only allow one
2570 * PSK to be provisionned on the client. This could be changed later if
2571 * someone needs that feature.
2572 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002573 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002574 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002575
2576 return( ret );
2577}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002578#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2581 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002582/*
2583 * Generate a pre-master secret and encrypt it with the server's RSA key
2584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002586 size_t offset, size_t *olen,
2587 size_t pms_offset )
2588{
Janos Follath865b3eb2019-12-16 11:46:15 +00002589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002590 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002591 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002592 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002593
Angus Grattond8213d02016-05-25 20:56:48 +10002594 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002595 {
2596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2597 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2598 }
2599
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002600 /*
2601 * Generate (part of) the pre-master as
2602 * struct {
2603 * ProtocolVersion client_version;
2604 * opaque random[46];
2605 * } PreMasterSecret;
2606 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002607 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
2608 ssl->conf->max_minor_ver,
2609 ssl->conf->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002610
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002611 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002614 return( ret );
2615 }
2616
2617 ssl->handshake->pmslen = 48;
2618
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002619#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2620 peer_pk = &ssl->handshake->peer_pubkey;
2621#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002622 if( ssl->session_negotiate->peer_cert == NULL )
2623 {
Hanno Becker8273df82019-02-06 17:37:32 +00002624 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002626 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002627 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002628 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2629#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002630
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002631 /*
2632 * Now write it out, encrypted
2633 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002634 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2637 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002638 }
2639
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002640 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002641 p, ssl->handshake->pmslen,
2642 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002643 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002644 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002647 return( ret );
2648 }
2649
TRodziewicz0f82ec62021-05-12 17:49:18 +02002650#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002651 if( len_bytes == 2 )
2652 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002653 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002654 *olen += 2;
2655 }
2656#endif
2657
Hanno Beckerae553dd2019-02-08 14:06:00 +00002658#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2659 /* We don't need the peer's public key anymore. Free it. */
2660 mbedtls_pk_free( peer_pk );
2661#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002662 return( 0 );
2663}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2665 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002668#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2669 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2670 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002672 unsigned char **p,
2673 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 mbedtls_md_type_t *md_alg,
2675 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002676{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02002677 ((void) ssl);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 *md_alg = MBEDTLS_MD_NONE;
2679 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002680
2681 /* Only in TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002683 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002684 return( 0 );
2685 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002686
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002687 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002688 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002689
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002690 /*
2691 * Get hash algorithm
2692 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002693 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2694 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002695 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002696 MBEDTLS_SSL_DEBUG_MSG( 1,
2697 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002698 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002699 }
2700
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002701 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002702 * Get signature algorithm
2703 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002704 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2705 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002706 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002707 MBEDTLS_SSL_DEBUG_MSG( 1,
2708 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002709 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002710 }
2711
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002712 /*
2713 * Check if the hash is acceptable
2714 */
2715 if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
2716 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002717 MBEDTLS_SSL_DEBUG_MSG( 1,
2718 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002719 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002720 }
2721
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2723 (*p)[1] ) );
2724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2725 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002726 *p += 2;
2727
2728 return( 0 );
2729}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002730#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2731 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2732 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2736 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2737static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002738{
Janos Follath865b3eb2019-12-16 11:46:15 +00002739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002741 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002742
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002743#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2744 peer_pk = &ssl->handshake->peer_pubkey;
2745#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002746 if( ssl->session_negotiate->peer_cert == NULL )
2747 {
Hanno Becker8273df82019-02-06 17:37:32 +00002748 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002750 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002751 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002752 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2753#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002754
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002755 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2758 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002759 }
2760
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002761 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2764 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002767 return( ret );
2768 }
2769
2770 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002773 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002774 }
2775
Hanno Beckerae553dd2019-02-08 14:06:00 +00002776#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2777 /* We don't need the peer's public key anymore. Free it,
2778 * so that more RAM is available for upcoming expensive
2779 * operations like ECDHE. */
2780 mbedtls_pk_free( peer_pk );
2781#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2782
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002783 return( ret );
2784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2786 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002789{
Janos Follath865b3eb2019-12-16 11:46:15 +00002790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002791 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002792 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002793 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2798 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 ssl->state++;
2802 return( 0 );
2803 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002804 ((void) p);
2805 ((void) end);
2806#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2809 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2810 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2811 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002812 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002813 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002816 mbedtls_ssl_send_alert_message(
2817 ssl,
2818 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2819 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002820 return( ret );
2821 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002824 ssl->state++;
2825 return( 0 );
2826 }
2827 ((void) p);
2828 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2830 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002831
Gilles Peskineeccd8882020-03-10 12:19:08 +01002832#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002833 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002834 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002835 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002836 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002837 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002838#endif
2839
Hanno Becker327c93b2018-08-15 13:56:18 +01002840 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 return( ret );
2844 }
2845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002849 mbedtls_ssl_send_alert_message(
2850 ssl,
2851 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2852 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002854 }
2855
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002856 /*
2857 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2858 * doesn't use a psk_identity_hint
2859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2863 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002864 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002865 /* Current message is probably either
2866 * CertificateRequest or ServerHelloDone */
2867 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002868 goto exit;
2869 }
2870
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002871 MBEDTLS_SSL_DEBUG_MSG( 1,
2872 ( "server key exchange message must not be skipped" ) );
2873 mbedtls_ssl_send_alert_message(
2874 ssl,
2875 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2876 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 }
2880
Gilles Peskineeccd8882020-03-10 12:19:08 +01002881#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002882 if( ssl->handshake->ecrs_enabled )
2883 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2884
2885start_processing:
2886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002888 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002890
Gilles Peskineeccd8882020-03-10 12:19:08 +01002891#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2893 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2894 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2895 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002896 {
2897 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002900 mbedtls_ssl_send_alert_message(
2901 ssl,
2902 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002903 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002904 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002905 }
2906 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002907#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2910 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2911 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2912 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002913 ; /* nothing more to do */
2914 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2916 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2917#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2918 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2919 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2920 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002921 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002922 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002925 mbedtls_ssl_send_alert_message(
2926 ssl,
2927 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2928 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002929 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002930 }
2931 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002932 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2934 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +00002935#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2936 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2937 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
2938 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2939 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2940 {
2941 if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 )
2942 {
2943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002944 mbedtls_ssl_send_alert_message(
2945 ssl,
2946 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2947 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanc50b7172021-06-29 14:40:23 +01002948 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002949 }
2950 }
2951 else
2952#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2953 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2954 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2956 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2957 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2958 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2959 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2960 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002961 {
2962 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002965 mbedtls_ssl_send_alert_message(
2966 ssl,
2967 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2968 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002969 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002970 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002971 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002972 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2974 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2975 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002976#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2977 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2978 {
2979 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2980 p, end - p );
2981 if( ret != 0 )
2982 {
2983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002984 mbedtls_ssl_send_alert_message(
2985 ssl,
2986 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002987 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2988 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002989 }
2990 }
2991 else
2992#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002996 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002997
Gilles Peskineeccd8882020-03-10 12:19:08 +01002998#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002999 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00003001 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02003002#if defined(MBEDTLS_USE_PSA_CRYPTO)
3003 unsigned char hash[PSA_HASH_MAX_SIZE];
3004#else
3005 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
3006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
3008 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
3009 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00003010 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003011 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003012
Hanno Beckera6899bb2019-02-06 18:26:03 +00003013 mbedtls_pk_context * peer_pk;
3014
Paul Bakker29e1f122013-04-16 13:07:56 +02003015 /*
3016 * Handle the digitally-signed structure
3017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3019 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003020 {
Paul Bakker9659dae2013-08-28 16:21:34 +02003021 if( ssl_parse_signature_algorithm( ssl, &p, end,
3022 &md_alg, &pk_alg ) != 0 )
3023 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003024 MBEDTLS_SSL_DEBUG_MSG( 1,
3025 ( "bad server key exchange message" ) );
3026 mbedtls_ssl_send_alert_message(
3027 ssl,
3028 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3029 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003030 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02003031 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003033 if( pk_alg !=
3034 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003035 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003036 MBEDTLS_SSL_DEBUG_MSG( 1,
3037 ( "bad server key exchange message" ) );
3038 mbedtls_ssl_send_alert_message(
3039 ssl,
3040 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3041 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003042 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003043 }
3044 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02003045 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02003047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3049 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02003050 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003051
3052 /*
3053 * Read signature
3054 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01003055
3056 if( p > end - 2 )
3057 {
3058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003059 mbedtls_ssl_send_alert_message(
3060 ssl,
3061 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3062 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003063 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01003064 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003065 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00003066 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003067
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01003068 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003071 mbedtls_ssl_send_alert_message(
3072 ssl,
3073 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3074 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003075 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01003076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003079
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003080 /*
3081 * Compute the hash that has been signed
3082 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02003085 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003086 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
3087 params, params_len,
3088 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003089 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02003090 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02003091 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003092 else
TRodziewicz0f82ec62021-05-12 17:49:18 +02003093#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02003094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3096 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003097 }
Paul Bakker29e1f122013-04-16 13:07:56 +02003098
Gilles Peskineca1d7422018-04-24 11:53:22 +02003099 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02003100
Hanno Beckera6899bb2019-02-06 18:26:03 +00003101#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3102 peer_pk = &ssl->handshake->peer_pubkey;
3103#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003104 if( ssl->session_negotiate->peer_cert == NULL )
3105 {
Hanno Becker8273df82019-02-06 17:37:32 +00003106 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00003107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00003108 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003109 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00003110 peer_pk = &ssl->session_negotiate->peer_cert->pk;
3111#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003112
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003113 /*
3114 * Verify signature
3115 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00003116 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003119 mbedtls_ssl_send_alert_message(
3120 ssl,
3121 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3122 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003124 }
3125
Gilles Peskineeccd8882020-03-10 12:19:08 +01003126#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003127 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003128 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003129#endif
3130
Hanno Beckera6899bb2019-02-06 18:26:03 +00003131 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003132 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003133 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01003134#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003135 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3136#endif
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_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003142#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003143 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3144 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3145#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02003146 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00003147 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00003148
3149#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3150 /* We don't need the peer's public key anymore. Free it,
3151 * so that more RAM is available for upcoming expensive
3152 * operations like ECDHE. */
3153 mbedtls_pk_free( peer_pk );
3154#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00003155 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003156#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003157
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003158exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 ssl->state++;
3160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
3163 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164}
3165
Gilles Peskineeccd8882020-03-10 12:19:08 +01003166#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003168{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003169 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003170 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003173
Hanno Becker1aa267c2017-04-28 17:08:27 +01003174 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003177 ssl->state++;
3178 return( 0 );
3179 }
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003183}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003184#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003186{
Janos Follath865b3eb2019-12-16 11:46:15 +00003187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003188 unsigned char *buf;
3189 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003190 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003191 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003192 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
Hanno Becker1aa267c2017-04-28 17:08:27 +01003196 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003199 ssl->state++;
3200 return( 0 );
3201 }
3202
Hanno Becker327c93b2018-08-15 13:56:18 +01003203 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3206 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 }
3208
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003209 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3210 {
3211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003212 mbedtls_ssl_send_alert_message(
3213 ssl,
3214 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3215 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003216 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3217 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003219 ssl->state++;
3220 ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 ssl->client_auth ? "a" : "no" ) );
3224
Paul Bakker926af752012-11-23 13:38:07 +01003225 if( ssl->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003226 {
Johan Pascala89ca862020-08-25 10:03:19 +02003227 /* Current message is probably the ServerHelloDone */
3228 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01003229 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003230 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003231
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02003232 /*
3233 * struct {
3234 * ClientCertificateType certificate_types<1..2^8-1>;
3235 * SignatureAndHashAlgorithm
3236 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
3237 * DistinguishedName certificate_authorities<0..2^16-1>;
3238 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003239 *
3240 * Since we only support a single certificate on clients, let's just
3241 * ignore all the information that's supposed to help us pick a
3242 * certificate.
3243 *
3244 * We could check that our certificate matches the request, and bail out
3245 * if it doesn't, but it's simpler to just send the certificate anyway,
3246 * and give the server the opportunity to decide if it should terminate
3247 * the connection when it doesn't like our certificate.
3248 *
3249 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
3250 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00003251 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003252 *
3253 * However, we still minimally parse the message to check it is at least
3254 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02003255 */
Paul Bakker926af752012-11-23 13:38:07 +01003256 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003257
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003258 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02003259 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
3260 {
3261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3262 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3263 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003264 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02003265 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01003267 n = cert_type_len;
3268
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01003269 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02003270 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01003271 * * the length of the signature algorithms field (if minor version of
3272 * SSL is 3),
3273 * * distinguished name length otherwise.
3274 * Both reach at most the index:
3275 * ...hdr_len + 2 + n,
3276 * therefore the buffer length at this point must be greater than that
3277 * regardless of the actual code path.
3278 */
3279 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01003280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003282 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3283 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003284 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01003285 }
3286
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003287 /* supported_signature_algorithms */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3289 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003290 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003291 size_t sig_alg_len =
3292 ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3293 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Simon Butcher99000142016-10-13 17:21:01 +01003294#if defined(MBEDTLS_DEBUG_C)
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003295 unsigned char* sig_alg;
Simon Butcher99000142016-10-13 17:21:01 +01003296 size_t i;
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003297#endif
Simon Butcher99000142016-10-13 17:21:01 +01003298
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003299 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02003300 * The furthest access in buf is in the loop few lines below:
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003301 * sig_alg[i + 1],
3302 * where:
3303 * sig_alg = buf + ...hdr_len + 3 + n,
3304 * max(i) = sig_alg_len - 1.
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02003305 * Therefore the furthest access is:
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003306 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
3307 * which reduces to:
3308 * buf[...hdr_len + 3 + n + sig_alg_len],
3309 * which is one less than we need the buf to be.
3310 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003311 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl )
3312 + 3 + n + sig_alg_len )
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003313 {
3314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003315 mbedtls_ssl_send_alert_message(
3316 ssl,
3317 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3318 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003319 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiakbc231cc2018-03-20 14:09:53 +01003320 }
3321
3322#if defined(MBEDTLS_DEBUG_C)
3323 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
Simon Butcher99000142016-10-13 17:21:01 +01003324 for( i = 0; i < sig_alg_len; i += 2 )
3325 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003326 MBEDTLS_SSL_DEBUG_MSG( 3,
3327 ( "Supported Signature Algorithm found: %d,%d",
3328 sig_alg[i], sig_alg[i + 1] ) );
Simon Butcher99000142016-10-13 17:21:01 +01003329 }
3330#endif
Paul Bakker926af752012-11-23 13:38:07 +01003331
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003332 n += 2 + sig_alg_len;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003333 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01003335
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003336 /* certificate_authorities */
3337 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3338 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01003339
3340 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003341 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01003342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003344 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3345 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003346 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01003347 }
3348
3349exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351
3352 return( 0 );
3353}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003354#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003357{
Janos Follath865b3eb2019-12-16 11:46:15 +00003358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003361
Hanno Becker327c93b2018-08-15 13:56:18 +01003362 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3365 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003367
3368 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3369 {
3370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
3371 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
3375 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003378 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3379 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01003380 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 }
3382
3383 ssl->state++;
3384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003386 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003388#endif
3389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
3392 return( 0 );
3393}
3394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003396{
Janos Follath865b3eb2019-12-16 11:46:15 +00003397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003398
3399 size_t header_len;
3400 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003401 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003402 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3407 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 /*
3410 * DHM key exchange -- send G^X mod P
3411 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003412 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00003413
Joe Subbiani6dd73642021-07-19 11:56:54 +01003414 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003415 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00003416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003418 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003419 &ssl->out_msg[header_len], content_len,
3420 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 if( ret != 0 )
3422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003424 return( ret );
3425 }
3426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3428 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003431 ssl->handshake->premaster,
3432 MBEDTLS_PREMASTER_SIZE,
3433 &ssl->handshake->pmslen,
3434 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003437 return( ret );
3438 }
3439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 }
3442 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00003444#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3445 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3446 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
3447 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3448 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3449 {
3450 psa_status_t status;
Janos Follath53b8ec22019-08-08 10:28:27 +01003451 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003452
3453 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3454
3455 unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
3456 size_t own_pubkey_len;
3457 unsigned char *own_pubkey_ecpoint;
3458 size_t own_pubkey_ecpoint_len;
3459
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003460 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003461
Hanno Becker0a94a642019-01-11 14:35:30 +00003462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3463
Hanno Becker4a63ed42019-01-08 11:39:35 +00003464 /*
3465 * Generate EC private key for ECDHE exchange.
3466 */
3467
Hanno Becker4a63ed42019-01-08 11:39:35 +00003468 /* The master secret is obtained from the shared ECDH secret by
3469 * applying the TLS 1.2 PRF with a specific salt and label. While
3470 * the PSA Crypto API encourages combining key agreement schemes
3471 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3472 * yet support the provisioning of salt + label to the KDF.
3473 * For the time being, we therefore need to split the computation
3474 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01003475 key_attributes = psa_key_attributes_init();
3476 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01003477 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01003478 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3479 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003480
3481 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01003482 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01003483 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003484 if( status != PSA_SUCCESS )
3485 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3486
3487 /* Export the public part of the ECDH private key from PSA
3488 * and convert it to ECPoint format used in ClientKeyExchange. */
3489 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3490 own_pubkey, sizeof( own_pubkey ),
3491 &own_pubkey_len );
3492 if( status != PSA_SUCCESS )
3493 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3494
3495 if( mbedtls_psa_tls_psa_ec_to_ecpoint( own_pubkey,
3496 own_pubkey_len,
3497 &own_pubkey_ecpoint,
3498 &own_pubkey_ecpoint_len ) != 0 )
3499 {
3500 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3501 }
3502
3503 /* Copy ECPoint structure to outgoing message buffer. */
Andrzej Kurekade9e282019-05-07 08:19:33 -04003504 ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003505 memcpy( ssl->out_msg + header_len + 1,
3506 own_pubkey_ecpoint, own_pubkey_ecpoint_len );
3507 content_len = own_pubkey_ecpoint_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003508
Hanno Becker4a63ed42019-01-08 11:39:35 +00003509 /* The ECDH secret is the premaster secret used for key derivation. */
3510
Janos Follathdf3b0892019-08-08 11:12:24 +01003511 /* Compute ECDH shared secret. */
3512 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3513 handshake->ecdh_psa_privkey,
3514 handshake->ecdh_psa_peerkey,
3515 handshake->ecdh_psa_peerkey_len,
3516 ssl->handshake->premaster,
3517 sizeof( ssl->handshake->premaster ),
3518 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003519 if( status != PSA_SUCCESS )
3520 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3521
3522 status = psa_destroy_key( handshake->ecdh_psa_privkey );
3523 if( status != PSA_SUCCESS )
3524 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Ronald Croncf56a0a2020-08-04 09:51:30 +02003525 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003526 }
3527 else
3528#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3529 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3530 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3532 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3533 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3534 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3535 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3536 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3537 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3538 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003539 {
3540 /*
3541 * ECDH key exchange -- send client public value
3542 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003543 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01003544
Gilles Peskineeccd8882020-03-10 12:19:08 +01003545#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003546 if( ssl->handshake->ecrs_enabled )
3547 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003548 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003549 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02003550
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003551 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
3552 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003553#endif
3554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003556 &content_len,
3557 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003558 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01003559 if( ret != 0 )
3560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003562#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003563 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3564 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3565#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003566 return( ret );
3567 }
3568
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003569 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3570 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003571
Gilles Peskineeccd8882020-03-10 12:19:08 +01003572#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003573 if( ssl->handshake->ecrs_enabled )
3574 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003575 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003576 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003577 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003578
3579ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003580 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003581 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003584 &ssl->handshake->pmslen,
3585 ssl->handshake->premaster,
3586 MBEDTLS_MPI_MAX_SIZE,
3587 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003590#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003591 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3592 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3593#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003594 return( ret );
3595 }
3596
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003597 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3598 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker41c83d32013-03-20 14:39:14 +01003599 }
3600 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3602 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3603 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3604 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003605#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003606 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003607 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003608 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003609 * opaque psk_identity<0..2^16-1>;
3610 */
Hanno Becker520224e2018-10-26 11:38:07 +01003611 if( ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003612 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003613 /* We don't offer PSK suites if we don't have a PSK,
3614 * and we check that the server's choice is among the
3615 * ciphersuites we offered, so this should never happen. */
3616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003617 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003618
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003619 header_len = 4;
3620 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003621
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003622 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003623 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003624 MBEDTLS_SSL_DEBUG_MSG( 1,
3625 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003626 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3627 }
3628
Joe Subbianifbeb6922021-07-16 14:27:50 +01003629 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3630 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003631
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003632 memcpy( ssl->out_msg + header_len,
3633 ssl->conf->psk_identity,
3634 ssl->conf->psk_identity_len );
3635 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3638 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003639 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003640 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003641 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003642 else
3643#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3645 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003646 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003647#if defined(MBEDTLS_USE_PSA_CRYPTO)
3648 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003649 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003650 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3651#endif /* MBEDTLS_USE_PSA_CRYPTO */
3652
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003653 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3654 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003655 return( ret );
3656 }
3657 else
3658#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3660 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003661 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003662#if defined(MBEDTLS_USE_PSA_CRYPTO)
3663 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003664 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003665 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3666#endif /* MBEDTLS_USE_PSA_CRYPTO */
3667
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003668 /*
3669 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3670 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003671 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003672
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003673 if( header_len + 2 + content_len >
3674 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003675 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003676 MBEDTLS_SSL_DEBUG_MSG( 1,
3677 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003678 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3679 }
3680
Joe Subbianifbeb6922021-07-16 14:27:50 +01003681 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3682 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003685 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003686 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003687 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003688 if( ret != 0 )
3689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003691 return( ret );
3692 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003693 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003694 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3696#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3697 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003698 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003699#if defined(MBEDTLS_USE_PSA_CRYPTO)
3700 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003701 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003702 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3703#endif /* MBEDTLS_USE_PSA_CRYPTO */
3704
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003705 /*
3706 * ClientECDiffieHellmanPublic public;
3707 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003708 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3709 &content_len,
3710 &ssl->out_msg[header_len],
3711 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003712 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003713 if( ret != 0 )
3714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003716 return( ret );
3717 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003718
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003719 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3720 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003721 }
3722 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003727 }
3728
Hanno Beckerafd311e2018-10-23 15:26:40 +01003729#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3730 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3731 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
3732 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Becker520224e2018-10-26 11:38:07 +01003733 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003734 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003735 MBEDTLS_SSL_DEBUG_MSG( 1,
3736 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003737 }
3738 else
3739#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3740 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003742 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003743 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003744 MBEDTLS_SSL_DEBUG_RET( 1,
3745 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003746 return( ret );
3747 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003748 }
3749 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003750#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3752 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003753 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003754 header_len = 4;
3755 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3756 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003757 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003758 }
Paul Bakkered27a042013-04-18 22:46:23 +02003759 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003761#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3762 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3763 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003764 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003765
3766 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003767 ssl->out_msg + header_len,
3768 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3769 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003770 ssl->conf->f_rng, ssl->conf->p_rng );
3771 if( ret != 0 )
3772 {
3773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3774 return( ret );
3775 }
3776
3777 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3778 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3779 ssl->conf->f_rng, ssl->conf->p_rng );
3780 if( ret != 0 )
3781 {
3782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3783 return( ret );
3784 }
3785 }
3786 else
3787#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003788 {
3789 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3791 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003792 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003793
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003794 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3796 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003797
3798 ssl->state++;
3799
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003800 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003803 return( ret );
3804 }
3805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003807
3808 return( 0 );
3809}
3810
Gilles Peskineeccd8882020-03-10 12:19:08 +01003811#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003813{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003814 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003815 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003816 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003823 return( ret );
3824 }
3825
Hanno Becker77adddc2019-02-07 12:32:43 +00003826 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003829 ssl->state++;
3830 return( 0 );
3831 }
3832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003835}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003836#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003840 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003841 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003842 size_t n = 0, offset = 0;
3843 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003844 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003846 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003847 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003848#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3849 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3850#else
3851 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3852#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003855
Gilles Peskineeccd8882020-03-10 12:19:08 +01003856#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003857 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003858 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003859 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003860 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003861 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003862#endif
3863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003867 return( ret );
3868 }
3869
Hanno Becker77adddc2019-02-07 12:32:43 +00003870 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003873 ssl->state++;
3874 return( 0 );
3875 }
3876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003878 {
Johan Pascala89ca862020-08-25 10:03:19 +02003879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3880 ssl->state++;
3881 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003882 }
3883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003885 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003888 }
3889
3890 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003891 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003892 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003893#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003894 if( ssl->handshake->ecrs_enabled )
3895 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3896
3897sign:
3898#endif
3899
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003900 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3903 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003904 {
3905 /*
3906 * digitally-signed struct {
3907 * opaque handshake_messages[handshake_messages_length];
3908 * };
3909 *
3910 * Taking shortcut here. We assume that the server always allows the
3911 * PRF Hash function and has sent it in the allowed signature
3912 * algorithms list received in the Certificate Request message.
3913 *
3914 * Until we encounter a server that does not, we will take this
3915 * shortcut.
3916 *
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003917 * Reason: Otherwise we should have running hashes for SHA512 and
3918 * SHA224 in order to satisfy 'weird' needs from the server
3919 * side.
Paul Bakker926af752012-11-23 13:38:07 +01003920 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00003921 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 md_alg = MBEDTLS_MD_SHA384;
3924 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003925 }
3926 else
3927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 md_alg = MBEDTLS_MD_SHA256;
3929 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003930 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003932
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003933 /* Info from md_alg will be used instead */
3934 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003935 offset = 2;
3936 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003937 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003942 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003943
Gilles Peskineeccd8882020-03-10 12:19:08 +01003944#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003945 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003946 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003947#endif
3948
3949 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3950 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003951 ssl->out_msg + 6 + offset,
3952 out_buf_len - 6 - offset,
3953 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003954 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003957#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003958 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3959 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3960#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003961 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003962 }
Paul Bakker926af752012-11-23 13:38:07 +01003963
Joe Subbiani6dd73642021-07-19 11:56:54 +01003964 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003965
Paul Bakker1ef83d62012-04-11 12:09:53 +00003966 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3968 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003969
3970 ssl->state++;
3971
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003972 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003973 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003975 return( ret );
3976 }
3977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003979
Paul Bakkered27a042013-04-18 22:46:23 +02003980 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003981}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003982#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3985static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003986{
Janos Follath865b3eb2019-12-16 11:46:15 +00003987 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003988 uint32_t lifetime;
3989 size_t ticket_len;
3990 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003991 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003994
Hanno Becker327c93b2018-08-15 13:56:18 +01003995 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003998 return( ret );
3999 }
4000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01004004 mbedtls_ssl_send_alert_message(
4005 ssl,
4006 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4007 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004009 }
4010
4011 /*
4012 * struct {
4013 * uint32 ticket_lifetime_hint;
4014 * opaque ticket<0..2^16-1>;
4015 * } NewSessionTicket;
4016 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02004017 * 0 . 3 ticket_lifetime_hint
4018 * 4 . 5 ticket_len (n)
4019 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
4022 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004025 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4026 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01004027 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004028 }
4029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004031
Philippe Antoineb5b25432018-05-11 11:06:29 +02004032 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
4033 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004034
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02004035 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
4036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004040 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4041 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01004042 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004043 }
4044
Paul Elliottd48d5c62021-01-07 14:47:05 +00004045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004046
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004047 /* We're not waiting for a NewSessionTicket message any more */
4048 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004050
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004051 /*
4052 * Zero-length ticket means the server changed his mind and doesn't want
4053 * to send a ticket after all, so just forget it
4054 */
Paul Bakker66d5d072014-06-17 16:39:18 +02004055 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004056 return( 0 );
4057
Hanno Beckerb2964cb2019-01-30 14:46:35 +00004058 if( ssl->session != NULL && ssl->session->ticket != NULL )
4059 {
4060 mbedtls_platform_zeroize( ssl->session->ticket,
4061 ssl->session->ticket_len );
4062 mbedtls_free( ssl->session->ticket );
4063 ssl->session->ticket = NULL;
4064 ssl->session->ticket_len = 0;
4065 }
4066
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004067 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
4068 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004070 ssl->session_negotiate->ticket = NULL;
4071 ssl->session_negotiate->ticket_len = 0;
4072
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004073 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004074 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004076 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4077 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004078 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004079 }
4080
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02004081 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004082
4083 ssl->session_negotiate->ticket = ticket;
4084 ssl->session_negotiate->ticket_len = ticket_len;
4085 ssl->session_negotiate->ticket_lifetime = lifetime;
4086
4087 /*
4088 * RFC 5077 section 3.4:
4089 * "If the client receives a session ticket from the server, then it
4090 * discards any Session ID that was sent in the ServerHello."
4091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02004093 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004096
4097 return( 0 );
4098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004100
Paul Bakker5121ce52009-01-03 21:22:43 +00004101/*
Paul Bakker1961b702013-01-25 14:49:24 +01004102 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004105{
4106 int ret = 0;
4107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004111 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4113 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004114 ssl->handshake->new_session_ticket != 0 )
4115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004117 }
4118#endif
4119
Paul Bakker1961b702013-01-25 14:49:24 +01004120 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 case MBEDTLS_SSL_HELLO_REQUEST:
4123 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 break;
4125
Paul Bakker1961b702013-01-25 14:49:24 +01004126 /*
4127 * ==> ClientHello
4128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004130 ret = ssl_write_client_hello( ssl );
4131 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004132
Paul Bakker1961b702013-01-25 14:49:24 +01004133 /*
4134 * <== ServerHello
4135 * Certificate
4136 * ( ServerKeyExchange )
4137 * ( CertificateRequest )
4138 * ServerHelloDone
4139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004141 ret = ssl_parse_server_hello( ssl );
4142 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4145 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004146 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004149 ret = ssl_parse_server_key_exchange( ssl );
4150 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004153 ret = ssl_parse_certificate_request( ssl );
4154 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004157 ret = ssl_parse_server_hello_done( ssl );
4158 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004159
Paul Bakker1961b702013-01-25 14:49:24 +01004160 /*
4161 * ==> ( Certificate/Alert )
4162 * ClientKeyExchange
4163 * ( CertificateVerify )
4164 * ChangeCipherSpec
4165 * Finished
4166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4168 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004169 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004172 ret = ssl_write_client_key_exchange( ssl );
4173 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004176 ret = ssl_write_certificate_verify( ssl );
4177 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4180 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004181 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 case MBEDTLS_SSL_CLIENT_FINISHED:
4184 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004185 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004186
Paul Bakker1961b702013-01-25 14:49:24 +01004187 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004188 * <== ( NewSessionTicket )
4189 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004190 * Finished
4191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4193 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004194 ret = ssl_parse_new_session_ticket( ssl );
4195 break;
Paul Bakkera503a632013-08-14 13:48:06 +02004196#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4199 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004200 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202 case MBEDTLS_SSL_SERVER_FINISHED:
4203 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004204 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 case MBEDTLS_SSL_FLUSH_BUFFERS:
4207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4208 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004209 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4212 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004213 break;
Paul Bakker48916f92012-09-16 19:57:18 +00004214
Paul Bakker1961b702013-01-25 14:49:24 +01004215 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01004218 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004219
4220 return( ret );
4221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222#endif /* MBEDTLS_SSL_CLI_C */