blob: b875fac53bd981d412b790e63dcc6680b347f2df [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é-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020025
SimonBd5800b72016-04-26 07:43:27 +010026#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010027#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000028#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000029#include "mbedtls/debug.h"
30#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020031#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010032
Hanno Beckerbb89e272019-01-08 12:54:37 +000033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020035#include "psa/crypto.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050036#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
37 psa_to_ssl_errors, \
38 psa_generic_status_to_mbedtls)
Hanno Beckerbb89e272019-01-08 12:54:37 +000039#endif /* MBEDTLS_USE_PSA_CRYPTO */
40
SimonBd5800b72016-04-26 07:43:27 +010041#include <string.h>
42
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020043#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010046#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050050#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020051#endif
52
Andrzej Kurek0ce59212022-08-17 07:54:34 -040053#include "hash_info.h"
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020056MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010057static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
58 unsigned char *buf,
59 const unsigned char *end,
60 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010061{
62 unsigned char *p = buf;
63
64 *olen = 0;
65
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010066 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010067 * initial ClientHello, in which case also adding the renegotiation
68 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
70 return 0;
71 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 MBEDTLS_SSL_DEBUG_MSG(3,
74 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010077
Paul Bakkerd3edc862013-03-20 16:07:17 +010078 /*
79 * Secure renegotiation
80 */
Gilles Peskine449bd832023-01-11 14:50:10 +010081 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010082 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010083
84 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
86 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010089
90 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010095
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +020096#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010097 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010098
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020099MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100100static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
101 unsigned char *buf,
102 const unsigned char *end,
103 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100104{
105 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100106 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100107
108 *olen = 0;
109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 MBEDTLS_SSL_DEBUG_MSG(3,
111 ("client hello, adding supported_point_formats extension"));
112 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100115 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
117 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200119
120 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100122
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200123 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100126}
Darryl Green11999bb2018-03-13 15:22:58 +0000127#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100128 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100129
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200130#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200131MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100132static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
133 unsigned char *buf,
134 const unsigned char *end,
135 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200136{
Janos Follath865b3eb2019-12-16 11:46:15 +0000137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200138 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100139 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200140
141 *olen = 0;
142
143 /* Skip costly extension if we can't use EC J-PAKE anyway */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
146 return 0;
147 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200148#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
150 return 0;
151 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200152#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 MBEDTLS_SSL_DEBUG_MSG(3,
155 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100160 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200161
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200162 /*
163 * We may need to send ClientHello multiple times for Hello verification.
164 * We don't want to compute fresh values every time (both for performance
165 * and consistency reasons), so cache the extension content.
166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (ssl->handshake->ecjpake_cache == NULL ||
168 ssl->handshake->ecjpake_cache_len == 0) {
169 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200170
Neil Armstrongca7d5062022-05-31 14:43:23 +0200171#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100172 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 p + 2, end - p - 2, &kkpp_len,
174 MBEDTLS_ECJPAKE_ROUND_ONE);
175 if (ret != 0) {
176 psa_destroy_key(ssl->handshake->psa_pake_password);
177 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
178 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
179 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200180 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200181#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
183 p + 2, end - p - 2, &kkpp_len,
184 ssl->conf->f_rng, ssl->conf->p_rng);
185 if (ret != 0) {
186 MBEDTLS_SSL_DEBUG_RET(1,
187 "mbedtls_ecjpake_write_round_one", ret);
188 return ret;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200189 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200190#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
193 if (ssl->handshake->ecjpake_cache == NULL) {
194 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
195 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200196 }
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200199 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 } else {
201 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200202
203 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200207 }
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100210 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200211
212 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200215}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200216#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000217
Hanno Beckera0e20d02019-05-15 14:03:01 +0100218#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200219MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100220static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
221 unsigned char *buf,
222 const unsigned char *end,
223 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100224{
225 unsigned char *p = buf;
226 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100227
228 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100229 * struct {
230 * opaque cid<0..2^8-1>;
231 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100233
234 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
236 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
237 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100238 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100240
241 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
242 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100244
245 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100247 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100248 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100250 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100251
252 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100254
255 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100258}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100259#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200262MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100263static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
264 unsigned char *buf,
265 const unsigned char *end,
266 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200267{
268 unsigned char *p = buf;
269
Simon Butcher0fc94e92015-09-28 20:52:04 +0100270 *olen = 0;
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
273 return 0;
274 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 MBEDTLS_SSL_DEBUG_MSG(3,
277 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100282 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200283
284 *p++ = 0x00;
285 *p++ = 1;
286
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200287 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200288
289 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200296MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100297static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
298 unsigned char *buf,
299 const unsigned char *end,
300 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100301{
302 unsigned char *p = buf;
303
Simon Butcher0fc94e92015-09-28 20:52:04 +0100304 *olen = 0;
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
307 return 0;
308 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 MBEDTLS_SSL_DEBUG_MSG(3,
311 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100316 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100317
318 *p++ = 0x00;
319 *p++ = 0x00;
320
321 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200328MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
330 unsigned char *buf,
331 const unsigned char *end,
332 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200333{
334 unsigned char *p = buf;
335
Simon Butcher0fc94e92015-09-28 20:52:04 +0100336 *olen = 0;
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
339 return 0;
340 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 MBEDTLS_SSL_DEBUG_MSG(3,
343 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100348 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200349
350 *p++ = 0x00;
351 *p++ = 0x00;
352
353 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200360MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100361static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
362 unsigned char *buf,
363 const unsigned char *end,
364 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200365{
366 unsigned char *p = buf;
367 size_t tlen = ssl->session_negotiate->ticket_len;
368
Simon Butcher0fc94e92015-09-28 20:52:04 +0100369 *olen = 0;
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
372 return 0;
373 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 MBEDTLS_SSL_DEBUG_MSG(3,
376 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200377
Hanno Becker261602c2017-04-12 14:54:42 +0100378 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100382 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100385 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200386
387 *olen = 4;
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
390 return 0;
391 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 MBEDTLS_SSL_DEBUG_MSG(3,
394 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200397
398 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200403
Ron Eldora9788042018-12-05 11:04:31 +0200404#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200405MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100406static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
407 unsigned char *buf,
408 const unsigned char *end,
409 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100410{
411 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200412 size_t protection_profiles_index = 0, ext_len = 0;
413 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100414
415 *olen = 0;
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
418 (ssl->conf->dtls_srtp_profile_list == NULL) ||
419 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
420 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100421 }
422
Ron Eldora9788042018-12-05 11:04:31 +0200423 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100424 * uint8 SRTPProtectionProfile[2];
425 *
426 * struct {
427 * SRTPProtectionProfiles SRTPProtectionProfiles;
428 * opaque srtp_mki<0..255>;
429 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100430 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100431 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200433 mki_len = ssl->dtls_srtp_info.mki_len;
434 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300435 /* Extension length = 2 bytes for profiles length,
436 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
437 * 1 byte for srtp_mki vector length and the mki_len value
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200442
443 /* Check there is room in the buffer for the extension + 4 bytes
444 * - the extension tag (2 bytes)
445 * - the extension length (2 bytes)
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100450 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100453 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100454
Ron Eldor3adb9922017-12-21 10:15:08 +0200455 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200456 /* micro-optimization:
457 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
458 * which is lower than 127, so the upper byte of the length is always 0
459 * For the documentation, the more generic code is left in comments
460 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
461 * >> 8 ) & 0xFF );
462 */
463 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300467 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200469 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
471 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
472 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
473 profile_value));
474 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100475 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200477 /*
478 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200479 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200480 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 MBEDTLS_SSL_DEBUG_MSG(3,
482 ("client hello, "
483 "illegal DTLS-SRTP protection profile %d",
484 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
485 ));
486 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100487 }
488 }
489
Ron Eldor591f1622018-01-22 12:30:04 +0200490 *p++ = mki_len & 0xFF;
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (mki_len != 0) {
493 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200494 /*
495 * Increment p to point to the current position.
496 */
497 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
499 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200500 }
501
Ron Eldoref72faf2018-07-12 11:54:20 +0300502 /*
503 * total extension length: extension type (2 bytes)
504 * + extension length (2 bytes)
505 * + protection profile length (2 bytes)
506 * + 2 * number of protection profiles
507 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200508 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300509 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200510 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100513}
514#endif /* MBEDTLS_SSL_DTLS_SRTP */
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
517 unsigned char *buf,
518 const unsigned char *end,
519 int uses_ec,
520 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100521{
522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
523 unsigned char *p = buf;
524 size_t ext_len = 0;
525
526 (void) ssl;
527 (void) end;
528 (void) uses_ec;
529 (void) ret;
530 (void) ext_len;
531
532 *out_len = 0;
533
534 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
535 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
536#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
538 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
539 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100540 }
541 p += ext_len;
542#endif
543
544#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
545 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (uses_ec) {
547 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
548 &ext_len)) != 0) {
549 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
550 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100551 }
552 p += ext_len;
553 }
554#endif
555
556#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
558 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
559 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100560 }
561 p += ext_len;
562#endif
563
564#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
566 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
567 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100568 }
569 p += ext_len;
570#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
571
572#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
574 &ext_len)) != 0) {
575 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
576 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100577 }
578 p += ext_len;
579#endif
580
581#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
583 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
584 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100585 }
586 p += ext_len;
587#endif
588
589#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
591 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
592 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100593 }
594 p += ext_len;
595#endif
596
597#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
599 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
600 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100601 }
602 p += ext_len;
603#endif
604
605#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
607 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
608 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100609 }
610 p += ext_len;
611#endif
612
613 *out_len = p - buf;
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100616}
617
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200618MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100619static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
620 const unsigned char *buf,
621 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100625 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000627 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 mbedtls_ct_memcmp(buf + 1,
629 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
630 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
631 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
632 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100633 mbedtls_ssl_send_alert_message(
634 ssl,
635 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
637 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000638 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100641 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (len != 1 || buf[0] != 0x00) {
643 MBEDTLS_SSL_DEBUG_MSG(1,
644 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100645 mbedtls_ssl_send_alert_message(
646 ssl,
647 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
649 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100650 }
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100653 }
Paul Bakker48916f92012-09-16 19:57:18 +0000654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000656}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200659MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100660static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
661 const unsigned char *buf,
662 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200663{
664 /*
665 * server should use the extension only if we did,
666 * and if so the server's value should match ours (and len is always 1)
667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200669 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 buf[0] != ssl->conf->mfl_code) {
671 MBEDTLS_SSL_DEBUG_MSG(1,
672 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100673 mbedtls_ssl_send_alert_message(
674 ssl,
675 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
677 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200678 }
679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000683
Hanno Beckera0e20d02019-05-15 14:03:01 +0100684#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200685MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100686static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
687 const unsigned char *buf,
688 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100689{
690 size_t peer_cid_len;
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100693 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
694 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
696 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
697 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
698 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
699 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100700 }
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (len == 0) {
703 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
704 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
705 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
706 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100707 }
708
709 peer_cid_len = *buf++;
710 len--;
711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
713 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
714 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
715 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
716 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100717 }
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if (len != peer_cid_len) {
720 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
721 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
722 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
723 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100724 }
725
Hanno Becker5a299902019-05-03 12:47:49 +0100726 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100727 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
731 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100734}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100735#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200738MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100739static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
740 const unsigned char *buf,
741 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100742{
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
744 len != 0) {
745 MBEDTLS_SSL_DEBUG_MSG(1,
746 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100747 mbedtls_ssl_send_alert_message(
748 ssl,
749 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
751 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100752 }
753
754 ((void) buf);
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200763MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100764static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
765 const unsigned char *buf,
766 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200767{
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
769 len != 0) {
770 MBEDTLS_SSL_DEBUG_MSG(1,
771 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100772 mbedtls_ssl_send_alert_message(
773 ssl,
774 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
776 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200777 }
778
779 ((void) buf);
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100789static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
790 const unsigned char *buf,
791 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200792{
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
794 len != 0) {
795 MBEDTLS_SSL_DEBUG_MSG(1,
796 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100797 mbedtls_ssl_send_alert_message(
798 ssl,
799 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
801 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200802 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200803
804 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200805
806 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200809}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200811
Robert Cragie136884c2015-10-02 13:34:31 +0100812#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100813 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200814MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100815static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
816 const unsigned char *buf,
817 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200818{
819 size_t list_size;
820 const unsigned char *p;
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (len == 0 || (size_t) (buf[0] + 1) != len) {
823 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
824 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
825 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
826 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200827 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200828 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200829
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200830 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 while (list_size > 0) {
832 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
833 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Valerio Setti46423162023-03-27 14:33:27 +0200834#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200835 ssl->handshake->ecdh_ctx.point_format = p[0];
Valerio Setti77a904c2023-03-24 07:28:49 +0100836#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_ECDH_C */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200837#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
839 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
840 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200841#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
843 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200844 }
845
846 list_size--;
847 p++;
848 }
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
851 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
852 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
853 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200854}
Darryl Green11999bb2018-03-13 15:22:58 +0000855#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100856 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200857
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200858#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200859MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100860static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
861 const unsigned char *buf,
862 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200863{
Janos Follath865b3eb2019-12-16 11:46:15 +0000864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (ssl->handshake->ciphersuite_info->key_exchange !=
867 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
868 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
869 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200870 }
871
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200872 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200874 ssl->handshake->ecjpake_cache = NULL;
875 ssl->handshake->ecjpake_cache_len = 0;
876
Neil Armstrongca7d5062022-05-31 14:43:23 +0200877#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if ((ret = mbedtls_psa_ecjpake_read_round(
879 &ssl->handshake->psa_pake_ctx, buf, len,
880 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
881 psa_destroy_key(ssl->handshake->psa_pake_password);
882 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100885 mbedtls_ssl_send_alert_message(
886 ssl,
887 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
889 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200890 }
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 return 0;
893#else
894 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
895 buf, len)) != 0) {
896 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
897 mbedtls_ssl_send_alert_message(
898 ssl,
899 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
900 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
901 return ret;
902 }
903
904 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200905#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200906}
907#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200910MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100911static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
912 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200913{
914 size_t list_len, name_len;
915 const char **p;
916
917 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (ssl->conf->alpn_list == NULL) {
919 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100920 mbedtls_ssl_send_alert_message(
921 ssl,
922 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
924 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200925 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200926
927 /*
928 * opaque ProtocolName<1..2^8-1>;
929 *
930 * struct {
931 * ProtocolName protocol_name_list<2..2^16-1>
932 * } ProtocolNameList;
933 *
934 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
935 */
936
937 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (len < 4) {
939 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
940 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
941 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200942 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 list_len = (buf[0] << 8) | buf[1];
945 if (list_len != len - 2) {
946 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
947 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
948 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200949 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200950
951 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (name_len != list_len - 1) {
953 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
954 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
955 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200956 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200957
958 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
960 if (name_len == strlen(*p) &&
961 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200962 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200964 }
965 }
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
968 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
969 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
970 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200973
Johan Pascalb62bb512015-12-03 21:56:45 +0100974#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200975MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
977 const unsigned char *buf,
978 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100979{
Johan Pascal43f94902020-09-22 12:25:52 +0200980 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200981 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100982 uint16_t server_protection_profile_value = 0;
983
984 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
986 (ssl->conf->dtls_srtp_profile_list == NULL) ||
987 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
988 return 0;
989 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100990
Ron Eldora9788042018-12-05 11:04:31 +0200991 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100992 * uint8 SRTPProtectionProfile[2];
993 *
994 * struct {
995 * SRTPProtectionProfiles SRTPProtectionProfiles;
996 * opaque srtp_mki<0..255>;
997 * } UseSRTPData;
998
999 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1000 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001003 mki_len = ssl->dtls_srtp_info.mki_len;
1004 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001005
Ron Eldoref72faf2018-07-12 11:54:20 +03001006 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001007 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1008 * + protection profile (2 bytes)
1009 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001010 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if ((len < 5) || (len != (buf[4] + 5u))) {
1013 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1014 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001015
1016 /*
1017 * get the server protection profile
1018 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001019
1020 /*
1021 * protection profile length must be 0x0002 as we must have only
1022 * one protection profile in server Hello
1023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if ((buf[0] != 0) || (buf[1] != 2)) {
1025 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1026 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001029 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 server_protection_profile_value);
1031 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1032 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1033 mbedtls_ssl_get_srtp_profile_as_string(
1034 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001035 }
1036
Johan Pascal43f94902020-09-22 12:25:52 +02001037 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001038
Johan Pascalb62bb512015-12-03 21:56:45 +01001039 /*
1040 * Check we have the server profile in our list
1041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1043 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001044 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001046 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001048 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001049 }
1050 }
1051
Ron Eldor591f1622018-01-22 12:30:04 +02001052 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1054 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1055 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1056 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001057 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001058
1059 /* If server does not use mki in its reply, make sure the client won't keep
1060 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001062 ssl->dtls_srtp_info.mki_len = 0;
1063 }
1064
Ron Eldoref72faf2018-07-12 11:54:20 +03001065 /*
1066 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001067 * If the client detects a nonzero-length MKI in the server's response
1068 * that is different than the one the client offered, then the client
1069 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (len > 5 && (buf[4] != mki_len ||
1072 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1073 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1074 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1075 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001077#if defined(MBEDTLS_DEBUG_C)
1078 if (len > 5) {
1079 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1080 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001081 }
1082#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001084}
1085#endif /* MBEDTLS_SSL_DTLS_SRTP */
1086
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001087/*
1088 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001092static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001093{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001094 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001096 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001097
1098#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1099 uint8_t cookie_len;
1100#else
1101 uint16_t cookie_len;
1102#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001105
Gilles Peskineb64bf062019-09-27 14:02:44 +02001106 /* Check that there is enough room for:
1107 * - 2 bytes of version
1108 * - 1 byte of cookie_len
1109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1111 MBEDTLS_SSL_DEBUG_MSG(1,
1112 ("incoming HelloVerifyRequest message is too short"));
1113 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1114 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1115 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001116 }
1117
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001118 /*
1119 * struct {
1120 * ProtocolVersion server_version;
1121 * opaque cookie<0..2^8-1>;
1122 * } HelloVerifyRequest;
1123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1125 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001126 p += 2;
1127
TRodziewicz2d8800e2021-05-13 19:14:19 +02001128 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001129 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1130 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1131 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1134 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1137 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001140 }
1141
1142 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1144 MBEDTLS_SSL_DEBUG_MSG(1,
1145 ("cookie length does not match incoming message size"));
1146 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1147 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1148 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001149 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1155 if (ssl->handshake->cookie == NULL) {
1156 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1157 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001158 }
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001161 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001162
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001163 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001165 ret = mbedtls_ssl_reset_checksum(ssl);
1166 if (0 != ret) {
1167 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1168 return ret;
1169 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001179MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001180static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001181{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001182 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001183 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001184 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001185 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001186 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001188 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001189#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001190 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001196 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1198 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
1200
Hanno Becker79594fd2019-05-08 09:38:41 +01001201 buf = ssl->in_msg;
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001206 ssl->renego_records_seen++;
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (ssl->conf->renego_max_records >= 0 &&
1209 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1210 MBEDTLS_SSL_DEBUG_MSG(1,
1211 ("renegotiation requested, but not honored by server"));
1212 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001213 }
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 MBEDTLS_SSL_DEBUG_MSG(1,
1216 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001217
1218 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001224 mbedtls_ssl_send_alert_message(
1225 ssl,
1226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1228 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 }
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1233 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1234 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1235 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1236 return ssl_parse_hello_verify_request(ssl);
1237 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001238 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001240 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001241 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001242 }
1243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1247 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1248 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1249 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1250 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1251 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 }
1253
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001254 /*
1255 * 0 . 1 server_version
1256 * 2 . 33 random (maybe including 4 bytes of Unix time)
1257 * 34 . 34 session_id length = n
1258 * 35 . 34+n session_id
1259 * 35+n . 36+n cipher_suite
1260 * 37+n . 37+n compression_method
1261 *
1262 * 38+n . 39+n extensions length (optional)
1263 * 40+n . .. extensions
1264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
1268 ssl->tls_version = mbedtls_ssl_read_version(buf, ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001269 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if (ssl->tls_version < ssl->conf->min_tls_version ||
1272 ssl->tls_version > ssl->conf->max_tls_version) {
1273 MBEDTLS_SSL_DEBUG_MSG(1,
1274 (
1275 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1276 (unsigned) ssl->conf->min_tls_version,
1277 (unsigned) ssl->tls_version,
1278 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1281 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001284 }
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1287 ((unsigned long) buf[2] << 24) |
1288 ((unsigned long) buf[3] << 16) |
1289 ((unsigned long) buf[4] << 8) |
1290 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001294 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (n > 32) {
1299 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1300 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1301 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1302 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
1306 ext_len = ((buf[38 + n] << 8)
1307 | (buf[39 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if ((ext_len > 0 && ext_len < 4) ||
1310 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1311 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001312 mbedtls_ssl_send_alert_message(
1313 ssl,
1314 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1316 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001317 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001319 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 } else {
1321 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1322 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1323 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1324 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001327 /* ciphersuite (used later) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 i = (buf[35 + n] << 8) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001329
1330 /*
1331 * Read and check compression
1332 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001333 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1336 MBEDTLS_SSL_DEBUG_MSG(1,
1337 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001338 mbedtls_ssl_send_alert_message(
1339 ssl,
1340 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1342 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001343 }
1344
Paul Bakker380da532012-04-18 16:10:25 +00001345 /*
1346 * Initialize update checksum functions
1347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1349 if (ssl->handshake->ciphersuite_info == NULL) {
1350 MBEDTLS_SSL_DEBUG_MSG(1,
1351 ("ciphersuite info for %04x not found", (unsigned int) i));
1352 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1353 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1354 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001355 }
Paul Bakker380da532012-04-18 16:10:25 +00001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1360 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 /*
1363 * Check if the session can be resumed
1364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_SSL_RENEGOTIATION)
1367 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001368#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001369 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001370 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001373 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001376#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001377 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001378 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 memcpy(ssl->session_negotiate->id, buf + 35, n);
1380 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1385 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1388 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1389 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Andrzej Kurek03bac442018-04-25 05:06:07 -04001391 /*
1392 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001393 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 while (1) {
1396 if (ssl->conf->ciphersuite_list[i] == 0) {
1397 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001398 mbedtls_ssl_send_alert_message(
1399 ssl,
1400 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1402 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 }
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (ssl->conf->ciphersuite_list[i++] ==
1406 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
1410
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001411 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 ssl->session_negotiate->ciphersuite);
1413 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1414 ssl->tls_version) != 0) {
1415 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001416 mbedtls_ssl_send_alert_message(
1417 ssl,
1418 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1420 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001421 }
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 MBEDTLS_SSL_DEBUG_MSG(3,
1424 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001425
Gilles Peskineeccd8882020-03-10 12:19:08 +01001426#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1428 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001429 ssl->handshake->ecrs_enabled = 1;
1430 }
1431#endif
1432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1434 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001435 mbedtls_ssl_send_alert_message(
1436 ssl,
1437 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1439 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001442 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 MBEDTLS_SSL_DEBUG_MSG(2,
1445 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1446 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 while (ext_len) {
1449 unsigned int ext_id = ((ext[0] << 8)
1450 | (ext[1]));
1451 unsigned int ext_size = ((ext[2] << 8)
1452 | (ext[3]));
Paul Bakker48916f92012-09-16 19:57:18 +00001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (ext_size + 4 > ext_len) {
1455 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001456 mbedtls_ssl_send_alert_message(
1457 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1459 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001460 }
1461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 switch (ext_id) {
1463 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1464 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001467#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1470 ext_size)) != 0) {
1471 return ret;
1472 }
Paul Bakker48916f92012-09-16 19:57:18 +00001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1478 MBEDTLS_SSL_DEBUG_MSG(3,
1479 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1482 ext + 4, ext_size)) != 0) {
1483 return ret;
1484 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001488
Hanno Beckera0e20d02019-05-15 14:03:01 +01001489#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 case MBEDTLS_TLS_EXT_CID:
1491 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if ((ret = ssl_parse_cid_ext(ssl,
1494 ext + 4,
1495 ext_size)) != 0) {
1496 return ret;
1497 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001500#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1504 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1507 ext + 4, ext_size)) != 0) {
1508 return ret;
1509 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1516 MBEDTLS_SSL_DEBUG_MSG(3,
1517 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if ((ret = ssl_parse_extended_ms_ext(ssl,
1520 ext + 4, ext_size)) != 0) {
1521 return ret;
1522 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1529 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 if ((ret = ssl_parse_session_ticket_ext(ssl,
1532 ext + 4, ext_size)) != 0) {
1533 return ret;
1534 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001538
Robert Cragie136884c2015-10-02 13:34:31 +01001539#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1541 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1542 MBEDTLS_SSL_DEBUG_MSG(3,
1543 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1546 ext + 4, ext_size)) != 0) {
1547 return ret;
1548 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001551#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1552 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001553
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001554#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1556 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1559 ext + 4, ext_size)) != 0) {
1560 return ret;
1561 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001564#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 case MBEDTLS_TLS_EXT_ALPN:
1568 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1571 return ret;
1572 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001576
Johan Pascalb62bb512015-12-03 21:56:45 +01001577#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 case MBEDTLS_TLS_EXT_USE_SRTP:
1579 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1582 return ret;
1583 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001586#endif /* MBEDTLS_SSL_DTLS_SRTP */
1587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 default:
1589 MBEDTLS_SSL_DEBUG_MSG(3,
1590 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001591 }
1592
1593 ext_len -= 4 + ext_size;
1594 ext += 4 + ext_size;
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (ext_len > 0 && ext_len < 4) {
1597 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1598 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001599 }
1600 }
1601
1602 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001603 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1604 * extensions. It sets the transform data for the resumed session which in
1605 * case of DTLS includes the server CID extracted from the CID extension.
1606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (ssl->handshake->resume) {
1608 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1609 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001610 mbedtls_ssl_send_alert_message(
1611 ssl,
1612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1614 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001615 }
1616 }
1617
Paul Bakker48916f92012-09-16 19:57:18 +00001618 /*
1619 * Renegotiation security checks
1620 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001622 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1624 MBEDTLS_SSL_DEBUG_MSG(1,
1625 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001626 handshake_failure = 1;
1627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 renegotiation_info_seen == 0) {
1632 MBEDTLS_SSL_DEBUG_MSG(1,
1633 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001634 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1636 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1637 ssl->conf->allow_legacy_renegotiation ==
1638 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1639 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001640 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1642 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1643 renegotiation_info_seen == 1) {
1644 MBEDTLS_SSL_DEBUG_MSG(1,
1645 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001646 handshake_failure = 1;
1647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001651 mbedtls_ssl_send_alert_message(
1652 ssl,
1653 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1655 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001661}
1662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1664 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001665MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001666static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1667 unsigned char **p,
1668 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001671 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001672
Paul Bakker29e1f122013-04-16 13:07:56 +02001673 /*
1674 * Ephemeral DH parameters:
1675 *
1676 * struct {
1677 * opaque dh_p<1..2^16-1>;
1678 * opaque dh_g<1..2^16-1>;
1679 * opaque dh_Ys<1..2^16-1>;
1680 * } ServerDHParams;
1681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1683 p, end)) != 0) {
1684 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1685 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001686 }
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1689 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1690 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1691 dhm_actual_bitlen,
1692 ssl->conf->dhm_min_bitlen));
1693 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001694 }
1695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1697 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1698 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1703 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001704
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001705#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001706#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1707 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1708 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001709MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001710static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1711 unsigned char **p,
1712 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001713{
1714 uint16_t tls_id;
1715 uint8_t ecpoint_len;
1716 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001717 psa_ecc_family_t ec_psa_family = 0;
1718 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001719
1720 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001721 * struct {
1722 * ECParameters curve_params;
1723 * ECPoint public;
1724 * } ServerECDHParams;
1725 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001726 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001727 * 2..3 NamedCurve
1728 * 4 ECPoint.len
1729 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001730 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if (end - *p < 4) {
1732 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1733 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001734
1735 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1737 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1738 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001739
1740 /* Next two bytes are the namedcurve value */
1741 tls_id = *(*p)++;
1742 tls_id <<= 8;
1743 tls_id |= *(*p)++;
1744
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001745 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1747 MBEDTLS_SSL_DEBUG_MSG(2,
1748 ("bad server key exchange message (ECDHE curve): %u",
1749 (unsigned) tls_id));
1750 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001751 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001752
Valerio Setti40d9ca92023-01-04 16:08:04 +01001753 /* Convert EC's TLS ID to PSA key type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ec_psa_family,
1755 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1756 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001757 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001759 handshake->ecdh_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001760
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001761 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001762 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if ((size_t) (end - *p) < ecpoint_len) {
1764 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1765 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if (ecpoint_len > sizeof(handshake->ecdh_psa_peerkey)) {
1768 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1769 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 memcpy(handshake->ecdh_psa_peerkey, *p, ecpoint_len);
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001772 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001773 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001776}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001777#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1778 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1779 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001780#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001781#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1782 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1783 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1784 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1785 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001786MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001787static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001788{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001789 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001790 mbedtls_ecp_group_id grp_id;
1791#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1792 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1793#else
1794 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1795#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1798 if (tls_id == 0) {
1799 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1800 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001801 }
1802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1804 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1807 return -1;
1808 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1811 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001814}
1815
Andrzej Kurek468c5062022-10-24 10:30:14 -04001816#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1817 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1818 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1819 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1820 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1821
1822#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1823 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1824 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001825MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001826static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1827 unsigned char **p,
1828 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001831
Paul Bakker29e1f122013-04-16 13:07:56 +02001832 /*
1833 * Ephemeral ECDH parameters:
1834 *
1835 * struct {
1836 * ECParameters curve_params;
1837 * ECPoint public;
1838 * } ServerECDHParams;
1839 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1841 (const unsigned char **) p, end)) != 0) {
1842 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001843#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001845 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001847#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001849 }
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if (ssl_check_server_ecdh_params(ssl) != 0) {
1852 MBEDTLS_SSL_DEBUG_MSG(1,
1853 ("bad server key exchange message (ECDHE curve)"));
1854 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001855 }
1856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001858}
Gilles Peskine449bd832023-01-11 14:50:10 +01001859#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1860 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001861 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1862#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001863#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001864MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001865static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1866 unsigned char **p,
1867 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001868{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001870 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001871 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001872
1873 /*
1874 * PSK parameters:
1875 *
1876 * opaque psk_identity_hint<0..2^16-1>;
1877 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 if (end - (*p) < 2) {
1879 MBEDTLS_SSL_DEBUG_MSG(1,
1880 ("bad server key exchange message (psk_identity_hint length)"));
1881 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001882 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001883 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001884 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 if (end - (*p) < len) {
1887 MBEDTLS_SSL_DEBUG_MSG(1,
1888 ("bad server key exchange message (psk_identity_hint length)"));
1889 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001890 }
1891
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001892 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001893 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001894 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001895 * someone needs that feature.
1896 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001897 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001898 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001901}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001902#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1905 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001906/*
1907 * Generate a pre-master secret and encrypt it with the server's RSA key
1908 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001909MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001910static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1911 size_t offset, size_t *olen,
1912 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001913{
Janos Follath865b3eb2019-12-16 11:46:15 +00001914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001915 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001916 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1920 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1921 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001922 }
1923
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001924 /*
1925 * Generate (part of) the pre-master as
1926 * struct {
1927 * ProtocolVersion client_version;
1928 * opaque random[46];
1929 * } PreMasterSecret;
1930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 mbedtls_ssl_write_version(p, ssl->conf->transport,
1932 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1935 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1936 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001937 }
1938
1939 ssl->handshake->pmslen = 48;
1940
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001941#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1942 peer_pk = &ssl->handshake->peer_pubkey;
1943#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001945 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1947 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001948 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001949 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1950#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001951
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001952 /*
1953 * Now write it out, encrypted
1954 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1956 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1957 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001958 }
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if ((ret = mbedtls_pk_encrypt(peer_pk,
1961 p, ssl->handshake->pmslen,
1962 ssl->out_msg + offset + len_bytes, olen,
1963 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1964 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1965 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1966 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001967 }
1968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (len_bytes == 2) {
1970 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001971 *olen += 2;
1972 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001973
Hanno Beckerae553dd2019-02-08 14:06:00 +00001974#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1975 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001977#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
1981 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1984 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001985MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001986static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001987{
Janos Follath865b3eb2019-12-16 11:46:15 +00001988 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001990
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001991#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1992 peer_pk = &ssl->handshake->peer_pubkey;
1993#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001995 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1997 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001998 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001999 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2000#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002001
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002002 /* This is a public key, so it can't be opaque, so can_do() is a good
2003 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2005 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2006 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002007 }
2008
Valerio Setti97207782023-05-18 18:59:06 +02002009#if defined(MBEDTLS_ECP_C)
2010 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
2011#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002012
Przemek Stekielea4000f2022-03-16 09:49:33 +01002013#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002014 uint16_t tls_id = 0;
2015 psa_ecc_family_t ecc_family;
Valerio Setti97207782023-05-18 18:59:06 +02002016 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002017
Valerio Setti97207782023-05-18 18:59:06 +02002018 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2020 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002021 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002022
Valerio Setti97207782023-05-18 18:59:06 +02002023 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (tls_id == 0) {
2025 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002026 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002028 }
2029
Valerio Setti1e868cc2023-01-09 17:30:01 +01002030 /* If the above conversion to TLS ID was fine, then also this one will be,
2031 so there is no need to check the return value here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ecc_family,
2033 &ssl->handshake->ecdh_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 ssl->handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ecc_family);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002036
Przemek Stekielea4000f2022-03-16 09:49:33 +01002037 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002038#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
2039 memcpy(ssl->handshake->ecdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2040 ssl->handshake->ecdh_psa_peerkey_len = peer_pk->pub_raw_len;
2041 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002042#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002043 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2045 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2046 ssl->handshake->ecdh_psa_peerkey,
2047 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (ret != 0) {
2050 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2051 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002052 }
Przemek Stekiel561a4232022-03-16 13:16:24 +01002053 ssl->handshake->ecdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002054#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2055#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2057 MBEDTLS_ECDH_THEIRS)) != 0) {
2058 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2059 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002060 }
2061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (ssl_check_server_ecdh_params(ssl) != 0) {
2063 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2064 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002065 }
Valerio Setti97207782023-05-18 18:59:06 +02002066#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002067#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2068 /* We don't need the peer's public key anymore. Free it,
2069 * so that more RAM is available for upcoming expensive
2070 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002072#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2077 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002078
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002079MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002080static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002081{
Janos Follath865b3eb2019-12-16 11:46:15 +00002082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002083 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002084 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002085 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2091 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002095 ((void) p);
2096 ((void) end);
2097#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2100 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2102 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2103 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2104 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002105 mbedtls_ssl_send_alert_message(
2106 ssl,
2107 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2109 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002110 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002113 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002115 }
2116 ((void) p);
2117 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2119 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002120
Gilles Peskineeccd8882020-03-10 12:19:08 +01002121#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (ssl->handshake->ecrs_enabled &&
2123 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002124 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002125 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002126#endif
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2129 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2130 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 }
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2134 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002135 mbedtls_ssl_send_alert_message(
2136 ssl,
2137 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2139 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
2141
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002142 /*
2143 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2144 * doesn't use a psk_identity_hint
2145 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2147 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2148 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002149 /* Current message is probably either
2150 * CertificateRequest or ServerHelloDone */
2151 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002152 goto exit;
2153 }
2154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 MBEDTLS_SSL_DEBUG_MSG(1,
2156 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002157 mbedtls_ssl_send_alert_message(
2158 ssl,
2159 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
2164
Gilles Peskineeccd8882020-03-10 12:19:08 +01002165#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002167 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002169
2170start_processing:
2171#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002173 end = ssl->in_msg + ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, end - p);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002175
Gilles Peskineeccd8882020-03-10 12:19:08 +01002176#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2179 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2181 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2182 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002183 mbedtls_ssl_send_alert_message(
2184 ssl,
2185 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2187 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002188 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002189 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002190#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2193 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2195 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002196 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2199 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2200#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2201 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2203 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2204 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2205 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002206 mbedtls_ssl_send_alert_message(
2207 ssl,
2208 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2210 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2214 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002215#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2216 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2221 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2222 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002223 mbedtls_ssl_send_alert_message(
2224 ssl,
2225 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2227 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002228 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2231 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2232 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002233#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002235#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002236 /*
2237 * The first 3 bytes are:
2238 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2239 * [1, 2] elliptic curve's TLS ID
2240 *
2241 * However since we only support secp256r1 for now, we check only
2242 * that TLS ID here
2243 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002245 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (exp_tls_id == 0) {
2249 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002250 }
2251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2253 (read_tls_id != exp_tls_id)) {
2254 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002255 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002256
2257 p += 3;
2258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if ((ret = mbedtls_psa_ecjpake_read_round(
2260 &ssl->handshake->psa_pake_ctx, p, end - p,
2261 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2262 psa_destroy_key(ssl->handshake->psa_pake_password);
2263 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002266 mbedtls_ssl_send_alert_message(
2267 ssl,
2268 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2270 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002271 }
2272#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2274 p, end - p);
2275 if (ret != 0) {
2276 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002277 mbedtls_ssl_send_alert_message(
2278 ssl,
2279 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2281 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002282 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002283#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002285#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002286 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2288 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002289 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002290
Gilles Peskineeccd8882020-03-10 12:19:08 +01002291#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002293 size_t sig_len, hashlen;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002294 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
2295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2297 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002299 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002300 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002301 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002304
Jerry Yu693a47a2022-06-23 14:02:28 +08002305#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2306 peer_pk = &ssl->handshake->peer_pubkey;
2307#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002309 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2311 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002312 }
2313 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2314#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2315
Paul Bakker29e1f122013-04-16 13:07:56 +02002316 /*
2317 * Handle the digitally-signed structure
2318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2320 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2321 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2322 sig_alg, &pk_alg, &md_alg) != 0 &&
2323 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2324 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2325 MBEDTLS_SSL_DEBUG_MSG(1,
2326 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002327 mbedtls_ssl_send_alert_message(
2328 ssl,
2329 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2331 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002332 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002333 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2336 MBEDTLS_SSL_DEBUG_MSG(1,
2337 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002338 mbedtls_ssl_send_alert_message(
2339 ssl,
2340 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2342 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002343 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002344
2345 /*
2346 * Read signature
2347 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (p > end - 2) {
2350 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002351 mbedtls_ssl_send_alert_message(
2352 ssl,
2353 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2355 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002356 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 sig_len = (p[0] << 8) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002358 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (p != end - sig_len) {
2361 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002362 mbedtls_ssl_send_alert_message(
2363 ssl,
2364 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2366 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002370
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002371 /*
2372 * Compute the hash that has been signed
2373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (md_alg != MBEDTLS_MD_NONE) {
2375 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2376 params, params_len,
2377 md_alg);
2378 if (ret != 0) {
2379 return ret;
2380 }
2381 } else {
2382 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2383 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002384 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002387
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002388 /*
2389 * Verify signature
2390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2392 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002393 mbedtls_ssl_send_alert_message(
2394 ssl,
2395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2397 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002398 }
2399
Gilles Peskineeccd8882020-03-10 12:19:08 +01002400#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002402 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002404#endif
2405
Jerry Yu693a47a2022-06-23 14:02:28 +08002406#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002408 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2409 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002410 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002411 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 if (rsassa_pss_options.expected_salt_len == 0) {
2413 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2414 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2417 peer_pk,
2418 md_alg, hash, hashlen,
2419 p, sig_len);
2420 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002421#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 ret = mbedtls_pk_verify_restartable(peer_pk,
2423 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002426 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002427#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002429#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002431 mbedtls_ssl_send_alert_message(
2432 ssl,
2433 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2435 }
2436 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002437#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002439 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002441#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002443 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002444
2445#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2446 /* We don't need the peer's public key anymore. Free it,
2447 * so that more RAM is available for upcoming expensive
2448 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002450#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002452#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002454exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 ssl->state++;
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002460}
2461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002463MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002464static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002465{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002466 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002467 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2472 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002473 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002475 }
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2478 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002479}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002480#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002481MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002482static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002483{
Janos Follath865b3eb2019-12-16 11:46:15 +00002484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002485 unsigned char *buf;
2486 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002487 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002488 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002489 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002490 size_t sig_alg_len;
2491#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 unsigned char *sig_alg;
2493 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002494#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2499 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002500 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002502 }
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2505 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2506 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 }
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2510 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002511 mbedtls_ssl_send_alert_message(
2512 ssl,
2513 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2515 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002518 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002519 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2523 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002526 /* Current message is probably the ServerHelloDone */
2527 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002528 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002529 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002530
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002531 /*
2532 * struct {
2533 * ClientCertificateType certificate_types<1..2^8-1>;
2534 * SignatureAndHashAlgorithm
2535 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2536 * DistinguishedName certificate_authorities<0..2^16-1>;
2537 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002538 *
2539 * Since we only support a single certificate on clients, let's just
2540 * ignore all the information that's supposed to help us pick a
2541 * certificate.
2542 *
2543 * We could check that our certificate matches the request, and bail out
2544 * if it doesn't, but it's simpler to just send the certificate anyway,
2545 * and give the server the opportunity to decide if it should terminate
2546 * the connection when it doesn't like our certificate.
2547 *
2548 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2549 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002550 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002551 *
2552 * However, we still minimally parse the message to check it is at least
2553 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002554 */
Paul Bakker926af752012-11-23 13:38:07 +01002555 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002556
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002557 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2559 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2560 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2561 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2562 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002563 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002565 n = cert_type_len;
2566
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002567 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002568 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002569 * * the length of the signature algorithms field (if minor version of
2570 * SSL is 3),
2571 * * distinguished name length otherwise.
2572 * Both reach at most the index:
2573 * ...hdr_len + 2 + n,
2574 * therefore the buffer length at this point must be greater than that
2575 * regardless of the actual code path.
2576 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2578 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2579 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2580 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2581 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002582 }
2583
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002584 /* supported_signature_algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 sig_alg_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2586 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Ronald Cron90915f22022-03-07 11:11:36 +01002587
2588 /*
2589 * The furthest access in buf is in the loop few lines below:
2590 * sig_alg[i + 1],
2591 * where:
2592 * sig_alg = buf + ...hdr_len + 3 + n,
2593 * max(i) = sig_alg_len - 1.
2594 * Therefore the furthest access is:
2595 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2596 * which reduces to:
2597 * buf[...hdr_len + 3 + n + sig_alg_len],
2598 * which is one less than we need the buf to be.
2599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2601 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002602 mbedtls_ssl_send_alert_message(
2603 ssl,
2604 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2606 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002607 }
Paul Bakker926af752012-11-23 13:38:07 +01002608
Ronald Cron90915f22022-03-07 11:11:36 +01002609#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2611 for (size_t i = 0; i < sig_alg_len; i += 2) {
2612 MBEDTLS_SSL_DEBUG_MSG(3,
2613 ("Supported Signature Algorithm found: %02x %02x",
2614 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002615 }
2616#endif
2617
2618 n += 2 + sig_alg_len;
2619
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002620 /* certificate_authorities */
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 dn_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2622 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Paul Bakker926af752012-11-23 13:38:07 +01002623
2624 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2626 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2627 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2628 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2629 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002630 }
2631
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002632#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2634 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002635 unsigned char *p = dn + i + 2;
2636 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002637 size_t asn1_len;
2638 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 memset(&name, 0, sizeof(name));
2640 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2641 if (dni_len > dn_len - i - 2 ||
2642 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2643 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2644 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2645 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002646 mbedtls_ssl_send_alert_message(
2647 ssl,
2648 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2650 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002651 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 MBEDTLS_SSL_DEBUG_MSG(3,
2653 ("DN hint: %.*s",
2654 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2655 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002656 }
2657#endif
2658
Paul Bakker926af752012-11-23 13:38:07 +01002659exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002663}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002664#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002666MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002667static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002668{
Janos Follath865b3eb2019-12-16 11:46:15 +00002669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2674 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2675 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2679 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2680 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2684 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2685 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2686 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2687 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2688 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 }
2690
2691 ssl->state++;
2692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2695 mbedtls_ssl_recv_flight_completed(ssl);
2696 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002697#endif
2698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002702}
2703
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002704MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002705static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002706{
Janos Follath865b3eb2019-12-16 11:46:15 +00002707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002708
2709 size_t header_len;
2710 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002711 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002712 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 /*
2719 * DHM key exchange -- send G^X mod P
2720 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002724 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2727 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2728 &ssl->out_msg[header_len], content_len,
2729 ssl->conf->f_rng, ssl->conf->p_rng);
2730 if (ret != 0) {
2731 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2732 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2736 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2739 ssl->handshake->premaster,
2740 MBEDTLS_PREMASTER_SIZE,
2741 &ssl->handshake->pmslen,
2742 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2743 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2744 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 }
2746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2748 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002750#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2751 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2752 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2753 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002755 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2756 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002758#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002759 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2760 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002761 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002762
2763 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2764
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002765 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002768
Hanno Becker4a63ed42019-01-08 11:39:35 +00002769 /*
2770 * Generate EC private key for ECDHE exchange.
2771 */
2772
Hanno Becker4a63ed42019-01-08 11:39:35 +00002773 /* The master secret is obtained from the shared ECDH secret by
2774 * applying the TLS 1.2 PRF with a specific salt and label. While
2775 * the PSA Crypto API encourages combining key agreement schemes
2776 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2777 * yet support the provisioning of salt + label to the KDF.
2778 * For the time being, we therefore need to split the computation
2779 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002780 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2782 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2783 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2784 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002785
2786 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 status = psa_generate_key(&key_attributes,
2788 &handshake->ecdh_psa_privkey);
2789 if (status != PSA_SUCCESS) {
2790 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2791 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002792
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002793 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002794 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002795 * but we just need to add a length byte before that. */
2796 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2797 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002799 size_t own_pubkey_len;
2800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2802 own_pubkey, own_pubkey_max_len,
2803 &own_pubkey_len);
2804 if (status != PSA_SUCCESS) {
2805 psa_destroy_key(handshake->ecdh_psa_privkey);
Andrzej Kureka0237f82022-02-24 13:24:52 -05002806 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002808 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002809
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002810 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2811 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002812
Hanno Becker4a63ed42019-01-08 11:39:35 +00002813 /* The ECDH secret is the premaster secret used for key derivation. */
2814
Janos Follathdf3b0892019-08-08 11:12:24 +01002815 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2817 handshake->ecdh_psa_privkey,
2818 handshake->ecdh_psa_peerkey,
2819 handshake->ecdh_psa_peerkey_len,
2820 ssl->handshake->premaster,
2821 sizeof(ssl->handshake->premaster),
2822 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Ronald Croncf56a0a2020-08-04 09:51:30 +02002825 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2828 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2829 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002830#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002831 /*
2832 * ECDH key exchange -- send client public value
2833 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002834 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002835
Gilles Peskineeccd8882020-03-10 12:19:08 +01002836#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 if (ssl->handshake->ecrs_enabled) {
2838 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002839 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002843 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002844#endif
2845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2847 &content_len,
2848 &ssl->out_msg[header_len], 1000,
2849 ssl->conf->f_rng, ssl->conf->p_rng);
2850 if (ret != 0) {
2851 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002852#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002854 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002856#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002858 }
2859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2861 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002862
Gilles Peskineeccd8882020-03-10 12:19:08 +01002863#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002865 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002866 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002867 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002868
2869ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002871 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002873#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2875 &ssl->handshake->pmslen,
2876 ssl->handshake->premaster,
2877 MBEDTLS_MPI_MAX_SIZE,
2878 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2879 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002880#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002882 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002884#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002886 }
2887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2889 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002890#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2893 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2894 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2895 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002896#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2897 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2900 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2901 psa_key_attributes_t key_attributes;
2902
2903 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2904
2905 /*
2906 * opaque psk_identity<0..2^16-1>;
2907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002909 /* We don't offer PSK suites if we don't have a PSK,
2910 * and we check that the server's choice is among the
2911 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2913 }
Neil Armstrong868af822022-03-09 10:26:25 +01002914
Neil Armstrongfc834f22022-03-23 17:54:38 +01002915 /* uint16 to store content length */
2916 const size_t content_len_size = 2;
2917
Neil Armstrong868af822022-03-09 10:26:25 +01002918 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 if (header_len + content_len_size + ssl->conf->psk_identity_len
2921 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2922 MBEDTLS_SSL_DEBUG_MSG(1,
2923 ("psk identity too long or SSL buffer too short"));
2924 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002925 }
2926
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002927 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2930 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002931 header_len += content_len_size;
2932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 memcpy(p, ssl->conf->psk_identity,
2934 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002935 p += ssl->conf->psk_identity_len;
2936
Neil Armstrong868af822022-03-09 10:26:25 +01002937 header_len += ssl->conf->psk_identity_len;
2938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002940
2941 /*
2942 * Generate EC private key for ECDHE exchange.
2943 */
2944
2945 /* The master secret is obtained from the shared ECDH secret by
2946 * applying the TLS 1.2 PRF with a specific salt and label. While
2947 * the PSA Crypto API encourages combining key agreement schemes
2948 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2949 * yet support the provisioning of salt + label to the KDF.
2950 * For the time being, we therefore need to split the computation
2951 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2952 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2954 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2955 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2956 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002957
2958 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 status = psa_generate_key(&key_attributes,
2960 &handshake->ecdh_psa_privkey);
2961 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002962 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 }
Neil Armstrong868af822022-03-09 10:26:25 +01002964
2965 /* Export the public part of the ECDH private key from PSA.
2966 * The export format is an ECPoint structure as expected by TLS,
2967 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002968 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002969 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002971 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2974 own_pubkey, own_pubkey_max_len,
2975 &own_pubkey_len);
2976 if (status != PSA_SUCCESS) {
2977 psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01002978 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002979 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002980 }
2981
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002982 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002983 content_len = own_pubkey_len + 1;
2984
Neil Armstrong25400452022-03-23 17:44:07 +01002985 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2986 * - a uint16 containing the length (in octets) of the ECDH computation
2987 * - the octet string produced by the ECDH computation
2988 * - a uint16 containing the length (in octets) of the PSK
2989 * - the PSK itself
2990 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002991 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 const unsigned char * const pms_end = pms +
2993 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002994 /* uint16 to store length (in octets) of the ECDH computation */
2995 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002996 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002997
Neil Armstrong25400452022-03-23 17:44:07 +01002998 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 status = psa_raw_key_agreement(PSA_ALG_ECDH,
3000 handshake->ecdh_psa_privkey,
3001 handshake->ecdh_psa_peerkey,
3002 handshake->ecdh_psa_peerkey_len,
3003 pms + zlen_size,
3004 pms_end - (pms + zlen_size),
3005 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01003008 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003011 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003013 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 }
Neil Armstrong868af822022-03-09 10:26:25 +01003015
Neil Armstrong25400452022-03-23 17:44:07 +01003016 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003018 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003020#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3021 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003022#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003024 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003025 * opaque psk_identity<0..2^16-1>;
3026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003028 /* We don't offer PSK suites if we don't have a PSK,
3029 * and we check that the server's choice is among the
3030 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003032 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003033
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003034 header_len = 4;
3035 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3038 MBEDTLS_SSL_DEBUG_MSG(1,
3039 ("psk identity too long or SSL buffer too short"));
3040 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003041 }
3042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3044 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 memcpy(ssl->out_msg + header_len,
3047 ssl->conf->psk_identity,
3048 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003049 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003053 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3058 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3059 &content_len, 2)) != 0) {
3060 return ret;
3061 }
3062 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003066 /*
3067 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (header_len + 2 + content_len >
3072 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3073 MBEDTLS_SSL_DEBUG_MSG(1,
3074 ("psk identity or DHM size too long or SSL buffer too short"));
3075 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003076 }
3077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3079 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3082 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3083 &ssl->out_msg[header_len], content_len,
3084 ssl->conf->f_rng, ssl->conf->p_rng);
3085 if (ret != 0) {
3086 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3087 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003088 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003089
3090#if defined(MBEDTLS_USE_PSA_CRYPTO)
3091 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003093 size_t pms_len;
3094
3095 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3097 pms + 2, pms_end - (pms + 2), &pms_len,
3098 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3099 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3100 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003101 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003103 pms += 2 + pms_len;
3104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003106#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003109#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3111 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003112 /*
3113 * ClientECDiffieHellmanPublic public;
3114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3116 &content_len,
3117 &ssl->out_msg[header_len],
3118 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3119 ssl->conf->f_rng, ssl->conf->p_rng);
3120 if (ret != 0) {
3121 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3122 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003123 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3126 MBEDTLS_DEBUG_ECDH_Q);
3127 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003128#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003129 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3131 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003132 }
3133
Neil Armstrong80f6f322022-05-03 17:56:38 +02003134#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3136 ciphersuite_info->key_exchange)) != 0) {
3137 MBEDTLS_SSL_DEBUG_RET(1,
3138 "mbedtls_ssl_psk_derive_premaster", ret);
3139 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003140 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003141#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003143#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003146 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3148 &content_len, 0)) != 0) {
3149 return ret;
3150 }
3151 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003153#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003155 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003156
Neil Armstrongca7d5062022-05-31 14:43:23 +02003157#if defined(MBEDTLS_USE_PSA_CRYPTO)
3158 unsigned char *out_p = ssl->out_msg + header_len;
3159 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3160 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3162 out_p, end_p - out_p, &content_len,
3163 MBEDTLS_ECJPAKE_ROUND_TWO);
3164 if (ret != 0) {
3165 psa_destroy_key(ssl->handshake->psa_pake_password);
3166 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3167 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3168 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003169 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003170#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3172 ssl->out_msg + header_len,
3173 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3174 &content_len,
3175 ssl->conf->f_rng, ssl->conf->p_rng);
3176 if (ret != 0) {
3177 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3178 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003179 }
3180
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3182 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3183 ssl->conf->f_rng, ssl->conf->p_rng);
3184 if (ret != 0) {
3185 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3186 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003187 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003188#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003190#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003191 {
3192 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3194 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003195 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003197 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3199 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
3201 ssl->state++;
3202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3204 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3205 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 }
3207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003211}
3212
Gilles Peskineeccd8882020-03-10 12:19:08 +01003213#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003214MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003215static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003216{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003217 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003218 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3224 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3225 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003226 }
3227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3229 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003230 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003232 }
3233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3235 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003236}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003237#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003238MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003239static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003242 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003243 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003244 size_t n = 0, offset = 0;
3245 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003246 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003248 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003249 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003250#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 size_t out_buf_len = ssl->out_buf_len - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003252#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003254#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003257
Gilles Peskineeccd8882020-03-10 12:19:08 +01003258#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 if (ssl->handshake->ecrs_enabled &&
3260 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003261 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003262 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003263#endif
3264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3266 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3267 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003268 }
3269
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3271 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003272 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003274 }
3275
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 if (ssl->handshake->client_auth == 0 ||
3277 mbedtls_ssl_own_cert(ssl) == NULL) {
3278 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003279 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003281 }
3282
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 if (mbedtls_ssl_own_key(ssl) == NULL) {
3284 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3285 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 }
3287
3288 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003289 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003291#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003293 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003295
3296sign:
3297#endif
3298
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003299 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3300 if (0 != ret) {
3301 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3302 return ret;
3303 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003304
Ronald Cron90915f22022-03-07 11:11:36 +01003305 /*
3306 * digitally-signed struct {
3307 * opaque handshake_messages[handshake_messages_length];
3308 * };
3309 *
3310 * Taking shortcut here. We assume that the server always allows the
3311 * PRF Hash function and has sent it in the allowed signature
3312 * algorithms list received in the Certificate Request message.
3313 *
3314 * Until we encounter a server that does not, we will take this
3315 * shortcut.
3316 *
3317 * Reason: Otherwise we should have running hashes for SHA512 and
3318 * SHA224 in order to satisfy 'weird' needs from the server
3319 * side.
3320 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003322 md_alg = MBEDTLS_MD_SHA384;
3323 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003325 md_alg = MBEDTLS_MD_SHA256;
3326 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003327 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003329
3330 /* Info from md_alg will be used instead */
3331 hashlen = 0;
3332 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003333
Gilles Peskineeccd8882020-03-10 12:19:08 +01003334#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003336 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003338#endif
3339
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3341 md_alg, hash_start, hashlen,
3342 ssl->out_msg + 6 + offset,
3343 out_buf_len - 6 - offset,
3344 &n,
3345 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3346 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003347#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003349 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003351#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003353 }
Paul Bakker926af752012-11-23 13:38:07 +01003354
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003356
Paul Bakker1ef83d62012-04-11 12:09:53 +00003357 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3359 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003360
3361 ssl->state++;
3362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3364 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3365 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 }
3367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003371}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003372#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003375MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003376static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003377{
Janos Follath865b3eb2019-12-16 11:46:15 +00003378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003379 uint32_t lifetime;
3380 size_t ticket_len;
3381 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003382 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3387 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3388 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003389 }
3390
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3392 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003393 mbedtls_ssl_send_alert_message(
3394 ssl,
3395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3397 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003398 }
3399
3400 /*
3401 * struct {
3402 * uint32 ticket_lifetime_hint;
3403 * opaque ticket<0..2^16-1>;
3404 * } NewSessionTicket;
3405 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003406 * 0 . 3 ticket_lifetime_hint
3407 * 4 . 5 ticket_len (n)
3408 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3411 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3412 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3413 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3414 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3415 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003416 }
3417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 lifetime = (((uint32_t) msg[0]) << 24) | (msg[1] << 16) |
3421 (msg[2] << 8) | (msg[3]);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 ticket_len = (msg[4] << 8) | (msg[5]);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003424
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3426 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3427 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3428 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3429 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003430 }
3431
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003433
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003434 /* We're not waiting for a NewSessionTicket message any more */
3435 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003437
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003438 /*
3439 * Zero-length ticket means the server changed his mind and doesn't want
3440 * to send a ticket after all, so just forget it
3441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 if (ticket_len == 0) {
3443 return 0;
3444 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 if (ssl->session != NULL && ssl->session->ticket != NULL) {
3447 mbedtls_platform_zeroize(ssl->session->ticket,
3448 ssl->session->ticket_len);
3449 mbedtls_free(ssl->session->ticket);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003450 ssl->session->ticket = NULL;
3451 ssl->session->ticket_len = 0;
3452 }
3453
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 mbedtls_platform_zeroize(ssl->session_negotiate->ticket,
3455 ssl->session_negotiate->ticket_len);
3456 mbedtls_free(ssl->session_negotiate->ticket);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003457 ssl->session_negotiate->ticket = NULL;
3458 ssl->session_negotiate->ticket_len = 0;
3459
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3461 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3462 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3463 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3464 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003465 }
3466
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003468
3469 ssl->session_negotiate->ticket = ticket;
3470 ssl->session_negotiate->ticket_len = ticket_len;
3471 ssl->session_negotiate->ticket_lifetime = lifetime;
3472
3473 /*
3474 * RFC 5077 section 3.4:
3475 * "If the client receives a session ticket from the server, then it
3476 * discards any Session ID that was sent in the ServerHello."
3477 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003479 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003480
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003482
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003486
Paul Bakker5121ce52009-01-03 21:22:43 +00003487/*
Paul Bakker1961b702013-01-25 14:49:24 +01003488 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003490int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003491{
3492 int ret = 0;
3493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003495 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3498 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003499 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003500 }
3501#endif
3502
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 case MBEDTLS_SSL_HELLO_REQUEST:
3505 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003506 break;
3507
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 /*
3509 * ==> ClientHello
3510 */
3511 case MBEDTLS_SSL_CLIENT_HELLO:
3512 ret = mbedtls_ssl_write_client_hello(ssl);
3513 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003514
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 /*
3516 * <== ServerHello
3517 * Certificate
3518 * ( ServerKeyExchange )
3519 * ( CertificateRequest )
3520 * ServerHelloDone
3521 */
3522 case MBEDTLS_SSL_SERVER_HELLO:
3523 ret = ssl_parse_server_hello(ssl);
3524 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003525
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3527 ret = mbedtls_ssl_parse_certificate(ssl);
3528 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3531 ret = ssl_parse_server_key_exchange(ssl);
3532 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003533
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3535 ret = ssl_parse_certificate_request(ssl);
3536 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003537
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3539 ret = ssl_parse_server_hello_done(ssl);
3540 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Gilles Peskine449bd832023-01-11 14:50:10 +01003542 /*
3543 * ==> ( Certificate/Alert )
3544 * ClientKeyExchange
3545 * ( CertificateVerify )
3546 * ChangeCipherSpec
3547 * Finished
3548 */
3549 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3550 ret = mbedtls_ssl_write_certificate(ssl);
3551 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3554 ret = ssl_write_client_key_exchange(ssl);
3555 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003556
Gilles Peskine449bd832023-01-11 14:50:10 +01003557 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3558 ret = ssl_write_certificate_verify(ssl);
3559 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3562 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3563 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003564
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 case MBEDTLS_SSL_CLIENT_FINISHED:
3566 ret = mbedtls_ssl_write_finished(ssl);
3567 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003568
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 /*
3570 * <== ( NewSessionTicket )
3571 * ChangeCipherSpec
3572 * Finished
3573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003575 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3576 ret = ssl_parse_new_session_ticket(ssl);
3577 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003578#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003579
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3581 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3582 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003583
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 case MBEDTLS_SSL_SERVER_FINISHED:
3585 ret = mbedtls_ssl_parse_finished(ssl);
3586 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 case MBEDTLS_SSL_FLUSH_BUFFERS:
3589 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3590 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3591 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003592
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3594 mbedtls_ssl_handshake_wrapup(ssl);
3595 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003596
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 default:
3598 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3599 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003601
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003603}
Jerry Yuc5aef882021-12-23 20:15:02 +08003604
Jerry Yufb4b6472022-01-27 15:03:26 +08003605#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */