blob: a0b0bfc9dcfb7c78295b3c1447bcb2b28739e309 [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)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020078MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +010079static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
80 unsigned char *buf,
81 const unsigned char *end,
82 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010083{
84 unsigned char *p = buf;
85
86 *olen = 0;
87
Hanno Becker40f8b512017-10-12 14:58:55 +010088 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
89 * initial ClientHello, in which case also adding the renegotiation
90 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +010092 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +010093
Hanno Beckerb2fff6d2017-05-08 11:06:19 +010094 MBEDTLS_SSL_DEBUG_MSG( 3,
95 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +010096
Hanno Becker261602c2017-04-12 14:54:42 +010097 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +010098
Paul Bakkerd3edc862013-03-20 16:07:17 +010099 /*
100 * Secure renegotiation
101 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100102 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
103 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100104
105 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100106 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
107 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100108
109 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
110
111 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100112
113 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100114}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200117#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100118 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100119
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200120MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100121static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
122 unsigned char *buf,
123 const unsigned char *end,
124 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100125{
126 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100127 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100128
129 *olen = 0;
130
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100131 MBEDTLS_SSL_DEBUG_MSG( 3,
132 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100133 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100134
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100135 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
136 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100137
138 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100139 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200140
141 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100143
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200144 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100145
146 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100147}
Darryl Green11999bb2018-03-13 15:22:58 +0000148#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100149 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100150
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200151#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200152MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100153static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
154 unsigned char *buf,
155 const unsigned char *end,
156 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200157{
Janos Follath865b3eb2019-12-16 11:46:15 +0000158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200159 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200160 size_t kkpp_len;
161
162 *olen = 0;
163
164 /* Skip costly extension if we can't use EC J-PAKE anyway */
165 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100166 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200167
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100168 MBEDTLS_SSL_DEBUG_MSG( 3,
169 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200170
Hanno Becker261602c2017-04-12 14:54:42 +0100171 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200172
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100173 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
174 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200175
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200176 /*
177 * We may need to send ClientHello multiple times for Hello verification.
178 * We don't want to compute fresh values every time (both for performance
179 * and consistency reasons), so cache the extension content.
180 */
181 if( ssl->handshake->ecjpake_cache == NULL ||
182 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200183 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
185
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200186 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100187 p + 2, end - p - 2, &kkpp_len,
188 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200189 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200190 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100191 MBEDTLS_SSL_DEBUG_RET( 1 ,
192 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100193 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200194 }
195
196 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
197 if( ssl->handshake->ecjpake_cache == NULL )
198 {
199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100200 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200201 }
202
203 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
204 ssl->handshake->ecjpake_cache_len = kkpp_len;
205 }
206 else
207 {
208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
209
210 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100211 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200212
213 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200214 }
215
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100216 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
217 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200218
219 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100220
221 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200222}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200223#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000224
Hanno Beckera0e20d02019-05-15 14:03:01 +0100225#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200226MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100227static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
228 unsigned char *buf,
229 const unsigned char *end,
230 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100231{
232 unsigned char *p = buf;
233 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100234
235 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100236 * Quoting draft-ietf-tls-dtls-connection-id-05
237 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100238 *
239 * struct {
240 * opaque cid<0..2^8-1>;
241 * } ConnectionId;
242 */
243
244 *olen = 0;
245 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
246 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
247 {
Hanno Becker261602c2017-04-12 14:54:42 +0100248 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100249 }
250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
251
252 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
253 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100254 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100255
256 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100257 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
258 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100259 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100260 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
261 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100262
263 *p++ = (uint8_t) ssl->own_cid_len;
264 memcpy( p, ssl->own_cid, ssl->own_cid_len );
265
266 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100267
268 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100269}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100270#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200273MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100274static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
275 unsigned char *buf,
276 const unsigned char *end,
277 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200278{
279 unsigned char *p = buf;
280
Simon Butcher0fc94e92015-09-28 20:52:04 +0100281 *olen = 0;
282
Hanno Becker261602c2017-04-12 14:54:42 +0100283 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
284 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200285
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100286 MBEDTLS_SSL_DEBUG_MSG( 3,
287 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200288
Hanno Becker261602c2017-04-12 14:54:42 +0100289 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100290
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100291 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
292 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200293
294 *p++ = 0x00;
295 *p++ = 1;
296
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200297 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200298
299 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100300
301 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200306MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100307static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
308 unsigned char *buf,
309 const unsigned char *end,
310 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100311{
312 unsigned char *p = buf;
313
Simon Butcher0fc94e92015-09-28 20:52:04 +0100314 *olen = 0;
315
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100316 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100317 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100318
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100319 MBEDTLS_SSL_DEBUG_MSG( 3,
320 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100321
Hanno Becker261602c2017-04-12 14:54:42 +0100322 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100323
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100324 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
325 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100326
327 *p++ = 0x00;
328 *p++ = 0x00;
329
330 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100331
332 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200337MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100338static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
339 unsigned char *buf,
340 const unsigned char *end,
341 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200342{
343 unsigned char *p = buf;
344
Simon Butcher0fc94e92015-09-28 20:52:04 +0100345 *olen = 0;
346
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100347 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100348 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200349
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100350 MBEDTLS_SSL_DEBUG_MSG( 3,
351 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200352
Hanno Becker261602c2017-04-12 14:54:42 +0100353 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100354
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100355 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
356 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200357
358 *p++ = 0x00;
359 *p++ = 0x00;
360
361 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100362
363 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200368MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100369static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
370 unsigned char *buf,
371 const unsigned char *end,
372 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200373{
374 unsigned char *p = buf;
375 size_t tlen = ssl->session_negotiate->ticket_len;
376
Simon Butcher0fc94e92015-09-28 20:52:04 +0100377 *olen = 0;
378
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200379 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100380 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200381
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100382 MBEDTLS_SSL_DEBUG_MSG( 3,
383 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200384
Hanno Becker261602c2017-04-12 14:54:42 +0100385 /* The addition is safe here since the ticket length is 16 bit. */
386 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100387
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100388 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
389 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200390
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100391 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
392 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200393
394 *olen = 4;
395
Simon Butchered997662015-09-28 02:14:30 +0100396 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100397 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200398
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100399 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000400 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200401
402 memcpy( p, ssl->session_negotiate->ticket, tlen );
403
404 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100405
406 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200409
Ron Eldora9788042018-12-05 11:04:31 +0200410#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200411MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascal77696ee2020-09-22 21:49:40 +0200412static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200413 unsigned char *buf,
414 const unsigned char *end,
415 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100416{
417 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200418 size_t protection_profiles_index = 0, ext_len = 0;
419 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100420
421 *olen = 0;
422
Johan Pascalc3ccd982020-10-28 17:18:18 +0100423 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
424 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
425 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100426 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200427 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100428 }
429
Ron Eldora9788042018-12-05 11:04:31 +0200430 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100431 * uint8 SRTPProtectionProfile[2];
432 *
433 * struct {
434 * SRTPProtectionProfiles SRTPProtectionProfiles;
435 * opaque srtp_mki<0..255>;
436 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100437 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100438 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200439 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200440 {
441 mki_len = ssl->dtls_srtp_info.mki_len;
442 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300443 /* Extension length = 2 bytes for profiles length,
444 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
445 * 1 byte for srtp_mki vector length and the mki_len value
446 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200447 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
448
Johan Pascal77696ee2020-09-22 21:49:40 +0200449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
450
451 /* Check there is room in the buffer for the extension + 4 bytes
452 * - the extension tag (2 bytes)
453 * - the extension length (2 bytes)
454 */
455 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
456
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100457 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
458 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200459
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100460 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
461 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100462
Ron Eldor3adb9922017-12-21 10:15:08 +0200463 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200464 /* micro-optimization:
465 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
466 * which is lower than 127, so the upper byte of the length is always 0
467 * For the documentation, the more generic code is left in comments
468 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
469 * >> 8 ) & 0xFF );
470 */
471 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100472 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100473
Ron Eldoref72faf2018-07-12 11:54:20 +0300474 for( protection_profiles_index=0;
475 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
476 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100477 {
Johan Pascal43f94902020-09-22 12:25:52 +0200478 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200479 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200480 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200481 {
482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
483 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100484 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
485 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200486 }
487 else
488 {
489 /*
490 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200491 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200492 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200493 MBEDTLS_SSL_DEBUG_MSG( 3,
494 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200495 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200496 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
497 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200498 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100499 }
500 }
501
Ron Eldor591f1622018-01-22 12:30:04 +0200502 *p++ = mki_len & 0xFF;
503
504 if( mki_len != 0 )
505 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200506 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200507 /*
508 * Increment p to point to the current position.
509 */
510 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300511 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
512 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200513 }
514
Ron Eldoref72faf2018-07-12 11:54:20 +0300515 /*
516 * total extension length: extension type (2 bytes)
517 * + extension length (2 bytes)
518 * + protection profile length (2 bytes)
519 * + 2 * number of protection profiles
520 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200521 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300522 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200523 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200524
525 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100526}
527#endif /* MBEDTLS_SSL_DTLS_SRTP */
528
Ronald Cron4079abc2022-02-20 10:35:26 +0100529int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
530 unsigned char *buf,
531 const unsigned char *end,
532 int uses_ec,
533 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100534{
535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
536 unsigned char *p = buf;
537 size_t ext_len = 0;
538
539 (void) ssl;
540 (void) end;
541 (void) uses_ec;
542 (void) ret;
543 (void) ext_len;
544
545 *out_len = 0;
546
547 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
548 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
549#if defined(MBEDTLS_SSL_RENEGOTIATION)
550 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
551 {
552 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
553 return( ret );
554 }
555 p += ext_len;
556#endif
557
558#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
559 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
560 if( uses_ec )
561 {
562 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
563 &ext_len ) ) != 0 )
564 {
565 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
566 return( ret );
567 }
568 p += ext_len;
569 }
570#endif
571
572#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
573 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
574 {
575 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
576 return( ret );
577 }
578 p += ext_len;
579#endif
580
581#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
582 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
583 {
584 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
585 return( ret );
586 }
587 p += ext_len;
588#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
589
590#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
591 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
592 &ext_len ) ) != 0 )
593 {
594 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
595 return( ret );
596 }
597 p += ext_len;
598#endif
599
600#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
601 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
602 {
603 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
604 return( ret );
605 }
606 p += ext_len;
607#endif
608
609#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
610 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
611 {
612 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
613 return( ret );
614 }
615 p += ext_len;
616#endif
617
618#if defined(MBEDTLS_SSL_DTLS_SRTP)
619 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
620 {
621 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
622 return( ret );
623 }
624 p += ext_len;
625#endif
626
627#if defined(MBEDTLS_SSL_SESSION_TICKETS)
628 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
629 {
630 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
631 return( ret );
632 }
633 p += ext_len;
634#endif
635
636 *out_len = p - buf;
637
638 return( 0 );
639}
640
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200641MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200643 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000644 size_t len )
645{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#if defined(MBEDTLS_SSL_RENEGOTIATION)
647 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000648 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100649 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000650 if( len != 1 + ssl->verify_data_len * 2 ||
651 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200652 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100653 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200654 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100655 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100658 mbedtls_ssl_send_alert_message(
659 ssl,
660 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
661 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100662 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000663 }
664 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100665 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100667 {
668 if( len != 1 || buf[0] != 0x00 )
669 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200670 MBEDTLS_SSL_DEBUG_MSG( 1,
671 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100672 mbedtls_ssl_send_alert_message(
673 ssl,
674 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
675 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100676 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100677 }
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100680 }
Paul Bakker48916f92012-09-16 19:57:18 +0000681
682 return( 0 );
683}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200686MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200688 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200689 size_t len )
690{
691 /*
692 * server should use the extension only if we did,
693 * and if so the server's value should match ours (and len is always 1)
694 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200695 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200696 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200697 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200698 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100699 MBEDTLS_SSL_DEBUG_MSG( 1,
700 ( "non-matching max fragment length extension" ) );
701 mbedtls_ssl_send_alert_message(
702 ssl,
703 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100704 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
705 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200706 }
707
708 return( 0 );
709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000711
Hanno Beckera0e20d02019-05-15 14:03:01 +0100712#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200713MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckera8373a12019-04-26 15:37:26 +0100714static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
715 const unsigned char *buf,
716 size_t len )
717{
718 size_t peer_cid_len;
719
720 if( /* CID extension only makes sense in DTLS */
721 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
722 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100723 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100724 {
Hanno Becker22626482019-05-03 12:46:59 +0100725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100726 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100727 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100728 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100729 }
730
731 if( len == 0 )
732 {
733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
734 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100735 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
736 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100737 }
738
739 peer_cid_len = *buf++;
740 len--;
741
742 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
743 {
Hanno Becker22626482019-05-03 12:46:59 +0100744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100746 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
747 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100748 }
749
750 if( len != peer_cid_len )
751 {
Hanno Becker22626482019-05-03 12:46:59 +0100752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100753 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100754 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
755 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100756 }
757
Hanno Becker5a299902019-05-03 12:47:49 +0100758 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100759 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
760 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
761
762 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
763 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
764
Hanno Beckera8373a12019-04-26 15:37:26 +0100765 return( 0 );
766}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100767#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200770MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100772 const unsigned char *buf,
773 size_t len )
774{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200775 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100776 len != 0 )
777 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100778 MBEDTLS_SSL_DEBUG_MSG( 1,
779 ( "non-matching encrypt-then-MAC extension" ) );
780 mbedtls_ssl_send_alert_message(
781 ssl,
782 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100783 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100784 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100785 }
786
787 ((void) buf);
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100790
791 return( 0 );
792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200796MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200798 const unsigned char *buf,
799 size_t len )
800{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200801 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200802 len != 0 )
803 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100804 MBEDTLS_SSL_DEBUG_MSG( 1,
805 ( "non-matching extended master secret extension" ) );
806 mbedtls_ssl_send_alert_message(
807 ssl,
808 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100809 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
810 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200811 }
812
813 ((void) buf);
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200816
817 return( 0 );
818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200822MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200824 const unsigned char *buf,
825 size_t len )
826{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200827 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200828 len != 0 )
829 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100830 MBEDTLS_SSL_DEBUG_MSG( 1,
831 ( "non-matching session ticket extension" ) );
832 mbedtls_ssl_send_alert_message(
833 ssl,
834 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100835 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
836 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200837 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200838
839 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200840
841 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200842
843 return( 0 );
844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200846
Robert Cragie136884c2015-10-02 13:34:31 +0100847#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100848 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200849MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200851 const unsigned char *buf,
852 size_t len )
853{
854 size_t list_size;
855 const unsigned char *p;
856
Philippe Antoine747fd532018-05-30 09:13:21 +0200857 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200860 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
861 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100862 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200863 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200864 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200865
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200866 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200867 while( list_size > 0 )
868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
870 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200871 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200872#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
873 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200874 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200875#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
876 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100877#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200878 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
879 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100880#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200882 return( 0 );
883 }
884
885 list_size--;
886 p++;
887 }
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200890 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
891 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100892 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200893}
Darryl Green11999bb2018-03-13 15:22:58 +0000894#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100895 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200896
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200897#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200898MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200899static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
900 const unsigned char *buf,
901 size_t len )
902{
Janos Follath865b3eb2019-12-16 11:46:15 +0000903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200904
Hanno Beckere694c3e2017-12-27 21:34:08 +0000905 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200906 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
907 {
908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
909 return( 0 );
910 }
911
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200912 /* If we got here, we no longer need our cached extension */
913 mbedtls_free( ssl->handshake->ecjpake_cache );
914 ssl->handshake->ecjpake_cache = NULL;
915 ssl->handshake->ecjpake_cache_len = 0;
916
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200917 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
918 buf, len ) ) != 0 )
919 {
920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100921 mbedtls_ssl_send_alert_message(
922 ssl,
923 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
924 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200925 return( ret );
926 }
927
928 return( 0 );
929}
930#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200933MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200935 const unsigned char *buf, size_t len )
936{
937 size_t list_len, name_len;
938 const char **p;
939
940 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200941 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200942 {
943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100944 mbedtls_ssl_send_alert_message(
945 ssl,
946 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100947 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
948 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200949 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200950
951 /*
952 * opaque ProtocolName<1..2^8-1>;
953 *
954 * struct {
955 * ProtocolName protocol_name_list<2..2^16-1>
956 * } ProtocolNameList;
957 *
958 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
959 */
960
961 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
962 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200963 {
964 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
965 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100966 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200967 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200968
969 list_len = ( buf[0] << 8 ) | buf[1];
970 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200971 {
972 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
973 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100974 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200975 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200976
977 name_len = buf[2];
978 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200979 {
980 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
981 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100982 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200983 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200984
985 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200986 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200987 {
988 if( name_len == strlen( *p ) &&
989 memcmp( buf + 3, *p, name_len ) == 0 )
990 {
991 ssl->alpn_chosen = *p;
992 return( 0 );
993 }
994 }
995
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200997 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
998 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100999 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001000}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001002
Johan Pascalb62bb512015-12-03 21:56:45 +01001003#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001004MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascalb62bb512015-12-03 21:56:45 +01001005static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001006 const unsigned char *buf,
1007 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001008{
Johan Pascal43f94902020-09-22 12:25:52 +02001009 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001010 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001011 uint16_t server_protection_profile_value = 0;
1012
1013 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001014 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1015 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1016 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001017 return( 0 );
1018
Ron Eldora9788042018-12-05 11:04:31 +02001019 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001020 * uint8 SRTPProtectionProfile[2];
1021 *
1022 * struct {
1023 * SRTPProtectionProfiles SRTPProtectionProfiles;
1024 * opaque srtp_mki<0..255>;
1025 * } UseSRTPData;
1026
1027 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1028 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001029 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001030 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001031 {
1032 mki_len = ssl->dtls_srtp_info.mki_len;
1033 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001034
Ron Eldoref72faf2018-07-12 11:54:20 +03001035 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001036 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1037 * + protection profile (2 bytes)
1038 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001039 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001040 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001041 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001042 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001043
1044 /*
1045 * get the server protection profile
1046 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001047
1048 /*
1049 * protection profile length must be 0x0002 as we must have only
1050 * one protection profile in server Hello
1051 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001052 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001053 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001054
1055 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001056 server_protection = mbedtls_ssl_check_srtp_profile_value(
1057 server_protection_profile_value );
1058 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001059 {
Johan Pascal43f94902020-09-22 12:25:52 +02001060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1061 mbedtls_ssl_get_srtp_profile_as_string(
1062 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001063 }
1064
Johan Pascal43f94902020-09-22 12:25:52 +02001065 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001066
Johan Pascalb62bb512015-12-03 21:56:45 +01001067 /*
1068 * Check we have the server profile in our list
1069 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001070 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001071 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001072 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1073 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001074 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001075 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1076 mbedtls_ssl_get_srtp_profile_as_string(
1077 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001078 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001079 }
1080 }
1081
Ron Eldor591f1622018-01-22 12:30:04 +02001082 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001083 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001084 {
1085 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001086 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001087 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001088 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001089
1090 /* If server does not use mki in its reply, make sure the client won't keep
1091 * one as negotiated */
1092 if( len == 5 )
1093 {
1094 ssl->dtls_srtp_info.mki_len = 0;
1095 }
1096
Ron Eldoref72faf2018-07-12 11:54:20 +03001097 /*
1098 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001099 * If the client detects a nonzero-length MKI in the server's response
1100 * that is different than the one the client offered, then the client
1101 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1102 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001103 if( len > 5 && ( buf[4] != mki_len ||
1104 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001105 {
1106 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1107 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001109 }
Ron Eldorb4655392018-07-05 18:25:39 +03001110#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001111 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001112 {
Ron Eldora9788042018-12-05 11:04:31 +02001113 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1114 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001115 }
1116#endif
Ron Eldora9788042018-12-05 11:04:31 +02001117 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001118}
1119#endif /* MBEDTLS_SSL_DTLS_SRTP */
1120
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001121/*
1122 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001125MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001129 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001130 unsigned char cookie_len;
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001133
Gilles Peskineb64bf062019-09-27 14:02:44 +02001134 /* Check that there is enough room for:
1135 * - 2 bytes of version
1136 * - 1 byte of cookie_len
1137 */
1138 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1139 {
1140 MBEDTLS_SSL_DEBUG_MSG( 1,
1141 ( "incoming HelloVerifyRequest message is too short" ) );
1142 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1143 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001144 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001145 }
1146
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001147 /*
1148 * struct {
1149 * ProtocolVersion server_version;
1150 * opaque cookie<0..2^8-1>;
1151 * } HelloVerifyRequest;
1152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001154 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001155 p += 2;
1156
TRodziewicz2d8800e2021-05-13 19:14:19 +02001157 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001158 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1159 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1160 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001161 */
Glenn Strauss83158112022-04-13 14:59:34 -04001162 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1167 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001168
Hanno Beckerbc000442021-06-24 09:18:19 +01001169 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001170 }
1171
1172 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001173 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1174 {
1175 MBEDTLS_SSL_DEBUG_MSG( 1,
1176 ( "cookie length does not match incoming message size" ) );
1177 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1178 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001179 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001180 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001181 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001182
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001183 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001184
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001185 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1186 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001187 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001189 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001190 }
1191
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001192 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001193 ssl->handshake->verify_cookie_len = cookie_len;
1194
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001195 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1197 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001202
1203 return( 0 );
1204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001206
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001207MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001209{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001210 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001211 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001212 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001213 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001214 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001216 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001217#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001218 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
Hanno Becker327c93b2018-08-15 13:56:18 +01001223 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001225 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 return( ret );
1228 }
1229
Hanno Becker79594fd2019-05-08 09:38:41 +01001230 buf = ssl->in_msg;
1231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_SSL_RENEGOTIATION)
1235 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001236 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001237 ssl->renego_records_seen++;
1238
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001239 if( ssl->conf->renego_max_records >= 0 &&
1240 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001241 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001242 MBEDTLS_SSL_DEBUG_MSG( 1,
1243 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001245 }
1246
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001247 MBEDTLS_SSL_DEBUG_MSG( 1,
1248 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001249
1250 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001252 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001256 mbedtls_ssl_send_alert_message(
1257 ssl,
1258 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1259 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
1262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001264 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001270 return( ssl_parse_hello_verify_request( ssl ) );
1271 }
1272 else
1273 {
1274 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001275 mbedtls_free( ssl->handshake->cookie );
1276 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001277 ssl->handshake->verify_cookie_len = 0;
1278 }
1279 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1283 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001286 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1287 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001288 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
1290
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001291 /*
1292 * 0 . 1 server_version
1293 * 2 . 33 random (maybe including 4 bytes of Unix time)
1294 * 34 . 34 session_id length = n
1295 * 35 . 34+n session_id
1296 * 35+n . 36+n cipher_suite
1297 * 37+n . 37+n compression_method
1298 *
1299 * 38+n . 39+n extensions length (optional)
1300 * 40+n . .. extensions
1301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001303
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001304 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 );
1305 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001306 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Glenn Strauss60bfe602022-03-14 19:04:24 -04001308 if( ssl->tls_version < ssl->conf->min_tls_version ||
1309 ssl->tls_version > ssl->conf->max_tls_version )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001310 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001311 MBEDTLS_SSL_DEBUG_MSG( 1,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001312 ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1313 (unsigned)ssl->conf->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -04001314 (unsigned)ssl->tls_version,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001315 (unsigned)ssl->conf->max_tls_version ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1318 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001319
Hanno Beckerbc000442021-06-24 09:18:19 +01001320 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001321 }
1322
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001324 ( (unsigned long) buf[2] << 24 ) |
1325 ( (unsigned long) buf[3] << 16 ) |
1326 ( (unsigned long) buf[4] << 8 ) |
1327 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001329 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001331 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Paul Bakker48916f92012-09-16 19:57:18 +00001335 if( n > 32 )
1336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001338 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1339 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001340 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001341 }
1342
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001343 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001345 ext_len = ( ( buf[38 + n] << 8 )
1346 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
Paul Bakker48916f92012-09-16 19:57:18 +00001348 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001352 mbedtls_ssl_send_alert_message(
1353 ssl,
1354 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1355 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001356 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001357 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001359 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001360 {
1361 ext_len = 0;
1362 }
1363 else
1364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001366 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1367 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001368 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001371 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001372 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001373
1374 /*
1375 * Read and check compression
1376 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001377 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001379 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001380 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001381 MBEDTLS_SSL_DEBUG_MSG( 1,
1382 ( "server hello, bad compression: %d", comp ) );
1383 mbedtls_ssl_send_alert_message(
1384 ssl,
1385 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1386 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001388 }
1389
Paul Bakker380da532012-04-18 16:10:25 +00001390 /*
1391 * Initialize update checksum functions
1392 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001393 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1394 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001395 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001396 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001397 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001398 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1399 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001401 }
Paul Bakker380da532012-04-18 16:10:25 +00001402
Hanno Beckere694c3e2017-12-27 21:34:08 +00001403 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001404
Paul Elliottd48d5c62021-01-07 14:47:05 +00001405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
1408 /*
1409 * Check if the session can be resumed
1410 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001411 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#if defined(MBEDTLS_SSL_RENEGOTIATION)
1413 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001414#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001415 ssl->session_negotiate->ciphersuite != i ||
1416 ssl->session_negotiate->compression != comp ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001417 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001418 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 {
1420 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001421 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001423 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001424#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001425 ssl->session_negotiate->ciphersuite = i;
1426 ssl->session_negotiate->compression = comp;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001427 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001428 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 }
1430 else
1431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001437 mbedtls_ssl_send_alert_message(
1438 ssl,
1439 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1440 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001441 return( ret );
1442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 }
1444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001446 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447
Paul Elliott3891caf2020-12-17 18:42:40 +00001448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1450 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451
Andrzej Kurek03bac442018-04-25 05:06:07 -04001452 /*
1453 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001454 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 i = 0;
1456 while( 1 )
1457 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001458 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001461 mbedtls_ssl_send_alert_message(
1462 ssl,
1463 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1464 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001465 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 }
1467
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001468 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001469 ssl->session_negotiate->ciphersuite )
1470 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 }
1474
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001475 suite_info = mbedtls_ssl_ciphersuite_from_id(
1476 ssl->session_negotiate->ciphersuite );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001477 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version,
1478 ssl->tls_version ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001479 {
1480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001481 mbedtls_ssl_send_alert_message(
1482 ssl,
1483 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001484 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1485 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001486 }
1487
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001488 MBEDTLS_SSL_DEBUG_MSG( 3,
1489 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001490
Gilles Peskineeccd8882020-03-10 12:19:08 +01001491#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001492 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
Glenn Strauss60bfe602022-03-14 19:04:24 -04001493 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001494 {
1495 ssl->handshake->ecrs_enabled = 1;
1496 }
1497#endif
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 if( comp != MBEDTLS_SSL_COMPRESS_NULL
Paul Bakker2770fbd2012-07-03 13:30:23 +00001500 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001503 mbedtls_ssl_send_alert_message(
1504 ssl,
1505 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1506 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001507 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 }
Paul Bakker48916f92012-09-16 19:57:18 +00001509 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001511 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001512
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001513 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001514 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001515
Paul Bakker48916f92012-09-16 19:57:18 +00001516 while( ext_len )
1517 {
1518 unsigned int ext_id = ( ( ext[0] << 8 )
1519 | ( ext[1] ) );
1520 unsigned int ext_size = ( ( ext[2] << 8 )
1521 | ( ext[3] ) );
1522
1523 if( ext_size + 4 > ext_len )
1524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001526 mbedtls_ssl_send_alert_message(
1527 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1528 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001529 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001530 }
1531
1532 switch( ext_id )
1533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1535 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1536#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001537 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001538#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001539
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001540 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1541 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001542 return( ret );
1543
1544 break;
1545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1547 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001548 MBEDTLS_SSL_DEBUG_MSG( 3,
1549 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001550
1551 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1552 ext + 4, ext_size ) ) != 0 )
1553 {
1554 return( ret );
1555 }
1556
1557 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001559
Hanno Beckera0e20d02019-05-15 14:03:01 +01001560#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001561 case MBEDTLS_TLS_EXT_CID:
1562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1563
1564 if( ( ret = ssl_parse_cid_ext( ssl,
1565 ext + 4,
1566 ext_size ) ) != 0 )
1567 {
1568 return( ret );
1569 }
1570
1571 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001572#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1575 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001577
1578 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1579 ext + 4, ext_size ) ) != 0 )
1580 {
1581 return( ret );
1582 }
1583
1584 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1588 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001589 MBEDTLS_SSL_DEBUG_MSG( 3,
1590 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001591
1592 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1593 ext + 4, ext_size ) ) != 0 )
1594 {
1595 return( ret );
1596 }
1597
1598 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1602 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1603 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001604
1605 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1606 ext + 4, ext_size ) ) != 0 )
1607 {
1608 return( ret );
1609 }
1610
1611 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001613
Robert Cragie136884c2015-10-02 13:34:31 +01001614#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001615 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001617 MBEDTLS_SSL_DEBUG_MSG( 3,
1618 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001619
1620 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1621 ext + 4, ext_size ) ) != 0 )
1622 {
1623 return( ret );
1624 }
1625
1626 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001627#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1628 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001629
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001630#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1631 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1633
1634 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1635 ext + 4, ext_size ) ) != 0 )
1636 {
1637 return( ret );
1638 }
1639
1640 break;
1641#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_SSL_ALPN)
1644 case MBEDTLS_TLS_EXT_ALPN:
1645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001646
Johan Pascal275874b2020-10-27 10:43:53 +01001647 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1648 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001649
1650 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001652
Johan Pascalb62bb512015-12-03 21:56:45 +01001653#if defined(MBEDTLS_SSL_DTLS_SRTP)
1654 case MBEDTLS_TLS_EXT_USE_SRTP:
1655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1656
Johan Pascalc3ccd982020-10-28 17:18:18 +01001657 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1658 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001659
1660 break;
1661#endif /* MBEDTLS_SSL_DTLS_SRTP */
1662
Paul Bakker48916f92012-09-16 19:57:18 +00001663 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001664 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001665 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001666 }
1667
1668 ext_len -= 4 + ext_size;
1669 ext += 4 + ext_size;
1670
1671 if( ext_len > 0 && ext_len < 4 )
1672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001674 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001675 }
1676 }
1677
1678 /*
1679 * Renegotiation security checks
1680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001682 ssl->conf->allow_legacy_renegotiation ==
1683 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001684 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001685 MBEDTLS_SSL_DEBUG_MSG( 1,
1686 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001687 handshake_failure = 1;
1688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_SSL_RENEGOTIATION)
1690 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1691 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001692 renegotiation_info_seen == 0 )
1693 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001694 MBEDTLS_SSL_DEBUG_MSG( 1,
1695 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001696 handshake_failure = 1;
1697 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1699 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001700 ssl->conf->allow_legacy_renegotiation ==
1701 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001704 handshake_failure = 1;
1705 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1707 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001708 renegotiation_info_seen == 1 )
1709 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001710 MBEDTLS_SSL_DEBUG_MSG( 1,
1711 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001712 handshake_failure = 1;
1713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001715
1716 if( handshake_failure == 1 )
1717 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001718 mbedtls_ssl_send_alert_message(
1719 ssl,
1720 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1721 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001722 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001723 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001726
1727 return( 0 );
1728}
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1731 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001732MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001733static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1734 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001735 unsigned char *end )
1736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001738 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001739
Paul Bakker29e1f122013-04-16 13:07:56 +02001740 /*
1741 * Ephemeral DH parameters:
1742 *
1743 * struct {
1744 * opaque dh_p<1..2^16-1>;
1745 * opaque dh_g<1..2^16-1>;
1746 * opaque dh_Ys<1..2^16-1>;
1747 * } ServerDHParams;
1748 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001749 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1750 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001753 return( ret );
1754 }
1755
Gilles Peskine487bbf62021-05-27 22:17:07 +02001756 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001757 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001758 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001760 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001761 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001762 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001763 }
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1766 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1767 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001768
1769 return( ret );
1770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1772 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001773
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001774#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1775 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1776 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1777#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001778MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001779static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001780 unsigned char **p,
1781 unsigned char *end )
1782{
1783 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001784 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001785 uint8_t ecpoint_len;
1786 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1787
1788 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001789 * struct {
1790 * ECParameters curve_params;
1791 * ECPoint public;
1792 * } ServerECDHParams;
1793 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001794 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001795 * 2..3 NamedCurve
1796 * 4 ECPoint.len
1797 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001798 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001799 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001800 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001801
1802 /* First byte is curve_type; only named_curve is handled */
1803 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001804 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001805
1806 /* Next two bytes are the namedcurve value */
1807 tls_id = *(*p)++;
1808 tls_id <<= 8;
1809 tls_id |= *(*p)++;
1810
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001811 /* Check it's a curve we offered */
1812 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001813 {
1814 MBEDTLS_SSL_DEBUG_MSG( 2,
1815 ( "bad server key exchange message (ECDHE curve): %u",
1816 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001817 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001818 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001819
Hanno Beckerbb89e272019-01-08 12:54:37 +00001820 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001821 if( ( handshake->ecdh_psa_type =
1822 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001823 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001824 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001825 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001826 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001827
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001828 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001829 ecpoint_len = *(*p)++;
1830 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001831 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001832
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001833 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1834 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001835
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001836 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1837 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001838 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001839
Hanno Beckerbb89e272019-01-08 12:54:37 +00001840 return( 0 );
1841}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001842#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001843MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrong1f198d82022-04-13 15:02:30 +02001844static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1845{
1846 const mbedtls_ecp_curve_info *curve_info;
1847 mbedtls_ecp_group_id grp_id;
1848#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1849 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1850#else
1851 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1852#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001853
Neil Armstrong1f198d82022-04-13 15:02:30 +02001854 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1855 if( curve_info == NULL )
1856 {
1857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1859 }
1860
1861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1862
1863 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1864 return( -1 );
1865
1866 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1867 MBEDTLS_DEBUG_ECDH_QP );
1868
1869 return( 0 );
1870}
1871
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001872MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001874 unsigned char **p,
1875 unsigned char *end )
1876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001878
Paul Bakker29e1f122013-04-16 13:07:56 +02001879 /*
1880 * Ephemeral ECDH parameters:
1881 *
1882 * struct {
1883 * ECParameters curve_params;
1884 * ECPoint public;
1885 * } ServerECDHParams;
1886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001888 (const unsigned char **) p, end ) ) != 0 )
1889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001891#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001892 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1893 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001894#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001895 return( ret );
1896 }
1897
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001898 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001899 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001900 MBEDTLS_SSL_DEBUG_MSG( 1,
1901 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001902 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001903 }
1904
Paul Bakker29e1f122013-04-16 13:07:56 +02001905 return( ret );
1906}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001907#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1909 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1910 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001911
Gilles Peskineeccd8882020-03-10 12:19:08 +01001912#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001913MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001915 unsigned char **p,
1916 unsigned char *end )
1917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001919 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001920 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001921
1922 /*
1923 * PSK parameters:
1924 *
1925 * opaque psk_identity_hint<0..2^16-1>;
1926 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001927 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001928 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001929 MBEDTLS_SSL_DEBUG_MSG( 1,
1930 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001931 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001932 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001933 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001934 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001935
irwir6527bd62019-09-21 18:51:25 +03001936 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001937 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001938 MBEDTLS_SSL_DEBUG_MSG( 1,
1939 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001940 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001941 }
1942
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001943 /*
1944 * Note: we currently ignore the PKS identity hint, as we only allow one
1945 * PSK to be provisionned on the client. This could be changed later if
1946 * someone needs that feature.
1947 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001948 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001949 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001950
1951 return( ret );
1952}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001953#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1956 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001957/*
1958 * Generate a pre-master secret and encrypt it with the server's RSA key
1959 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001960MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001962 size_t offset, size_t *olen,
1963 size_t pms_offset )
1964{
Janos Follath865b3eb2019-12-16 11:46:15 +00001965 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001966 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001967 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001968 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001969
Angus Grattond8213d02016-05-25 20:56:48 +10001970 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001971 {
1972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1973 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1974 }
1975
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001976 /*
1977 * Generate (part of) the pre-master as
1978 * struct {
1979 * ProtocolVersion client_version;
1980 * opaque random[46];
1981 * } PreMasterSecret;
1982 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001983 mbedtls_ssl_write_version( p, ssl->conf->transport,
1984 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001985
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001986 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001989 return( ret );
1990 }
1991
1992 ssl->handshake->pmslen = 48;
1993
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001994#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1995 peer_pk = &ssl->handshake->peer_pubkey;
1996#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001997 if( ssl->session_negotiate->peer_cert == NULL )
1998 {
Hanno Becker8273df82019-02-06 17:37:32 +00001999 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002002 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002003 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2004#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002005
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002006 /*
2007 * Now write it out, encrypted
2008 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002009 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2012 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002013 }
2014
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002015 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002016 p, ssl->handshake->pmslen,
2017 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002018 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002019 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002022 return( ret );
2023 }
2024
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002025 if( len_bytes == 2 )
2026 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002027 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002028 *olen += 2;
2029 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002030
Hanno Beckerae553dd2019-02-08 14:06:00 +00002031#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2032 /* We don't need the peer's public key anymore. Free it. */
2033 mbedtls_pk_free( peer_pk );
2034#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002035 return( 0 );
2036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2038 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002039
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002040#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2041 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2042 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002043MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002045 unsigned char **p,
2046 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 mbedtls_md_type_t *md_alg,
2048 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 *md_alg = MBEDTLS_MD_NONE;
2051 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002052
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002053 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002054 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002055
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002056 /*
2057 * Get hash algorithm
2058 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002059 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2060 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002061 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002062 MBEDTLS_SSL_DEBUG_MSG( 1,
2063 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002064 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002065 }
2066
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002067 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002068 * Get signature algorithm
2069 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002070 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2071 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002072 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002073 MBEDTLS_SSL_DEBUG_MSG( 1,
2074 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002075 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002076 }
2077
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002078 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002079 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002080 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002081 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002082 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002083 MBEDTLS_SSL_DEBUG_MSG( 1,
2084 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002085 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002086 }
2087
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2089 (*p)[1] ) );
2090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2091 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002092 *p += 2;
2093
2094 return( 0 );
2095}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002096#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2097 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2098 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2101 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002102MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002104{
Janos Follath865b3eb2019-12-16 11:46:15 +00002105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002107 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002108
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002109#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2110 peer_pk = &ssl->handshake->peer_pubkey;
2111#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002112 if( ssl->session_negotiate->peer_cert == NULL )
2113 {
Hanno Becker8273df82019-02-06 17:37:32 +00002114 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002116 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002117 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002118 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2119#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002120
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002121 /* This is a public key, so it can't be opaque, so can_do() is a good
2122 * enough check to ensure pk_ec() is safe to use below. */
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002123 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2126 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002127 }
2128
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002129 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002130
Przemek Stekielea4000f2022-03-16 09:49:33 +01002131#if defined(MBEDTLS_USE_PSA_CRYPTO)
2132 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002133 size_t olen = 0;
2134
2135 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2136 {
2137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2138 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2139 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002140
2141 ssl->handshake->ecdh_psa_type =
2142 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2143 &ecdh_bits ) );
2144
2145 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2146 {
2147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2148 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2149 }
2150
2151 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2152
Przemek Stekielea4000f2022-03-16 09:49:33 +01002153 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002154 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2155 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2156 ssl->handshake->ecdh_psa_peerkey,
2157 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002158
Przemek Stekiel561a4232022-03-16 13:16:24 +01002159 if ( ret != 0 )
2160 {
2161 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2162 return( ret );
2163 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002164
Przemek Stekiel561a4232022-03-16 13:16:24 +01002165 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002166#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2168 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002171 return( ret );
2172 }
2173
2174 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002177 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002178 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002179#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002180#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2181 /* We don't need the peer's public key anymore. Free it,
2182 * so that more RAM is available for upcoming expensive
2183 * operations like ECDHE. */
2184 mbedtls_pk_free( peer_pk );
2185#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2186
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002187 return( ret );
2188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2190 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002191
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002192MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002194{
Janos Follath865b3eb2019-12-16 11:46:15 +00002195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002196 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002197 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002198 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2203 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 ssl->state++;
2207 return( 0 );
2208 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002209 ((void) p);
2210 ((void) end);
2211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2214 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2215 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2216 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002217 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002218 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002221 mbedtls_ssl_send_alert_message(
2222 ssl,
2223 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2224 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002225 return( ret );
2226 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002229 ssl->state++;
2230 return( 0 );
2231 }
2232 ((void) p);
2233 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2235 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002236
Gilles Peskineeccd8882020-03-10 12:19:08 +01002237#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002238 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002239 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002240 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002241 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002242 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002243#endif
2244
Hanno Becker327c93b2018-08-15 13:56:18 +01002245 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 return( ret );
2249 }
2250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002254 mbedtls_ssl_send_alert_message(
2255 ssl,
2256 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2257 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 }
2260
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002261 /*
2262 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2263 * doesn't use a psk_identity_hint
2264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 {
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 )
Paul Bakker188c8de2013-04-19 09:13:37 +02002269 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002270 /* Current message is probably either
2271 * CertificateRequest or ServerHelloDone */
2272 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002273 goto exit;
2274 }
2275
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002276 MBEDTLS_SSL_DEBUG_MSG( 1,
2277 ( "server key exchange message must not be skipped" ) );
2278 mbedtls_ssl_send_alert_message(
2279 ssl,
2280 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2281 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
2285
Gilles Peskineeccd8882020-03-10 12:19:08 +01002286#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002287 if( ssl->handshake->ecrs_enabled )
2288 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2289
2290start_processing:
2291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002293 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002295
Gilles Peskineeccd8882020-03-10 12:19:08 +01002296#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2298 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2299 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2300 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002301 {
2302 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002305 mbedtls_ssl_send_alert_message(
2306 ssl,
2307 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002308 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002309 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002310 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002311 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002312#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2315 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2316 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2317 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002318 ; /* nothing more to do */
2319 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2321 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2322#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2323 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2324 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2325 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002327 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002330 mbedtls_ssl_send_alert_message(
2331 ssl,
2332 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2333 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002334 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002335 }
2336 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002337 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2339 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002340#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2341 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2343 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2344 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2345 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002346 {
2347 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002350 mbedtls_ssl_send_alert_message(
2351 ssl,
2352 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2353 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002354 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002355 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002356 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002357 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2359 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2360 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002361#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2362 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2363 {
2364 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2365 p, end - p );
2366 if( ret != 0 )
2367 {
2368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002369 mbedtls_ssl_send_alert_message(
2370 ssl,
2371 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002372 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2373 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002374 }
2375 }
2376 else
2377#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002381 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002382
Gilles Peskineeccd8882020-03-10 12:19:08 +01002383#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002384 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002385 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002386 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002387#if defined(MBEDTLS_USE_PSA_CRYPTO)
2388 unsigned char hash[PSA_HASH_MAX_SIZE];
2389#else
2390 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2391#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2393 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2394 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002395 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002396 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002397
Hanno Beckera6899bb2019-02-06 18:26:03 +00002398 mbedtls_pk_context * peer_pk;
2399
Paul Bakker29e1f122013-04-16 13:07:56 +02002400 /*
2401 * Handle the digitally-signed structure
2402 */
Ronald Cron90915f22022-03-07 11:11:36 +01002403 if( ssl_parse_signature_algorithm( ssl, &p, end,
2404 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002405 {
Ronald Cron90915f22022-03-07 11:11:36 +01002406 MBEDTLS_SSL_DEBUG_MSG( 1,
2407 ( "bad server key exchange message" ) );
2408 mbedtls_ssl_send_alert_message(
2409 ssl,
2410 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2411 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2412 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002413 }
Ronald Cron90915f22022-03-07 11:11:36 +01002414
2415 if( pk_alg !=
2416 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002417 {
Ronald Cron90915f22022-03-07 11:11:36 +01002418 MBEDTLS_SSL_DEBUG_MSG( 1,
2419 ( "bad server key exchange message" ) );
2420 mbedtls_ssl_send_alert_message(
2421 ssl,
2422 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2423 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2424 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002425 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002426
2427 /*
2428 * Read signature
2429 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002430
2431 if( p > end - 2 )
2432 {
2433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002434 mbedtls_ssl_send_alert_message(
2435 ssl,
2436 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2437 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002438 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002439 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002440 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002441 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002442
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002443 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002446 mbedtls_ssl_send_alert_message(
2447 ssl,
2448 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2449 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002450 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002454
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002455 /*
2456 * Compute the hash that has been signed
2457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002459 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002460 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2461 params, params_len,
2462 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002463 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002464 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002465 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002466 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002470 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002471
Gilles Peskineca1d7422018-04-24 11:53:22 +02002472 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002473
Hanno Beckera6899bb2019-02-06 18:26:03 +00002474#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2475 peer_pk = &ssl->handshake->peer_pubkey;
2476#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002477 if( ssl->session_negotiate->peer_cert == NULL )
2478 {
Hanno Becker8273df82019-02-06 17:37:32 +00002479 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002481 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002482 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00002483 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2484#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002485
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002486 /*
2487 * Verify signature
2488 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002489 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002492 mbedtls_ssl_send_alert_message(
2493 ssl,
2494 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2495 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002497 }
2498
Gilles Peskineeccd8882020-03-10 12:19:08 +01002499#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002500 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002501 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002502#endif
2503
Hanno Beckera6899bb2019-02-06 18:26:03 +00002504 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002505 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002506 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002507#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002508 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2509#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002510 mbedtls_ssl_send_alert_message(
2511 ssl,
2512 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2513 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002515#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002516 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2517 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2518#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002519 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002520 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002521
2522#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2523 /* We don't need the peer's public key anymore. Free it,
2524 * so that more RAM is available for upcoming expensive
2525 * operations like ECDHE. */
2526 mbedtls_pk_free( peer_pk );
2527#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002529#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002531exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 ssl->state++;
2533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
2536 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002537}
2538
Gilles Peskineeccd8882020-03-10 12:19:08 +01002539#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002540MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002542{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002543 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002544 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002547
Hanno Becker1aa267c2017-04-28 17:08:27 +01002548 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002551 ssl->state++;
2552 return( 0 );
2553 }
2554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002557}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002558#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002559MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002561{
Janos Follath865b3eb2019-12-16 11:46:15 +00002562 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002563 unsigned char *buf;
2564 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002565 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002566 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002567 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002568 size_t sig_alg_len;
2569#if defined(MBEDTLS_DEBUG_C)
2570 unsigned char *sig_alg;
2571#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Hanno Becker1aa267c2017-04-28 17:08:27 +01002575 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002578 ssl->state++;
2579 return( 0 );
2580 }
2581
Hanno Becker327c93b2018-08-15 13:56:18 +01002582 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2585 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
2587
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002588 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2589 {
2590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002591 mbedtls_ssl_send_alert_message(
2592 ssl,
2593 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2594 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002595 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002598 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002599 ssl->handshake->client_auth =
2600 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002603 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Jerry Yufb28b882022-01-28 11:05:58 +08002605 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002606 {
Johan Pascala89ca862020-08-25 10:03:19 +02002607 /* Current message is probably the ServerHelloDone */
2608 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002609 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002610 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002611
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002612 /*
2613 * struct {
2614 * ClientCertificateType certificate_types<1..2^8-1>;
2615 * SignatureAndHashAlgorithm
2616 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2617 * DistinguishedName certificate_authorities<0..2^16-1>;
2618 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002619 *
2620 * Since we only support a single certificate on clients, let's just
2621 * ignore all the information that's supposed to help us pick a
2622 * certificate.
2623 *
2624 * We could check that our certificate matches the request, and bail out
2625 * if it doesn't, but it's simpler to just send the certificate anyway,
2626 * and give the server the opportunity to decide if it should terminate
2627 * the connection when it doesn't like our certificate.
2628 *
2629 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2630 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002631 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002632 *
2633 * However, we still minimally parse the message to check it is at least
2634 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002635 */
Paul Bakker926af752012-11-23 13:38:07 +01002636 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002637
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002638 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002639 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2640 {
2641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2642 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2643 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002644 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002645 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002647 n = cert_type_len;
2648
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002649 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002650 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002651 * * the length of the signature algorithms field (if minor version of
2652 * SSL is 3),
2653 * * distinguished name length otherwise.
2654 * Both reach at most the index:
2655 * ...hdr_len + 2 + n,
2656 * therefore the buffer length at this point must be greater than that
2657 * regardless of the actual code path.
2658 */
2659 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002662 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2663 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002664 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002665 }
2666
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002667 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002668 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2669 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2670
2671 /*
2672 * The furthest access in buf is in the loop few lines below:
2673 * sig_alg[i + 1],
2674 * where:
2675 * sig_alg = buf + ...hdr_len + 3 + n,
2676 * max(i) = sig_alg_len - 1.
2677 * Therefore the furthest access is:
2678 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2679 * which reduces to:
2680 * buf[...hdr_len + 3 + n + sig_alg_len],
2681 * which is one less than we need the buf to be.
2682 */
2683 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002684 {
Ronald Cron90915f22022-03-07 11:11:36 +01002685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2686 mbedtls_ssl_send_alert_message(
2687 ssl,
2688 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2689 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2690 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002691 }
Paul Bakker926af752012-11-23 13:38:07 +01002692
Ronald Cron90915f22022-03-07 11:11:36 +01002693#if defined(MBEDTLS_DEBUG_C)
2694 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2695 for( size_t i = 0; i < sig_alg_len; i += 2 )
2696 {
2697 MBEDTLS_SSL_DEBUG_MSG( 3,
2698 ( "Supported Signature Algorithm found: %d,%d",
2699 sig_alg[i], sig_alg[i + 1] ) );
2700 }
2701#endif
2702
2703 n += 2 + sig_alg_len;
2704
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002705 /* certificate_authorities */
2706 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2707 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002708
2709 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002710 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002713 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2714 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002715 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002716 }
2717
2718exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
2721 return( 0 );
2722}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002723#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002725MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002727{
Janos Follath865b3eb2019-12-16 11:46:15 +00002728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Hanno Becker327c93b2018-08-15 13:56:18 +01002732 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002734 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2735 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002737
2738 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2739 {
2740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2741 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2742 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2745 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002748 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2749 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002750 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 }
2752
2753 ssl->state++;
2754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002758#endif
2759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
2762 return( 0 );
2763}
2764
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002765MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002767{
Janos Follath865b3eb2019-12-16 11:46:15 +00002768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002769
2770 size_t header_len;
2771 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002772 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002773 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2778 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 /*
2781 * DHM key exchange -- send G^X mod P
2782 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002783 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Joe Subbiani6dd73642021-07-19 11:56:54 +01002785 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002786 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002789 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002790 &ssl->out_msg[header_len], content_len,
2791 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 if( ret != 0 )
2793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002795 return( ret );
2796 }
2797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2799 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002802 ssl->handshake->premaster,
2803 MBEDTLS_PREMASTER_SIZE,
2804 &ssl->handshake->pmslen,
2805 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 return( ret );
2809 }
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 }
2813 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002815#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2816 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2817 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2818 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002819 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002820 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2821 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2822 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002823 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002824#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002825 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2826 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002827 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002828
2829 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2830
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002831 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002832
Hanno Becker0a94a642019-01-11 14:35:30 +00002833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2834
Hanno Becker4a63ed42019-01-08 11:39:35 +00002835 /*
2836 * Generate EC private key for ECDHE exchange.
2837 */
2838
Hanno Becker4a63ed42019-01-08 11:39:35 +00002839 /* The master secret is obtained from the shared ECDH secret by
2840 * applying the TLS 1.2 PRF with a specific salt and label. While
2841 * the PSA Crypto API encourages combining key agreement schemes
2842 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2843 * yet support the provisioning of salt + label to the KDF.
2844 * For the time being, we therefore need to split the computation
2845 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002846 key_attributes = psa_key_attributes_init();
2847 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002848 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002849 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2850 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002851
2852 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002853 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002854 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002855 if( status != PSA_SUCCESS )
2856 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2857
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002858 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002859 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002860 * but we just need to add a length byte before that. */
2861 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2862 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2863 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2864 size_t own_pubkey_len;
2865
Hanno Becker4a63ed42019-01-08 11:39:35 +00002866 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002867 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002868 &own_pubkey_len );
2869 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002870 {
2871 psa_destroy_key( handshake->ecdh_psa_privkey );
2872 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002873 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002874 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002875
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002876 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2877 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002878
Hanno Becker4a63ed42019-01-08 11:39:35 +00002879 /* The ECDH secret is the premaster secret used for key derivation. */
2880
Janos Follathdf3b0892019-08-08 11:12:24 +01002881 /* Compute ECDH shared secret. */
2882 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2883 handshake->ecdh_psa_privkey,
2884 handshake->ecdh_psa_peerkey,
2885 handshake->ecdh_psa_peerkey_len,
2886 ssl->handshake->premaster,
2887 sizeof( ssl->handshake->premaster ),
2888 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002889
Andrzej Kureka0237f82022-02-24 13:24:52 -05002890 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002891 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002892
2893 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2894 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002895#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002896 /*
2897 * ECDH key exchange -- send client public value
2898 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002899 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002900
Gilles Peskineeccd8882020-03-10 12:19:08 +01002901#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002902 if( ssl->handshake->ecrs_enabled )
2903 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002904 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002905 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002906
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002907 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2908 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002909#endif
2910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002912 &content_len,
2913 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002914 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002915 if( ret != 0 )
2916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002918#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002919 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2920 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2921#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002922 return( ret );
2923 }
2924
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002925 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2926 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002927
Gilles Peskineeccd8882020-03-10 12:19:08 +01002928#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002929 if( ssl->handshake->ecrs_enabled )
2930 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002931 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002932 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002933 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002934
2935ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002936 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002937 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002940 &ssl->handshake->pmslen,
2941 ssl->handshake->premaster,
2942 MBEDTLS_MPI_MAX_SIZE,
2943 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002946#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002947 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2948 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2949#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002950 return( ret );
2951 }
2952
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002953 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2954 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02002955#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01002956 }
2957 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2959 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2960 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2961 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002962#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2963 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2964 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2965 {
2966 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2967 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2968 psa_key_attributes_t key_attributes;
2969
2970 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2971
2972 /*
2973 * opaque psk_identity<0..2^16-1>;
2974 */
2975 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002976 /* We don't offer PSK suites if we don't have a PSK,
2977 * and we check that the server's choice is among the
2978 * ciphersuites we offered, so this should never happen. */
2979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002980
Neil Armstrongfc834f22022-03-23 17:54:38 +01002981 /* uint16 to store content length */
2982 const size_t content_len_size = 2;
2983
Neil Armstrong868af822022-03-09 10:26:25 +01002984 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002985
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002986 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002987 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002988 {
2989 MBEDTLS_SSL_DEBUG_MSG( 1,
2990 ( "psk identity too long or SSL buffer too short" ) );
2991 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2992 }
2993
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002994 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002995
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002996 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
2997 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
2998 header_len += content_len_size;
2999
3000 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003001 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003002 p += ssl->conf->psk_identity_len;
3003
Neil Armstrong868af822022-03-09 10:26:25 +01003004 header_len += ssl->conf->psk_identity_len;
3005
3006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3007
3008 /*
3009 * Generate EC private key for ECDHE exchange.
3010 */
3011
3012 /* The master secret is obtained from the shared ECDH secret by
3013 * applying the TLS 1.2 PRF with a specific salt and label. While
3014 * the PSA Crypto API encourages combining key agreement schemes
3015 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3016 * yet support the provisioning of salt + label to the KDF.
3017 * For the time being, we therefore need to split the computation
3018 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3019 key_attributes = psa_key_attributes_init();
3020 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3021 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3022 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3023 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3024
3025 /* Generate ECDH private key. */
3026 status = psa_generate_key( &key_attributes,
3027 &handshake->ecdh_psa_privkey );
3028 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003029 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003030
3031 /* Export the public part of the ECDH private key from PSA.
3032 * The export format is an ECPoint structure as expected by TLS,
3033 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003034 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003035 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3036 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003037 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003038
3039 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3040 own_pubkey, own_pubkey_max_len,
3041 &own_pubkey_len );
3042 if( status != PSA_SUCCESS )
3043 {
3044 psa_destroy_key( handshake->ecdh_psa_privkey );
3045 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003046 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003047 }
3048
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003049 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003050 content_len = own_pubkey_len + 1;
3051
Neil Armstrong25400452022-03-23 17:44:07 +01003052 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3053 * - a uint16 containing the length (in octets) of the ECDH computation
3054 * - the octet string produced by the ECDH computation
3055 * - a uint16 containing the length (in octets) of the PSK
3056 * - the PSK itself
3057 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003058 unsigned char *pms = ssl->handshake->premaster;
3059 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003060 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003061 /* uint16 to store length (in octets) of the ECDH computation */
3062 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003063 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003064
Neil Armstrong25400452022-03-23 17:44:07 +01003065 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003066 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3067 handshake->ecdh_psa_privkey,
3068 handshake->ecdh_psa_peerkey,
3069 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003070 pms + zlen_size,
3071 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003072 &zlen );
3073
3074 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3075 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3076
Neil Armstrongc530aa62022-03-23 17:45:01 +01003077 if( status != PSA_SUCCESS )
3078 return( psa_ssl_status_to_mbedtls( status ) );
3079 else if( destruction_status != PSA_SUCCESS )
3080 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003081
Neil Armstrong25400452022-03-23 17:44:07 +01003082 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003083 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3084 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003085 }
3086 else
3087#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3088 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003089#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003090 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003091 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003092 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003093 * opaque psk_identity<0..2^16-1>;
3094 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003095 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003096 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003097 /* We don't offer PSK suites if we don't have a PSK,
3098 * and we check that the server's choice is among the
3099 * ciphersuites we offered, so this should never happen. */
3100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003101 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003102
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003103 header_len = 4;
3104 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003105
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003106 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003107 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003108 MBEDTLS_SSL_DEBUG_MSG( 1,
3109 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003110 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3111 }
3112
Joe Subbianifbeb6922021-07-16 14:27:50 +01003113 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3114 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003115
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003116 memcpy( ssl->out_msg + header_len,
3117 ssl->conf->psk_identity,
3118 ssl->conf->psk_identity_len );
3119 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3122 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003123 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003124 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003125 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003126 else
3127#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3129 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003130 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003131 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3132 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003133 return( ret );
3134 }
3135 else
3136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3138 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003139 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003140 /*
3141 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3142 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003143 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003144
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003145 if( header_len + 2 + content_len >
3146 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003147 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003148 MBEDTLS_SSL_DEBUG_MSG( 1,
3149 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003150 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3151 }
3152
Joe Subbianifbeb6922021-07-16 14:27:50 +01003153 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3154 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003157 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003158 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003159 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003160 if( ret != 0 )
3161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003163 return( ret );
3164 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003165
3166#if defined(MBEDTLS_USE_PSA_CRYPTO)
3167 unsigned char *pms = ssl->handshake->premaster;
3168 unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster );
3169 size_t pms_len;
3170
3171 /* Write length only when we know the actual value */
3172 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3173 pms + 2, pms_end - ( pms + 2 ), &pms_len,
3174 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3175 {
3176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3177 return( ret );
3178 }
3179 MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 );
3180 pms += 2 + pms_len;
3181
3182 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3183#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003184 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003185 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003187#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3188 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003190 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003191 /*
3192 * ClientECDiffieHellmanPublic public;
3193 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003194 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3195 &content_len,
3196 &ssl->out_msg[header_len],
3197 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003198 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003199 if( ret != 0 )
3200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003202 return( ret );
3203 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003204
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003205 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3206 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003207 }
3208 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003209#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003213 }
3214
Neil Armstrong80f6f322022-05-03 17:56:38 +02003215#if !defined(MBEDTLS_USE_PSA_CRYPTO)
3216 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3217 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003218 {
Neil Armstrong80f6f322022-05-03 17:56:38 +02003219 MBEDTLS_SSL_DEBUG_RET( 1,
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003220 "mbedtls_ssl_psk_derive_premaster", ret );
Neil Armstrong80f6f322022-05-03 17:56:38 +02003221 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003222 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003223#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003224 }
3225 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003226#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3228 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003230 header_len = 4;
3231 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3232 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003233 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003234 }
Paul Bakkered27a042013-04-18 22:46:23 +02003235 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003237#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3238 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3239 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003240 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003241
3242 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003243 ssl->out_msg + header_len,
3244 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3245 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003246 ssl->conf->f_rng, ssl->conf->p_rng );
3247 if( ret != 0 )
3248 {
3249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3250 return( ret );
3251 }
3252
3253 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3254 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3255 ssl->conf->f_rng, ssl->conf->p_rng );
3256 if( ret != 0 )
3257 {
3258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3259 return( ret );
3260 }
3261 }
3262 else
3263#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003264 {
3265 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003269
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003270 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3272 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
3274 ssl->state++;
3275
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003276 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 return( ret );
3280 }
3281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
3284 return( 0 );
3285}
3286
Gilles Peskineeccd8882020-03-10 12:19:08 +01003287#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003288MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003290{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003291 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003292 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003300 return( ret );
3301 }
3302
Hanno Becker77adddc2019-02-07 12:32:43 +00003303 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003306 ssl->state++;
3307 return( 0 );
3308 }
3309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3311 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003312}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003313#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003314MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003318 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003319 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003320 size_t n = 0, offset = 0;
3321 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003322 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003324 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003325 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003326#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3327 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3328#else
3329 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3330#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003333
Gilles Peskineeccd8882020-03-10 12:19:08 +01003334#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003335 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003336 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003337 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003338 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003339 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003340#endif
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003345 return( ret );
3346 }
3347
Hanno Becker77adddc2019-02-07 12:32:43 +00003348 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003351 ssl->state++;
3352 return( 0 );
3353 }
3354
Jerry Yufb28b882022-01-28 11:05:58 +08003355 if( ssl->handshake->client_auth == 0 ||
3356 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 {
Johan Pascala89ca862020-08-25 10:03:19 +02003358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3359 ssl->state++;
3360 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 }
3362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003367 }
3368
3369 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003370 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003371 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003372#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003373 if( ssl->handshake->ecrs_enabled )
3374 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3375
3376sign:
3377#endif
3378
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003379 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003380
Ronald Cron90915f22022-03-07 11:11:36 +01003381 /*
3382 * digitally-signed struct {
3383 * opaque handshake_messages[handshake_messages_length];
3384 * };
3385 *
3386 * Taking shortcut here. We assume that the server always allows the
3387 * PRF Hash function and has sent it in the allowed signature
3388 * algorithms list received in the Certificate Request message.
3389 *
3390 * Until we encounter a server that does not, we will take this
3391 * shortcut.
3392 *
3393 * Reason: Otherwise we should have running hashes for SHA512 and
3394 * SHA224 in order to satisfy 'weird' needs from the server
3395 * side.
3396 */
3397 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003398 {
Ronald Cron90915f22022-03-07 11:11:36 +01003399 md_alg = MBEDTLS_MD_SHA384;
3400 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003401 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003402 else
Paul Bakker577e0062013-08-28 11:57:20 +02003403 {
Ronald Cron90915f22022-03-07 11:11:36 +01003404 md_alg = MBEDTLS_MD_SHA256;
3405 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003406 }
Ronald Cron90915f22022-03-07 11:11:36 +01003407 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3408
3409 /* Info from md_alg will be used instead */
3410 hashlen = 0;
3411 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003412
Gilles Peskineeccd8882020-03-10 12:19:08 +01003413#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003414 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003415 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003416#endif
3417
3418 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3419 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003420 ssl->out_msg + 6 + offset,
3421 out_buf_len - 6 - offset,
3422 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003423 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003426#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003427 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3428 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3429#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003430 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003431 }
Paul Bakker926af752012-11-23 13:38:07 +01003432
Joe Subbiani6dd73642021-07-19 11:56:54 +01003433 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003434
Paul Bakker1ef83d62012-04-11 12:09:53 +00003435 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3437 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003438
3439 ssl->state++;
3440
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003441 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 return( ret );
3445 }
3446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003448
Paul Bakkered27a042013-04-18 22:46:23 +02003449 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003450}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003451#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003454MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003456{
Janos Follath865b3eb2019-12-16 11:46:15 +00003457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003458 uint32_t lifetime;
3459 size_t ticket_len;
3460 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003461 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003464
Hanno Becker327c93b2018-08-15 13:56:18 +01003465 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003468 return( ret );
3469 }
3470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003474 mbedtls_ssl_send_alert_message(
3475 ssl,
3476 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3477 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003479 }
3480
3481 /*
3482 * struct {
3483 * uint32 ticket_lifetime_hint;
3484 * opaque ticket<0..2^16-1>;
3485 * } NewSessionTicket;
3486 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003487 * 0 . 3 ticket_lifetime_hint
3488 * 4 . 5 ticket_len (n)
3489 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3492 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003495 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3496 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003497 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003498 }
3499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003501
Philippe Antoineb5b25432018-05-11 11:06:29 +02003502 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3503 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003504
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003505 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003510 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3511 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003512 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003513 }
3514
Paul Elliottd48d5c62021-01-07 14:47:05 +00003515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003516
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003517 /* We're not waiting for a NewSessionTicket message any more */
3518 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003520
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003521 /*
3522 * Zero-length ticket means the server changed his mind and doesn't want
3523 * to send a ticket after all, so just forget it
3524 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003525 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003526 return( 0 );
3527
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003528 if( ssl->session != NULL && ssl->session->ticket != NULL )
3529 {
3530 mbedtls_platform_zeroize( ssl->session->ticket,
3531 ssl->session->ticket_len );
3532 mbedtls_free( ssl->session->ticket );
3533 ssl->session->ticket = NULL;
3534 ssl->session->ticket_len = 0;
3535 }
3536
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003537 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3538 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003540 ssl->session_negotiate->ticket = NULL;
3541 ssl->session_negotiate->ticket_len = 0;
3542
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003543 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003544 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003546 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3547 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003549 }
3550
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003551 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003552
3553 ssl->session_negotiate->ticket = ticket;
3554 ssl->session_negotiate->ticket_len = ticket_len;
3555 ssl->session_negotiate->ticket_lifetime = lifetime;
3556
3557 /*
3558 * RFC 5077 section 3.4:
3559 * "If the client receives a session ticket from the server, then it
3560 * discards any Session ID that was sent in the ServerHello."
3561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003563 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003566
3567 return( 0 );
3568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003570
Paul Bakker5121ce52009-01-03 21:22:43 +00003571/*
Paul Bakker1961b702013-01-25 14:49:24 +01003572 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003575{
3576 int ret = 0;
3577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003579 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3581 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003582 ssl->handshake->new_session_ticket != 0 )
3583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003585 }
3586#endif
3587
Paul Bakker1961b702013-01-25 14:49:24 +01003588 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 case MBEDTLS_SSL_HELLO_REQUEST:
3591 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003592 break;
3593
Paul Bakker1961b702013-01-25 14:49:24 +01003594 /*
3595 * ==> ClientHello
3596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003598 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003599 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003600
Paul Bakker1961b702013-01-25 14:49:24 +01003601 /*
3602 * <== ServerHello
3603 * Certificate
3604 * ( ServerKeyExchange )
3605 * ( CertificateRequest )
3606 * ServerHelloDone
3607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003609 ret = ssl_parse_server_hello( ssl );
3610 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3613 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003614 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003617 ret = ssl_parse_server_key_exchange( ssl );
3618 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003621 ret = ssl_parse_certificate_request( ssl );
3622 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003625 ret = ssl_parse_server_hello_done( ssl );
3626 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
Paul Bakker1961b702013-01-25 14:49:24 +01003628 /*
3629 * ==> ( Certificate/Alert )
3630 * ClientKeyExchange
3631 * ( CertificateVerify )
3632 * ChangeCipherSpec
3633 * Finished
3634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3636 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003637 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003640 ret = ssl_write_client_key_exchange( ssl );
3641 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003644 ret = ssl_write_certificate_verify( ssl );
3645 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3648 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003649 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 case MBEDTLS_SSL_CLIENT_FINISHED:
3652 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003653 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003654
Paul Bakker1961b702013-01-25 14:49:24 +01003655 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003656 * <== ( NewSessionTicket )
3657 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003658 * Finished
3659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3661 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003662 ret = ssl_parse_new_session_ticket( ssl );
3663 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003664#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3667 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003668 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 case MBEDTLS_SSL_SERVER_FINISHED:
3671 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003672 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 case MBEDTLS_SSL_FLUSH_BUFFERS:
3675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3676 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003677 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3680 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003681 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003682
Paul Bakker1961b702013-01-25 14:49:24 +01003683 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
3688 return( ret );
3689}
Jerry Yuc5aef882021-12-23 20:15:02 +08003690
Jerry Yufb4b6472022-01-27 15:03:26 +08003691#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */