blob: 890e9a906e48671f4e4f15319ac2fd659ed31ac2 [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) {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200834#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 (defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200836 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200837#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
838 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200839#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
841 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
842 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200843#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
845 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200846 }
847
848 list_size--;
849 p++;
850 }
851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
853 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
854 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
855 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200856}
Darryl Green11999bb2018-03-13 15:22:58 +0000857#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100858 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200859
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200860#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200861MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100862static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
863 const unsigned char *buf,
864 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200865{
Janos Follath865b3eb2019-12-16 11:46:15 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (ssl->handshake->ciphersuite_info->key_exchange !=
869 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
870 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
871 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200872 }
873
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200874 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200876 ssl->handshake->ecjpake_cache = NULL;
877 ssl->handshake->ecjpake_cache_len = 0;
878
Neil Armstrongca7d5062022-05-31 14:43:23 +0200879#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if ((ret = mbedtls_psa_ecjpake_read_round(
881 &ssl->handshake->psa_pake_ctx, buf, len,
882 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
883 psa_destroy_key(ssl->handshake->psa_pake_password);
884 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100887 mbedtls_ssl_send_alert_message(
888 ssl,
889 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
891 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200892 }
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 return 0;
895#else
896 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
897 buf, len)) != 0) {
898 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
899 mbedtls_ssl_send_alert_message(
900 ssl,
901 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
902 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
903 return ret;
904 }
905
906 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200907#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200908}
909#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200912MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100913static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
914 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200915{
916 size_t list_len, name_len;
917 const char **p;
918
919 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (ssl->conf->alpn_list == NULL) {
921 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100922 mbedtls_ssl_send_alert_message(
923 ssl,
924 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
926 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200927 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200928
929 /*
930 * opaque ProtocolName<1..2^8-1>;
931 *
932 * struct {
933 * ProtocolName protocol_name_list<2..2^16-1>
934 * } ProtocolNameList;
935 *
936 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
937 */
938
939 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (len < 4) {
941 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
942 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
943 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200944 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 list_len = (buf[0] << 8) | buf[1];
947 if (list_len != len - 2) {
948 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
949 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
950 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200951 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200952
953 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (name_len != list_len - 1) {
955 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
956 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
957 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200958 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200959
960 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
962 if (name_len == strlen(*p) &&
963 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200964 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200966 }
967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
970 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
971 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
972 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200975
Johan Pascalb62bb512015-12-03 21:56:45 +0100976#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200977MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100978static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
979 const unsigned char *buf,
980 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100981{
Johan Pascal43f94902020-09-22 12:25:52 +0200982 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200983 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100984 uint16_t server_protection_profile_value = 0;
985
986 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
988 (ssl->conf->dtls_srtp_profile_list == NULL) ||
989 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
990 return 0;
991 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100992
Ron Eldora9788042018-12-05 11:04:31 +0200993 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100994 * uint8 SRTPProtectionProfile[2];
995 *
996 * struct {
997 * SRTPProtectionProfiles SRTPProtectionProfiles;
998 * opaque srtp_mki<0..255>;
999 * } UseSRTPData;
1000
1001 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1002 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001005 mki_len = ssl->dtls_srtp_info.mki_len;
1006 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001007
Ron Eldoref72faf2018-07-12 11:54:20 +03001008 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001009 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1010 * + protection profile (2 bytes)
1011 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001012 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if ((len < 5) || (len != (buf[4] + 5u))) {
1015 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1016 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001017
1018 /*
1019 * get the server protection profile
1020 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001021
1022 /*
1023 * protection profile length must be 0x0002 as we must have only
1024 * one protection profile in server Hello
1025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if ((buf[0] != 0) || (buf[1] != 2)) {
1027 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1028 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001031 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 server_protection_profile_value);
1033 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1034 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1035 mbedtls_ssl_get_srtp_profile_as_string(
1036 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001037 }
1038
Johan Pascal43f94902020-09-22 12:25:52 +02001039 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001040
Johan Pascalb62bb512015-12-03 21:56:45 +01001041 /*
1042 * Check we have the server profile in our list
1043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1045 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001046 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001048 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001050 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001051 }
1052 }
1053
Ron Eldor591f1622018-01-22 12:30:04 +02001054 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1056 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1057 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1058 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001059 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001060
1061 /* If server does not use mki in its reply, make sure the client won't keep
1062 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001064 ssl->dtls_srtp_info.mki_len = 0;
1065 }
1066
Ron Eldoref72faf2018-07-12 11:54:20 +03001067 /*
1068 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001069 * If the client detects a nonzero-length MKI in the server's response
1070 * that is different than the one the client offered, then the client
1071 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (len > 5 && (buf[4] != mki_len ||
1074 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1075 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1076 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1077 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001078 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001079#if defined(MBEDTLS_DEBUG_C)
1080 if (len > 5) {
1081 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1082 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001083 }
1084#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001086}
1087#endif /* MBEDTLS_SSL_DTLS_SRTP */
1088
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001089/*
1090 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001093MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001094static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001095{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001096 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001098 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001099
1100#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1101 uint8_t cookie_len;
1102#else
1103 uint16_t cookie_len;
1104#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001107
Gilles Peskineb64bf062019-09-27 14:02:44 +02001108 /* Check that there is enough room for:
1109 * - 2 bytes of version
1110 * - 1 byte of cookie_len
1111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1113 MBEDTLS_SSL_DEBUG_MSG(1,
1114 ("incoming HelloVerifyRequest message is too short"));
1115 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1116 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1117 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001118 }
1119
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001120 /*
1121 * struct {
1122 * ProtocolVersion server_version;
1123 * opaque cookie<0..2^8-1>;
1124 * } HelloVerifyRequest;
1125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1127 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001128 p += 2;
1129
TRodziewicz2d8800e2021-05-13 19:14:19 +02001130 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001131 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1132 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1133 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001134 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1136 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1139 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001142 }
1143
1144 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1146 MBEDTLS_SSL_DEBUG_MSG(1,
1147 ("cookie length does not match incoming message size"));
1148 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1149 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1150 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001151 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1157 if (ssl->handshake->cookie == NULL) {
1158 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1159 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001160 }
1161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001163 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001165 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001167 ret = mbedtls_ssl_reset_checksum(ssl);
1168 if (0 != ret) {
1169 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1170 return ret;
1171 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001180
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001181MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001182static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001183{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001184 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001185 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001186 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001187 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001188 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001190 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001191#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001192 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001198 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1200 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 }
1202
Hanno Becker79594fd2019-05-08 09:38:41 +01001203 buf = ssl->in_msg;
1204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001208 ssl->renego_records_seen++;
1209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if (ssl->conf->renego_max_records >= 0 &&
1211 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1212 MBEDTLS_SSL_DEBUG_MSG(1,
1213 ("renegotiation requested, but not honored by server"));
1214 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001215 }
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 MBEDTLS_SSL_DEBUG_MSG(1,
1218 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001219
1220 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001222 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001226 mbedtls_ssl_send_alert_message(
1227 ssl,
1228 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1230 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 }
1232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1235 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1236 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1237 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1238 return ssl_parse_hello_verify_request(ssl);
1239 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001240 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001242 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001243 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001244 }
1245 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1249 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1250 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1251 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1252 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1253 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 }
1255
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001256 /*
1257 * 0 . 1 server_version
1258 * 2 . 33 random (maybe including 4 bytes of Unix time)
1259 * 34 . 34 session_id length = n
1260 * 35 . 34+n session_id
1261 * 35+n . 36+n cipher_suite
1262 * 37+n . 37+n compression_method
1263 *
1264 * 38+n . 39+n extensions length (optional)
1265 * 40+n . .. extensions
1266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
1270 ssl->tls_version = mbedtls_ssl_read_version(buf, ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001271 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (ssl->tls_version < ssl->conf->min_tls_version ||
1274 ssl->tls_version > ssl->conf->max_tls_version) {
1275 MBEDTLS_SSL_DEBUG_MSG(1,
1276 (
1277 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1278 (unsigned) ssl->conf->min_tls_version,
1279 (unsigned) ssl->tls_version,
1280 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1283 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001286 }
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1289 ((unsigned long) buf[2] << 24) |
1290 ((unsigned long) buf[3] << 16) |
1291 ((unsigned long) buf[4] << 8) |
1292 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001296 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (n > 32) {
1301 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1302 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1303 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1304 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001305 }
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
1308 ext_len = ((buf[38 + n] << 8)
1309 | (buf[39 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if ((ext_len > 0 && ext_len < 4) ||
1312 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1313 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001314 mbedtls_ssl_send_alert_message(
1315 ssl,
1316 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1318 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001319 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001321 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 } else {
1323 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1324 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1325 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1326 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001327 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001329 /* ciphersuite (used later) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 i = (buf[35 + n] << 8) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001331
1332 /*
1333 * Read and check compression
1334 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001335 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1338 MBEDTLS_SSL_DEBUG_MSG(1,
1339 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001340 mbedtls_ssl_send_alert_message(
1341 ssl,
1342 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1344 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001345 }
1346
Paul Bakker380da532012-04-18 16:10:25 +00001347 /*
1348 * Initialize update checksum functions
1349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1351 if (ssl->handshake->ciphersuite_info == NULL) {
1352 MBEDTLS_SSL_DEBUG_MSG(1,
1353 ("ciphersuite info for %04x not found", (unsigned int) i));
1354 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1355 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1356 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001357 }
Paul Bakker380da532012-04-18 16:10:25 +00001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1362 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
1364 /*
1365 * Check if the session can be resumed
1366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_SSL_RENEGOTIATION)
1369 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001370#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001371 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001372 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001375 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001378#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001379 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001380 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 memcpy(ssl->session_negotiate->id, buf + 35, n);
1382 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001384 }
1385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1387 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1390 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1391 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Andrzej Kurek03bac442018-04-25 05:06:07 -04001393 /*
1394 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001395 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 while (1) {
1398 if (ssl->conf->ciphersuite_list[i] == 0) {
1399 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001400 mbedtls_ssl_send_alert_message(
1401 ssl,
1402 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1404 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 }
1406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (ssl->conf->ciphersuite_list[i++] ==
1408 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 }
1412
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001413 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 ssl->session_negotiate->ciphersuite);
1415 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1416 ssl->tls_version) != 0) {
1417 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001418 mbedtls_ssl_send_alert_message(
1419 ssl,
1420 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1422 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001423 }
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 MBEDTLS_SSL_DEBUG_MSG(3,
1426 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001427
Gilles Peskineeccd8882020-03-10 12:19:08 +01001428#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1430 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001431 ssl->handshake->ecrs_enabled = 1;
1432 }
1433#endif
1434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1436 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001437 mbedtls_ssl_send_alert_message(
1438 ssl,
1439 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1441 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
1443
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001444 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 MBEDTLS_SSL_DEBUG_MSG(2,
1447 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1448 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 while (ext_len) {
1451 unsigned int ext_id = ((ext[0] << 8)
1452 | (ext[1]));
1453 unsigned int ext_size = ((ext[2] << 8)
1454 | (ext[3]));
Paul Bakker48916f92012-09-16 19:57:18 +00001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (ext_size + 4 > ext_len) {
1457 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001458 mbedtls_ssl_send_alert_message(
1459 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1461 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001462 }
1463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 switch (ext_id) {
1465 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1466 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001469#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1472 ext_size)) != 0) {
1473 return ret;
1474 }
Paul Bakker48916f92012-09-16 19:57:18 +00001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1480 MBEDTLS_SSL_DEBUG_MSG(3,
1481 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1484 ext + 4, ext_size)) != 0) {
1485 return ret;
1486 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001490
Hanno Beckera0e20d02019-05-15 14:03:01 +01001491#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 case MBEDTLS_TLS_EXT_CID:
1493 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if ((ret = ssl_parse_cid_ext(ssl,
1496 ext + 4,
1497 ext_size)) != 0) {
1498 return ret;
1499 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001502#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1506 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1509 ext + 4, ext_size)) != 0) {
1510 return ret;
1511 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1518 MBEDTLS_SSL_DEBUG_MSG(3,
1519 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if ((ret = ssl_parse_extended_ms_ext(ssl,
1522 ext + 4, ext_size)) != 0) {
1523 return ret;
1524 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1531 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if ((ret = ssl_parse_session_ticket_ext(ssl,
1534 ext + 4, ext_size)) != 0) {
1535 return ret;
1536 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001540
Robert Cragie136884c2015-10-02 13:34:31 +01001541#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1543 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1544 MBEDTLS_SSL_DEBUG_MSG(3,
1545 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1548 ext + 4, ext_size)) != 0) {
1549 return ret;
1550 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001553#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1554 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001555
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001556#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1558 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1561 ext + 4, ext_size)) != 0) {
1562 return ret;
1563 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001566#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 case MBEDTLS_TLS_EXT_ALPN:
1570 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1573 return ret;
1574 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001578
Johan Pascalb62bb512015-12-03 21:56:45 +01001579#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 case MBEDTLS_TLS_EXT_USE_SRTP:
1581 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1584 return ret;
1585 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001588#endif /* MBEDTLS_SSL_DTLS_SRTP */
1589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 default:
1591 MBEDTLS_SSL_DEBUG_MSG(3,
1592 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001593 }
1594
1595 ext_len -= 4 + ext_size;
1596 ext += 4 + ext_size;
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (ext_len > 0 && ext_len < 4) {
1599 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1600 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001601 }
1602 }
1603
1604 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001605 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1606 * extensions. It sets the transform data for the resumed session which in
1607 * case of DTLS includes the server CID extracted from the CID extension.
1608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (ssl->handshake->resume) {
1610 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1611 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001612 mbedtls_ssl_send_alert_message(
1613 ssl,
1614 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1616 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001617 }
1618 }
1619
Paul Bakker48916f92012-09-16 19:57:18 +00001620 /*
1621 * Renegotiation security checks
1622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001624 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1626 MBEDTLS_SSL_DEBUG_MSG(1,
1627 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001628 handshake_failure = 1;
1629 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 renegotiation_info_seen == 0) {
1634 MBEDTLS_SSL_DEBUG_MSG(1,
1635 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001636 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1638 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1639 ssl->conf->allow_legacy_renegotiation ==
1640 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1641 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001642 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1644 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1645 renegotiation_info_seen == 1) {
1646 MBEDTLS_SSL_DEBUG_MSG(1,
1647 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001648 handshake_failure = 1;
1649 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001653 mbedtls_ssl_send_alert_message(
1654 ssl,
1655 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1657 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001658 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001663}
1664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1666 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001667MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001668static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1669 unsigned char **p,
1670 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001671{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001673 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001674
Paul Bakker29e1f122013-04-16 13:07:56 +02001675 /*
1676 * Ephemeral DH parameters:
1677 *
1678 * struct {
1679 * opaque dh_p<1..2^16-1>;
1680 * opaque dh_g<1..2^16-1>;
1681 * opaque dh_Ys<1..2^16-1>;
1682 * } ServerDHParams;
1683 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1685 p, end)) != 0) {
1686 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1687 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001688 }
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1691 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1692 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1693 dhm_actual_bitlen,
1694 ssl->conf->dhm_min_bitlen));
1695 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001696 }
1697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1699 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1700 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1705 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001706
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001707#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001708#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1709 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1710 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001711MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001712static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1713 unsigned char **p,
1714 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001715{
1716 uint16_t tls_id;
1717 uint8_t ecpoint_len;
1718 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001719 psa_ecc_family_t ec_psa_family = 0;
1720 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001721
1722 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001723 * struct {
1724 * ECParameters curve_params;
1725 * ECPoint public;
1726 * } ServerECDHParams;
1727 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001728 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001729 * 2..3 NamedCurve
1730 * 4 ECPoint.len
1731 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001732 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if (end - *p < 4) {
1734 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1735 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001736
1737 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1739 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1740 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001741
1742 /* Next two bytes are the namedcurve value */
1743 tls_id = *(*p)++;
1744 tls_id <<= 8;
1745 tls_id |= *(*p)++;
1746
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001747 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1749 MBEDTLS_SSL_DEBUG_MSG(2,
1750 ("bad server key exchange message (ECDHE curve): %u",
1751 (unsigned) tls_id));
1752 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001753 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001754
Valerio Setti40d9ca92023-01-04 16:08:04 +01001755 /* Convert EC's TLS ID to PSA key type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ec_psa_family,
1757 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1758 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001759 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001761 handshake->ecdh_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001762
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001763 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001764 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 if ((size_t) (end - *p) < ecpoint_len) {
1766 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1767 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (ecpoint_len > sizeof(handshake->ecdh_psa_peerkey)) {
1770 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1771 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 memcpy(handshake->ecdh_psa_peerkey, *p, ecpoint_len);
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001774 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001775 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001778}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001779#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1780 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1781 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001782#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001783#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1784 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1785 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1786 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1787 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001789static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001790{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001791 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001792 mbedtls_ecp_group_id grp_id;
1793#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1794 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1795#else
1796 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1797#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1800 if (tls_id == 0) {
1801 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1802 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001803 }
1804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1806 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1809 return -1;
1810 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1813 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001816}
1817
Andrzej Kurek468c5062022-10-24 10:30:14 -04001818#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1819 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1820 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1821 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1822 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1823
1824#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1825 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1826 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001827MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001828static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1829 unsigned char **p,
1830 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001831{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001833
Paul Bakker29e1f122013-04-16 13:07:56 +02001834 /*
1835 * Ephemeral ECDH parameters:
1836 *
1837 * struct {
1838 * ECParameters curve_params;
1839 * ECPoint public;
1840 * } ServerECDHParams;
1841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1843 (const unsigned char **) p, end)) != 0) {
1844 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001845#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001847 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001849#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001851 }
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (ssl_check_server_ecdh_params(ssl) != 0) {
1854 MBEDTLS_SSL_DEBUG_MSG(1,
1855 ("bad server key exchange message (ECDHE curve)"));
1856 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001857 }
1858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001860}
Gilles Peskine449bd832023-01-11 14:50:10 +01001861#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1862 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001863 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1864#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001865#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001866MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001867static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1868 unsigned char **p,
1869 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001872 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001873 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001874
1875 /*
1876 * PSK parameters:
1877 *
1878 * opaque psk_identity_hint<0..2^16-1>;
1879 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if (end - (*p) < 2) {
1881 MBEDTLS_SSL_DEBUG_MSG(1,
1882 ("bad server key exchange message (psk_identity_hint length)"));
1883 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001884 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001885 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001886 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 if (end - (*p) < len) {
1889 MBEDTLS_SSL_DEBUG_MSG(1,
1890 ("bad server key exchange message (psk_identity_hint length)"));
1891 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001892 }
1893
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001894 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001895 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001896 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001897 * someone needs that feature.
1898 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001899 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001900 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001903}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001904#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1907 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001908/*
1909 * Generate a pre-master secret and encrypt it with the server's RSA key
1910 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001911MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001912static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1913 size_t offset, size_t *olen,
1914 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001915{
Janos Follath865b3eb2019-12-16 11:46:15 +00001916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001917 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001918 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1922 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1923 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001924 }
1925
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001926 /*
1927 * Generate (part of) the pre-master as
1928 * struct {
1929 * ProtocolVersion client_version;
1930 * opaque random[46];
1931 * } PreMasterSecret;
1932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 mbedtls_ssl_write_version(p, ssl->conf->transport,
1934 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1937 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1938 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001939 }
1940
1941 ssl->handshake->pmslen = 48;
1942
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001943#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1944 peer_pk = &ssl->handshake->peer_pubkey;
1945#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001947 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1949 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001950 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001951 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1952#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001953
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001954 /*
1955 * Now write it out, encrypted
1956 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1958 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1959 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001960 }
1961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 if ((ret = mbedtls_pk_encrypt(peer_pk,
1963 p, ssl->handshake->pmslen,
1964 ssl->out_msg + offset + len_bytes, olen,
1965 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1966 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1967 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1968 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001969 }
1970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (len_bytes == 2) {
1972 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001973 *olen += 2;
1974 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001975
Hanno Beckerae553dd2019-02-08 14:06:00 +00001976#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1977 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001979#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
1983 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1986 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001987MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001988static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001989{
Janos Follath865b3eb2019-12-16 11:46:15 +00001990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 const mbedtls_ecp_keypair *peer_key;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001993
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001994#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1995 peer_pk = &ssl->handshake->peer_pubkey;
1996#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001998 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2000 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002001 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002002 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2003#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002004
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002005 /* This is a public key, so it can't be opaque, so can_do() is a good
2006 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2008 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2009 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002010 }
2011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 peer_key = mbedtls_pk_ec(*peer_pk);
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002013
Przemek Stekielea4000f2022-03-16 09:49:33 +01002014#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel561a4232022-03-16 13:16:24 +01002015 size_t olen = 0;
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002016 uint16_t tls_id = 0;
2017 psa_ecc_family_t ecc_family;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if (mbedtls_ssl_check_curve(ssl, peer_key->grp.id) != 0) {
2020 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2021 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002022 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(peer_key->grp.id);
2025 if (tls_id == 0) {
2026 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
2027 peer_key->grp.id));
2028 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002029 }
2030
Valerio Setti1e868cc2023-01-09 17:30:01 +01002031 /* If the above conversion to TLS ID was fine, then also this one will be,
2032 so there is no need to check the return value here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ecc_family,
2034 &ssl->handshake->ecdh_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 ssl->handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ecc_family);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002037
Przemek Stekielea4000f2022-03-16 09:49:33 +01002038 /* Store peer's public key in psa format. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2040 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2041 ssl->handshake->ecdh_psa_peerkey,
2042 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (ret != 0) {
2045 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2046 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002047 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002048
Przemek Stekiel561a4232022-03-16 13:16:24 +01002049 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002050#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2052 MBEDTLS_ECDH_THEIRS)) != 0) {
2053 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2054 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002055 }
2056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if (ssl_check_server_ecdh_params(ssl) != 0) {
2058 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2059 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002060 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002061#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002062#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2063 /* We don't need the peer's public key anymore. Free it,
2064 * so that more RAM is available for upcoming expensive
2065 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002067#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2072 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002073
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002075static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002076{
Janos Follath865b3eb2019-12-16 11:46:15 +00002077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002078 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002079 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002080 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2086 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002090 ((void) p);
2091 ((void) end);
2092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2095 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2097 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2098 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2099 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002100 mbedtls_ssl_send_alert_message(
2101 ssl,
2102 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2104 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002105 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002108 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002110 }
2111 ((void) p);
2112 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2114 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002115
Gilles Peskineeccd8882020-03-10 12:19:08 +01002116#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (ssl->handshake->ecrs_enabled &&
2118 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002119 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002120 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002121#endif
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2124 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2125 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2129 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002130 mbedtls_ssl_send_alert_message(
2131 ssl,
2132 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2134 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 }
2136
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002137 /*
2138 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2139 * doesn't use a psk_identity_hint
2140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2142 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2143 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002144 /* Current message is probably either
2145 * CertificateRequest or ServerHelloDone */
2146 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002147 goto exit;
2148 }
2149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 MBEDTLS_SSL_DEBUG_MSG(1,
2151 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002152 mbedtls_ssl_send_alert_message(
2153 ssl,
2154 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 }
2159
Gilles Peskineeccd8882020-03-10 12:19:08 +01002160#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002162 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002164
2165start_processing:
2166#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002168 end = ssl->in_msg + ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, end - p);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002170
Gilles Peskineeccd8882020-03-10 12:19:08 +01002171#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2174 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2176 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2177 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002178 mbedtls_ssl_send_alert_message(
2179 ssl,
2180 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2182 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002183 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002184 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002185#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2188 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2190 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002191 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2194 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2195#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2196 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2198 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2199 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2200 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002201 mbedtls_ssl_send_alert_message(
2202 ssl,
2203 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2205 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002206 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2209 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002210#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2211 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2216 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2217 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002218 mbedtls_ssl_send_alert_message(
2219 ssl,
2220 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2222 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002223 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2226 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2227 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002228#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002230#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002231 /*
2232 * The first 3 bytes are:
2233 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2234 * [1, 2] elliptic curve's TLS ID
2235 *
2236 * However since we only support secp256r1 for now, we check only
2237 * that TLS ID here
2238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002240 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 if (exp_tls_id == 0) {
2244 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002245 }
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2248 (read_tls_id != exp_tls_id)) {
2249 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002250 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002251
2252 p += 3;
2253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if ((ret = mbedtls_psa_ecjpake_read_round(
2255 &ssl->handshake->psa_pake_ctx, p, end - p,
2256 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2257 psa_destroy_key(ssl->handshake->psa_pake_password);
2258 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002261 mbedtls_ssl_send_alert_message(
2262 ssl,
2263 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2265 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002266 }
2267#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2269 p, end - p);
2270 if (ret != 0) {
2271 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002272 mbedtls_ssl_send_alert_message(
2273 ssl,
2274 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2276 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002277 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002278#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002280#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002281 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2283 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002284 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002285
Gilles Peskineeccd8882020-03-10 12:19:08 +01002286#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002288 size_t sig_len, hashlen;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002289 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
2290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2292 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002294 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002295 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002296 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002299
Jerry Yu693a47a2022-06-23 14:02:28 +08002300#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2301 peer_pk = &ssl->handshake->peer_pubkey;
2302#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002304 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2306 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002307 }
2308 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2309#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2310
Paul Bakker29e1f122013-04-16 13:07:56 +02002311 /*
2312 * Handle the digitally-signed structure
2313 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2315 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2316 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2317 sig_alg, &pk_alg, &md_alg) != 0 &&
2318 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2319 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2320 MBEDTLS_SSL_DEBUG_MSG(1,
2321 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002322 mbedtls_ssl_send_alert_message(
2323 ssl,
2324 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2326 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002327 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002328 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2331 MBEDTLS_SSL_DEBUG_MSG(1,
2332 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002333 mbedtls_ssl_send_alert_message(
2334 ssl,
2335 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2337 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002338 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002339
2340 /*
2341 * Read signature
2342 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (p > end - 2) {
2345 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002346 mbedtls_ssl_send_alert_message(
2347 ssl,
2348 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2350 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002351 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 sig_len = (p[0] << 8) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002353 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (p != end - sig_len) {
2356 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002357 mbedtls_ssl_send_alert_message(
2358 ssl,
2359 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2361 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002362 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002365
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002366 /*
2367 * Compute the hash that has been signed
2368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (md_alg != MBEDTLS_MD_NONE) {
2370 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2371 params, params_len,
2372 md_alg);
2373 if (ret != 0) {
2374 return ret;
2375 }
2376 } else {
2377 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2378 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002379 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002382
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002383 /*
2384 * Verify signature
2385 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2387 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002388 mbedtls_ssl_send_alert_message(
2389 ssl,
2390 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2392 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002393 }
2394
Gilles Peskineeccd8882020-03-10 12:19:08 +01002395#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002397 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002399#endif
2400
Jerry Yu693a47a2022-06-23 14:02:28 +08002401#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002403 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2404 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002405 rsassa_pss_options.expected_salt_len =
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 mbedtls_hash_info_get_size(md_alg);
2407 if (rsassa_pss_options.expected_salt_len == 0) {
2408 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2409 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2412 peer_pk,
2413 md_alg, hash, hashlen,
2414 p, sig_len);
2415 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002416#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 ret = mbedtls_pk_verify_restartable(peer_pk,
2418 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002421 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002422#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002424#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002426 mbedtls_ssl_send_alert_message(
2427 ssl,
2428 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2430 }
2431 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002432#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002434 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002436#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002438 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002439
2440#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2441 /* We don't need the peer's public key anymore. Free it,
2442 * so that more RAM is available for upcoming expensive
2443 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002445#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002447#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002449exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 ssl->state++;
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002455}
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002458MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002459static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002460{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002461 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002462 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2467 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002468 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002470 }
2471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2473 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002474}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002475#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002476MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002477static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002478{
Janos Follath865b3eb2019-12-16 11:46:15 +00002479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002480 unsigned char *buf;
2481 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002482 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002483 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002484 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002485 size_t sig_alg_len;
2486#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 unsigned char *sig_alg;
2488 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002489#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2494 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002495 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002497 }
2498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2500 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2501 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 }
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2505 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002506 mbedtls_ssl_send_alert_message(
2507 ssl,
2508 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2510 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002513 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002514 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2518 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002521 /* Current message is probably the ServerHelloDone */
2522 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002523 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002524 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002525
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002526 /*
2527 * struct {
2528 * ClientCertificateType certificate_types<1..2^8-1>;
2529 * SignatureAndHashAlgorithm
2530 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2531 * DistinguishedName certificate_authorities<0..2^16-1>;
2532 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002533 *
2534 * Since we only support a single certificate on clients, let's just
2535 * ignore all the information that's supposed to help us pick a
2536 * certificate.
2537 *
2538 * We could check that our certificate matches the request, and bail out
2539 * if it doesn't, but it's simpler to just send the certificate anyway,
2540 * and give the server the opportunity to decide if it should terminate
2541 * the connection when it doesn't like our certificate.
2542 *
2543 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2544 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002545 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002546 *
2547 * However, we still minimally parse the message to check it is at least
2548 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002549 */
Paul Bakker926af752012-11-23 13:38:07 +01002550 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002551
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002552 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2554 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2555 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2556 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2557 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002558 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002560 n = cert_type_len;
2561
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002562 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002563 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002564 * * the length of the signature algorithms field (if minor version of
2565 * SSL is 3),
2566 * * distinguished name length otherwise.
2567 * Both reach at most the index:
2568 * ...hdr_len + 2 + n,
2569 * therefore the buffer length at this point must be greater than that
2570 * regardless of the actual code path.
2571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2573 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2574 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2575 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2576 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002577 }
2578
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002579 /* supported_signature_algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 sig_alg_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2581 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Ronald Cron90915f22022-03-07 11:11:36 +01002582
2583 /*
2584 * The furthest access in buf is in the loop few lines below:
2585 * sig_alg[i + 1],
2586 * where:
2587 * sig_alg = buf + ...hdr_len + 3 + n,
2588 * max(i) = sig_alg_len - 1.
2589 * Therefore the furthest access is:
2590 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2591 * which reduces to:
2592 * buf[...hdr_len + 3 + n + sig_alg_len],
2593 * which is one less than we need the buf to be.
2594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2596 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002597 mbedtls_ssl_send_alert_message(
2598 ssl,
2599 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2601 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002602 }
Paul Bakker926af752012-11-23 13:38:07 +01002603
Ronald Cron90915f22022-03-07 11:11:36 +01002604#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2606 for (size_t i = 0; i < sig_alg_len; i += 2) {
2607 MBEDTLS_SSL_DEBUG_MSG(3,
2608 ("Supported Signature Algorithm found: %02x %02x",
2609 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002610 }
2611#endif
2612
2613 n += 2 + sig_alg_len;
2614
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002615 /* certificate_authorities */
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 dn_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2617 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Paul Bakker926af752012-11-23 13:38:07 +01002618
2619 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2621 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2622 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2623 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2624 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002625 }
2626
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002627#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2629 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002630 unsigned char *p = dn + i + 2;
2631 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002632 size_t asn1_len;
2633 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 memset(&name, 0, sizeof(name));
2635 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2636 if (dni_len > dn_len - i - 2 ||
2637 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2638 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2639 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2640 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002641 mbedtls_ssl_send_alert_message(
2642 ssl,
2643 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2645 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002646 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 MBEDTLS_SSL_DEBUG_MSG(3,
2648 ("DN hint: %.*s",
2649 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2650 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002651 }
2652#endif
2653
Paul Bakker926af752012-11-23 13:38:07 +01002654exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002658}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002659#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002661MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002662static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002663{
Janos Follath865b3eb2019-12-16 11:46:15 +00002664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2669 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2670 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2674 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2675 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2679 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2680 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2681 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2682 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2683 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 }
2685
2686 ssl->state++;
2687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2690 mbedtls_ssl_recv_flight_completed(ssl);
2691 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002692#endif
2693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002697}
2698
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002699MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002700static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002701{
Janos Follath865b3eb2019-12-16 11:46:15 +00002702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002703
2704 size_t header_len;
2705 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002706 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002707 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 /*
2714 * DHM key exchange -- send G^X mod P
2715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002719 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2722 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2723 &ssl->out_msg[header_len], content_len,
2724 ssl->conf->f_rng, ssl->conf->p_rng);
2725 if (ret != 0) {
2726 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2727 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 }
2729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2731 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2734 ssl->handshake->premaster,
2735 MBEDTLS_PREMASTER_SIZE,
2736 &ssl->handshake->pmslen,
2737 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2738 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2739 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 }
2741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2743 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002745#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2746 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2747 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2748 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002750 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2751 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002753#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002754 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2755 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002756 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002757
2758 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2759
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002760 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002763
Hanno Becker4a63ed42019-01-08 11:39:35 +00002764 /*
2765 * Generate EC private key for ECDHE exchange.
2766 */
2767
Hanno Becker4a63ed42019-01-08 11:39:35 +00002768 /* The master secret is obtained from the shared ECDH secret by
2769 * applying the TLS 1.2 PRF with a specific salt and label. While
2770 * the PSA Crypto API encourages combining key agreement schemes
2771 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2772 * yet support the provisioning of salt + label to the KDF.
2773 * For the time being, we therefore need to split the computation
2774 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002775 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2777 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2778 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2779 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002780
2781 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 status = psa_generate_key(&key_attributes,
2783 &handshake->ecdh_psa_privkey);
2784 if (status != PSA_SUCCESS) {
2785 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2786 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002787
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002788 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002789 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002790 * but we just need to add a length byte before that. */
2791 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2792 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002794 size_t own_pubkey_len;
2795
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2797 own_pubkey, own_pubkey_max_len,
2798 &own_pubkey_len);
2799 if (status != PSA_SUCCESS) {
2800 psa_destroy_key(handshake->ecdh_psa_privkey);
Andrzej Kureka0237f82022-02-24 13:24:52 -05002801 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002803 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002804
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002805 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2806 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002807
Hanno Becker4a63ed42019-01-08 11:39:35 +00002808 /* The ECDH secret is the premaster secret used for key derivation. */
2809
Janos Follathdf3b0892019-08-08 11:12:24 +01002810 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2812 handshake->ecdh_psa_privkey,
2813 handshake->ecdh_psa_peerkey,
2814 handshake->ecdh_psa_peerkey_len,
2815 ssl->handshake->premaster,
2816 sizeof(ssl->handshake->premaster),
2817 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Ronald Croncf56a0a2020-08-04 09:51:30 +02002820 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2823 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2824 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002825#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002826 /*
2827 * ECDH key exchange -- send client public value
2828 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002829 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002830
Gilles Peskineeccd8882020-03-10 12:19:08 +01002831#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 if (ssl->handshake->ecrs_enabled) {
2833 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002834 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002838 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002839#endif
2840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2842 &content_len,
2843 &ssl->out_msg[header_len], 1000,
2844 ssl->conf->f_rng, ssl->conf->p_rng);
2845 if (ret != 0) {
2846 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002847#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002849 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002851#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002853 }
2854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2856 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002857
Gilles Peskineeccd8882020-03-10 12:19:08 +01002858#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002860 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002861 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002862 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002863
2864ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002866 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002868#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2870 &ssl->handshake->pmslen,
2871 ssl->handshake->premaster,
2872 MBEDTLS_MPI_MAX_SIZE,
2873 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2874 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002875#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002877 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002879#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002881 }
2882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2884 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002885#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2888 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2889 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2890 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002891#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2892 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002894 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2895 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2896 psa_key_attributes_t key_attributes;
2897
2898 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2899
2900 /*
2901 * opaque psk_identity<0..2^16-1>;
2902 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002904 /* We don't offer PSK suites if we don't have a PSK,
2905 * and we check that the server's choice is among the
2906 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2908 }
Neil Armstrong868af822022-03-09 10:26:25 +01002909
Neil Armstrongfc834f22022-03-23 17:54:38 +01002910 /* uint16 to store content length */
2911 const size_t content_len_size = 2;
2912
Neil Armstrong868af822022-03-09 10:26:25 +01002913 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 if (header_len + content_len_size + ssl->conf->psk_identity_len
2916 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2917 MBEDTLS_SSL_DEBUG_MSG(1,
2918 ("psk identity too long or SSL buffer too short"));
2919 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002920 }
2921
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002922 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2925 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002926 header_len += content_len_size;
2927
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 memcpy(p, ssl->conf->psk_identity,
2929 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002930 p += ssl->conf->psk_identity_len;
2931
Neil Armstrong868af822022-03-09 10:26:25 +01002932 header_len += ssl->conf->psk_identity_len;
2933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002935
2936 /*
2937 * Generate EC private key for ECDHE exchange.
2938 */
2939
2940 /* The master secret is obtained from the shared ECDH secret by
2941 * applying the TLS 1.2 PRF with a specific salt and label. While
2942 * the PSA Crypto API encourages combining key agreement schemes
2943 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2944 * yet support the provisioning of salt + label to the KDF.
2945 * For the time being, we therefore need to split the computation
2946 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2947 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2949 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2950 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2951 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002952
2953 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 status = psa_generate_key(&key_attributes,
2955 &handshake->ecdh_psa_privkey);
2956 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002957 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 }
Neil Armstrong868af822022-03-09 10:26:25 +01002959
2960 /* Export the public part of the ECDH private key from PSA.
2961 * The export format is an ECPoint structure as expected by TLS,
2962 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002963 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002964 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002966 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002967
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2969 own_pubkey, own_pubkey_max_len,
2970 &own_pubkey_len);
2971 if (status != PSA_SUCCESS) {
2972 psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01002973 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002974 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002975 }
2976
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002977 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002978 content_len = own_pubkey_len + 1;
2979
Neil Armstrong25400452022-03-23 17:44:07 +01002980 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2981 * - a uint16 containing the length (in octets) of the ECDH computation
2982 * - the octet string produced by the ECDH computation
2983 * - a uint16 containing the length (in octets) of the PSK
2984 * - the PSK itself
2985 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002986 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 const unsigned char * const pms_end = pms +
2988 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002989 /* uint16 to store length (in octets) of the ECDH computation */
2990 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002991 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002992
Neil Armstrong25400452022-03-23 17:44:07 +01002993 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2995 handshake->ecdh_psa_privkey,
2996 handshake->ecdh_psa_peerkey,
2997 handshake->ecdh_psa_peerkey_len,
2998 pms + zlen_size,
2999 pms_end - (pms + zlen_size),
3000 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01003003 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003006 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003008 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 }
Neil Armstrong868af822022-03-09 10:26:25 +01003010
Neil Armstrong25400452022-03-23 17:44:07 +01003011 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003013 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003015#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3016 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003017#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003019 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003020 * opaque psk_identity<0..2^16-1>;
3021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003023 /* We don't offer PSK suites if we don't have a PSK,
3024 * and we check that the server's choice is among the
3025 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003027 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003028
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003029 header_len = 4;
3030 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3033 MBEDTLS_SSL_DEBUG_MSG(1,
3034 ("psk identity too long or SSL buffer too short"));
3035 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003036 }
3037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3039 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 memcpy(ssl->out_msg + header_len,
3042 ssl->conf->psk_identity,
3043 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003044 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003048 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3053 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3054 &content_len, 2)) != 0) {
3055 return ret;
3056 }
3057 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003061 /*
3062 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3063 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if (header_len + 2 + content_len >
3067 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3068 MBEDTLS_SSL_DEBUG_MSG(1,
3069 ("psk identity or DHM size too long or SSL buffer too short"));
3070 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003071 }
3072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3074 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3077 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3078 &ssl->out_msg[header_len], content_len,
3079 ssl->conf->f_rng, ssl->conf->p_rng);
3080 if (ret != 0) {
3081 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3082 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003083 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003084
3085#if defined(MBEDTLS_USE_PSA_CRYPTO)
3086 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003088 size_t pms_len;
3089
3090 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3092 pms + 2, pms_end - (pms + 2), &pms_len,
3093 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3094 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3095 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003096 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003098 pms += 2 + pms_len;
3099
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003101#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003104#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3106 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003107 /*
3108 * ClientECDiffieHellmanPublic public;
3109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3111 &content_len,
3112 &ssl->out_msg[header_len],
3113 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3114 ssl->conf->f_rng, ssl->conf->p_rng);
3115 if (ret != 0) {
3116 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3117 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003118 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3121 MBEDTLS_DEBUG_ECDH_Q);
3122 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003123#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003124 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3126 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003127 }
3128
Neil Armstrong80f6f322022-05-03 17:56:38 +02003129#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3131 ciphersuite_info->key_exchange)) != 0) {
3132 MBEDTLS_SSL_DEBUG_RET(1,
3133 "mbedtls_ssl_psk_derive_premaster", ret);
3134 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003135 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003136#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003138#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003141 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3143 &content_len, 0)) != 0) {
3144 return ret;
3145 }
3146 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003148#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003150 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003151
Neil Armstrongca7d5062022-05-31 14:43:23 +02003152#if defined(MBEDTLS_USE_PSA_CRYPTO)
3153 unsigned char *out_p = ssl->out_msg + header_len;
3154 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3155 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3157 out_p, end_p - out_p, &content_len,
3158 MBEDTLS_ECJPAKE_ROUND_TWO);
3159 if (ret != 0) {
3160 psa_destroy_key(ssl->handshake->psa_pake_password);
3161 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3162 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3163 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003164 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003165#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3167 ssl->out_msg + header_len,
3168 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3169 &content_len,
3170 ssl->conf->f_rng, ssl->conf->p_rng);
3171 if (ret != 0) {
3172 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3173 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003174 }
3175
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3177 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3178 ssl->conf->f_rng, ssl->conf->p_rng);
3179 if (ret != 0) {
3180 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3181 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003182 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003183#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003185#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003186 {
3187 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3189 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003190 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003192 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3194 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
3196 ssl->state++;
3197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3199 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3200 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 }
3202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003206}
3207
Gilles Peskineeccd8882020-03-10 12:19:08 +01003208#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003209MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003210static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003211{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003212 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003213 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3219 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3220 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003221 }
3222
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3224 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003225 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003227 }
3228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3230 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003231}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003232#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003233MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003234static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003237 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003238 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003239 size_t n = 0, offset = 0;
3240 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003241 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003243 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003244 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003245#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 size_t out_buf_len = ssl->out_buf_len - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003247#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003249#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003252
Gilles Peskineeccd8882020-03-10 12:19:08 +01003253#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 if (ssl->handshake->ecrs_enabled &&
3255 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003256 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003257 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003258#endif
3259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3261 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3262 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003263 }
3264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3266 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003267 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003269 }
3270
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 if (ssl->handshake->client_auth == 0 ||
3272 mbedtls_ssl_own_cert(ssl) == NULL) {
3273 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003274 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (mbedtls_ssl_own_key(ssl) == NULL) {
3279 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3280 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003281 }
3282
3283 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003284 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003286#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003288 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003290
3291sign:
3292#endif
3293
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003294 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3295 if (0 != ret) {
3296 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3297 return ret;
3298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003299
Ronald Cron90915f22022-03-07 11:11:36 +01003300 /*
3301 * digitally-signed struct {
3302 * opaque handshake_messages[handshake_messages_length];
3303 * };
3304 *
3305 * Taking shortcut here. We assume that the server always allows the
3306 * PRF Hash function and has sent it in the allowed signature
3307 * algorithms list received in the Certificate Request message.
3308 *
3309 * Until we encounter a server that does not, we will take this
3310 * shortcut.
3311 *
3312 * Reason: Otherwise we should have running hashes for SHA512 and
3313 * SHA224 in order to satisfy 'weird' needs from the server
3314 * side.
3315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003317 md_alg = MBEDTLS_MD_SHA384;
3318 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003320 md_alg = MBEDTLS_MD_SHA256;
3321 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003322 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003324
3325 /* Info from md_alg will be used instead */
3326 hashlen = 0;
3327 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003328
Gilles Peskineeccd8882020-03-10 12:19:08 +01003329#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003331 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003333#endif
3334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3336 md_alg, hash_start, hashlen,
3337 ssl->out_msg + 6 + offset,
3338 out_buf_len - 6 - offset,
3339 &n,
3340 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3341 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003342#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003344 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003346#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003348 }
Paul Bakker926af752012-11-23 13:38:07 +01003349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003351
Paul Bakker1ef83d62012-04-11 12:09:53 +00003352 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3354 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
3356 ssl->state++;
3357
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3359 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3360 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 }
3362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003366}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003367#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003370MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003371static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003372{
Janos Follath865b3eb2019-12-16 11:46:15 +00003373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003374 uint32_t lifetime;
3375 size_t ticket_len;
3376 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003377 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3382 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3383 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003384 }
3385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3387 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003388 mbedtls_ssl_send_alert_message(
3389 ssl,
3390 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3392 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003393 }
3394
3395 /*
3396 * struct {
3397 * uint32 ticket_lifetime_hint;
3398 * opaque ticket<0..2^16-1>;
3399 * } NewSessionTicket;
3400 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003401 * 0 . 3 ticket_lifetime_hint
3402 * 4 . 5 ticket_len (n)
3403 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3406 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3407 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3408 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3409 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3410 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003411 }
3412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003414
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 lifetime = (((uint32_t) msg[0]) << 24) | (msg[1] << 16) |
3416 (msg[2] << 8) | (msg[3]);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 ticket_len = (msg[4] << 8) | (msg[5]);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3421 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3422 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3423 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3424 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003425 }
3426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003428
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003429 /* We're not waiting for a NewSessionTicket message any more */
3430 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003432
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003433 /*
3434 * Zero-length ticket means the server changed his mind and doesn't want
3435 * to send a ticket after all, so just forget it
3436 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 if (ticket_len == 0) {
3438 return 0;
3439 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003440
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 if (ssl->session != NULL && ssl->session->ticket != NULL) {
3442 mbedtls_platform_zeroize(ssl->session->ticket,
3443 ssl->session->ticket_len);
3444 mbedtls_free(ssl->session->ticket);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003445 ssl->session->ticket = NULL;
3446 ssl->session->ticket_len = 0;
3447 }
3448
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 mbedtls_platform_zeroize(ssl->session_negotiate->ticket,
3450 ssl->session_negotiate->ticket_len);
3451 mbedtls_free(ssl->session_negotiate->ticket);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003452 ssl->session_negotiate->ticket = NULL;
3453 ssl->session_negotiate->ticket_len = 0;
3454
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3456 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3457 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3458 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3459 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003460 }
3461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003463
3464 ssl->session_negotiate->ticket = ticket;
3465 ssl->session_negotiate->ticket_len = ticket_len;
3466 ssl->session_negotiate->ticket_lifetime = lifetime;
3467
3468 /*
3469 * RFC 5077 section 3.4:
3470 * "If the client receives a session ticket from the server, then it
3471 * discards any Session ID that was sent in the ServerHello."
3472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003474 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003475
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003477
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003481
Paul Bakker5121ce52009-01-03 21:22:43 +00003482/*
Paul Bakker1961b702013-01-25 14:49:24 +01003483 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003485int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003486{
3487 int ret = 0;
3488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003490 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3493 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003494 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003495 }
3496#endif
3497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 case MBEDTLS_SSL_HELLO_REQUEST:
3500 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003501 break;
3502
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 /*
3504 * ==> ClientHello
3505 */
3506 case MBEDTLS_SSL_CLIENT_HELLO:
3507 ret = mbedtls_ssl_write_client_hello(ssl);
3508 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 /*
3511 * <== ServerHello
3512 * Certificate
3513 * ( ServerKeyExchange )
3514 * ( CertificateRequest )
3515 * ServerHelloDone
3516 */
3517 case MBEDTLS_SSL_SERVER_HELLO:
3518 ret = ssl_parse_server_hello(ssl);
3519 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3522 ret = mbedtls_ssl_parse_certificate(ssl);
3523 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3526 ret = ssl_parse_server_key_exchange(ssl);
3527 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3530 ret = ssl_parse_certificate_request(ssl);
3531 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003532
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3534 ret = ssl_parse_server_hello_done(ssl);
3535 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003536
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 /*
3538 * ==> ( Certificate/Alert )
3539 * ClientKeyExchange
3540 * ( CertificateVerify )
3541 * ChangeCipherSpec
3542 * Finished
3543 */
3544 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3545 ret = mbedtls_ssl_write_certificate(ssl);
3546 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3549 ret = ssl_write_client_key_exchange(ssl);
3550 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003551
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3553 ret = ssl_write_certificate_verify(ssl);
3554 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003555
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3557 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3558 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003559
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 case MBEDTLS_SSL_CLIENT_FINISHED:
3561 ret = mbedtls_ssl_write_finished(ssl);
3562 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003563
Gilles Peskine449bd832023-01-11 14:50:10 +01003564 /*
3565 * <== ( NewSessionTicket )
3566 * ChangeCipherSpec
3567 * Finished
3568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3571 ret = ssl_parse_new_session_ticket(ssl);
3572 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003573#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003574
Gilles Peskine449bd832023-01-11 14:50:10 +01003575 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3576 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3577 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003578
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 case MBEDTLS_SSL_SERVER_FINISHED:
3580 ret = mbedtls_ssl_parse_finished(ssl);
3581 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003582
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 case MBEDTLS_SSL_FLUSH_BUFFERS:
3584 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3585 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3586 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3589 mbedtls_ssl_handshake_wrapup(ssl);
3590 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003591
Gilles Peskine449bd832023-01-11 14:50:10 +01003592 default:
3593 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3594 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003596
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003598}
Jerry Yuc5aef882021-12-23 20:15:02 +08003599
Jerry Yufb4b6472022-01-27 15:03:26 +08003600#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */