blob: 3d0e3a759c2882d2b7944b2ba51b9bb9e2b99af2 [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
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080023
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"
Ronald Cron7320e642022-03-08 13:34:49 +010033#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000034#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/debug.h"
36#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020037#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010038
Hanno Beckerbb89e272019-01-08 12:54:37 +000039#if defined(MBEDTLS_USE_PSA_CRYPTO)
40#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020041#include "psa/crypto.h"
Hanno Beckerbb89e272019-01-08 12:54:37 +000042#endif /* MBEDTLS_USE_PSA_CRYPTO */
43
SimonBd5800b72016-04-26 07:43:27 +010044#include <string.h>
45
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020046#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010049#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050053#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020054#endif
55
Gilles Peskineeccd8882020-03-10 12:19:08 +010056#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Ronald Crond491c2d2022-02-19 18:30:46 +010057int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2e4f6162018-10-23 11:54:44 +010058{
59 if( conf->psk_identity == NULL ||
60 conf->psk_identity_len == 0 )
61 {
62 return( 0 );
63 }
64
Hanno Becker2e4f6162018-10-23 11:54:44 +010065#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020066 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010067 return( 1 );
Neil Armstrong8ecd6682022-05-05 11:40:35 +020068#endif /* MBEDTLS_USE_PSA_CRYPTO */
69
Neil Armstronge952a302022-05-03 10:22:14 +020070 if( conf->psk != NULL && conf->psk_len != 0 )
71 return( 1 );
Hanno Becker2e4f6162018-10-23 11:54:44 +010072
73 return( 0 );
74}
Gilles Peskineeccd8882020-03-10 12:19:08 +010075#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +010078static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
79 unsigned char *buf,
80 const unsigned char *end,
81 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010082{
83 unsigned char *p = buf;
84
85 *olen = 0;
86
Hanno Becker40f8b512017-10-12 14:58:55 +010087 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
88 * initial ClientHello, in which case also adding the renegotiation
89 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +010091 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +010092
Hanno Beckerb2fff6d2017-05-08 11:06:19 +010093 MBEDTLS_SSL_DEBUG_MSG( 3,
94 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +010095
Hanno Becker261602c2017-04-12 14:54:42 +010096 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +010097
Paul Bakkerd3edc862013-03-20 16:07:17 +010098 /*
99 * Secure renegotiation
100 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100101 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
102 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100103
104 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100105 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
106 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100107
108 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
109
110 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100111
112 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100113}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100115
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200116#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100117 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118
Hanno Becker261602c2017-04-12 14:54:42 +0100119static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
120 unsigned char *buf,
121 const unsigned char *end,
122 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100123{
124 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100125 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100126
127 *olen = 0;
128
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100129 MBEDTLS_SSL_DEBUG_MSG( 3,
130 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100131 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100132
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100133 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
134 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100135
136 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100137 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200138
139 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100141
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200142 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100143
144 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100145}
Darryl Green11999bb2018-03-13 15:22:58 +0000146#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100147 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100148
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200149#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100150static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
151 unsigned char *buf,
152 const unsigned char *end,
153 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200154{
Janos Follath865b3eb2019-12-16 11:46:15 +0000155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200156 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200157 size_t kkpp_len;
158
159 *olen = 0;
160
161 /* Skip costly extension if we can't use EC J-PAKE anyway */
162 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100163 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200164
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100165 MBEDTLS_SSL_DEBUG_MSG( 3,
166 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200167
Hanno Becker261602c2017-04-12 14:54:42 +0100168 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200169
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100170 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
171 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200172
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200173 /*
174 * We may need to send ClientHello multiple times for Hello verification.
175 * We don't want to compute fresh values every time (both for performance
176 * and consistency reasons), so cache the extension content.
177 */
178 if( ssl->handshake->ecjpake_cache == NULL ||
179 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200180 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
182
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200183 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100184 p + 2, end - p - 2, &kkpp_len,
185 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200186 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200187 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100188 MBEDTLS_SSL_DEBUG_RET( 1 ,
189 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100190 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200191 }
192
193 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
194 if( ssl->handshake->ecjpake_cache == NULL )
195 {
196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100197 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200198 }
199
200 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
201 ssl->handshake->ecjpake_cache_len = kkpp_len;
202 }
203 else
204 {
205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
206
207 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100208 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200209
210 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200211 }
212
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100213 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
214 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200215
216 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100217
218 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200219}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200220#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000221
Hanno Beckera0e20d02019-05-15 14:03:01 +0100222#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +0100223static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
224 unsigned char *buf,
225 const unsigned char *end,
226 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100227{
228 unsigned char *p = buf;
229 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100230
231 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100232 * Quoting draft-ietf-tls-dtls-connection-id-05
233 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100234 *
235 * struct {
236 * opaque cid<0..2^8-1>;
237 * } ConnectionId;
238 */
239
240 *olen = 0;
241 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
242 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
243 {
Hanno Becker261602c2017-04-12 14:54:42 +0100244 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100245 }
246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
247
248 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
249 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100250 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100251
252 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100253 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
254 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100255 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100256 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
257 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100258
259 *p++ = (uint8_t) ssl->own_cid_len;
260 memcpy( p, ssl->own_cid, ssl->own_cid_len );
261
262 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100263
264 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100265}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100266#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +0100269static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
270 unsigned char *buf,
271 const unsigned char *end,
272 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200273{
274 unsigned char *p = buf;
275
Simon Butcher0fc94e92015-09-28 20:52:04 +0100276 *olen = 0;
277
Hanno Becker261602c2017-04-12 14:54:42 +0100278 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
279 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200280
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100281 MBEDTLS_SSL_DEBUG_MSG( 3,
282 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200283
Hanno Becker261602c2017-04-12 14:54:42 +0100284 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100285
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100286 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
287 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200288
289 *p++ = 0x00;
290 *p++ = 1;
291
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200292 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200293
294 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100295
296 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +0100301static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
302 unsigned char *buf,
303 const unsigned char *end,
304 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100305{
306 unsigned char *p = buf;
307
Simon Butcher0fc94e92015-09-28 20:52:04 +0100308 *olen = 0;
309
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100310 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100311 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100312
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100313 MBEDTLS_SSL_DEBUG_MSG( 3,
314 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100315
Hanno Becker261602c2017-04-12 14:54:42 +0100316 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100317
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100318 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
319 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100320
321 *p++ = 0x00;
322 *p++ = 0x00;
323
324 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100325
326 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +0100331static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
332 unsigned char *buf,
333 const unsigned char *end,
334 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200335{
336 unsigned char *p = buf;
337
Simon Butcher0fc94e92015-09-28 20:52:04 +0100338 *olen = 0;
339
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100340 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100341 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200342
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100343 MBEDTLS_SSL_DEBUG_MSG( 3,
344 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200345
Hanno Becker261602c2017-04-12 14:54:42 +0100346 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100347
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100348 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
349 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200350
351 *p++ = 0x00;
352 *p++ = 0x00;
353
354 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100355
356 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +0100361static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
362 unsigned char *buf,
363 const unsigned char *end,
364 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200365{
366 unsigned char *p = buf;
367 size_t tlen = ssl->session_negotiate->ticket_len;
368
Simon Butcher0fc94e92015-09-28 20:52:04 +0100369 *olen = 0;
370
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200371 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100372 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200373
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100374 MBEDTLS_SSL_DEBUG_MSG( 3,
375 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200376
Hanno Becker261602c2017-04-12 14:54:42 +0100377 /* The addition is safe here since the ticket length is 16 bit. */
378 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100379
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100380 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
381 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200382
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100383 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
384 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200385
386 *olen = 4;
387
Simon Butchered997662015-09-28 02:14:30 +0100388 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100389 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200390
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100391 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000392 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200393
394 memcpy( p, ssl->session_negotiate->ticket, tlen );
395
396 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100397
398 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200401
Ron Eldora9788042018-12-05 11:04:31 +0200402#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal77696ee2020-09-22 21:49:40 +0200403static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200404 unsigned char *buf,
405 const unsigned char *end,
406 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100407{
408 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200409 size_t protection_profiles_index = 0, ext_len = 0;
410 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100411
412 *olen = 0;
413
Johan Pascalc3ccd982020-10-28 17:18:18 +0100414 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
415 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
416 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100417 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200418 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100419 }
420
Ron Eldora9788042018-12-05 11:04:31 +0200421 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100422 * uint8 SRTPProtectionProfile[2];
423 *
424 * struct {
425 * SRTPProtectionProfiles SRTPProtectionProfiles;
426 * opaque srtp_mki<0..255>;
427 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100428 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100429 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200430 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200431 {
432 mki_len = ssl->dtls_srtp_info.mki_len;
433 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300434 /* Extension length = 2 bytes for profiles length,
435 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
436 * 1 byte for srtp_mki vector length and the mki_len value
437 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200438 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
439
Johan Pascal77696ee2020-09-22 21:49:40 +0200440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
441
442 /* Check there is room in the buffer for the extension + 4 bytes
443 * - the extension tag (2 bytes)
444 * - the extension length (2 bytes)
445 */
446 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
447
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100448 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
449 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200450
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100451 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
452 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100453
Ron Eldor3adb9922017-12-21 10:15:08 +0200454 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200455 /* micro-optimization:
456 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
457 * which is lower than 127, so the upper byte of the length is always 0
458 * For the documentation, the more generic code is left in comments
459 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
460 * >> 8 ) & 0xFF );
461 */
462 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100463 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100464
Ron Eldoref72faf2018-07-12 11:54:20 +0300465 for( protection_profiles_index=0;
466 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
467 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100468 {
Johan Pascal43f94902020-09-22 12:25:52 +0200469 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200470 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200471 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200472 {
473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
474 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100475 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
476 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200477 }
478 else
479 {
480 /*
481 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200482 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200483 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200484 MBEDTLS_SSL_DEBUG_MSG( 3,
485 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200486 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200487 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
488 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200489 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100490 }
491 }
492
Ron Eldor591f1622018-01-22 12:30:04 +0200493 *p++ = mki_len & 0xFF;
494
495 if( mki_len != 0 )
496 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200497 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200498 /*
499 * Increment p to point to the current position.
500 */
501 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300502 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
503 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200504 }
505
Ron Eldoref72faf2018-07-12 11:54:20 +0300506 /*
507 * total extension length: extension type (2 bytes)
508 * + extension length (2 bytes)
509 * + protection profile length (2 bytes)
510 * + 2 * number of protection profiles
511 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200512 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300513 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200514 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200515
516 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100517}
518#endif /* MBEDTLS_SSL_DTLS_SRTP */
519
Ronald Cron4079abc2022-02-20 10:35:26 +0100520int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
521 unsigned char *buf,
522 const unsigned char *end,
523 int uses_ec,
524 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100525{
526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
527 unsigned char *p = buf;
528 size_t ext_len = 0;
529
530 (void) ssl;
531 (void) end;
532 (void) uses_ec;
533 (void) ret;
534 (void) ext_len;
535
536 *out_len = 0;
537
538 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
539 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
540#if defined(MBEDTLS_SSL_RENEGOTIATION)
541 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
542 {
543 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
544 return( ret );
545 }
546 p += ext_len;
547#endif
548
549#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
550 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
551 if( uses_ec )
552 {
553 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
554 &ext_len ) ) != 0 )
555 {
556 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
557 return( ret );
558 }
559 p += ext_len;
560 }
561#endif
562
563#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
564 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
565 {
566 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
567 return( ret );
568 }
569 p += ext_len;
570#endif
571
572#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
573 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
574 {
575 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
576 return( ret );
577 }
578 p += ext_len;
579#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
580
581#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
582 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
583 &ext_len ) ) != 0 )
584 {
585 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
586 return( ret );
587 }
588 p += ext_len;
589#endif
590
591#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
592 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
593 {
594 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
595 return( ret );
596 }
597 p += ext_len;
598#endif
599
600#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
601 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
602 {
603 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
604 return( ret );
605 }
606 p += ext_len;
607#endif
608
609#if defined(MBEDTLS_SSL_DTLS_SRTP)
610 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
611 {
612 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
613 return( ret );
614 }
615 p += ext_len;
616#endif
617
618#if defined(MBEDTLS_SSL_SESSION_TICKETS)
619 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
620 {
621 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
622 return( ret );
623 }
624 p += ext_len;
625#endif
626
627 *out_len = p - buf;
628
629 return( 0 );
630}
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200633 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000634 size_t len )
635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#if defined(MBEDTLS_SSL_RENEGOTIATION)
637 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000638 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100639 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000640 if( len != 1 + ssl->verify_data_len * 2 ||
641 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200642 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100643 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200644 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100645 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100648 mbedtls_ssl_send_alert_message(
649 ssl,
650 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
651 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100652 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000653 }
654 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100655 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100657 {
658 if( len != 1 || buf[0] != 0x00 )
659 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200660 MBEDTLS_SSL_DEBUG_MSG( 1,
661 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100662 mbedtls_ssl_send_alert_message(
663 ssl,
664 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
665 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100666 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100667 }
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100670 }
Paul Bakker48916f92012-09-16 19:57:18 +0000671
672 return( 0 );
673}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
676static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200677 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200678 size_t len )
679{
680 /*
681 * server should use the extension only if we did,
682 * and if so the server's value should match ours (and len is always 1)
683 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200684 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200685 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200686 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200687 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100688 MBEDTLS_SSL_DEBUG_MSG( 1,
689 ( "non-matching max fragment length extension" ) );
690 mbedtls_ssl_send_alert_message(
691 ssl,
692 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100693 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
694 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200695 }
696
697 return( 0 );
698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000700
Hanno Beckera0e20d02019-05-15 14:03:01 +0100701#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +0100702static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
703 const unsigned char *buf,
704 size_t len )
705{
706 size_t peer_cid_len;
707
708 if( /* CID extension only makes sense in DTLS */
709 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
710 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100711 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100712 {
Hanno Becker22626482019-05-03 12:46:59 +0100713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100715 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100716 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100717 }
718
719 if( len == 0 )
720 {
721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
724 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100725 }
726
727 peer_cid_len = *buf++;
728 len--;
729
730 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
731 {
Hanno Becker22626482019-05-03 12:46:59 +0100732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100733 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100734 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
735 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100736 }
737
738 if( len != peer_cid_len )
739 {
Hanno Becker22626482019-05-03 12:46:59 +0100740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100741 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100742 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
743 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100744 }
745
Hanno Becker5a299902019-05-03 12:47:49 +0100746 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100747 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
748 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
749
750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
751 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
752
Hanno Beckera8373a12019-04-26 15:37:26 +0100753 return( 0 );
754}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100755#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
758static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100759 const unsigned char *buf,
760 size_t len )
761{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200762 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100763 len != 0 )
764 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100765 MBEDTLS_SSL_DEBUG_MSG( 1,
766 ( "non-matching encrypt-then-MAC extension" ) );
767 mbedtls_ssl_send_alert_message(
768 ssl,
769 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100770 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100771 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100772 }
773
774 ((void) buf);
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100777
778 return( 0 );
779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
783static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200784 const unsigned char *buf,
785 size_t len )
786{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200787 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200788 len != 0 )
789 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100790 MBEDTLS_SSL_DEBUG_MSG( 1,
791 ( "non-matching extended master secret extension" ) );
792 mbedtls_ssl_send_alert_message(
793 ssl,
794 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100795 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
796 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200797 }
798
799 ((void) buf);
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200802
803 return( 0 );
804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_SSL_SESSION_TICKETS)
808static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200809 const unsigned char *buf,
810 size_t len )
811{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200812 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200813 len != 0 )
814 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100815 MBEDTLS_SSL_DEBUG_MSG( 1,
816 ( "non-matching session ticket extension" ) );
817 mbedtls_ssl_send_alert_message(
818 ssl,
819 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100820 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
821 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200822 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200823
824 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200825
826 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200827
828 return( 0 );
829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200831
Robert Cragie136884c2015-10-02 13:34:31 +0100832#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100833 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200835 const unsigned char *buf,
836 size_t len )
837{
838 size_t list_size;
839 const unsigned char *p;
840
Philippe Antoine747fd532018-05-30 09:13:21 +0200841 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200844 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
845 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100846 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200847 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200848 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200849
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200850 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200851 while( list_size > 0 )
852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
854 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200855 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200856#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
857 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200858 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200859#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
860 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100861#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200862 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
863 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200866 return( 0 );
867 }
868
869 list_size--;
870 p++;
871 }
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200874 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
875 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100876 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200877}
Darryl Green11999bb2018-03-13 15:22:58 +0000878#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100879 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200880
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200881#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
882static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
883 const unsigned char *buf,
884 size_t len )
885{
Janos Follath865b3eb2019-12-16 11:46:15 +0000886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200887
Hanno Beckere694c3e2017-12-27 21:34:08 +0000888 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200889 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
890 {
891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
892 return( 0 );
893 }
894
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200895 /* If we got here, we no longer need our cached extension */
896 mbedtls_free( ssl->handshake->ecjpake_cache );
897 ssl->handshake->ecjpake_cache = NULL;
898 ssl->handshake->ecjpake_cache_len = 0;
899
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200900 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
901 buf, len ) ) != 0 )
902 {
903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100904 mbedtls_ssl_send_alert_message(
905 ssl,
906 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
907 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200908 return( ret );
909 }
910
911 return( 0 );
912}
913#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_SSL_ALPN)
916static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200917 const unsigned char *buf, size_t len )
918{
919 size_t list_len, name_len;
920 const char **p;
921
922 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200923 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200924 {
925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100926 mbedtls_ssl_send_alert_message(
927 ssl,
928 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100929 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
930 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200931 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200932
933 /*
934 * opaque ProtocolName<1..2^8-1>;
935 *
936 * struct {
937 * ProtocolName protocol_name_list<2..2^16-1>
938 * } ProtocolNameList;
939 *
940 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
941 */
942
943 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
944 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200945 {
946 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
947 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100948 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200949 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200950
951 list_len = ( buf[0] << 8 ) | buf[1];
952 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200953 {
954 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
955 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100956 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200957 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200958
959 name_len = buf[2];
960 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200961 {
962 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
963 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100964 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200965 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200966
967 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200968 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200969 {
970 if( name_len == strlen( *p ) &&
971 memcmp( buf + 3, *p, name_len ) == 0 )
972 {
973 ssl->alpn_chosen = *p;
974 return( 0 );
975 }
976 }
977
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200979 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
980 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100981 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200982}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200984
Johan Pascalb62bb512015-12-03 21:56:45 +0100985#if defined(MBEDTLS_SSL_DTLS_SRTP)
986static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300987 const unsigned char *buf,
988 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100989{
Johan Pascal43f94902020-09-22 12:25:52 +0200990 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200991 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100992 uint16_t server_protection_profile_value = 0;
993
994 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +0100995 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
996 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
997 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100998 return( 0 );
999
Ron Eldora9788042018-12-05 11:04:31 +02001000 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001001 * uint8 SRTPProtectionProfile[2];
1002 *
1003 * struct {
1004 * SRTPProtectionProfiles SRTPProtectionProfiles;
1005 * opaque srtp_mki<0..255>;
1006 * } UseSRTPData;
1007
1008 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1009 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001010 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001011 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001012 {
1013 mki_len = ssl->dtls_srtp_info.mki_len;
1014 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001015
Ron Eldoref72faf2018-07-12 11:54:20 +03001016 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001017 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1018 * + protection profile (2 bytes)
1019 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001020 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001021 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001022 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001023 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001024
1025 /*
1026 * get the server protection profile
1027 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001028
1029 /*
1030 * protection profile length must be 0x0002 as we must have only
1031 * one protection profile in server Hello
1032 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001033 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001034 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001035
1036 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001037 server_protection = mbedtls_ssl_check_srtp_profile_value(
1038 server_protection_profile_value );
1039 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001040 {
Johan Pascal43f94902020-09-22 12:25:52 +02001041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1042 mbedtls_ssl_get_srtp_profile_as_string(
1043 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001044 }
1045
Johan Pascal43f94902020-09-22 12:25:52 +02001046 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001047
Johan Pascalb62bb512015-12-03 21:56:45 +01001048 /*
1049 * Check we have the server profile in our list
1050 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001051 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001052 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001053 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1054 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001055 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1057 mbedtls_ssl_get_srtp_profile_as_string(
1058 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001059 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001060 }
1061 }
1062
Ron Eldor591f1622018-01-22 12:30:04 +02001063 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001064 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001065 {
1066 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001067 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001068 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001069 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001070
1071 /* If server does not use mki in its reply, make sure the client won't keep
1072 * one as negotiated */
1073 if( len == 5 )
1074 {
1075 ssl->dtls_srtp_info.mki_len = 0;
1076 }
1077
Ron Eldoref72faf2018-07-12 11:54:20 +03001078 /*
1079 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001080 * If the client detects a nonzero-length MKI in the server's response
1081 * that is different than the one the client offered, then the client
1082 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1083 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001084 if( len > 5 && ( buf[4] != mki_len ||
1085 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001086 {
1087 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1088 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001089 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001090 }
Ron Eldorb4655392018-07-05 18:25:39 +03001091#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001092 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001093 {
Ron Eldora9788042018-12-05 11:04:31 +02001094 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1095 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001096 }
1097#endif
Ron Eldora9788042018-12-05 11:04:31 +02001098 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001099}
1100#endif /* MBEDTLS_SSL_DTLS_SRTP */
1101
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001102/*
1103 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#if defined(MBEDTLS_SSL_PROTO_DTLS)
1106static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001109 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001110 unsigned char cookie_len;
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001113
Gilles Peskineb64bf062019-09-27 14:02:44 +02001114 /* Check that there is enough room for:
1115 * - 2 bytes of version
1116 * - 1 byte of cookie_len
1117 */
1118 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1119 {
1120 MBEDTLS_SSL_DEBUG_MSG( 1,
1121 ( "incoming HelloVerifyRequest message is too short" ) );
1122 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1123 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001124 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001125 }
1126
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001127 /*
1128 * struct {
1129 * ProtocolVersion server_version;
1130 * opaque cookie<0..2^8-1>;
1131 * } HelloVerifyRequest;
1132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001134 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001135 p += 2;
1136
TRodziewicz2d8800e2021-05-13 19:14:19 +02001137 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001138 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1139 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1140 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001141 */
Glenn Strauss83158112022-04-13 14:59:34 -04001142 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1147 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001148
Hanno Beckerbc000442021-06-24 09:18:19 +01001149 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001150 }
1151
1152 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001153 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1154 {
1155 MBEDTLS_SSL_DEBUG_MSG( 1,
1156 ( "cookie length does not match incoming message size" ) );
1157 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1158 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001159 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001160 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001161 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001162
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001163 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001165 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1166 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001167 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001169 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001170 }
1171
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001172 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001173 ssl->handshake->verify_cookie_len = cookie_len;
1174
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001175 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1177 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001182
1183 return( 0 );
1184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001188{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001189 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001190 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001191 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001192 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001193 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001195 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001196#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001197 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Hanno Becker327c93b2018-08-15 13:56:18 +01001202 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001204 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 return( ret );
1207 }
1208
Hanno Becker79594fd2019-05-08 09:38:41 +01001209 buf = ssl->in_msg;
1210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#if defined(MBEDTLS_SSL_RENEGOTIATION)
1214 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001215 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001216 ssl->renego_records_seen++;
1217
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001218 if( ssl->conf->renego_max_records >= 0 &&
1219 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001220 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001221 MBEDTLS_SSL_DEBUG_MSG( 1,
1222 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001224 }
1225
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001226 MBEDTLS_SSL_DEBUG_MSG( 1,
1227 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001228
1229 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001231 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001235 mbedtls_ssl_send_alert_message(
1236 ssl,
1237 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1238 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 }
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é-Gonnard74848812014-07-11 02:43:49 +02001244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001249 return( ssl_parse_hello_verify_request( ssl ) );
1250 }
1251 else
1252 {
1253 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001254 mbedtls_free( ssl->handshake->cookie );
1255 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001256 ssl->handshake->verify_cookie_len = 0;
1257 }
1258 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1262 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1266 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001267 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 }
1269
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001270 /*
1271 * 0 . 1 server_version
1272 * 2 . 33 random (maybe including 4 bytes of Unix time)
1273 * 34 . 34 session_id length = n
1274 * 35 . 34+n session_id
1275 * 35+n . 36+n cipher_suite
1276 * 37+n . 37+n compression_method
1277 *
1278 * 38+n . 39+n extensions length (optional)
1279 * 40+n . .. extensions
1280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001282
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001283 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 );
1284 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001285 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Glenn Strauss60bfe602022-03-14 19:04:24 -04001287 if( ssl->tls_version < ssl->conf->min_tls_version ||
1288 ssl->tls_version > ssl->conf->max_tls_version )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001289 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001290 MBEDTLS_SSL_DEBUG_MSG( 1,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001291 ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1292 (unsigned)ssl->conf->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -04001293 (unsigned)ssl->tls_version,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001294 (unsigned)ssl->conf->max_tls_version ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1297 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001298
Hanno Beckerbc000442021-06-24 09:18:19 +01001299 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001300 }
1301
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001303 ( (unsigned long) buf[2] << 24 ) |
1304 ( (unsigned long) buf[3] << 16 ) |
1305 ( (unsigned long) buf[4] << 8 ) |
1306 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001308 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001310 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
Paul Bakker48916f92012-09-16 19:57:18 +00001314 if( n > 32 )
1315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001317 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1318 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001319 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001320 }
1321
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001322 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001324 ext_len = ( ( buf[38 + n] << 8 )
1325 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Paul Bakker48916f92012-09-16 19:57:18 +00001327 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001331 mbedtls_ssl_send_alert_message(
1332 ssl,
1333 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1334 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001335 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001336 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001338 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001339 {
1340 ext_len = 0;
1341 }
1342 else
1343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001345 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1346 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001347 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001350 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001351 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001352
1353 /*
1354 * Read and check compression
1355 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001356 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001358 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001359 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001360 MBEDTLS_SSL_DEBUG_MSG( 1,
1361 ( "server hello, bad compression: %d", comp ) );
1362 mbedtls_ssl_send_alert_message(
1363 ssl,
1364 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1365 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001367 }
1368
Paul Bakker380da532012-04-18 16:10:25 +00001369 /*
1370 * Initialize update checksum functions
1371 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001372 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1373 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001374 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001375 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001376 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001377 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1378 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001380 }
Paul Bakker380da532012-04-18 16:10:25 +00001381
Hanno Beckere694c3e2017-12-27 21:34:08 +00001382 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001383
Paul Elliottd48d5c62021-01-07 14:47:05 +00001384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
1387 /*
1388 * Check if the session can be resumed
1389 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001390 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_SSL_RENEGOTIATION)
1392 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001393#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001394 ssl->session_negotiate->ciphersuite != i ||
1395 ssl->session_negotiate->compression != comp ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001396 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001397 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 {
1399 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001400 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001402 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001403#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001404 ssl->session_negotiate->ciphersuite = i;
1405 ssl->session_negotiate->compression = comp;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001406 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001407 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 }
1409 else
1410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001416 mbedtls_ssl_send_alert_message(
1417 ssl,
1418 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1419 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001420 return( ret );
1421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 }
1423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001425 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Paul Elliott3891caf2020-12-17 18:42:40 +00001427 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001428 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1429 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Andrzej Kurek03bac442018-04-25 05:06:07 -04001431 /*
1432 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001433 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 i = 0;
1435 while( 1 )
1436 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001437 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001440 mbedtls_ssl_send_alert_message(
1441 ssl,
1442 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1443 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001444 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 }
1446
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001447 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001448 ssl->session_negotiate->ciphersuite )
1449 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 }
1453
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001454 suite_info = mbedtls_ssl_ciphersuite_from_id(
1455 ssl->session_negotiate->ciphersuite );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001456 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version,
1457 ssl->tls_version ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001458 {
1459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001460 mbedtls_ssl_send_alert_message(
1461 ssl,
1462 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001463 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1464 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001465 }
1466
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001467 MBEDTLS_SSL_DEBUG_MSG( 3,
1468 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001469
Gilles Peskineeccd8882020-03-10 12:19:08 +01001470#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001471 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
Glenn Strauss60bfe602022-03-14 19:04:24 -04001472 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001473 {
1474 ssl->handshake->ecrs_enabled = 1;
1475 }
1476#endif
1477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 if( comp != MBEDTLS_SSL_COMPRESS_NULL
Paul Bakker2770fbd2012-07-03 13:30:23 +00001479 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001482 mbedtls_ssl_send_alert_message(
1483 ssl,
1484 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1485 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001486 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 }
Paul Bakker48916f92012-09-16 19:57:18 +00001488 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001490 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001491
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001492 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001493 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001494
Paul Bakker48916f92012-09-16 19:57:18 +00001495 while( ext_len )
1496 {
1497 unsigned int ext_id = ( ( ext[0] << 8 )
1498 | ( ext[1] ) );
1499 unsigned int ext_size = ( ( ext[2] << 8 )
1500 | ( ext[3] ) );
1501
1502 if( ext_size + 4 > ext_len )
1503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001505 mbedtls_ssl_send_alert_message(
1506 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1507 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001508 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001509 }
1510
1511 switch( ext_id )
1512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1515#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001516 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001517#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001518
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001519 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1520 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001521 return( ret );
1522
1523 break;
1524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1526 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001527 MBEDTLS_SSL_DEBUG_MSG( 3,
1528 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001529
1530 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1531 ext + 4, ext_size ) ) != 0 )
1532 {
1533 return( ret );
1534 }
1535
1536 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001538
Hanno Beckera0e20d02019-05-15 14:03:01 +01001539#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001540 case MBEDTLS_TLS_EXT_CID:
1541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1542
1543 if( ( ret = ssl_parse_cid_ext( ssl,
1544 ext + 4,
1545 ext_size ) ) != 0 )
1546 {
1547 return( ret );
1548 }
1549
1550 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001551#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1554 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001556
1557 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1558 ext + 4, ext_size ) ) != 0 )
1559 {
1560 return( ret );
1561 }
1562
1563 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1567 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001568 MBEDTLS_SSL_DEBUG_MSG( 3,
1569 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001570
1571 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1572 ext + 4, ext_size ) ) != 0 )
1573 {
1574 return( ret );
1575 }
1576
1577 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1581 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001583
1584 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1585 ext + 4, ext_size ) ) != 0 )
1586 {
1587 return( ret );
1588 }
1589
1590 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001592
Robert Cragie136884c2015-10-02 13:34:31 +01001593#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001594 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001596 MBEDTLS_SSL_DEBUG_MSG( 3,
1597 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001598
1599 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1600 ext + 4, ext_size ) ) != 0 )
1601 {
1602 return( ret );
1603 }
1604
1605 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001606#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1607 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001608
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001609#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1610 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1612
1613 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1614 ext + 4, ext_size ) ) != 0 )
1615 {
1616 return( ret );
1617 }
1618
1619 break;
1620#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#if defined(MBEDTLS_SSL_ALPN)
1623 case MBEDTLS_TLS_EXT_ALPN:
1624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001625
Johan Pascal275874b2020-10-27 10:43:53 +01001626 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1627 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001628
1629 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001631
Johan Pascalb62bb512015-12-03 21:56:45 +01001632#if defined(MBEDTLS_SSL_DTLS_SRTP)
1633 case MBEDTLS_TLS_EXT_USE_SRTP:
1634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1635
Johan Pascalc3ccd982020-10-28 17:18:18 +01001636 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1637 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001638
1639 break;
1640#endif /* MBEDTLS_SSL_DTLS_SRTP */
1641
Paul Bakker48916f92012-09-16 19:57:18 +00001642 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001643 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001644 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001645 }
1646
1647 ext_len -= 4 + ext_size;
1648 ext += 4 + ext_size;
1649
1650 if( ext_len > 0 && ext_len < 4 )
1651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001653 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001654 }
1655 }
1656
1657 /*
1658 * Renegotiation security checks
1659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001661 ssl->conf->allow_legacy_renegotiation ==
1662 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001663 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001664 MBEDTLS_SSL_DEBUG_MSG( 1,
1665 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001666 handshake_failure = 1;
1667 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_SSL_RENEGOTIATION)
1669 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1670 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001671 renegotiation_info_seen == 0 )
1672 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001673 MBEDTLS_SSL_DEBUG_MSG( 1,
1674 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001675 handshake_failure = 1;
1676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1678 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001679 ssl->conf->allow_legacy_renegotiation ==
1680 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001683 handshake_failure = 1;
1684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1686 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001687 renegotiation_info_seen == 1 )
1688 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001689 MBEDTLS_SSL_DEBUG_MSG( 1,
1690 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001691 handshake_failure = 1;
1692 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001694
1695 if( handshake_failure == 1 )
1696 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001697 mbedtls_ssl_send_alert_message(
1698 ssl,
1699 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1700 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001701 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001702 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001705
1706 return( 0 );
1707}
1708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1710 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001711static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1712 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001713 unsigned char *end )
1714{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001716 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001717
Paul Bakker29e1f122013-04-16 13:07:56 +02001718 /*
1719 * Ephemeral DH parameters:
1720 *
1721 * struct {
1722 * opaque dh_p<1..2^16-1>;
1723 * opaque dh_g<1..2^16-1>;
1724 * opaque dh_Ys<1..2^16-1>;
1725 * } ServerDHParams;
1726 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001727 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1728 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001731 return( ret );
1732 }
1733
Gilles Peskine487bbf62021-05-27 22:17:07 +02001734 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001735 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001736 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001738 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001739 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001740 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001741 }
1742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1744 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1745 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001746
1747 return( ret );
1748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1750 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001751
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001752#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1753 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1754 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1755#if defined(MBEDTLS_USE_PSA_CRYPTO)
1756static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001757 unsigned char **p,
1758 unsigned char *end )
1759{
1760 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001761 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001762 uint8_t ecpoint_len;
1763 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1764
1765 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001766 * struct {
1767 * ECParameters curve_params;
1768 * ECPoint public;
1769 * } ServerECDHParams;
1770 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001771 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001772 * 2..3 NamedCurve
1773 * 4 ECPoint.len
1774 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001775 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001776 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001777 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001778
1779 /* First byte is curve_type; only named_curve is handled */
1780 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001781 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001782
1783 /* Next two bytes are the namedcurve value */
1784 tls_id = *(*p)++;
1785 tls_id <<= 8;
1786 tls_id |= *(*p)++;
1787
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001788 /* Check it's a curve we offered */
1789 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001790 {
1791 MBEDTLS_SSL_DEBUG_MSG( 2,
1792 ( "bad server key exchange message (ECDHE curve): %u",
1793 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001794 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001795 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001796
Hanno Beckerbb89e272019-01-08 12:54:37 +00001797 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001798 if( ( handshake->ecdh_psa_type =
1799 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001800 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001801 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001802 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001803 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001804
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001805 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001806 ecpoint_len = *(*p)++;
1807 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001808 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001809
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001810 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1811 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001812
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001813 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1814 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001815 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001816
Hanno Beckerbb89e272019-01-08 12:54:37 +00001817 return( 0 );
1818}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001819#else
Neil Armstrong1f198d82022-04-13 15:02:30 +02001820static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1821{
1822 const mbedtls_ecp_curve_info *curve_info;
1823 mbedtls_ecp_group_id grp_id;
1824#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1825 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1826#else
1827 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1828#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001829
Neil Armstrong1f198d82022-04-13 15:02:30 +02001830 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1831 if( curve_info == NULL )
1832 {
1833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1835 }
1836
1837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1838
1839 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1840 return( -1 );
1841
1842 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1843 MBEDTLS_DEBUG_ECDH_QP );
1844
1845 return( 0 );
1846}
1847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001849 unsigned char **p,
1850 unsigned char *end )
1851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001853
Paul Bakker29e1f122013-04-16 13:07:56 +02001854 /*
1855 * Ephemeral ECDH parameters:
1856 *
1857 * struct {
1858 * ECParameters curve_params;
1859 * ECPoint public;
1860 * } ServerECDHParams;
1861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001863 (const unsigned char **) p, end ) ) != 0 )
1864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001866#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001867 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1868 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001869#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001870 return( ret );
1871 }
1872
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001873 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001874 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001875 MBEDTLS_SSL_DEBUG_MSG( 1,
1876 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001877 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001878 }
1879
Paul Bakker29e1f122013-04-16 13:07:56 +02001880 return( ret );
1881}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001882#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1884 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1885 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001886
Gilles Peskineeccd8882020-03-10 12:19:08 +01001887#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001889 unsigned char **p,
1890 unsigned char *end )
1891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001893 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001894 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001895
1896 /*
1897 * PSK parameters:
1898 *
1899 * opaque psk_identity_hint<0..2^16-1>;
1900 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001901 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001902 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001903 MBEDTLS_SSL_DEBUG_MSG( 1,
1904 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001905 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001906 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001907 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001908 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001909
irwir6527bd62019-09-21 18:51:25 +03001910 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001911 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001912 MBEDTLS_SSL_DEBUG_MSG( 1,
1913 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001914 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001915 }
1916
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001917 /*
1918 * Note: we currently ignore the PKS identity hint, as we only allow one
1919 * PSK to be provisionned on the client. This could be changed later if
1920 * someone needs that feature.
1921 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001922 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001923 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001924
1925 return( ret );
1926}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001927#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1930 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001931/*
1932 * Generate a pre-master secret and encrypt it with the server's RSA key
1933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001935 size_t offset, size_t *olen,
1936 size_t pms_offset )
1937{
Janos Follath865b3eb2019-12-16 11:46:15 +00001938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001939 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001940 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001941 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001942
Angus Grattond8213d02016-05-25 20:56:48 +10001943 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001944 {
1945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1946 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1947 }
1948
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001949 /*
1950 * Generate (part of) the pre-master as
1951 * struct {
1952 * ProtocolVersion client_version;
1953 * opaque random[46];
1954 * } PreMasterSecret;
1955 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001956 mbedtls_ssl_write_version( p, ssl->conf->transport,
1957 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001958
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001959 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001962 return( ret );
1963 }
1964
1965 ssl->handshake->pmslen = 48;
1966
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001967#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1968 peer_pk = &ssl->handshake->peer_pubkey;
1969#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001970 if( ssl->session_negotiate->peer_cert == NULL )
1971 {
Hanno Becker8273df82019-02-06 17:37:32 +00001972 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00001974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001975 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001976 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1977#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001978
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001979 /*
1980 * Now write it out, encrypted
1981 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001982 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1985 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001986 }
1987
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001988 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001989 p, ssl->handshake->pmslen,
1990 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10001991 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001992 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001995 return( ret );
1996 }
1997
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001998 if( len_bytes == 2 )
1999 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002000 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002001 *olen += 2;
2002 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002003
Hanno Beckerae553dd2019-02-08 14:06:00 +00002004#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2005 /* We don't need the peer's public key anymore. Free it. */
2006 mbedtls_pk_free( peer_pk );
2007#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002008 return( 0 );
2009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2011 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002012
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002013#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2014 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2015 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002017 unsigned char **p,
2018 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 mbedtls_md_type_t *md_alg,
2020 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002021{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 *md_alg = MBEDTLS_MD_NONE;
2023 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002024
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002025 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002026 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002027
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002028 /*
2029 * Get hash algorithm
2030 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002031 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2032 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002033 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002034 MBEDTLS_SSL_DEBUG_MSG( 1,
2035 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002036 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002037 }
2038
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002039 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002040 * Get signature algorithm
2041 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002042 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2043 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002044 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002045 MBEDTLS_SSL_DEBUG_MSG( 1,
2046 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002047 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002048 }
2049
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002050 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002051 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002052 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002053 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002054 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002055 MBEDTLS_SSL_DEBUG_MSG( 1,
2056 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002057 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002058 }
2059
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2061 (*p)[1] ) );
2062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2063 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002064 *p += 2;
2065
2066 return( 0 );
2067}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002068#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2069 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2070 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2073 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2074static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002075{
Janos Follath865b3eb2019-12-16 11:46:15 +00002076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002078 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002079
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002080#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2081 peer_pk = &ssl->handshake->peer_pubkey;
2082#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002083 if( ssl->session_negotiate->peer_cert == NULL )
2084 {
Hanno Becker8273df82019-02-06 17:37:32 +00002085 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002088 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002089 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2090#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002091
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002092 /* This is a public key, so it can't be opaque, so can_do() is a good
2093 * enough check to ensure pk_ec() is safe to use below. */
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002094 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2097 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002098 }
2099
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002100 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002101
Przemek Stekielea4000f2022-03-16 09:49:33 +01002102#if defined(MBEDTLS_USE_PSA_CRYPTO)
2103 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002104 size_t olen = 0;
2105
2106 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2107 {
2108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2109 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2110 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002111
2112 ssl->handshake->ecdh_psa_type =
2113 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2114 &ecdh_bits ) );
2115
2116 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2117 {
2118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2119 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2120 }
2121
2122 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2123
Przemek Stekielea4000f2022-03-16 09:49:33 +01002124 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002125 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2126 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2127 ssl->handshake->ecdh_psa_peerkey,
2128 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002129
Przemek Stekiel561a4232022-03-16 13:16:24 +01002130 if ( ret != 0 )
2131 {
2132 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2133 return( ret );
2134 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002135
Przemek Stekiel561a4232022-03-16 13:16:24 +01002136 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002137#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2139 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002142 return( ret );
2143 }
2144
2145 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002148 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002149 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002150#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002151#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2152 /* We don't need the peer's public key anymore. Free it,
2153 * so that more RAM is available for upcoming expensive
2154 * operations like ECDHE. */
2155 mbedtls_pk_free( peer_pk );
2156#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2157
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002158 return( ret );
2159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2161 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002164{
Janos Follath865b3eb2019-12-16 11:46:15 +00002165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002166 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002167 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002168 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2173 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 ssl->state++;
2177 return( 0 );
2178 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002179 ((void) p);
2180 ((void) end);
2181#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2184 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2185 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2186 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002187 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002188 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002191 mbedtls_ssl_send_alert_message(
2192 ssl,
2193 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2194 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002195 return( ret );
2196 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002199 ssl->state++;
2200 return( 0 );
2201 }
2202 ((void) p);
2203 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2205 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002206
Gilles Peskineeccd8882020-03-10 12:19:08 +01002207#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002208 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002209 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002210 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002211 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002212 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002213#endif
2214
Hanno Becker327c93b2018-08-15 13:56:18 +01002215 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 return( ret );
2219 }
2220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002224 mbedtls_ssl_send_alert_message(
2225 ssl,
2226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2227 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002231 /*
2232 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2233 * doesn't use a psk_identity_hint
2234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2238 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002239 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002240 /* Current message is probably either
2241 * CertificateRequest or ServerHelloDone */
2242 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002243 goto exit;
2244 }
2245
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002246 MBEDTLS_SSL_DEBUG_MSG( 1,
2247 ( "server key exchange message must not be skipped" ) );
2248 mbedtls_ssl_send_alert_message(
2249 ssl,
2250 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2251 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 }
2255
Gilles Peskineeccd8882020-03-10 12:19:08 +01002256#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002257 if( ssl->handshake->ecrs_enabled )
2258 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2259
2260start_processing:
2261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002263 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002265
Gilles Peskineeccd8882020-03-10 12:19:08 +01002266#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2268 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2269 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2270 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002271 {
2272 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002275 mbedtls_ssl_send_alert_message(
2276 ssl,
2277 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002278 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002279 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002280 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002281 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002282#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2285 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2286 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2287 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002288 ; /* nothing more to do */
2289 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2291 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2292#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2293 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2294 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2295 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002297 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002300 mbedtls_ssl_send_alert_message(
2301 ssl,
2302 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2303 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002304 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002305 }
2306 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2309 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002310#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2311 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2313 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2314 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2315 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002316 {
2317 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002320 mbedtls_ssl_send_alert_message(
2321 ssl,
2322 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2323 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002324 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002325 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002326 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002327 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2329 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2330 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002331#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2332 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2333 {
2334 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2335 p, end - p );
2336 if( ret != 0 )
2337 {
2338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002339 mbedtls_ssl_send_alert_message(
2340 ssl,
2341 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002342 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2343 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002344 }
2345 }
2346 else
2347#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002351 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002352
Gilles Peskineeccd8882020-03-10 12:19:08 +01002353#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002354 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002355 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002356 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002357#if defined(MBEDTLS_USE_PSA_CRYPTO)
2358 unsigned char hash[PSA_HASH_MAX_SIZE];
2359#else
2360 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2361#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2363 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2364 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002365 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002366 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002367
Hanno Beckera6899bb2019-02-06 18:26:03 +00002368 mbedtls_pk_context * peer_pk;
2369
Paul Bakker29e1f122013-04-16 13:07:56 +02002370 /*
2371 * Handle the digitally-signed structure
2372 */
Ronald Cron90915f22022-03-07 11:11:36 +01002373 if( ssl_parse_signature_algorithm( ssl, &p, end,
2374 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002375 {
Ronald Cron90915f22022-03-07 11:11:36 +01002376 MBEDTLS_SSL_DEBUG_MSG( 1,
2377 ( "bad server key exchange message" ) );
2378 mbedtls_ssl_send_alert_message(
2379 ssl,
2380 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2381 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2382 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002383 }
Ronald Cron90915f22022-03-07 11:11:36 +01002384
2385 if( pk_alg !=
2386 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002387 {
Ronald Cron90915f22022-03-07 11:11:36 +01002388 MBEDTLS_SSL_DEBUG_MSG( 1,
2389 ( "bad server key exchange message" ) );
2390 mbedtls_ssl_send_alert_message(
2391 ssl,
2392 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2393 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2394 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002395 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002396
2397 /*
2398 * Read signature
2399 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002400
2401 if( p > end - 2 )
2402 {
2403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002404 mbedtls_ssl_send_alert_message(
2405 ssl,
2406 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2407 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002408 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002409 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002410 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002411 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002412
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002413 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002416 mbedtls_ssl_send_alert_message(
2417 ssl,
2418 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2419 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002420 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002424
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002425 /*
2426 * Compute the hash that has been signed
2427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002429 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002430 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2431 params, params_len,
2432 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002433 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002434 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002435 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002436 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2439 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002440 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002441
Gilles Peskineca1d7422018-04-24 11:53:22 +02002442 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002443
Hanno Beckera6899bb2019-02-06 18:26:03 +00002444#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2445 peer_pk = &ssl->handshake->peer_pubkey;
2446#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002447 if( ssl->session_negotiate->peer_cert == NULL )
2448 {
Hanno Becker8273df82019-02-06 17:37:32 +00002449 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002452 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00002453 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2454#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002455
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002456 /*
2457 * Verify signature
2458 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002459 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002462 mbedtls_ssl_send_alert_message(
2463 ssl,
2464 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2465 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002467 }
2468
Gilles Peskineeccd8882020-03-10 12:19:08 +01002469#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002470 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002471 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002472#endif
2473
Hanno Beckera6899bb2019-02-06 18:26:03 +00002474 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002475 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002476 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002477#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002478 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2479#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002480 mbedtls_ssl_send_alert_message(
2481 ssl,
2482 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2483 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002485#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002486 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2487 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2488#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002489 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002490 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002491
2492#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2493 /* We don't need the peer's public key anymore. Free it,
2494 * so that more RAM is available for upcoming expensive
2495 * operations like ECDHE. */
2496 mbedtls_pk_free( peer_pk );
2497#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002499#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002501exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 ssl->state++;
2503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
2506 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002507}
2508
Gilles Peskineeccd8882020-03-10 12:19:08 +01002509#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002511{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002512 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002513 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002516
Hanno Becker1aa267c2017-04-28 17:08:27 +01002517 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002520 ssl->state++;
2521 return( 0 );
2522 }
2523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002526}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002527#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002529{
Janos Follath865b3eb2019-12-16 11:46:15 +00002530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002531 unsigned char *buf;
2532 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002534 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002535 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002536 size_t sig_alg_len;
2537#if defined(MBEDTLS_DEBUG_C)
2538 unsigned char *sig_alg;
2539#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Hanno Becker1aa267c2017-04-28 17:08:27 +01002543 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002546 ssl->state++;
2547 return( 0 );
2548 }
2549
Hanno Becker327c93b2018-08-15 13:56:18 +01002550 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2553 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002556 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2557 {
2558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002559 mbedtls_ssl_send_alert_message(
2560 ssl,
2561 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2562 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002563 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2564 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002566 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002567 ssl->handshake->client_auth =
2568 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002571 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Jerry Yufb28b882022-01-28 11:05:58 +08002573 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002574 {
Johan Pascala89ca862020-08-25 10:03:19 +02002575 /* Current message is probably the ServerHelloDone */
2576 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002577 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002578 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002579
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002580 /*
2581 * struct {
2582 * ClientCertificateType certificate_types<1..2^8-1>;
2583 * SignatureAndHashAlgorithm
2584 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2585 * DistinguishedName certificate_authorities<0..2^16-1>;
2586 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002587 *
2588 * Since we only support a single certificate on clients, let's just
2589 * ignore all the information that's supposed to help us pick a
2590 * certificate.
2591 *
2592 * We could check that our certificate matches the request, and bail out
2593 * if it doesn't, but it's simpler to just send the certificate anyway,
2594 * and give the server the opportunity to decide if it should terminate
2595 * the connection when it doesn't like our certificate.
2596 *
2597 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2598 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002599 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002600 *
2601 * However, we still minimally parse the message to check it is at least
2602 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002603 */
Paul Bakker926af752012-11-23 13:38:07 +01002604 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002605
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002606 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002607 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2608 {
2609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2610 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2611 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002612 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002615 n = cert_type_len;
2616
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002617 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002618 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002619 * * the length of the signature algorithms field (if minor version of
2620 * SSL is 3),
2621 * * distinguished name length otherwise.
2622 * Both reach at most the index:
2623 * ...hdr_len + 2 + n,
2624 * therefore the buffer length at this point must be greater than that
2625 * regardless of the actual code path.
2626 */
2627 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002630 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2631 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002632 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002633 }
2634
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002635 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002636 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2637 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2638
2639 /*
2640 * The furthest access in buf is in the loop few lines below:
2641 * sig_alg[i + 1],
2642 * where:
2643 * sig_alg = buf + ...hdr_len + 3 + n,
2644 * max(i) = sig_alg_len - 1.
2645 * Therefore the furthest access is:
2646 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2647 * which reduces to:
2648 * buf[...hdr_len + 3 + n + sig_alg_len],
2649 * which is one less than we need the buf to be.
2650 */
2651 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002652 {
Ronald Cron90915f22022-03-07 11:11:36 +01002653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2654 mbedtls_ssl_send_alert_message(
2655 ssl,
2656 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2657 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2658 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002659 }
Paul Bakker926af752012-11-23 13:38:07 +01002660
Ronald Cron90915f22022-03-07 11:11:36 +01002661#if defined(MBEDTLS_DEBUG_C)
2662 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2663 for( size_t i = 0; i < sig_alg_len; i += 2 )
2664 {
2665 MBEDTLS_SSL_DEBUG_MSG( 3,
2666 ( "Supported Signature Algorithm found: %d,%d",
2667 sig_alg[i], sig_alg[i + 1] ) );
2668 }
2669#endif
2670
2671 n += 2 + sig_alg_len;
2672
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002673 /* certificate_authorities */
2674 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2675 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002676
2677 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002678 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002681 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2682 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002683 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002684 }
2685
2686exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
2689 return( 0 );
2690}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002691#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002694{
Janos Follath865b3eb2019-12-16 11:46:15 +00002695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Hanno Becker327c93b2018-08-15 13:56:18 +01002699 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2702 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002704
2705 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2706 {
2707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2708 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2709 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2712 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002715 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2716 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002717 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 }
2719
2720 ssl->state++;
2721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002723 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002725#endif
2726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
2729 return( 0 );
2730}
2731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002733{
Janos Follath865b3eb2019-12-16 11:46:15 +00002734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002735
2736 size_t header_len;
2737 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002738 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002739 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2744 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 /*
2747 * DHM key exchange -- send G^X mod P
2748 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002749 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Joe Subbiani6dd73642021-07-19 11:56:54 +01002751 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002752 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002755 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002756 &ssl->out_msg[header_len], content_len,
2757 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 if( ret != 0 )
2759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 return( ret );
2762 }
2763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2765 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002768 ssl->handshake->premaster,
2769 MBEDTLS_PREMASTER_SIZE,
2770 &ssl->handshake->pmslen,
2771 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774 return( ret );
2775 }
2776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 }
2779 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002781#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2782 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2783 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2784 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002785 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002786 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2787 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2788 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002789 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002790#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002791 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2792 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002793 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002794
2795 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2796
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002797 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002798
Hanno Becker0a94a642019-01-11 14:35:30 +00002799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2800
Hanno Becker4a63ed42019-01-08 11:39:35 +00002801 /*
2802 * Generate EC private key for ECDHE exchange.
2803 */
2804
Hanno Becker4a63ed42019-01-08 11:39:35 +00002805 /* The master secret is obtained from the shared ECDH secret by
2806 * applying the TLS 1.2 PRF with a specific salt and label. While
2807 * the PSA Crypto API encourages combining key agreement schemes
2808 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2809 * yet support the provisioning of salt + label to the KDF.
2810 * For the time being, we therefore need to split the computation
2811 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002812 key_attributes = psa_key_attributes_init();
2813 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002814 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002815 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2816 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002817
2818 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002819 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002820 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002821 if( status != PSA_SUCCESS )
2822 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2823
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002824 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002825 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002826 * but we just need to add a length byte before that. */
2827 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2828 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2829 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2830 size_t own_pubkey_len;
2831
Hanno Becker4a63ed42019-01-08 11:39:35 +00002832 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002833 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002834 &own_pubkey_len );
2835 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002836 {
2837 psa_destroy_key( handshake->ecdh_psa_privkey );
2838 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002839 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002840 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002841
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002842 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2843 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002844
Hanno Becker4a63ed42019-01-08 11:39:35 +00002845 /* The ECDH secret is the premaster secret used for key derivation. */
2846
Janos Follathdf3b0892019-08-08 11:12:24 +01002847 /* Compute ECDH shared secret. */
2848 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2849 handshake->ecdh_psa_privkey,
2850 handshake->ecdh_psa_peerkey,
2851 handshake->ecdh_psa_peerkey_len,
2852 ssl->handshake->premaster,
2853 sizeof( ssl->handshake->premaster ),
2854 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002855
Andrzej Kureka0237f82022-02-24 13:24:52 -05002856 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002857 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002858
2859 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2860 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002861#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002862 /*
2863 * ECDH key exchange -- send client public value
2864 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002865 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002866
Gilles Peskineeccd8882020-03-10 12:19:08 +01002867#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002868 if( ssl->handshake->ecrs_enabled )
2869 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002870 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002871 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002872
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002873 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2874 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002875#endif
2876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002878 &content_len,
2879 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002880 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002881 if( ret != 0 )
2882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002884#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002885 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2886 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2887#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002888 return( ret );
2889 }
2890
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002891 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2892 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002893
Gilles Peskineeccd8882020-03-10 12:19:08 +01002894#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002895 if( ssl->handshake->ecrs_enabled )
2896 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002897 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002898 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002899 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002900
2901ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002902 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002903 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002906 &ssl->handshake->pmslen,
2907 ssl->handshake->premaster,
2908 MBEDTLS_MPI_MAX_SIZE,
2909 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002912#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002913 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2914 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2915#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002916 return( ret );
2917 }
2918
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002919 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2920 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02002921#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01002922 }
2923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2925 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2926 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2927 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002928#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2929 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2930 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2931 {
2932 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2933 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2934 psa_key_attributes_t key_attributes;
2935
2936 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2937
2938 /*
2939 * opaque psk_identity<0..2^16-1>;
2940 */
2941 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002942 /* We don't offer PSK suites if we don't have a PSK,
2943 * and we check that the server's choice is among the
2944 * ciphersuites we offered, so this should never happen. */
2945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002946
Neil Armstrongfc834f22022-03-23 17:54:38 +01002947 /* uint16 to store content length */
2948 const size_t content_len_size = 2;
2949
Neil Armstrong868af822022-03-09 10:26:25 +01002950 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002951
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002952 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002953 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002954 {
2955 MBEDTLS_SSL_DEBUG_MSG( 1,
2956 ( "psk identity too long or SSL buffer too short" ) );
2957 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2958 }
2959
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002960 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002961
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002962 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
2963 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
2964 header_len += content_len_size;
2965
2966 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01002967 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002968 p += ssl->conf->psk_identity_len;
2969
Neil Armstrong868af822022-03-09 10:26:25 +01002970 header_len += ssl->conf->psk_identity_len;
2971
2972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2973
2974 /*
2975 * Generate EC private key for ECDHE exchange.
2976 */
2977
2978 /* The master secret is obtained from the shared ECDH secret by
2979 * applying the TLS 1.2 PRF with a specific salt and label. While
2980 * the PSA Crypto API encourages combining key agreement schemes
2981 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2982 * yet support the provisioning of salt + label to the KDF.
2983 * For the time being, we therefore need to split the computation
2984 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2985 key_attributes = psa_key_attributes_init();
2986 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2987 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
2988 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2989 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
2990
2991 /* Generate ECDH private key. */
2992 status = psa_generate_key( &key_attributes,
2993 &handshake->ecdh_psa_privkey );
2994 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01002995 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01002996
2997 /* Export the public part of the ECDH private key from PSA.
2998 * The export format is an ECPoint structure as expected by TLS,
2999 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003000 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003001 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3002 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003003 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003004
3005 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3006 own_pubkey, own_pubkey_max_len,
3007 &own_pubkey_len );
3008 if( status != PSA_SUCCESS )
3009 {
3010 psa_destroy_key( handshake->ecdh_psa_privkey );
3011 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003012 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003013 }
3014
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003015 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003016 content_len = own_pubkey_len + 1;
3017
Neil Armstrong25400452022-03-23 17:44:07 +01003018 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3019 * - a uint16 containing the length (in octets) of the ECDH computation
3020 * - the octet string produced by the ECDH computation
3021 * - a uint16 containing the length (in octets) of the PSK
3022 * - the PSK itself
3023 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003024 unsigned char *pms = ssl->handshake->premaster;
3025 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003026 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003027 /* uint16 to store length (in octets) of the ECDH computation */
3028 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003029 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003030
Neil Armstrong25400452022-03-23 17:44:07 +01003031 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003032 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3033 handshake->ecdh_psa_privkey,
3034 handshake->ecdh_psa_peerkey,
3035 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003036 pms + zlen_size,
3037 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003038 &zlen );
3039
3040 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3041 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3042
Neil Armstrongc530aa62022-03-23 17:45:01 +01003043 if( status != PSA_SUCCESS )
3044 return( psa_ssl_status_to_mbedtls( status ) );
3045 else if( destruction_status != PSA_SUCCESS )
3046 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003047
Neil Armstrong25400452022-03-23 17:44:07 +01003048 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003049 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3050 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003051 }
3052 else
3053#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3054 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003055#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003056 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003057 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003058 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003059 * opaque psk_identity<0..2^16-1>;
3060 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003061 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003062 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003063 /* We don't offer PSK suites if we don't have a PSK,
3064 * and we check that the server's choice is among the
3065 * ciphersuites we offered, so this should never happen. */
3066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003067 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003068
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003069 header_len = 4;
3070 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003071
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003072 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003073 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003074 MBEDTLS_SSL_DEBUG_MSG( 1,
3075 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003076 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3077 }
3078
Joe Subbianifbeb6922021-07-16 14:27:50 +01003079 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3080 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003081
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003082 memcpy( ssl->out_msg + header_len,
3083 ssl->conf->psk_identity,
3084 ssl->conf->psk_identity_len );
3085 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3088 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003089 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003090 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003091 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003092 else
3093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3095 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003096 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003097 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3098 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003099 return( ret );
3100 }
3101 else
3102#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3104 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003105 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003106 /*
3107 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3108 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003109 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003110
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003111 if( header_len + 2 + content_len >
3112 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003113 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003114 MBEDTLS_SSL_DEBUG_MSG( 1,
3115 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003116 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3117 }
3118
Joe Subbianifbeb6922021-07-16 14:27:50 +01003119 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3120 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003123 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003124 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003125 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003126 if( ret != 0 )
3127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003129 return( ret );
3130 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003131
3132#if defined(MBEDTLS_USE_PSA_CRYPTO)
3133 unsigned char *pms = ssl->handshake->premaster;
3134 unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster );
3135 size_t pms_len;
3136
3137 /* Write length only when we know the actual value */
3138 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3139 pms + 2, pms_end - ( pms + 2 ), &pms_len,
3140 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3141 {
3142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3143 return( ret );
3144 }
3145 MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 );
3146 pms += 2 + pms_len;
3147
3148 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3149#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003150 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003153#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3154 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003156 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003157 /*
3158 * ClientECDiffieHellmanPublic public;
3159 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003160 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3161 &content_len,
3162 &ssl->out_msg[header_len],
3163 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003164 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003165 if( ret != 0 )
3166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003168 return( ret );
3169 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003170
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003171 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3172 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003173 }
3174 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003175#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003179 }
3180
Neil Armstrong80f6f322022-05-03 17:56:38 +02003181#if !defined(MBEDTLS_USE_PSA_CRYPTO)
3182 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3183 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003184 {
Neil Armstrong80f6f322022-05-03 17:56:38 +02003185 MBEDTLS_SSL_DEBUG_RET( 1,
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003186 "mbedtls_ssl_psk_derive_premaster", ret );
Neil Armstrong80f6f322022-05-03 17:56:38 +02003187 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003188 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003189#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003190 }
3191 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003192#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3194 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003196 header_len = 4;
3197 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3198 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003199 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 }
Paul Bakkered27a042013-04-18 22:46:23 +02003201 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003203#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3204 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3205 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003206 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003207
3208 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003209 ssl->out_msg + header_len,
3210 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3211 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003212 ssl->conf->f_rng, ssl->conf->p_rng );
3213 if( ret != 0 )
3214 {
3215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3216 return( ret );
3217 }
3218
3219 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3220 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3221 ssl->conf->f_rng, ssl->conf->p_rng );
3222 if( ret != 0 )
3223 {
3224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3225 return( ret );
3226 }
3227 }
3228 else
3229#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003230 {
3231 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003234 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003236 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3238 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
3240 ssl->state++;
3241
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003242 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003243 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 return( ret );
3246 }
3247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249
3250 return( 0 );
3251}
3252
Gilles Peskineeccd8882020-03-10 12:19:08 +01003253#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003255{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003256 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003257 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003265 return( ret );
3266 }
3267
Hanno Becker77adddc2019-02-07 12:32:43 +00003268 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003271 ssl->state++;
3272 return( 0 );
3273 }
3274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003277}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003278#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003282 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003283 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003284 size_t n = 0, offset = 0;
3285 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003286 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003288 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003289 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003290#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3291 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3292#else
3293 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3294#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003297
Gilles Peskineeccd8882020-03-10 12:19:08 +01003298#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003299 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003300 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003301 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003302 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003303 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003304#endif
3305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003309 return( ret );
3310 }
3311
Hanno Becker77adddc2019-02-07 12:32:43 +00003312 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003315 ssl->state++;
3316 return( 0 );
3317 }
3318
Jerry Yufb28b882022-01-28 11:05:58 +08003319 if( ssl->handshake->client_auth == 0 ||
3320 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 {
Johan Pascala89ca862020-08-25 10:03:19 +02003322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3323 ssl->state++;
3324 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 }
3326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 }
3332
3333 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003334 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003336#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003337 if( ssl->handshake->ecrs_enabled )
3338 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3339
3340sign:
3341#endif
3342
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003343 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003344
Ronald Cron90915f22022-03-07 11:11:36 +01003345 /*
3346 * digitally-signed struct {
3347 * opaque handshake_messages[handshake_messages_length];
3348 * };
3349 *
3350 * Taking shortcut here. We assume that the server always allows the
3351 * PRF Hash function and has sent it in the allowed signature
3352 * algorithms list received in the Certificate Request message.
3353 *
3354 * Until we encounter a server that does not, we will take this
3355 * shortcut.
3356 *
3357 * Reason: Otherwise we should have running hashes for SHA512 and
3358 * SHA224 in order to satisfy 'weird' needs from the server
3359 * side.
3360 */
3361 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003362 {
Ronald Cron90915f22022-03-07 11:11:36 +01003363 md_alg = MBEDTLS_MD_SHA384;
3364 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003365 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003366 else
Paul Bakker577e0062013-08-28 11:57:20 +02003367 {
Ronald Cron90915f22022-03-07 11:11:36 +01003368 md_alg = MBEDTLS_MD_SHA256;
3369 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003370 }
Ronald Cron90915f22022-03-07 11:11:36 +01003371 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3372
3373 /* Info from md_alg will be used instead */
3374 hashlen = 0;
3375 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003376
Gilles Peskineeccd8882020-03-10 12:19:08 +01003377#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003378 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003379 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003380#endif
3381
3382 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3383 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003384 ssl->out_msg + 6 + offset,
3385 out_buf_len - 6 - offset,
3386 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003387 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003390#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003391 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3392 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3393#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003394 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003395 }
Paul Bakker926af752012-11-23 13:38:07 +01003396
Joe Subbiani6dd73642021-07-19 11:56:54 +01003397 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398
Paul Bakker1ef83d62012-04-11 12:09:53 +00003399 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3401 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003402
3403 ssl->state++;
3404
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003405 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003406 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 return( ret );
3409 }
3410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003412
Paul Bakkered27a042013-04-18 22:46:23 +02003413 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003414}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003415#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3418static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003419{
Janos Follath865b3eb2019-12-16 11:46:15 +00003420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003421 uint32_t lifetime;
3422 size_t ticket_len;
3423 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003424 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003427
Hanno Becker327c93b2018-08-15 13:56:18 +01003428 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003431 return( ret );
3432 }
3433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003437 mbedtls_ssl_send_alert_message(
3438 ssl,
3439 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3440 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003442 }
3443
3444 /*
3445 * struct {
3446 * uint32 ticket_lifetime_hint;
3447 * opaque ticket<0..2^16-1>;
3448 * } NewSessionTicket;
3449 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003450 * 0 . 3 ticket_lifetime_hint
3451 * 4 . 5 ticket_len (n)
3452 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3455 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003458 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3459 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003460 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003461 }
3462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003464
Philippe Antoineb5b25432018-05-11 11:06:29 +02003465 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3466 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003467
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003468 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003473 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3474 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003475 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003476 }
3477
Paul Elliottd48d5c62021-01-07 14:47:05 +00003478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003479
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003480 /* We're not waiting for a NewSessionTicket message any more */
3481 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003483
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003484 /*
3485 * Zero-length ticket means the server changed his mind and doesn't want
3486 * to send a ticket after all, so just forget it
3487 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003488 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003489 return( 0 );
3490
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003491 if( ssl->session != NULL && ssl->session->ticket != NULL )
3492 {
3493 mbedtls_platform_zeroize( ssl->session->ticket,
3494 ssl->session->ticket_len );
3495 mbedtls_free( ssl->session->ticket );
3496 ssl->session->ticket = NULL;
3497 ssl->session->ticket_len = 0;
3498 }
3499
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003500 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3501 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003503 ssl->session_negotiate->ticket = NULL;
3504 ssl->session_negotiate->ticket_len = 0;
3505
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003506 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003507 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3510 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003511 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003512 }
3513
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003514 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003515
3516 ssl->session_negotiate->ticket = ticket;
3517 ssl->session_negotiate->ticket_len = ticket_len;
3518 ssl->session_negotiate->ticket_lifetime = lifetime;
3519
3520 /*
3521 * RFC 5077 section 3.4:
3522 * "If the client receives a session ticket from the server, then it
3523 * discards any Session ID that was sent in the ServerHello."
3524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003526 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003529
3530 return( 0 );
3531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003533
Paul Bakker5121ce52009-01-03 21:22:43 +00003534/*
Paul Bakker1961b702013-01-25 14:49:24 +01003535 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003538{
3539 int ret = 0;
3540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003542 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3544 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003545 ssl->handshake->new_session_ticket != 0 )
3546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003548 }
3549#endif
3550
Paul Bakker1961b702013-01-25 14:49:24 +01003551 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 case MBEDTLS_SSL_HELLO_REQUEST:
3554 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003555 break;
3556
Paul Bakker1961b702013-01-25 14:49:24 +01003557 /*
3558 * ==> ClientHello
3559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003561 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003562 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003563
Paul Bakker1961b702013-01-25 14:49:24 +01003564 /*
3565 * <== ServerHello
3566 * Certificate
3567 * ( ServerKeyExchange )
3568 * ( CertificateRequest )
3569 * ServerHelloDone
3570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003572 ret = ssl_parse_server_hello( ssl );
3573 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3576 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003577 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003580 ret = ssl_parse_server_key_exchange( ssl );
3581 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003584 ret = ssl_parse_certificate_request( ssl );
3585 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003588 ret = ssl_parse_server_hello_done( ssl );
3589 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003590
Paul Bakker1961b702013-01-25 14:49:24 +01003591 /*
3592 * ==> ( Certificate/Alert )
3593 * ClientKeyExchange
3594 * ( CertificateVerify )
3595 * ChangeCipherSpec
3596 * Finished
3597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3599 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003600 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003603 ret = ssl_write_client_key_exchange( ssl );
3604 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003607 ret = ssl_write_certificate_verify( ssl );
3608 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3611 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003612 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 case MBEDTLS_SSL_CLIENT_FINISHED:
3615 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003616 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003617
Paul Bakker1961b702013-01-25 14:49:24 +01003618 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003619 * <== ( NewSessionTicket )
3620 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003621 * Finished
3622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3624 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003625 ret = ssl_parse_new_session_ticket( ssl );
3626 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003627#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3630 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003631 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 case MBEDTLS_SSL_SERVER_FINISHED:
3634 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003635 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 case MBEDTLS_SSL_FLUSH_BUFFERS:
3638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3639 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003640 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3643 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003644 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003645
Paul Bakker1961b702013-01-25 14:49:24 +01003646 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003649 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003650
3651 return( ret );
3652}
Jerry Yuc5aef882021-12-23 20:15:02 +08003653
Jerry Yufb4b6472022-01-27 15:03:26 +08003654#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */