blob: d286764c598244d93c1197c35443c2ebd317a3ef [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
65 if( conf->psk != NULL && conf->psk_len != 0 )
66 return( 1 );
67
68#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020069 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010070 return( 1 );
71#endif /* MBEDTLS_USE_PSA_CRYPTO */
72
73 return( 0 );
74}
Hanno Beckerdfab8e22018-10-23 11:59:34 +010075
Przemek Stekiel99114f32022-04-22 11:20:09 +020076#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
77 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ) || \
78 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) )
Hanno Becker520224e2018-10-26 11:38:07 +010079static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
Hanno Beckerdfab8e22018-10-23 11:59:34 +010080{
81 if( conf->psk_identity == NULL ||
82 conf->psk_identity_len == 0 )
83 {
84 return( 0 );
85 }
86
87 if( conf->psk != NULL && conf->psk_len != 0 )
88 return( 1 );
89
90 return( 0 );
91}
Przemek Stekiel99114f32022-04-22 11:20:09 +020092#endif /* MBEDTLS_USE_PSA_CRYPTO &&
93 ( MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
94 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ) */
Hanno Beckerdfab8e22018-10-23 11:59:34 +010095
Gilles Peskineeccd8882020-03-10 12:19:08 +010096#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +010099static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
100 unsigned char *buf,
101 const unsigned char *end,
102 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100103{
104 unsigned char *p = buf;
105
106 *olen = 0;
107
Hanno Becker40f8b512017-10-12 14:58:55 +0100108 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
109 * initial ClientHello, in which case also adding the renegotiation
110 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +0100112 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100113
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100114 MBEDTLS_SSL_DEBUG_MSG( 3,
115 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
Hanno Becker261602c2017-04-12 14:54:42 +0100117 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +0100118
Paul Bakkerd3edc862013-03-20 16:07:17 +0100119 /*
120 * Secure renegotiation
121 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100122 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
123 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100124
125 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100126 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
127 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100128
129 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
130
131 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100132
133 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100136
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200137#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100138 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100139
Hanno Becker261602c2017-04-12 14:54:42 +0100140static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
141 unsigned char *buf,
142 const unsigned char *end,
143 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100144{
145 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100146 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100147
148 *olen = 0;
149
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100150 MBEDTLS_SSL_DEBUG_MSG( 3,
151 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100152 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100153
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100154 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
155 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100156
157 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100158 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200159
160 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100162
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200163 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100164
165 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100166}
Darryl Green11999bb2018-03-13 15:22:58 +0000167#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100168 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100169
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200170#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100171static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
172 unsigned char *buf,
173 const unsigned char *end,
174 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200175{
Janos Follath865b3eb2019-12-16 11:46:15 +0000176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200177 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200178 size_t kkpp_len;
179
180 *olen = 0;
181
182 /* Skip costly extension if we can't use EC J-PAKE anyway */
183 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100184 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200185
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100186 MBEDTLS_SSL_DEBUG_MSG( 3,
187 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200188
Hanno Becker261602c2017-04-12 14:54:42 +0100189 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200190
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100191 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
192 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200193
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200194 /*
195 * We may need to send ClientHello multiple times for Hello verification.
196 * We don't want to compute fresh values every time (both for performance
197 * and consistency reasons), so cache the extension content.
198 */
199 if( ssl->handshake->ecjpake_cache == NULL ||
200 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200201 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
203
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200204 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100205 p + 2, end - p - 2, &kkpp_len,
206 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200207 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200208 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100209 MBEDTLS_SSL_DEBUG_RET( 1 ,
210 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100211 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200212 }
213
214 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
215 if( ssl->handshake->ecjpake_cache == NULL )
216 {
217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100218 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200219 }
220
221 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
222 ssl->handshake->ecjpake_cache_len = kkpp_len;
223 }
224 else
225 {
226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
227
228 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100229 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200230
231 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200232 }
233
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100234 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
235 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200236
237 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100238
239 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200240}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200241#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000242
Hanno Beckera0e20d02019-05-15 14:03:01 +0100243#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +0100244static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
245 unsigned char *buf,
246 const unsigned char *end,
247 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100248{
249 unsigned char *p = buf;
250 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100251
252 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100253 * Quoting draft-ietf-tls-dtls-connection-id-05
254 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100255 *
256 * struct {
257 * opaque cid<0..2^8-1>;
258 * } ConnectionId;
259 */
260
261 *olen = 0;
262 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
263 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
264 {
Hanno Becker261602c2017-04-12 14:54:42 +0100265 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100266 }
267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
268
269 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
270 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100271 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100272
273 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100274 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
275 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100276 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100277 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
278 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100279
280 *p++ = (uint8_t) ssl->own_cid_len;
281 memcpy( p, ssl->own_cid, ssl->own_cid_len );
282
283 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100284
285 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100286}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100287#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +0100290static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
291 unsigned char *buf,
292 const unsigned char *end,
293 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200294{
295 unsigned char *p = buf;
296
Simon Butcher0fc94e92015-09-28 20:52:04 +0100297 *olen = 0;
298
Hanno Becker261602c2017-04-12 14:54:42 +0100299 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
300 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200301
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100302 MBEDTLS_SSL_DEBUG_MSG( 3,
303 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200304
Hanno Becker261602c2017-04-12 14:54:42 +0100305 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100306
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100307 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
308 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200309
310 *p++ = 0x00;
311 *p++ = 1;
312
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200313 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200314
315 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100316
317 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200318}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +0100322static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
323 unsigned char *buf,
324 const unsigned char *end,
325 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100326{
327 unsigned char *p = buf;
328
Simon Butcher0fc94e92015-09-28 20:52:04 +0100329 *olen = 0;
330
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100331 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100332 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100333
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100334 MBEDTLS_SSL_DEBUG_MSG( 3,
335 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100336
Hanno Becker261602c2017-04-12 14:54:42 +0100337 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100338
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100339 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
340 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100341
342 *p++ = 0x00;
343 *p++ = 0x00;
344
345 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100346
347 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100348}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +0100352static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
353 unsigned char *buf,
354 const unsigned char *end,
355 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200356{
357 unsigned char *p = buf;
358
Simon Butcher0fc94e92015-09-28 20:52:04 +0100359 *olen = 0;
360
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100361 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100362 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200363
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100364 MBEDTLS_SSL_DEBUG_MSG( 3,
365 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200366
Hanno Becker261602c2017-04-12 14:54:42 +0100367 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100368
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100369 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
370 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200371
372 *p++ = 0x00;
373 *p++ = 0x00;
374
375 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100376
377 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +0100382static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
383 unsigned char *buf,
384 const unsigned char *end,
385 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200386{
387 unsigned char *p = buf;
388 size_t tlen = ssl->session_negotiate->ticket_len;
389
Simon Butcher0fc94e92015-09-28 20:52:04 +0100390 *olen = 0;
391
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200392 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100393 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200394
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100395 MBEDTLS_SSL_DEBUG_MSG( 3,
396 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200397
Hanno Becker261602c2017-04-12 14:54:42 +0100398 /* The addition is safe here since the ticket length is 16 bit. */
399 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100400
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100401 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
402 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200403
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100404 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
405 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200406
407 *olen = 4;
408
Simon Butchered997662015-09-28 02:14:30 +0100409 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100410 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200411
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100412 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000413 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200414
415 memcpy( p, ssl->session_negotiate->ticket, tlen );
416
417 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100418
419 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200422
Ron Eldora9788042018-12-05 11:04:31 +0200423#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal77696ee2020-09-22 21:49:40 +0200424static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200425 unsigned char *buf,
426 const unsigned char *end,
427 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100428{
429 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200430 size_t protection_profiles_index = 0, ext_len = 0;
431 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100432
433 *olen = 0;
434
Johan Pascalc3ccd982020-10-28 17:18:18 +0100435 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
436 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
437 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100438 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200439 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100440 }
441
Ron Eldora9788042018-12-05 11:04:31 +0200442 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100443 * uint8 SRTPProtectionProfile[2];
444 *
445 * struct {
446 * SRTPProtectionProfiles SRTPProtectionProfiles;
447 * opaque srtp_mki<0..255>;
448 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100449 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100450 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200451 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200452 {
453 mki_len = ssl->dtls_srtp_info.mki_len;
454 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300455 /* Extension length = 2 bytes for profiles length,
456 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
457 * 1 byte for srtp_mki vector length and the mki_len value
458 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200459 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
460
Johan Pascal77696ee2020-09-22 21:49:40 +0200461 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
462
463 /* Check there is room in the buffer for the extension + 4 bytes
464 * - the extension tag (2 bytes)
465 * - the extension length (2 bytes)
466 */
467 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
468
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100469 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
470 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200471
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100472 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
473 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100474
Ron Eldor3adb9922017-12-21 10:15:08 +0200475 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200476 /* micro-optimization:
477 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
478 * which is lower than 127, so the upper byte of the length is always 0
479 * For the documentation, the more generic code is left in comments
480 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
481 * >> 8 ) & 0xFF );
482 */
483 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100484 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100485
Ron Eldoref72faf2018-07-12 11:54:20 +0300486 for( protection_profiles_index=0;
487 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
488 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100489 {
Johan Pascal43f94902020-09-22 12:25:52 +0200490 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200491 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200492 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200493 {
494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
495 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100496 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
497 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200498 }
499 else
500 {
501 /*
502 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200503 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200504 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200505 MBEDTLS_SSL_DEBUG_MSG( 3,
506 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200507 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200508 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
509 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200510 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100511 }
512 }
513
Ron Eldor591f1622018-01-22 12:30:04 +0200514 *p++ = mki_len & 0xFF;
515
516 if( mki_len != 0 )
517 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200518 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200519 /*
520 * Increment p to point to the current position.
521 */
522 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300523 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
524 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200525 }
526
Ron Eldoref72faf2018-07-12 11:54:20 +0300527 /*
528 * total extension length: extension type (2 bytes)
529 * + extension length (2 bytes)
530 * + protection profile length (2 bytes)
531 * + 2 * number of protection profiles
532 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200533 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300534 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200535 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200536
537 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100538}
539#endif /* MBEDTLS_SSL_DTLS_SRTP */
540
Ronald Cron4079abc2022-02-20 10:35:26 +0100541int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
542 unsigned char *buf,
543 const unsigned char *end,
544 int uses_ec,
545 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100546{
547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
548 unsigned char *p = buf;
549 size_t ext_len = 0;
550
551 (void) ssl;
552 (void) end;
553 (void) uses_ec;
554 (void) ret;
555 (void) ext_len;
556
557 *out_len = 0;
558
559 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
560 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
561#if defined(MBEDTLS_SSL_RENEGOTIATION)
562 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
563 {
564 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
565 return( ret );
566 }
567 p += ext_len;
568#endif
569
570#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
571 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
572 if( uses_ec )
573 {
574 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
575 &ext_len ) ) != 0 )
576 {
577 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
578 return( ret );
579 }
580 p += ext_len;
581 }
582#endif
583
584#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
585 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
586 {
587 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
588 return( ret );
589 }
590 p += ext_len;
591#endif
592
593#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
594 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
595 {
596 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
597 return( ret );
598 }
599 p += ext_len;
600#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
601
602#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
603 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
604 &ext_len ) ) != 0 )
605 {
606 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
607 return( ret );
608 }
609 p += ext_len;
610#endif
611
612#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
613 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
614 {
615 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
616 return( ret );
617 }
618 p += ext_len;
619#endif
620
621#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
622 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
623 {
624 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
625 return( ret );
626 }
627 p += ext_len;
628#endif
629
630#if defined(MBEDTLS_SSL_DTLS_SRTP)
631 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
632 {
633 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
634 return( ret );
635 }
636 p += ext_len;
637#endif
638
639#if defined(MBEDTLS_SSL_SESSION_TICKETS)
640 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
641 {
642 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
643 return( ret );
644 }
645 p += ext_len;
646#endif
647
648 *out_len = p - buf;
649
650 return( 0 );
651}
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200654 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000655 size_t len )
656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_SSL_RENEGOTIATION)
658 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000659 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100660 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000661 if( len != 1 + ssl->verify_data_len * 2 ||
662 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200663 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100664 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200665 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100666 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100669 mbedtls_ssl_send_alert_message(
670 ssl,
671 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
672 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100673 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000674 }
675 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100676 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100678 {
679 if( len != 1 || buf[0] != 0x00 )
680 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200681 MBEDTLS_SSL_DEBUG_MSG( 1,
682 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100683 mbedtls_ssl_send_alert_message(
684 ssl,
685 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
686 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100687 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100688 }
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100691 }
Paul Bakker48916f92012-09-16 19:57:18 +0000692
693 return( 0 );
694}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
697static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200698 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200699 size_t len )
700{
701 /*
702 * server should use the extension only if we did,
703 * and if so the server's value should match ours (and len is always 1)
704 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200705 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200706 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200707 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200708 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100709 MBEDTLS_SSL_DEBUG_MSG( 1,
710 ( "non-matching max fragment length extension" ) );
711 mbedtls_ssl_send_alert_message(
712 ssl,
713 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100714 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
715 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200716 }
717
718 return( 0 );
719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000721
Hanno Beckera0e20d02019-05-15 14:03:01 +0100722#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +0100723static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
724 const unsigned char *buf,
725 size_t len )
726{
727 size_t peer_cid_len;
728
729 if( /* CID extension only makes sense in DTLS */
730 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
731 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100732 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100733 {
Hanno Becker22626482019-05-03 12:46:59 +0100734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100735 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100736 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100737 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100738 }
739
740 if( len == 0 )
741 {
742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
743 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100744 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
745 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100746 }
747
748 peer_cid_len = *buf++;
749 len--;
750
751 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
752 {
Hanno Becker22626482019-05-03 12:46:59 +0100753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100754 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100755 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
756 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100757 }
758
759 if( len != peer_cid_len )
760 {
Hanno Becker22626482019-05-03 12:46:59 +0100761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100762 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100763 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
764 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100765 }
766
Hanno Becker5a299902019-05-03 12:47:49 +0100767 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100768 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
769 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
770
771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
772 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
773
Hanno Beckera8373a12019-04-26 15:37:26 +0100774 return( 0 );
775}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100776#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
779static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100780 const unsigned char *buf,
781 size_t len )
782{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200783 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100784 len != 0 )
785 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100786 MBEDTLS_SSL_DEBUG_MSG( 1,
787 ( "non-matching encrypt-then-MAC extension" ) );
788 mbedtls_ssl_send_alert_message(
789 ssl,
790 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100791 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100792 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100793 }
794
795 ((void) buf);
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100798
799 return( 0 );
800}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
804static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200805 const unsigned char *buf,
806 size_t len )
807{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200808 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200809 len != 0 )
810 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100811 MBEDTLS_SSL_DEBUG_MSG( 1,
812 ( "non-matching extended master secret extension" ) );
813 mbedtls_ssl_send_alert_message(
814 ssl,
815 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100816 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
817 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200818 }
819
820 ((void) buf);
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200823
824 return( 0 );
825}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SSL_SESSION_TICKETS)
829static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200830 const unsigned char *buf,
831 size_t len )
832{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200833 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200834 len != 0 )
835 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100836 MBEDTLS_SSL_DEBUG_MSG( 1,
837 ( "non-matching session ticket extension" ) );
838 mbedtls_ssl_send_alert_message(
839 ssl,
840 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100841 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
842 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200843 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200844
845 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200846
847 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200848
849 return( 0 );
850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200852
Robert Cragie136884c2015-10-02 13:34:31 +0100853#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100854 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200856 const unsigned char *buf,
857 size_t len )
858{
859 size_t list_size;
860 const unsigned char *p;
861
Philippe Antoine747fd532018-05-30 09:13:21 +0200862 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200865 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
866 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100867 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200868 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200869 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200870
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200871 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200872 while( list_size > 0 )
873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
875 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200876 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200877#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
878 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200879 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200880#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
881 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100882#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200883 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
884 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100885#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200887 return( 0 );
888 }
889
890 list_size--;
891 p++;
892 }
893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
896 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100897 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200898}
Darryl Green11999bb2018-03-13 15:22:58 +0000899#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100900 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200901
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200902#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
903static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
904 const unsigned char *buf,
905 size_t len )
906{
Janos Follath865b3eb2019-12-16 11:46:15 +0000907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200908
Hanno Beckere694c3e2017-12-27 21:34:08 +0000909 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200910 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
911 {
912 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
913 return( 0 );
914 }
915
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200916 /* If we got here, we no longer need our cached extension */
917 mbedtls_free( ssl->handshake->ecjpake_cache );
918 ssl->handshake->ecjpake_cache = NULL;
919 ssl->handshake->ecjpake_cache_len = 0;
920
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200921 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
922 buf, len ) ) != 0 )
923 {
924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100925 mbedtls_ssl_send_alert_message(
926 ssl,
927 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
928 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200929 return( ret );
930 }
931
932 return( 0 );
933}
934#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#if defined(MBEDTLS_SSL_ALPN)
937static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200938 const unsigned char *buf, size_t len )
939{
940 size_t list_len, name_len;
941 const char **p;
942
943 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200944 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200945 {
946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100947 mbedtls_ssl_send_alert_message(
948 ssl,
949 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100950 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
951 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200952 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200953
954 /*
955 * opaque ProtocolName<1..2^8-1>;
956 *
957 * struct {
958 * ProtocolName protocol_name_list<2..2^16-1>
959 * } ProtocolNameList;
960 *
961 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
962 */
963
964 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
965 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200966 {
967 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
968 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100969 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200970 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200971
972 list_len = ( buf[0] << 8 ) | buf[1];
973 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200974 {
975 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
976 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100977 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200978 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200979
980 name_len = buf[2];
981 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200982 {
983 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
984 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100985 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200986 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200987
988 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200989 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200990 {
991 if( name_len == strlen( *p ) &&
992 memcmp( buf + 3, *p, name_len ) == 0 )
993 {
994 ssl->alpn_chosen = *p;
995 return( 0 );
996 }
997 }
998
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001000 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1001 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001002 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001005
Johan Pascalb62bb512015-12-03 21:56:45 +01001006#if defined(MBEDTLS_SSL_DTLS_SRTP)
1007static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001008 const unsigned char *buf,
1009 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001010{
Johan Pascal43f94902020-09-22 12:25:52 +02001011 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001012 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001013 uint16_t server_protection_profile_value = 0;
1014
1015 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001016 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1017 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1018 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001019 return( 0 );
1020
Ron Eldora9788042018-12-05 11:04:31 +02001021 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001022 * uint8 SRTPProtectionProfile[2];
1023 *
1024 * struct {
1025 * SRTPProtectionProfiles SRTPProtectionProfiles;
1026 * opaque srtp_mki<0..255>;
1027 * } UseSRTPData;
1028
1029 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1030 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001031 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001032 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001033 {
1034 mki_len = ssl->dtls_srtp_info.mki_len;
1035 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001036
Ron Eldoref72faf2018-07-12 11:54:20 +03001037 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001038 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1039 * + protection profile (2 bytes)
1040 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001041 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001042 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001043 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001044 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001045
1046 /*
1047 * get the server protection profile
1048 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001049
1050 /*
1051 * protection profile length must be 0x0002 as we must have only
1052 * one protection profile in server Hello
1053 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001054 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001055 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001056
1057 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001058 server_protection = mbedtls_ssl_check_srtp_profile_value(
1059 server_protection_profile_value );
1060 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001061 {
Johan Pascal43f94902020-09-22 12:25:52 +02001062 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1063 mbedtls_ssl_get_srtp_profile_as_string(
1064 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001065 }
1066
Johan Pascal43f94902020-09-22 12:25:52 +02001067 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001068
Johan Pascalb62bb512015-12-03 21:56:45 +01001069 /*
1070 * Check we have the server profile in our list
1071 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001072 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001073 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001074 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1075 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001076 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1078 mbedtls_ssl_get_srtp_profile_as_string(
1079 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001080 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001081 }
1082 }
1083
Ron Eldor591f1622018-01-22 12:30:04 +02001084 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001085 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001086 {
1087 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001088 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001089 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001090 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001091
1092 /* If server does not use mki in its reply, make sure the client won't keep
1093 * one as negotiated */
1094 if( len == 5 )
1095 {
1096 ssl->dtls_srtp_info.mki_len = 0;
1097 }
1098
Ron Eldoref72faf2018-07-12 11:54:20 +03001099 /*
1100 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001101 * If the client detects a nonzero-length MKI in the server's response
1102 * that is different than the one the client offered, then the client
1103 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1104 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001105 if( len > 5 && ( buf[4] != mki_len ||
1106 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001107 {
1108 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1109 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001111 }
Ron Eldorb4655392018-07-05 18:25:39 +03001112#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001113 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001114 {
Ron Eldora9788042018-12-05 11:04:31 +02001115 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1116 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001117 }
1118#endif
Ron Eldora9788042018-12-05 11:04:31 +02001119 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001120}
1121#endif /* MBEDTLS_SSL_DTLS_SRTP */
1122
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001123/*
1124 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126#if defined(MBEDTLS_SSL_PROTO_DTLS)
1127static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001130 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001131 unsigned char cookie_len;
1132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001134
Gilles Peskineb64bf062019-09-27 14:02:44 +02001135 /* Check that there is enough room for:
1136 * - 2 bytes of version
1137 * - 1 byte of cookie_len
1138 */
1139 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1140 {
1141 MBEDTLS_SSL_DEBUG_MSG( 1,
1142 ( "incoming HelloVerifyRequest message is too short" ) );
1143 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1144 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001145 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001146 }
1147
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001148 /*
1149 * struct {
1150 * ProtocolVersion server_version;
1151 * opaque cookie<0..2^8-1>;
1152 * } HelloVerifyRequest;
1153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001155 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001156 p += 2;
1157
TRodziewicz2d8800e2021-05-13 19:14:19 +02001158 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001159 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1160 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1161 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001162 */
Glenn Strauss83158112022-04-13 14:59:34 -04001163 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1168 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001169
Hanno Beckerbc000442021-06-24 09:18:19 +01001170 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001171 }
1172
1173 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001174 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1175 {
1176 MBEDTLS_SSL_DEBUG_MSG( 1,
1177 ( "cookie length does not match incoming message size" ) );
1178 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1179 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001180 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001181 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001182 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001183
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001184 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001185
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001186 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1187 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001188 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001190 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001191 }
1192
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001193 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001194 ssl->handshake->verify_cookie_len = cookie_len;
1195
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001196 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1198 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001203
1204 return( 0 );
1205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001207
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)
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001732static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1733 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001734 unsigned char *end )
1735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001737 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001738
Paul Bakker29e1f122013-04-16 13:07:56 +02001739 /*
1740 * Ephemeral DH parameters:
1741 *
1742 * struct {
1743 * opaque dh_p<1..2^16-1>;
1744 * opaque dh_g<1..2^16-1>;
1745 * opaque dh_Ys<1..2^16-1>;
1746 * } ServerDHParams;
1747 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001748 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1749 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001752 return( ret );
1753 }
1754
Gilles Peskine487bbf62021-05-27 22:17:07 +02001755 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001756 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001757 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001759 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001760 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001761 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001762 }
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1765 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1766 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001767
1768 return( ret );
1769}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1771 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001772
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001773#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1774 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1775 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1776#if defined(MBEDTLS_USE_PSA_CRYPTO)
1777static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001778 unsigned char **p,
1779 unsigned char *end )
1780{
1781 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001782 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001783 uint8_t ecpoint_len;
1784 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1785
1786 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001787 * struct {
1788 * ECParameters curve_params;
1789 * ECPoint public;
1790 * } ServerECDHParams;
1791 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001792 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001793 * 2..3 NamedCurve
1794 * 4 ECPoint.len
1795 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001796 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001797 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001798 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001799
1800 /* First byte is curve_type; only named_curve is handled */
1801 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001802 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001803
1804 /* Next two bytes are the namedcurve value */
1805 tls_id = *(*p)++;
1806 tls_id <<= 8;
1807 tls_id |= *(*p)++;
1808
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001809 /* Check it's a curve we offered */
1810 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001811 {
1812 MBEDTLS_SSL_DEBUG_MSG( 2,
1813 ( "bad server key exchange message (ECDHE curve): %u",
1814 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001815 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001816 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001817
Hanno Beckerbb89e272019-01-08 12:54:37 +00001818 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001819 if( ( handshake->ecdh_psa_type =
1820 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001821 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001822 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001823 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001824 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001825
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001826 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001827 ecpoint_len = *(*p)++;
1828 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001829 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001830
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001831 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1832 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001833
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001834 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1835 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001836 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001837
Hanno Beckerbb89e272019-01-08 12:54:37 +00001838 return( 0 );
1839}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001840#else
Neil Armstrong1f198d82022-04-13 15:02:30 +02001841static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1842{
1843 const mbedtls_ecp_curve_info *curve_info;
1844 mbedtls_ecp_group_id grp_id;
1845#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1846 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1847#else
1848 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1849#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001850
Neil Armstrong1f198d82022-04-13 15:02:30 +02001851 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1852 if( curve_info == NULL )
1853 {
1854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1856 }
1857
1858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1859
1860 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1861 return( -1 );
1862
1863 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1864 MBEDTLS_DEBUG_ECDH_QP );
1865
1866 return( 0 );
1867}
1868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001870 unsigned char **p,
1871 unsigned char *end )
1872{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001874
Paul Bakker29e1f122013-04-16 13:07:56 +02001875 /*
1876 * Ephemeral ECDH parameters:
1877 *
1878 * struct {
1879 * ECParameters curve_params;
1880 * ECPoint public;
1881 * } ServerECDHParams;
1882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001884 (const unsigned char **) p, end ) ) != 0 )
1885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001887#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001888 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1889 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001890#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001891 return( ret );
1892 }
1893
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001894 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001895 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001896 MBEDTLS_SSL_DEBUG_MSG( 1,
1897 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001898 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001899 }
1900
Paul Bakker29e1f122013-04-16 13:07:56 +02001901 return( ret );
1902}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001903#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1905 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1906 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001907
Gilles Peskineeccd8882020-03-10 12:19:08 +01001908#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001910 unsigned char **p,
1911 unsigned char *end )
1912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001914 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001915 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001916
1917 /*
1918 * PSK parameters:
1919 *
1920 * opaque psk_identity_hint<0..2^16-1>;
1921 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001922 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001923 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001924 MBEDTLS_SSL_DEBUG_MSG( 1,
1925 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001926 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001927 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001928 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001929 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001930
irwir6527bd62019-09-21 18:51:25 +03001931 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001932 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001933 MBEDTLS_SSL_DEBUG_MSG( 1,
1934 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001935 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001936 }
1937
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001938 /*
1939 * Note: we currently ignore the PKS identity hint, as we only allow one
1940 * PSK to be provisionned on the client. This could be changed later if
1941 * someone needs that feature.
1942 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001943 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001944 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001945
1946 return( ret );
1947}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001948#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1951 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001952/*
1953 * Generate a pre-master secret and encrypt it with the server's RSA key
1954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001956 size_t offset, size_t *olen,
1957 size_t pms_offset )
1958{
Janos Follath865b3eb2019-12-16 11:46:15 +00001959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001960 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001961 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001962 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001963
Angus Grattond8213d02016-05-25 20:56:48 +10001964 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001965 {
1966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1967 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1968 }
1969
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001970 /*
1971 * Generate (part of) the pre-master as
1972 * struct {
1973 * ProtocolVersion client_version;
1974 * opaque random[46];
1975 * } PreMasterSecret;
1976 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001977 mbedtls_ssl_write_version( p, ssl->conf->transport,
1978 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001979
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001980 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001983 return( ret );
1984 }
1985
1986 ssl->handshake->pmslen = 48;
1987
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001988#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1989 peer_pk = &ssl->handshake->peer_pubkey;
1990#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001991 if( ssl->session_negotiate->peer_cert == NULL )
1992 {
Hanno Becker8273df82019-02-06 17:37:32 +00001993 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00001994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00001995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001996 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001997 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1998#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001999
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002000 /*
2001 * Now write it out, encrypted
2002 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002003 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2006 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002007 }
2008
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002009 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002010 p, ssl->handshake->pmslen,
2011 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002012 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002013 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002016 return( ret );
2017 }
2018
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002019 if( len_bytes == 2 )
2020 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002021 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002022 *olen += 2;
2023 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002024
Hanno Beckerae553dd2019-02-08 14:06:00 +00002025#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2026 /* We don't need the peer's public key anymore. Free it. */
2027 mbedtls_pk_free( peer_pk );
2028#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002029 return( 0 );
2030}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2032 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002033
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002034#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2035 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2036 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002038 unsigned char **p,
2039 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 mbedtls_md_type_t *md_alg,
2041 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 *md_alg = MBEDTLS_MD_NONE;
2044 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002045
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002046 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002047 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002048
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002049 /*
2050 * Get hash algorithm
2051 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002052 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2053 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002054 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002055 MBEDTLS_SSL_DEBUG_MSG( 1,
2056 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002057 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002058 }
2059
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002060 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002061 * Get signature algorithm
2062 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002063 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2064 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002065 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002066 MBEDTLS_SSL_DEBUG_MSG( 1,
2067 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002068 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002069 }
2070
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002071 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002072 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002073 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002074 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002075 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002076 MBEDTLS_SSL_DEBUG_MSG( 1,
2077 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002078 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002079 }
2080
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2082 (*p)[1] ) );
2083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2084 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002085 *p += 2;
2086
2087 return( 0 );
2088}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002089#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2090 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2091 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2094 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2095static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002096{
Janos Follath865b3eb2019-12-16 11:46:15 +00002097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002099 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002100
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002101#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2102 peer_pk = &ssl->handshake->peer_pubkey;
2103#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002104 if( ssl->session_negotiate->peer_cert == NULL )
2105 {
Hanno Becker8273df82019-02-06 17:37:32 +00002106 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002108 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002109 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002110 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2111#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002112
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002113 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2116 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002117 }
2118
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002119 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002120
Przemek Stekielea4000f2022-03-16 09:49:33 +01002121#if defined(MBEDTLS_USE_PSA_CRYPTO)
2122 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002123 size_t olen = 0;
2124
2125 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2126 {
2127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2128 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2129 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002130
2131 ssl->handshake->ecdh_psa_type =
2132 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2133 &ecdh_bits ) );
2134
2135 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2136 {
2137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2138 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2139 }
2140
2141 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2142
Przemek Stekielea4000f2022-03-16 09:49:33 +01002143 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002144 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2145 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2146 ssl->handshake->ecdh_psa_peerkey,
2147 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002148
Przemek Stekiel561a4232022-03-16 13:16:24 +01002149 if ( ret != 0 )
2150 {
2151 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2152 return( ret );
2153 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002154
Przemek Stekiel561a4232022-03-16 13:16:24 +01002155 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002156#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2158 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002161 return( ret );
2162 }
2163
2164 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002167 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002168 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002169#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002170#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2171 /* We don't need the peer's public key anymore. Free it,
2172 * so that more RAM is available for upcoming expensive
2173 * operations like ECDHE. */
2174 mbedtls_pk_free( peer_pk );
2175#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2176
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002177 return( ret );
2178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2180 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002183{
Janos Follath865b3eb2019-12-16 11:46:15 +00002184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002185 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002186 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002187 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2192 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 ssl->state++;
2196 return( 0 );
2197 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002198 ((void) p);
2199 ((void) end);
2200#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2203 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2204 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2205 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002206 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002207 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002210 mbedtls_ssl_send_alert_message(
2211 ssl,
2212 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2213 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002214 return( ret );
2215 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002218 ssl->state++;
2219 return( 0 );
2220 }
2221 ((void) p);
2222 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2224 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002225
Gilles Peskineeccd8882020-03-10 12:19:08 +01002226#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002227 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002228 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002229 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002230 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002231 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002232#endif
2233
Hanno Becker327c93b2018-08-15 13:56:18 +01002234 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 return( ret );
2238 }
2239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002243 mbedtls_ssl_send_alert_message(
2244 ssl,
2245 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2246 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 }
2249
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002250 /*
2251 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2252 * doesn't use a psk_identity_hint
2253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2257 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002258 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002259 /* Current message is probably either
2260 * CertificateRequest or ServerHelloDone */
2261 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002262 goto exit;
2263 }
2264
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002265 MBEDTLS_SSL_DEBUG_MSG( 1,
2266 ( "server key exchange message must not be skipped" ) );
2267 mbedtls_ssl_send_alert_message(
2268 ssl,
2269 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2270 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 }
2274
Gilles Peskineeccd8882020-03-10 12:19:08 +01002275#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002276 if( ssl->handshake->ecrs_enabled )
2277 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2278
2279start_processing:
2280#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002282 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002284
Gilles Peskineeccd8882020-03-10 12:19:08 +01002285#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2287 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2288 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2289 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002290 {
2291 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002294 mbedtls_ssl_send_alert_message(
2295 ssl,
2296 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002297 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002298 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002299 }
2300 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002301#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2304 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2305 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2306 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002307 ; /* nothing more to do */
2308 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2310 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2311#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2312 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2313 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2314 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002315 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002316 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002319 mbedtls_ssl_send_alert_message(
2320 ssl,
2321 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2322 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002323 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002324 }
2325 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002326 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2328 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002329#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2330 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2332 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2333 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2334 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002335 {
2336 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002339 mbedtls_ssl_send_alert_message(
2340 ssl,
2341 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2342 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002343 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002344 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002345 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002346 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2348 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2349 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002350#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2351 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2352 {
2353 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2354 p, end - p );
2355 if( ret != 0 )
2356 {
2357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002358 mbedtls_ssl_send_alert_message(
2359 ssl,
2360 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002361 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2362 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002363 }
2364 }
2365 else
2366#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2369 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002370 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002371
Gilles Peskineeccd8882020-03-10 12:19:08 +01002372#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002373 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002374 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002375 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002376#if defined(MBEDTLS_USE_PSA_CRYPTO)
2377 unsigned char hash[PSA_HASH_MAX_SIZE];
2378#else
2379 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2382 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2383 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002384 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002385 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002386
Hanno Beckera6899bb2019-02-06 18:26:03 +00002387 mbedtls_pk_context * peer_pk;
2388
Paul Bakker29e1f122013-04-16 13:07:56 +02002389 /*
2390 * Handle the digitally-signed structure
2391 */
Ronald Cron90915f22022-03-07 11:11:36 +01002392 if( ssl_parse_signature_algorithm( ssl, &p, end,
2393 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002394 {
Ronald Cron90915f22022-03-07 11:11:36 +01002395 MBEDTLS_SSL_DEBUG_MSG( 1,
2396 ( "bad server key exchange message" ) );
2397 mbedtls_ssl_send_alert_message(
2398 ssl,
2399 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2400 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2401 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002402 }
Ronald Cron90915f22022-03-07 11:11:36 +01002403
2404 if( pk_alg !=
2405 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002406 {
Ronald Cron90915f22022-03-07 11:11:36 +01002407 MBEDTLS_SSL_DEBUG_MSG( 1,
2408 ( "bad server key exchange message" ) );
2409 mbedtls_ssl_send_alert_message(
2410 ssl,
2411 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2412 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2413 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002414 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002415
2416 /*
2417 * Read signature
2418 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002419
2420 if( p > end - 2 )
2421 {
2422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002423 mbedtls_ssl_send_alert_message(
2424 ssl,
2425 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2426 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002427 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002428 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002429 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002430 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002431
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002432 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002435 mbedtls_ssl_send_alert_message(
2436 ssl,
2437 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2438 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002439 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002440 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002443
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002444 /*
2445 * Compute the hash that has been signed
2446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002448 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002449 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2450 params, params_len,
2451 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002452 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002453 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002454 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002455 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002459 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002460
Gilles Peskineca1d7422018-04-24 11:53:22 +02002461 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002462
Hanno Beckera6899bb2019-02-06 18:26:03 +00002463#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2464 peer_pk = &ssl->handshake->peer_pubkey;
2465#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002466 if( ssl->session_negotiate->peer_cert == NULL )
2467 {
Hanno Becker8273df82019-02-06 17:37:32 +00002468 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002471 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00002472 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2473#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002474
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002475 /*
2476 * Verify signature
2477 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002478 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002481 mbedtls_ssl_send_alert_message(
2482 ssl,
2483 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2484 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002486 }
2487
Gilles Peskineeccd8882020-03-10 12:19:08 +01002488#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002489 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002490 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002491#endif
2492
Hanno Beckera6899bb2019-02-06 18:26:03 +00002493 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002494 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002495 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002496#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002497 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2498#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002499 mbedtls_ssl_send_alert_message(
2500 ssl,
2501 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2502 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002504#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002505 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2506 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2507#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002508 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002509 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002510
2511#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2512 /* We don't need the peer's public key anymore. Free it,
2513 * so that more RAM is available for upcoming expensive
2514 * operations like ECDHE. */
2515 mbedtls_pk_free( peer_pk );
2516#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002518#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002520exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 ssl->state++;
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
2525 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526}
2527
Gilles Peskineeccd8882020-03-10 12:19:08 +01002528#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002530{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002531 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002532 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002535
Hanno Becker1aa267c2017-04-28 17:08:27 +01002536 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002539 ssl->state++;
2540 return( 0 );
2541 }
2542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2544 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002545}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002546#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002548{
Janos Follath865b3eb2019-12-16 11:46:15 +00002549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002550 unsigned char *buf;
2551 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002552 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002553 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002554 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002555 size_t sig_alg_len;
2556#if defined(MBEDTLS_DEBUG_C)
2557 unsigned char *sig_alg;
2558#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Hanno Becker1aa267c2017-04-28 17:08:27 +01002562 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002565 ssl->state++;
2566 return( 0 );
2567 }
2568
Hanno Becker327c93b2018-08-15 13:56:18 +01002569 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2572 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 }
2574
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002575 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2576 {
2577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002578 mbedtls_ssl_send_alert_message(
2579 ssl,
2580 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2581 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002582 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002585 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002586 ssl->handshake->client_auth =
2587 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002590 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Jerry Yufb28b882022-01-28 11:05:58 +08002592 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002593 {
Johan Pascala89ca862020-08-25 10:03:19 +02002594 /* Current message is probably the ServerHelloDone */
2595 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002596 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002597 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002598
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002599 /*
2600 * struct {
2601 * ClientCertificateType certificate_types<1..2^8-1>;
2602 * SignatureAndHashAlgorithm
2603 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2604 * DistinguishedName certificate_authorities<0..2^16-1>;
2605 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002606 *
2607 * Since we only support a single certificate on clients, let's just
2608 * ignore all the information that's supposed to help us pick a
2609 * certificate.
2610 *
2611 * We could check that our certificate matches the request, and bail out
2612 * if it doesn't, but it's simpler to just send the certificate anyway,
2613 * and give the server the opportunity to decide if it should terminate
2614 * the connection when it doesn't like our certificate.
2615 *
2616 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2617 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002618 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002619 *
2620 * However, we still minimally parse the message to check it is at least
2621 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002622 */
Paul Bakker926af752012-11-23 13:38:07 +01002623 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002624
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002625 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002626 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2627 {
2628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2629 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2630 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002631 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002632 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002634 n = cert_type_len;
2635
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002636 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002637 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002638 * * the length of the signature algorithms field (if minor version of
2639 * SSL is 3),
2640 * * distinguished name length otherwise.
2641 * Both reach at most the index:
2642 * ...hdr_len + 2 + n,
2643 * therefore the buffer length at this point must be greater than that
2644 * regardless of the actual code path.
2645 */
2646 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002649 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2650 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002651 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002652 }
2653
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002654 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002655 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2656 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2657
2658 /*
2659 * The furthest access in buf is in the loop few lines below:
2660 * sig_alg[i + 1],
2661 * where:
2662 * sig_alg = buf + ...hdr_len + 3 + n,
2663 * max(i) = sig_alg_len - 1.
2664 * Therefore the furthest access is:
2665 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2666 * which reduces to:
2667 * buf[...hdr_len + 3 + n + sig_alg_len],
2668 * which is one less than we need the buf to be.
2669 */
2670 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002671 {
Ronald Cron90915f22022-03-07 11:11:36 +01002672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2673 mbedtls_ssl_send_alert_message(
2674 ssl,
2675 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2676 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2677 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002678 }
Paul Bakker926af752012-11-23 13:38:07 +01002679
Ronald Cron90915f22022-03-07 11:11:36 +01002680#if defined(MBEDTLS_DEBUG_C)
2681 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2682 for( size_t i = 0; i < sig_alg_len; i += 2 )
2683 {
2684 MBEDTLS_SSL_DEBUG_MSG( 3,
2685 ( "Supported Signature Algorithm found: %d,%d",
2686 sig_alg[i], sig_alg[i + 1] ) );
2687 }
2688#endif
2689
2690 n += 2 + sig_alg_len;
2691
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002692 /* certificate_authorities */
2693 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2694 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002695
2696 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002697 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002700 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2701 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002702 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002703 }
2704
2705exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
2708 return( 0 );
2709}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002710#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002713{
Janos Follath865b3eb2019-12-16 11:46:15 +00002714 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Hanno Becker327c93b2018-08-15 13:56:18 +01002718 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002720 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2721 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002723
2724 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2725 {
2726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2727 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2728 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2731 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002734 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2735 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002736 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 }
2738
2739 ssl->state++;
2740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002742 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002744#endif
2745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
2748 return( 0 );
2749}
2750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002752{
Janos Follath865b3eb2019-12-16 11:46:15 +00002753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002754
2755 size_t header_len;
2756 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002757 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002758 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2763 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 /*
2766 * DHM key exchange -- send G^X mod P
2767 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002768 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Joe Subbiani6dd73642021-07-19 11:56:54 +01002770 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002771 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002774 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002775 &ssl->out_msg[header_len], content_len,
2776 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 if( ret != 0 )
2778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 return( ret );
2781 }
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2784 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002787 ssl->handshake->premaster,
2788 MBEDTLS_PREMASTER_SIZE,
2789 &ssl->handshake->pmslen,
2790 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 return( ret );
2794 }
2795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 }
2798 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002800#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2801 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2802 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2803 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002804 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002805 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2806 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2807 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002808 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002809#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002810 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2811 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002812 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002813
2814 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2815
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002816 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002817
Hanno Becker0a94a642019-01-11 14:35:30 +00002818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2819
Hanno Becker4a63ed42019-01-08 11:39:35 +00002820 /*
2821 * Generate EC private key for ECDHE exchange.
2822 */
2823
Hanno Becker4a63ed42019-01-08 11:39:35 +00002824 /* The master secret is obtained from the shared ECDH secret by
2825 * applying the TLS 1.2 PRF with a specific salt and label. While
2826 * the PSA Crypto API encourages combining key agreement schemes
2827 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2828 * yet support the provisioning of salt + label to the KDF.
2829 * For the time being, we therefore need to split the computation
2830 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002831 key_attributes = psa_key_attributes_init();
2832 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002833 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002834 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2835 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002836
2837 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002838 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002839 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002840 if( status != PSA_SUCCESS )
2841 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2842
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002843 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002844 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002845 * but we just need to add a length byte before that. */
2846 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2847 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2848 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2849 size_t own_pubkey_len;
2850
Hanno Becker4a63ed42019-01-08 11:39:35 +00002851 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002852 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002853 &own_pubkey_len );
2854 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002855 {
2856 psa_destroy_key( handshake->ecdh_psa_privkey );
2857 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002858 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002859 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002860
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002861 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2862 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002863
Hanno Becker4a63ed42019-01-08 11:39:35 +00002864 /* The ECDH secret is the premaster secret used for key derivation. */
2865
Janos Follathdf3b0892019-08-08 11:12:24 +01002866 /* Compute ECDH shared secret. */
2867 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2868 handshake->ecdh_psa_privkey,
2869 handshake->ecdh_psa_peerkey,
2870 handshake->ecdh_psa_peerkey_len,
2871 ssl->handshake->premaster,
2872 sizeof( ssl->handshake->premaster ),
2873 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002874
Andrzej Kureka0237f82022-02-24 13:24:52 -05002875 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002876 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002877
2878 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2879 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002880#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002881 /*
2882 * ECDH key exchange -- send client public value
2883 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002884 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002885
Gilles Peskineeccd8882020-03-10 12:19:08 +01002886#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002887 if( ssl->handshake->ecrs_enabled )
2888 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002889 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002890 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002891
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002892 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2893 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002894#endif
2895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002897 &content_len,
2898 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002899 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002900 if( ret != 0 )
2901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002903#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002904 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2905 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2906#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002907 return( ret );
2908 }
2909
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002910 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2911 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002912
Gilles Peskineeccd8882020-03-10 12:19:08 +01002913#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002914 if( ssl->handshake->ecrs_enabled )
2915 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002916 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002917 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002918 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002919
2920ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002921 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002922 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002925 &ssl->handshake->pmslen,
2926 ssl->handshake->premaster,
2927 MBEDTLS_MPI_MAX_SIZE,
2928 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002931#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002932 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2933 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2934#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002935 return( ret );
2936 }
2937
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002938 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2939 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02002940#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01002941 }
2942 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2944 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2945 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2946 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002947#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2948 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2949 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2950 {
2951 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2952 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2953 psa_key_attributes_t key_attributes;
2954
2955 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2956
2957 /*
2958 * opaque psk_identity<0..2^16-1>;
2959 */
2960 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002961 /* We don't offer PSK suites if we don't have a PSK,
2962 * and we check that the server's choice is among the
2963 * ciphersuites we offered, so this should never happen. */
2964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002965
Neil Armstrongfc834f22022-03-23 17:54:38 +01002966 /* uint16 to store content length */
2967 const size_t content_len_size = 2;
2968
Neil Armstrong868af822022-03-09 10:26:25 +01002969 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002970
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002971 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002972 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002973 {
2974 MBEDTLS_SSL_DEBUG_MSG( 1,
2975 ( "psk identity too long or SSL buffer too short" ) );
2976 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2977 }
2978
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002979 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002980
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002981 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
2982 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
2983 header_len += content_len_size;
2984
2985 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01002986 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002987 p += ssl->conf->psk_identity_len;
2988
Neil Armstrong868af822022-03-09 10:26:25 +01002989 header_len += ssl->conf->psk_identity_len;
2990
2991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2992
2993 /*
2994 * Generate EC private key for ECDHE exchange.
2995 */
2996
2997 /* The master secret is obtained from the shared ECDH secret by
2998 * applying the TLS 1.2 PRF with a specific salt and label. While
2999 * the PSA Crypto API encourages combining key agreement schemes
3000 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3001 * yet support the provisioning of salt + label to the KDF.
3002 * For the time being, we therefore need to split the computation
3003 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3004 key_attributes = psa_key_attributes_init();
3005 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3006 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3007 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3008 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3009
3010 /* Generate ECDH private key. */
3011 status = psa_generate_key( &key_attributes,
3012 &handshake->ecdh_psa_privkey );
3013 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003014 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003015
3016 /* Export the public part of the ECDH private key from PSA.
3017 * The export format is an ECPoint structure as expected by TLS,
3018 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003019 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003020 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3021 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003022 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003023
3024 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3025 own_pubkey, own_pubkey_max_len,
3026 &own_pubkey_len );
3027 if( status != PSA_SUCCESS )
3028 {
3029 psa_destroy_key( handshake->ecdh_psa_privkey );
3030 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003031 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003032 }
3033
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003034 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003035 content_len = own_pubkey_len + 1;
3036
Neil Armstrong25400452022-03-23 17:44:07 +01003037 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3038 * - a uint16 containing the length (in octets) of the ECDH computation
3039 * - the octet string produced by the ECDH computation
3040 * - a uint16 containing the length (in octets) of the PSK
3041 * - the PSK itself
3042 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003043 unsigned char *pms = ssl->handshake->premaster;
3044 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003045 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003046 /* uint16 to store length (in octets) of the ECDH computation */
3047 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003048 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003049
Neil Armstrong25400452022-03-23 17:44:07 +01003050 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003051 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3052 handshake->ecdh_psa_privkey,
3053 handshake->ecdh_psa_peerkey,
3054 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003055 pms + zlen_size,
3056 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003057 &zlen );
3058
3059 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3060 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3061
Neil Armstrongc530aa62022-03-23 17:45:01 +01003062 if( status != PSA_SUCCESS )
3063 return( psa_ssl_status_to_mbedtls( status ) );
3064 else if( destruction_status != PSA_SUCCESS )
3065 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003066
Neil Armstrong25400452022-03-23 17:44:07 +01003067 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003068 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3069 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003070
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003071 /* In case of opaque psk skip writting psk to pms.
3072 * Opaque key will be handled later. */
3073 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 1 )
3074 {
3075 const unsigned char *psk = NULL;
3076 size_t psk_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003077
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003078 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
3079 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
3080 /*
3081 * This should never happen because the existence of a PSK is always
3082 * checked before calling this function
3083 */
3084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01003085
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003086 /* opaque psk<0..2^16-1>; */
3087 if( (size_t)( pms_end - pms ) < ( 2 + psk_len ) )
3088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstronge18ff952022-04-04 18:34:55 +02003089
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003090 /* Write the PSK length as uint16 */
3091 MBEDTLS_PUT_UINT16_BE( psk_len, pms, 0 );
3092 pms += 2;
Neil Armstrong868af822022-03-09 10:26:25 +01003093
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003094 /* Write the PSK itself */
3095 memcpy( pms, psk, psk_len );
3096 pms += psk_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003097
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003098 ssl->handshake->pmslen = pms - ssl->handshake->premaster;
3099 }
3100 else
3101 {
3102 MBEDTLS_SSL_DEBUG_MSG( 1,
3103 ( "skip PMS generation for opaque ECDHE-PSK" ) );
3104 }
Neil Armstrong868af822022-03-09 10:26:25 +01003105 }
3106 else
3107#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3108 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003109#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003110 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003111 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003112 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003113 * opaque psk_identity<0..2^16-1>;
3114 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003115 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003116 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003117 /* We don't offer PSK suites if we don't have a PSK,
3118 * and we check that the server's choice is among the
3119 * ciphersuites we offered, so this should never happen. */
3120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003121 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003122
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003123 header_len = 4;
3124 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003125
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003126 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003127 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003128 MBEDTLS_SSL_DEBUG_MSG( 1,
3129 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003130 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3131 }
3132
Joe Subbianifbeb6922021-07-16 14:27:50 +01003133 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3134 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003135
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003136 memcpy( ssl->out_msg + header_len,
3137 ssl->conf->psk_identity,
3138 ssl->conf->psk_identity_len );
3139 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3142 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003143 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003144 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003145 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003146 else
3147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3149 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003150 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003151 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3152 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003153 return( ret );
3154 }
3155 else
3156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3158 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003159 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003160 /*
3161 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3162 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003163 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003164
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003165 if( header_len + 2 + content_len >
3166 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003167 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003168 MBEDTLS_SSL_DEBUG_MSG( 1,
3169 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003170 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3171 }
3172
Joe Subbianifbeb6922021-07-16 14:27:50 +01003173 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3174 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003177 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003178 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003179 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003180 if( ret != 0 )
3181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003183 return( ret );
3184 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003185 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003188#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3189 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003191 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003192 /*
3193 * ClientECDiffieHellmanPublic public;
3194 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003195 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3196 &content_len,
3197 &ssl->out_msg[header_len],
3198 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003199 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003200 if( ret != 0 )
3201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003203 return( ret );
3204 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003205
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003206 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3207 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003208 }
3209 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003210#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3213 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003214 }
3215
Hanno Beckerafd311e2018-10-23 15:26:40 +01003216#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3217 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3218 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Hanno Becker520224e2018-10-26 11:38:07 +01003219 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003220 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003221 MBEDTLS_SSL_DEBUG_MSG( 1,
3222 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003223 }
3224 else
Przemek Stekielf2534ba2022-04-05 17:21:14 +02003225 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK &&
3226 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
3227 {
3228 MBEDTLS_SSL_DEBUG_MSG( 1,
3229 ( "skip PMS generation for opaque RSA-PSK" ) );
3230 }
3231 else
Hanno Beckerafd311e2018-10-23 15:26:40 +01003232#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3233 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003235 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003236 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003237 MBEDTLS_SSL_DEBUG_RET( 1,
3238 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003239 return( ret );
3240 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003241 }
3242 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003243#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3245 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003247 header_len = 4;
3248 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3249 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003250 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 }
Paul Bakkered27a042013-04-18 22:46:23 +02003252 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003254#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3255 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3256 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003257 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003258
3259 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003260 ssl->out_msg + header_len,
3261 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3262 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003263 ssl->conf->f_rng, ssl->conf->p_rng );
3264 if( ret != 0 )
3265 {
3266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3267 return( ret );
3268 }
3269
3270 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3271 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3272 ssl->conf->f_rng, ssl->conf->p_rng );
3273 if( ret != 0 )
3274 {
3275 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3276 return( ret );
3277 }
3278 }
3279 else
3280#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003281 {
3282 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003287 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3289 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
3291 ssl->state++;
3292
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003293 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 return( ret );
3297 }
3298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
3301 return( 0 );
3302}
3303
Gilles Peskineeccd8882020-03-10 12:19:08 +01003304#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003306{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003307 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003308 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003316 return( ret );
3317 }
3318
Hanno Becker77adddc2019-02-07 12:32:43 +00003319 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003322 ssl->state++;
3323 return( 0 );
3324 }
3325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003328}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003329#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003333 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003334 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003335 size_t n = 0, offset = 0;
3336 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003337 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003339 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003340 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003341#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3342 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3343#else
3344 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3345#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003348
Gilles Peskineeccd8882020-03-10 12:19:08 +01003349#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003350 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003351 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003352 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003353 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003354 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003355#endif
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003360 return( ret );
3361 }
3362
Hanno Becker77adddc2019-02-07 12:32:43 +00003363 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003366 ssl->state++;
3367 return( 0 );
3368 }
3369
Jerry Yufb28b882022-01-28 11:05:58 +08003370 if( ssl->handshake->client_auth == 0 ||
3371 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003372 {
Johan Pascala89ca862020-08-25 10:03:19 +02003373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3374 ssl->state++;
3375 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 }
3377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003379 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003382 }
3383
3384 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003385 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003387#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003388 if( ssl->handshake->ecrs_enabled )
3389 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3390
3391sign:
3392#endif
3393
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003394 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003395
Ronald Cron90915f22022-03-07 11:11:36 +01003396 /*
3397 * digitally-signed struct {
3398 * opaque handshake_messages[handshake_messages_length];
3399 * };
3400 *
3401 * Taking shortcut here. We assume that the server always allows the
3402 * PRF Hash function and has sent it in the allowed signature
3403 * algorithms list received in the Certificate Request message.
3404 *
3405 * Until we encounter a server that does not, we will take this
3406 * shortcut.
3407 *
3408 * Reason: Otherwise we should have running hashes for SHA512 and
3409 * SHA224 in order to satisfy 'weird' needs from the server
3410 * side.
3411 */
3412 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003413 {
Ronald Cron90915f22022-03-07 11:11:36 +01003414 md_alg = MBEDTLS_MD_SHA384;
3415 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003416 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003417 else
Paul Bakker577e0062013-08-28 11:57:20 +02003418 {
Ronald Cron90915f22022-03-07 11:11:36 +01003419 md_alg = MBEDTLS_MD_SHA256;
3420 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003421 }
Ronald Cron90915f22022-03-07 11:11:36 +01003422 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3423
3424 /* Info from md_alg will be used instead */
3425 hashlen = 0;
3426 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003427
Gilles Peskineeccd8882020-03-10 12:19:08 +01003428#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003429 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003430 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003431#endif
3432
3433 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3434 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003435 ssl->out_msg + 6 + offset,
3436 out_buf_len - 6 - offset,
3437 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003438 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003441#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003442 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3443 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3444#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003445 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003446 }
Paul Bakker926af752012-11-23 13:38:07 +01003447
Joe Subbiani6dd73642021-07-19 11:56:54 +01003448 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003449
Paul Bakker1ef83d62012-04-11 12:09:53 +00003450 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3452 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003453
3454 ssl->state++;
3455
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003456 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 return( ret );
3460 }
3461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003463
Paul Bakkered27a042013-04-18 22:46:23 +02003464 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003466#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3469static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003470{
Janos Follath865b3eb2019-12-16 11:46:15 +00003471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003472 uint32_t lifetime;
3473 size_t ticket_len;
3474 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003475 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003478
Hanno Becker327c93b2018-08-15 13:56:18 +01003479 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003482 return( ret );
3483 }
3484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003488 mbedtls_ssl_send_alert_message(
3489 ssl,
3490 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3491 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003493 }
3494
3495 /*
3496 * struct {
3497 * uint32 ticket_lifetime_hint;
3498 * opaque ticket<0..2^16-1>;
3499 * } NewSessionTicket;
3500 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003501 * 0 . 3 ticket_lifetime_hint
3502 * 4 . 5 ticket_len (n)
3503 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3506 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3510 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003511 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003512 }
3513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003515
Philippe Antoineb5b25432018-05-11 11:06:29 +02003516 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3517 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003518
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003519 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003524 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3525 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003526 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003527 }
3528
Paul Elliottd48d5c62021-01-07 14:47:05 +00003529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003530
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003531 /* We're not waiting for a NewSessionTicket message any more */
3532 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003534
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003535 /*
3536 * Zero-length ticket means the server changed his mind and doesn't want
3537 * to send a ticket after all, so just forget it
3538 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003539 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003540 return( 0 );
3541
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003542 if( ssl->session != NULL && ssl->session->ticket != NULL )
3543 {
3544 mbedtls_platform_zeroize( ssl->session->ticket,
3545 ssl->session->ticket_len );
3546 mbedtls_free( ssl->session->ticket );
3547 ssl->session->ticket = NULL;
3548 ssl->session->ticket_len = 0;
3549 }
3550
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003551 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3552 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003554 ssl->session_negotiate->ticket = NULL;
3555 ssl->session_negotiate->ticket_len = 0;
3556
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003557 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003558 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003560 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3561 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003562 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003563 }
3564
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003565 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003566
3567 ssl->session_negotiate->ticket = ticket;
3568 ssl->session_negotiate->ticket_len = ticket_len;
3569 ssl->session_negotiate->ticket_lifetime = lifetime;
3570
3571 /*
3572 * RFC 5077 section 3.4:
3573 * "If the client receives a session ticket from the server, then it
3574 * discards any Session ID that was sent in the ServerHello."
3575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003577 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003580
3581 return( 0 );
3582}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003584
Paul Bakker5121ce52009-01-03 21:22:43 +00003585/*
Paul Bakker1961b702013-01-25 14:49:24 +01003586 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003589{
3590 int ret = 0;
3591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003593 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3595 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003596 ssl->handshake->new_session_ticket != 0 )
3597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003599 }
3600#endif
3601
Paul Bakker1961b702013-01-25 14:49:24 +01003602 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 case MBEDTLS_SSL_HELLO_REQUEST:
3605 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 break;
3607
Paul Bakker1961b702013-01-25 14:49:24 +01003608 /*
3609 * ==> ClientHello
3610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003612 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003613 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003614
Paul Bakker1961b702013-01-25 14:49:24 +01003615 /*
3616 * <== ServerHello
3617 * Certificate
3618 * ( ServerKeyExchange )
3619 * ( CertificateRequest )
3620 * ServerHelloDone
3621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003623 ret = ssl_parse_server_hello( ssl );
3624 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3627 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003628 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003631 ret = ssl_parse_server_key_exchange( ssl );
3632 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003635 ret = ssl_parse_certificate_request( ssl );
3636 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003639 ret = ssl_parse_server_hello_done( ssl );
3640 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003641
Paul Bakker1961b702013-01-25 14:49:24 +01003642 /*
3643 * ==> ( Certificate/Alert )
3644 * ClientKeyExchange
3645 * ( CertificateVerify )
3646 * ChangeCipherSpec
3647 * Finished
3648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3650 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003651 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003654 ret = ssl_write_client_key_exchange( ssl );
3655 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003658 ret = ssl_write_certificate_verify( ssl );
3659 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3662 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003663 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 case MBEDTLS_SSL_CLIENT_FINISHED:
3666 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003667 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003668
Paul Bakker1961b702013-01-25 14:49:24 +01003669 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003670 * <== ( NewSessionTicket )
3671 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003672 * Finished
3673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3675 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003676 ret = ssl_parse_new_session_ticket( ssl );
3677 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003678#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3681 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003682 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 case MBEDTLS_SSL_SERVER_FINISHED:
3685 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003686 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 case MBEDTLS_SSL_FLUSH_BUFFERS:
3689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3690 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003691 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3694 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003695 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003696
Paul Bakker1961b702013-01-25 14:49:24 +01003697 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
3702 return( ret );
3703}
Jerry Yuc5aef882021-12-23 20:15:02 +08003704
Jerry Yufb4b6472022-01-27 15:03:26 +08003705#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */