blob: 38c61ba177f30230805c260477f78ea201ee5c2d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS client-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020025
SimonBd5800b72016-04-26 07:43:27 +010026#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010027#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000028#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000029#include "mbedtls/debug.h"
30#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020031#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010032
Hanno Beckerbb89e272019-01-08 12:54:37 +000033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020035#include "psa/crypto.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050036#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
37 psa_to_ssl_errors, \
38 psa_generic_status_to_mbedtls)
Hanno Beckerbb89e272019-01-08 12:54:37 +000039#endif /* MBEDTLS_USE_PSA_CRYPTO */
40
SimonBd5800b72016-04-26 07:43:27 +010041#include <string.h>
42
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020043#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010046#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050050#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020051#endif
52
Andrzej Kurek0ce59212022-08-17 07:54:34 -040053#include "hash_info.h"
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020056MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010057static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
58 unsigned char *buf,
59 const unsigned char *end,
60 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010061{
62 unsigned char *p = buf;
63
64 *olen = 0;
65
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010066 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010067 * initial ClientHello, in which case also adding the renegotiation
68 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
70 return 0;
71 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 MBEDTLS_SSL_DEBUG_MSG(3,
74 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010077
Paul Bakkerd3edc862013-03-20 16:07:17 +010078 /*
79 * Secure renegotiation
80 */
Gilles Peskine449bd832023-01-11 14:50:10 +010081 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010082 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010083
84 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
86 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010089
90 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010095
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +020096#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010097 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010098
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020099MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100100static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
101 unsigned char *buf,
102 const unsigned char *end,
103 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100104{
105 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100106 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100107
108 *olen = 0;
109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 MBEDTLS_SSL_DEBUG_MSG(3,
111 ("client hello, adding supported_point_formats extension"));
112 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100115 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
117 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200119
120 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100122
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200123 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100126}
Darryl Green11999bb2018-03-13 15:22:58 +0000127#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100128 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100129
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200130#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200131MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100132static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
133 unsigned char *buf,
134 const unsigned char *end,
135 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200136{
Janos Follath865b3eb2019-12-16 11:46:15 +0000137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200138 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100139 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200140
141 *olen = 0;
142
143 /* Skip costly extension if we can't use EC J-PAKE anyway */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
146 return 0;
147 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200148#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
150 return 0;
151 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200152#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 MBEDTLS_SSL_DEBUG_MSG(3,
155 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100160 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200161
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200162 /*
163 * We may need to send ClientHello multiple times for Hello verification.
164 * We don't want to compute fresh values every time (both for performance
165 * and consistency reasons), so cache the extension content.
166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (ssl->handshake->ecjpake_cache == NULL ||
168 ssl->handshake->ecjpake_cache_len == 0) {
169 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200170
Neil Armstrongca7d5062022-05-31 14:43:23 +0200171#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100172 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 p + 2, end - p - 2, &kkpp_len,
174 MBEDTLS_ECJPAKE_ROUND_ONE);
175 if (ret != 0) {
176 psa_destroy_key(ssl->handshake->psa_pake_password);
177 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
178 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
179 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200180 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200181#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
183 p + 2, end - p - 2, &kkpp_len,
184 ssl->conf->f_rng, ssl->conf->p_rng);
185 if (ret != 0) {
186 MBEDTLS_SSL_DEBUG_RET(1,
187 "mbedtls_ecjpake_write_round_one", ret);
188 return ret;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200189 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200190#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
193 if (ssl->handshake->ecjpake_cache == NULL) {
194 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
195 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200196 }
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200199 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 } else {
201 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200202
203 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200207 }
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100210 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200211
212 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200215}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200216#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000217
Hanno Beckera0e20d02019-05-15 14:03:01 +0100218#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200219MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100220static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
221 unsigned char *buf,
222 const unsigned char *end,
223 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100224{
225 unsigned char *p = buf;
226 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100227
228 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100229 * struct {
230 * opaque cid<0..2^8-1>;
231 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100233
234 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
236 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
237 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100238 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100240
241 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
242 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100244
245 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100247 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100248 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100250 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100251
252 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100254
255 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100258}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100259#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200262MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100263static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
264 unsigned char *buf,
265 const unsigned char *end,
266 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200267{
268 unsigned char *p = buf;
269
Simon Butcher0fc94e92015-09-28 20:52:04 +0100270 *olen = 0;
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
273 return 0;
274 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 MBEDTLS_SSL_DEBUG_MSG(3,
277 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100282 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200283
284 *p++ = 0x00;
285 *p++ = 1;
286
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200287 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200288
289 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200296MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100297static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
298 unsigned char *buf,
299 const unsigned char *end,
300 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100301{
302 unsigned char *p = buf;
303
Simon Butcher0fc94e92015-09-28 20:52:04 +0100304 *olen = 0;
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
307 return 0;
308 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 MBEDTLS_SSL_DEBUG_MSG(3,
311 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100316 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100317
318 *p++ = 0x00;
319 *p++ = 0x00;
320
321 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200328MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
330 unsigned char *buf,
331 const unsigned char *end,
332 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200333{
334 unsigned char *p = buf;
335
Simon Butcher0fc94e92015-09-28 20:52:04 +0100336 *olen = 0;
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
339 return 0;
340 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 MBEDTLS_SSL_DEBUG_MSG(3,
343 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100348 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200349
350 *p++ = 0x00;
351 *p++ = 0x00;
352
353 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200360MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100361static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
362 unsigned char *buf,
363 const unsigned char *end,
364 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200365{
366 unsigned char *p = buf;
367 size_t tlen = ssl->session_negotiate->ticket_len;
368
Simon Butcher0fc94e92015-09-28 20:52:04 +0100369 *olen = 0;
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
372 return 0;
373 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 MBEDTLS_SSL_DEBUG_MSG(3,
376 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200377
Hanno Becker261602c2017-04-12 14:54:42 +0100378 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100382 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100385 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200386
387 *olen = 4;
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
390 return 0;
391 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 MBEDTLS_SSL_DEBUG_MSG(3,
394 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200397
398 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200403
Ron Eldora9788042018-12-05 11:04:31 +0200404#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200405MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100406static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
407 unsigned char *buf,
408 const unsigned char *end,
409 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100410{
411 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200412 size_t protection_profiles_index = 0, ext_len = 0;
413 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100414
415 *olen = 0;
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
418 (ssl->conf->dtls_srtp_profile_list == NULL) ||
419 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
420 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100421 }
422
Ron Eldora9788042018-12-05 11:04:31 +0200423 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100424 * uint8 SRTPProtectionProfile[2];
425 *
426 * struct {
427 * SRTPProtectionProfiles SRTPProtectionProfiles;
428 * opaque srtp_mki<0..255>;
429 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100430 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100431 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200433 mki_len = ssl->dtls_srtp_info.mki_len;
434 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300435 /* Extension length = 2 bytes for profiles length,
436 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
437 * 1 byte for srtp_mki vector length and the mki_len value
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200442
443 /* Check there is room in the buffer for the extension + 4 bytes
444 * - the extension tag (2 bytes)
445 * - the extension length (2 bytes)
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100450 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100453 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100454
Ron Eldor3adb9922017-12-21 10:15:08 +0200455 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200456 /* micro-optimization:
457 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
458 * which is lower than 127, so the upper byte of the length is always 0
459 * For the documentation, the more generic code is left in comments
460 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
461 * >> 8 ) & 0xFF );
462 */
463 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300467 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200469 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
471 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
472 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
473 profile_value));
474 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100475 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200477 /*
478 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200479 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200480 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 MBEDTLS_SSL_DEBUG_MSG(3,
482 ("client hello, "
483 "illegal DTLS-SRTP protection profile %d",
484 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
485 ));
486 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100487 }
488 }
489
Ron Eldor591f1622018-01-22 12:30:04 +0200490 *p++ = mki_len & 0xFF;
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (mki_len != 0) {
493 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200494 /*
495 * Increment p to point to the current position.
496 */
497 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
499 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200500 }
501
Ron Eldoref72faf2018-07-12 11:54:20 +0300502 /*
503 * total extension length: extension type (2 bytes)
504 * + extension length (2 bytes)
505 * + protection profile length (2 bytes)
506 * + 2 * number of protection profiles
507 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200508 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300509 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200510 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100513}
514#endif /* MBEDTLS_SSL_DTLS_SRTP */
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
517 unsigned char *buf,
518 const unsigned char *end,
519 int uses_ec,
520 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100521{
522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
523 unsigned char *p = buf;
524 size_t ext_len = 0;
525
526 (void) ssl;
527 (void) end;
528 (void) uses_ec;
529 (void) ret;
530 (void) ext_len;
531
532 *out_len = 0;
533
534 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
535 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
536#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
538 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
539 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100540 }
541 p += ext_len;
542#endif
543
544#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
545 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (uses_ec) {
547 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
548 &ext_len)) != 0) {
549 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
550 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100551 }
552 p += ext_len;
553 }
554#endif
555
556#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
558 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
559 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100560 }
561 p += ext_len;
562#endif
563
564#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
566 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
567 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100568 }
569 p += ext_len;
570#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
571
572#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
574 &ext_len)) != 0) {
575 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
576 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100577 }
578 p += ext_len;
579#endif
580
581#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
583 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
584 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100585 }
586 p += ext_len;
587#endif
588
589#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
591 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
592 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100593 }
594 p += ext_len;
595#endif
596
597#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
599 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
600 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100601 }
602 p += ext_len;
603#endif
604
605#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
607 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
608 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100609 }
610 p += ext_len;
611#endif
612
613 *out_len = p - buf;
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100616}
617
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200618MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100619static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
620 const unsigned char *buf,
621 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100625 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000627 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 mbedtls_ct_memcmp(buf + 1,
629 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
630 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
631 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
632 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100633 mbedtls_ssl_send_alert_message(
634 ssl,
635 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
637 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000638 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100641 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (len != 1 || buf[0] != 0x00) {
643 MBEDTLS_SSL_DEBUG_MSG(1,
644 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100645 mbedtls_ssl_send_alert_message(
646 ssl,
647 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
649 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100650 }
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100653 }
Paul Bakker48916f92012-09-16 19:57:18 +0000654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000656}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200659MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100660static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
661 const unsigned char *buf,
662 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200663{
664 /*
665 * server should use the extension only if we did,
666 * and if so the server's value should match ours (and len is always 1)
667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200669 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 buf[0] != ssl->conf->mfl_code) {
671 MBEDTLS_SSL_DEBUG_MSG(1,
672 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100673 mbedtls_ssl_send_alert_message(
674 ssl,
675 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
677 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200678 }
679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000683
Hanno Beckera0e20d02019-05-15 14:03:01 +0100684#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200685MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100686static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
687 const unsigned char *buf,
688 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100689{
690 size_t peer_cid_len;
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100693 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
694 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
696 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
697 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
698 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
699 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100700 }
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (len == 0) {
703 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
704 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
705 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
706 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100707 }
708
709 peer_cid_len = *buf++;
710 len--;
711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
713 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
714 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
715 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
716 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100717 }
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if (len != peer_cid_len) {
720 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
721 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
722 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
723 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100724 }
725
Hanno Becker5a299902019-05-03 12:47:49 +0100726 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100727 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
731 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100734}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100735#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200738MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100739static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
740 const unsigned char *buf,
741 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100742{
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
744 len != 0) {
745 MBEDTLS_SSL_DEBUG_MSG(1,
746 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100747 mbedtls_ssl_send_alert_message(
748 ssl,
749 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
751 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100752 }
753
754 ((void) buf);
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200763MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100764static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
765 const unsigned char *buf,
766 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200767{
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
769 len != 0) {
770 MBEDTLS_SSL_DEBUG_MSG(1,
771 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100772 mbedtls_ssl_send_alert_message(
773 ssl,
774 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
776 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200777 }
778
779 ((void) buf);
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100789static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
790 const unsigned char *buf,
791 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200792{
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
794 len != 0) {
795 MBEDTLS_SSL_DEBUG_MSG(1,
796 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100797 mbedtls_ssl_send_alert_message(
798 ssl,
799 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
801 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200802 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200803
804 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200805
806 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200809}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200811
Robert Cragie136884c2015-10-02 13:34:31 +0100812#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100813 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200814MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100815static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
816 const unsigned char *buf,
817 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200818{
819 size_t list_size;
820 const unsigned char *p;
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (len == 0 || (size_t) (buf[0] + 1) != len) {
823 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
824 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
825 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
826 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200827 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200828 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200829
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200830 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 while (list_size > 0) {
832 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
833 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Valerio Setti46423162023-03-27 14:33:27 +0200834#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200835 ssl->handshake->ecdh_ctx.point_format = p[0];
Valerio Setti77a904c2023-03-24 07:28:49 +0100836#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_ECDH_C */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200837#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
839 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
840 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200841#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
843 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200844 }
845
846 list_size--;
847 p++;
848 }
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
851 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
852 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
853 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200854}
Darryl Green11999bb2018-03-13 15:22:58 +0000855#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100856 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200857
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200858#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200859MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100860static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
861 const unsigned char *buf,
862 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200863{
Janos Follath865b3eb2019-12-16 11:46:15 +0000864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (ssl->handshake->ciphersuite_info->key_exchange !=
867 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
868 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
869 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200870 }
871
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200872 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200874 ssl->handshake->ecjpake_cache = NULL;
875 ssl->handshake->ecjpake_cache_len = 0;
876
Neil Armstrongca7d5062022-05-31 14:43:23 +0200877#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if ((ret = mbedtls_psa_ecjpake_read_round(
879 &ssl->handshake->psa_pake_ctx, buf, len,
880 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
881 psa_destroy_key(ssl->handshake->psa_pake_password);
882 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100885 mbedtls_ssl_send_alert_message(
886 ssl,
887 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
889 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200890 }
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 return 0;
893#else
894 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
895 buf, len)) != 0) {
896 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
897 mbedtls_ssl_send_alert_message(
898 ssl,
899 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
900 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
901 return ret;
902 }
903
904 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200905#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200906}
907#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200910MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100911static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
912 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200913{
914 size_t list_len, name_len;
915 const char **p;
916
917 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (ssl->conf->alpn_list == NULL) {
919 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100920 mbedtls_ssl_send_alert_message(
921 ssl,
922 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
924 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200925 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200926
927 /*
928 * opaque ProtocolName<1..2^8-1>;
929 *
930 * struct {
931 * ProtocolName protocol_name_list<2..2^16-1>
932 * } ProtocolNameList;
933 *
934 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
935 */
936
937 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (len < 4) {
939 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
940 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
941 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200942 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 list_len = (buf[0] << 8) | buf[1];
945 if (list_len != len - 2) {
946 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
947 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
948 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200949 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200950
951 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (name_len != list_len - 1) {
953 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
954 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
955 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200956 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200957
958 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
960 if (name_len == strlen(*p) &&
961 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200962 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200964 }
965 }
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
968 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
969 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
970 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200973
Johan Pascalb62bb512015-12-03 21:56:45 +0100974#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200975MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
977 const unsigned char *buf,
978 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100979{
Johan Pascal43f94902020-09-22 12:25:52 +0200980 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200981 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100982 uint16_t server_protection_profile_value = 0;
983
984 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
986 (ssl->conf->dtls_srtp_profile_list == NULL) ||
987 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
988 return 0;
989 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100990
Ron Eldora9788042018-12-05 11:04:31 +0200991 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100992 * uint8 SRTPProtectionProfile[2];
993 *
994 * struct {
995 * SRTPProtectionProfiles SRTPProtectionProfiles;
996 * opaque srtp_mki<0..255>;
997 * } UseSRTPData;
998
999 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1000 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001003 mki_len = ssl->dtls_srtp_info.mki_len;
1004 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001005
Ron Eldoref72faf2018-07-12 11:54:20 +03001006 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001007 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1008 * + protection profile (2 bytes)
1009 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001010 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if ((len < 5) || (len != (buf[4] + 5u))) {
1013 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1014 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001015
1016 /*
1017 * get the server protection profile
1018 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001019
1020 /*
1021 * protection profile length must be 0x0002 as we must have only
1022 * one protection profile in server Hello
1023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if ((buf[0] != 0) || (buf[1] != 2)) {
1025 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1026 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001029 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 server_protection_profile_value);
1031 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1032 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1033 mbedtls_ssl_get_srtp_profile_as_string(
1034 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001035 }
1036
Johan Pascal43f94902020-09-22 12:25:52 +02001037 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001038
Johan Pascalb62bb512015-12-03 21:56:45 +01001039 /*
1040 * Check we have the server profile in our list
1041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1043 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001044 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001046 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001048 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001049 }
1050 }
1051
Ron Eldor591f1622018-01-22 12:30:04 +02001052 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1054 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1055 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1056 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001057 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001058
1059 /* If server does not use mki in its reply, make sure the client won't keep
1060 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001062 ssl->dtls_srtp_info.mki_len = 0;
1063 }
1064
Ron Eldoref72faf2018-07-12 11:54:20 +03001065 /*
1066 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001067 * If the client detects a nonzero-length MKI in the server's response
1068 * that is different than the one the client offered, then the client
1069 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (len > 5 && (buf[4] != mki_len ||
1072 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1073 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1074 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1075 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001077#if defined(MBEDTLS_DEBUG_C)
1078 if (len > 5) {
1079 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1080 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001081 }
1082#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001084}
1085#endif /* MBEDTLS_SSL_DTLS_SRTP */
1086
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001087/*
1088 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001092static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001093{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001094 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001096 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001097
1098#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1099 uint8_t cookie_len;
1100#else
1101 uint16_t cookie_len;
1102#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001105
Gilles Peskineb64bf062019-09-27 14:02:44 +02001106 /* Check that there is enough room for:
1107 * - 2 bytes of version
1108 * - 1 byte of cookie_len
1109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1111 MBEDTLS_SSL_DEBUG_MSG(1,
1112 ("incoming HelloVerifyRequest message is too short"));
1113 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1114 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1115 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001116 }
1117
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001118 /*
1119 * struct {
1120 * ProtocolVersion server_version;
1121 * opaque cookie<0..2^8-1>;
1122 * } HelloVerifyRequest;
1123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1125 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001126 p += 2;
1127
TRodziewicz2d8800e2021-05-13 19:14:19 +02001128 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001129 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1130 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1131 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1134 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1137 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001140 }
1141
1142 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1144 MBEDTLS_SSL_DEBUG_MSG(1,
1145 ("cookie length does not match incoming message size"));
1146 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1147 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1148 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001149 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1155 if (ssl->handshake->cookie == NULL) {
1156 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1157 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001158 }
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001161 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001162
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001163 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001165 ret = mbedtls_ssl_reset_checksum(ssl);
1166 if (0 != ret) {
1167 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1168 return ret;
1169 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001179MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001180static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001181{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001182 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001183 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001184 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001185 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001186 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001188 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001189#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001190 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001196 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1198 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
1200
Hanno Becker79594fd2019-05-08 09:38:41 +01001201 buf = ssl->in_msg;
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001206 ssl->renego_records_seen++;
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (ssl->conf->renego_max_records >= 0 &&
1209 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1210 MBEDTLS_SSL_DEBUG_MSG(1,
1211 ("renegotiation requested, but not honored by server"));
1212 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001213 }
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 MBEDTLS_SSL_DEBUG_MSG(1,
1216 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001217
1218 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001224 mbedtls_ssl_send_alert_message(
1225 ssl,
1226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1228 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 }
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1233 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1234 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1235 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1236 return ssl_parse_hello_verify_request(ssl);
1237 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001238 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001240 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001241 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001242 }
1243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1247 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1248 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1249 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1250 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1251 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 }
1253
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001254 /*
1255 * 0 . 1 server_version
1256 * 2 . 33 random (maybe including 4 bytes of Unix time)
1257 * 34 . 34 session_id length = n
1258 * 35 . 34+n session_id
1259 * 35+n . 36+n cipher_suite
1260 * 37+n . 37+n compression_method
1261 *
1262 * 38+n . 39+n extensions length (optional)
1263 * 40+n . .. extensions
1264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
1268 ssl->tls_version = mbedtls_ssl_read_version(buf, ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001269 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if (ssl->tls_version < ssl->conf->min_tls_version ||
1272 ssl->tls_version > ssl->conf->max_tls_version) {
1273 MBEDTLS_SSL_DEBUG_MSG(1,
1274 (
1275 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1276 (unsigned) ssl->conf->min_tls_version,
1277 (unsigned) ssl->tls_version,
1278 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1281 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001284 }
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1287 ((unsigned long) buf[2] << 24) |
1288 ((unsigned long) buf[3] << 16) |
1289 ((unsigned long) buf[4] << 8) |
1290 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001294 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (n > 32) {
1299 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1300 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1301 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1302 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
1306 ext_len = ((buf[38 + n] << 8)
1307 | (buf[39 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if ((ext_len > 0 && ext_len < 4) ||
1310 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1311 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001312 mbedtls_ssl_send_alert_message(
1313 ssl,
1314 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1316 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001317 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001319 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 } else {
1321 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1322 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1323 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1324 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001327 /* ciphersuite (used later) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 i = (buf[35 + n] << 8) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001329
1330 /*
1331 * Read and check compression
1332 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001333 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1336 MBEDTLS_SSL_DEBUG_MSG(1,
1337 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001338 mbedtls_ssl_send_alert_message(
1339 ssl,
1340 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1342 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001343 }
1344
Paul Bakker380da532012-04-18 16:10:25 +00001345 /*
1346 * Initialize update checksum functions
1347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1349 if (ssl->handshake->ciphersuite_info == NULL) {
1350 MBEDTLS_SSL_DEBUG_MSG(1,
1351 ("ciphersuite info for %04x not found", (unsigned int) i));
1352 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1353 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1354 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001355 }
Paul Bakker380da532012-04-18 16:10:25 +00001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1360 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 /*
1363 * Check if the session can be resumed
1364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_SSL_RENEGOTIATION)
1367 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001368#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001369 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001370 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001373 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001376#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001377 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001378 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 memcpy(ssl->session_negotiate->id, buf + 35, n);
1380 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1385 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1388 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1389 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Andrzej Kurek03bac442018-04-25 05:06:07 -04001391 /*
1392 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001393 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 while (1) {
1396 if (ssl->conf->ciphersuite_list[i] == 0) {
1397 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001398 mbedtls_ssl_send_alert_message(
1399 ssl,
1400 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1402 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 }
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (ssl->conf->ciphersuite_list[i++] ==
1406 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
1410
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001411 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 ssl->session_negotiate->ciphersuite);
1413 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1414 ssl->tls_version) != 0) {
1415 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001416 mbedtls_ssl_send_alert_message(
1417 ssl,
1418 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1420 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001421 }
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 MBEDTLS_SSL_DEBUG_MSG(3,
1424 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001425
Gilles Peskineeccd8882020-03-10 12:19:08 +01001426#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1428 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001429 ssl->handshake->ecrs_enabled = 1;
1430 }
1431#endif
1432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1434 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001435 mbedtls_ssl_send_alert_message(
1436 ssl,
1437 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1439 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001442 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 MBEDTLS_SSL_DEBUG_MSG(2,
1445 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1446 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 while (ext_len) {
1449 unsigned int ext_id = ((ext[0] << 8)
1450 | (ext[1]));
1451 unsigned int ext_size = ((ext[2] << 8)
1452 | (ext[3]));
Paul Bakker48916f92012-09-16 19:57:18 +00001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (ext_size + 4 > ext_len) {
1455 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001456 mbedtls_ssl_send_alert_message(
1457 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1459 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001460 }
1461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 switch (ext_id) {
1463 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1464 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001467#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1470 ext_size)) != 0) {
1471 return ret;
1472 }
Paul Bakker48916f92012-09-16 19:57:18 +00001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1478 MBEDTLS_SSL_DEBUG_MSG(3,
1479 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1482 ext + 4, ext_size)) != 0) {
1483 return ret;
1484 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001488
Hanno Beckera0e20d02019-05-15 14:03:01 +01001489#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 case MBEDTLS_TLS_EXT_CID:
1491 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if ((ret = ssl_parse_cid_ext(ssl,
1494 ext + 4,
1495 ext_size)) != 0) {
1496 return ret;
1497 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001500#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1504 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1507 ext + 4, ext_size)) != 0) {
1508 return ret;
1509 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1516 MBEDTLS_SSL_DEBUG_MSG(3,
1517 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if ((ret = ssl_parse_extended_ms_ext(ssl,
1520 ext + 4, ext_size)) != 0) {
1521 return ret;
1522 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1529 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 if ((ret = ssl_parse_session_ticket_ext(ssl,
1532 ext + 4, ext_size)) != 0) {
1533 return ret;
1534 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001538
Robert Cragie136884c2015-10-02 13:34:31 +01001539#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1541 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1542 MBEDTLS_SSL_DEBUG_MSG(3,
1543 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1546 ext + 4, ext_size)) != 0) {
1547 return ret;
1548 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001551#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1552 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001553
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001554#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1556 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1559 ext + 4, ext_size)) != 0) {
1560 return ret;
1561 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001564#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 case MBEDTLS_TLS_EXT_ALPN:
1568 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1571 return ret;
1572 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001576
Johan Pascalb62bb512015-12-03 21:56:45 +01001577#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 case MBEDTLS_TLS_EXT_USE_SRTP:
1579 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1582 return ret;
1583 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001586#endif /* MBEDTLS_SSL_DTLS_SRTP */
1587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 default:
1589 MBEDTLS_SSL_DEBUG_MSG(3,
1590 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001591 }
1592
1593 ext_len -= 4 + ext_size;
1594 ext += 4 + ext_size;
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (ext_len > 0 && ext_len < 4) {
1597 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1598 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001599 }
1600 }
1601
1602 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001603 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1604 * extensions. It sets the transform data for the resumed session which in
1605 * case of DTLS includes the server CID extracted from the CID extension.
1606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (ssl->handshake->resume) {
1608 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1609 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001610 mbedtls_ssl_send_alert_message(
1611 ssl,
1612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1614 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001615 }
1616 }
1617
Paul Bakker48916f92012-09-16 19:57:18 +00001618 /*
1619 * Renegotiation security checks
1620 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001622 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1624 MBEDTLS_SSL_DEBUG_MSG(1,
1625 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001626 handshake_failure = 1;
1627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 renegotiation_info_seen == 0) {
1632 MBEDTLS_SSL_DEBUG_MSG(1,
1633 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001634 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1636 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1637 ssl->conf->allow_legacy_renegotiation ==
1638 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1639 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001640 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1642 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1643 renegotiation_info_seen == 1) {
1644 MBEDTLS_SSL_DEBUG_MSG(1,
1645 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001646 handshake_failure = 1;
1647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001651 mbedtls_ssl_send_alert_message(
1652 ssl,
1653 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1655 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001661}
1662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1664 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001665MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001666static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1667 unsigned char **p,
1668 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001671 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001672
Paul Bakker29e1f122013-04-16 13:07:56 +02001673 /*
1674 * Ephemeral DH parameters:
1675 *
1676 * struct {
1677 * opaque dh_p<1..2^16-1>;
1678 * opaque dh_g<1..2^16-1>;
1679 * opaque dh_Ys<1..2^16-1>;
1680 * } ServerDHParams;
1681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1683 p, end)) != 0) {
1684 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1685 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001686 }
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1689 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1690 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1691 dhm_actual_bitlen,
1692 ssl->conf->dhm_min_bitlen));
1693 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001694 }
1695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1697 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1698 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1703 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001704
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001705#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001706#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1707 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1708 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001709MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001710static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1711 unsigned char **p,
1712 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001713{
1714 uint16_t tls_id;
1715 uint8_t ecpoint_len;
1716 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001717 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001718 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001719
1720 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001721 * struct {
1722 * ECParameters curve_params;
1723 * ECPoint public;
1724 * } ServerECDHParams;
1725 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001726 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001727 * 2..3 NamedCurve
1728 * 4 ECPoint.len
1729 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001730 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if (end - *p < 4) {
1732 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1733 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001734
1735 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1737 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1738 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001739
1740 /* Next two bytes are the namedcurve value */
1741 tls_id = *(*p)++;
1742 tls_id <<= 8;
1743 tls_id |= *(*p)++;
1744
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001745 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1747 MBEDTLS_SSL_DEBUG_MSG(2,
1748 ("bad server key exchange message (ECDHE curve): %u",
1749 (unsigned) tls_id));
1750 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001751 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001752
Valerio Setti40d9ca92023-01-04 16:08:04 +01001753 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001754 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1756 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001757 }
Przemek Stekielda4fba62023-06-02 14:52:28 +02001758 handshake->ecdh_psa_type = key_type;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001759 handshake->ecdh_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001760
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001761 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001762 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if ((size_t) (end - *p) < ecpoint_len) {
1764 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1765 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001766
Przemek Stekiel24e50d32023-05-19 10:21:38 +02001767#if !defined(PSA_WANT_ALG_FFDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if (ecpoint_len > sizeof(handshake->ecdh_psa_peerkey)) {
1769 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1770 }
Przemek Stekiel24e50d32023-05-19 10:21:38 +02001771#endif
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;
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001992
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001993#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1994 peer_pk = &ssl->handshake->peer_pubkey;
1995#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001997 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1999 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002000 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002001 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2002#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002003
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002004 /* This is a public key, so it can't be opaque, so can_do() is a good
2005 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2007 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2008 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002009 }
2010
Valerio Setti97207782023-05-18 18:59:06 +02002011#if defined(MBEDTLS_ECP_C)
2012 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
2013#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002014
Przemek Stekielea4000f2022-03-16 09:49:33 +01002015#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002016 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02002017 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti97207782023-05-18 18:59:06 +02002018 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002019
Valerio Setti97207782023-05-18 18:59:06 +02002020 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2022 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002023 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002024
Valerio Setti97207782023-05-18 18:59:06 +02002025 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (tls_id == 0) {
2027 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002028 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002030 }
2031
Valerio Setti1e868cc2023-01-09 17:30:01 +01002032 /* If the above conversion to TLS ID was fine, then also this one will be,
2033 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02002034 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 &ssl->handshake->ecdh_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002036
Przemek Stekielda4fba62023-06-02 14:52:28 +02002037 ssl->handshake->ecdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002038
Przemek Stekielea4000f2022-03-16 09:49:33 +01002039 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002040#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
2041 memcpy(ssl->handshake->ecdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2042 ssl->handshake->ecdh_psa_peerkey_len = peer_pk->pub_raw_len;
2043 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002044#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002045 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2047 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2048 ssl->handshake->ecdh_psa_peerkey,
2049 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 if (ret != 0) {
2052 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2053 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002054 }
Przemek Stekiel561a4232022-03-16 13:16:24 +01002055 ssl->handshake->ecdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002056#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2057#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2059 MBEDTLS_ECDH_THEIRS)) != 0) {
2060 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2061 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002062 }
2063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (ssl_check_server_ecdh_params(ssl) != 0) {
2065 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2066 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002067 }
Valerio Setti97207782023-05-18 18:59:06 +02002068#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002069#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2070 /* We don't need the peer's public key anymore. Free it,
2071 * so that more RAM is available for upcoming expensive
2072 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002074#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002077}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2079 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002080
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002081MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002082static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002083{
Janos Follath865b3eb2019-12-16 11:46:15 +00002084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002085 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002086 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002087 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2093 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002097 ((void) p);
2098 ((void) end);
2099#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2102 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2104 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2105 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2106 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002107 mbedtls_ssl_send_alert_message(
2108 ssl,
2109 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2111 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002112 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002115 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002117 }
2118 ((void) p);
2119 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2121 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002122
Gilles Peskineeccd8882020-03-10 12:19:08 +01002123#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (ssl->handshake->ecrs_enabled &&
2125 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002126 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002127 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002128#endif
2129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2131 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2132 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 }
2134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2136 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002137 mbedtls_ssl_send_alert_message(
2138 ssl,
2139 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2141 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 }
2143
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002144 /*
2145 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2146 * doesn't use a psk_identity_hint
2147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2149 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2150 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002151 /* Current message is probably either
2152 * CertificateRequest or ServerHelloDone */
2153 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002154 goto exit;
2155 }
2156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 MBEDTLS_SSL_DEBUG_MSG(1,
2158 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002159 mbedtls_ssl_send_alert_message(
2160 ssl,
2161 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 }
2166
Gilles Peskineeccd8882020-03-10 12:19:08 +01002167#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002169 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002171
2172start_processing:
2173#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002175 end = ssl->in_msg + ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, end - p);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002177
Gilles Peskineeccd8882020-03-10 12:19:08 +01002178#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2181 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2183 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2184 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002185 mbedtls_ssl_send_alert_message(
2186 ssl,
2187 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2189 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002190 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002191 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002192#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2195 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2197 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002198 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2201 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2202#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2203 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2205 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2206 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2207 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002208 mbedtls_ssl_send_alert_message(
2209 ssl,
2210 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2212 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002213 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2216 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002217#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2218 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2223 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2224 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002225 mbedtls_ssl_send_alert_message(
2226 ssl,
2227 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2229 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002230 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2233 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2234 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002235#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002237#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002238 /*
2239 * The first 3 bytes are:
2240 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2241 * [1, 2] elliptic curve's TLS ID
2242 *
2243 * However since we only support secp256r1 for now, we check only
2244 * that TLS ID here
2245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002247 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if (exp_tls_id == 0) {
2251 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002252 }
2253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2255 (read_tls_id != exp_tls_id)) {
2256 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002257 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002258
2259 p += 3;
2260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if ((ret = mbedtls_psa_ecjpake_read_round(
2262 &ssl->handshake->psa_pake_ctx, p, end - p,
2263 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2264 psa_destroy_key(ssl->handshake->psa_pake_password);
2265 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002268 mbedtls_ssl_send_alert_message(
2269 ssl,
2270 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2272 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002273 }
2274#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2276 p, end - p);
2277 if (ret != 0) {
2278 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002279 mbedtls_ssl_send_alert_message(
2280 ssl,
2281 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2283 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002284 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002285#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002287#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002288 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2290 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002291 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002292
Gilles Peskineeccd8882020-03-10 12:19:08 +01002293#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002295 size_t sig_len, hashlen;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002296 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
2297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2299 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002301 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002302 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002303 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002306
Jerry Yu693a47a2022-06-23 14:02:28 +08002307#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2308 peer_pk = &ssl->handshake->peer_pubkey;
2309#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002311 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2313 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002314 }
2315 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2316#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2317
Paul Bakker29e1f122013-04-16 13:07:56 +02002318 /*
2319 * Handle the digitally-signed structure
2320 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2322 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2323 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2324 sig_alg, &pk_alg, &md_alg) != 0 &&
2325 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2326 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2327 MBEDTLS_SSL_DEBUG_MSG(1,
2328 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002329 mbedtls_ssl_send_alert_message(
2330 ssl,
2331 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2333 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002334 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002335 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2338 MBEDTLS_SSL_DEBUG_MSG(1,
2339 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002340 mbedtls_ssl_send_alert_message(
2341 ssl,
2342 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2344 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002345 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002346
2347 /*
2348 * Read signature
2349 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (p > end - 2) {
2352 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002353 mbedtls_ssl_send_alert_message(
2354 ssl,
2355 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2357 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002358 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 sig_len = (p[0] << 8) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002360 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (p != end - sig_len) {
2363 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002364 mbedtls_ssl_send_alert_message(
2365 ssl,
2366 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2368 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002372
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002373 /*
2374 * Compute the hash that has been signed
2375 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 if (md_alg != MBEDTLS_MD_NONE) {
2377 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2378 params, params_len,
2379 md_alg);
2380 if (ret != 0) {
2381 return ret;
2382 }
2383 } else {
2384 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2385 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002386 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002389
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002390 /*
2391 * Verify signature
2392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2394 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002395 mbedtls_ssl_send_alert_message(
2396 ssl,
2397 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2399 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002400 }
2401
Gilles Peskineeccd8882020-03-10 12:19:08 +01002402#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002404 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002406#endif
2407
Jerry Yu693a47a2022-06-23 14:02:28 +08002408#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002410 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2411 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002412 rsassa_pss_options.expected_salt_len =
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 mbedtls_hash_info_get_size(md_alg);
2414 if (rsassa_pss_options.expected_salt_len == 0) {
2415 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2416 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2419 peer_pk,
2420 md_alg, hash, hashlen,
2421 p, sig_len);
2422 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002423#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 ret = mbedtls_pk_verify_restartable(peer_pk,
2425 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002428 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002429#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002431#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002433 mbedtls_ssl_send_alert_message(
2434 ssl,
2435 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2437 }
2438 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002439#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002441 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002443#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002445 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002446
2447#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2448 /* We don't need the peer's public key anymore. Free it,
2449 * so that more RAM is available for upcoming expensive
2450 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002452#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002454#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002456exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 ssl->state++;
2458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002462}
2463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002465MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002466static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002467{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002468 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002469 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2474 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002475 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002477 }
2478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2480 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002481}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002482#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002483MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002484static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002485{
Janos Follath865b3eb2019-12-16 11:46:15 +00002486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002487 unsigned char *buf;
2488 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002489 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002490 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002491 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002492 size_t sig_alg_len;
2493#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 unsigned char *sig_alg;
2495 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002496#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2501 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002502 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002504 }
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2507 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2508 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 }
2510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2512 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002513 mbedtls_ssl_send_alert_message(
2514 ssl,
2515 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2517 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002520 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002521 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2525 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002528 /* Current message is probably the ServerHelloDone */
2529 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002530 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002531 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002532
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002533 /*
2534 * struct {
2535 * ClientCertificateType certificate_types<1..2^8-1>;
2536 * SignatureAndHashAlgorithm
2537 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2538 * DistinguishedName certificate_authorities<0..2^16-1>;
2539 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002540 *
2541 * Since we only support a single certificate on clients, let's just
2542 * ignore all the information that's supposed to help us pick a
2543 * certificate.
2544 *
2545 * We could check that our certificate matches the request, and bail out
2546 * if it doesn't, but it's simpler to just send the certificate anyway,
2547 * and give the server the opportunity to decide if it should terminate
2548 * the connection when it doesn't like our certificate.
2549 *
2550 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2551 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002552 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002553 *
2554 * However, we still minimally parse the message to check it is at least
2555 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002556 */
Paul Bakker926af752012-11-23 13:38:07 +01002557 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002558
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002559 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2561 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2562 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2563 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2564 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002565 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002567 n = cert_type_len;
2568
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002569 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002570 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002571 * * the length of the signature algorithms field (if minor version of
2572 * SSL is 3),
2573 * * distinguished name length otherwise.
2574 * Both reach at most the index:
2575 * ...hdr_len + 2 + n,
2576 * therefore the buffer length at this point must be greater than that
2577 * regardless of the actual code path.
2578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2580 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2581 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2582 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2583 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002584 }
2585
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002586 /* supported_signature_algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 sig_alg_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2588 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Ronald Cron90915f22022-03-07 11:11:36 +01002589
2590 /*
2591 * The furthest access in buf is in the loop few lines below:
2592 * sig_alg[i + 1],
2593 * where:
2594 * sig_alg = buf + ...hdr_len + 3 + n,
2595 * max(i) = sig_alg_len - 1.
2596 * Therefore the furthest access is:
2597 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2598 * which reduces to:
2599 * buf[...hdr_len + 3 + n + sig_alg_len],
2600 * which is one less than we need the buf to be.
2601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2603 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002604 mbedtls_ssl_send_alert_message(
2605 ssl,
2606 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2608 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002609 }
Paul Bakker926af752012-11-23 13:38:07 +01002610
Ronald Cron90915f22022-03-07 11:11:36 +01002611#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2613 for (size_t i = 0; i < sig_alg_len; i += 2) {
2614 MBEDTLS_SSL_DEBUG_MSG(3,
2615 ("Supported Signature Algorithm found: %02x %02x",
2616 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002617 }
2618#endif
2619
2620 n += 2 + sig_alg_len;
2621
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002622 /* certificate_authorities */
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 dn_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2624 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Paul Bakker926af752012-11-23 13:38:07 +01002625
2626 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2628 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2629 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2630 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2631 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002632 }
2633
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002634#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2636 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002637 unsigned char *p = dn + i + 2;
2638 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002639 size_t asn1_len;
2640 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 memset(&name, 0, sizeof(name));
2642 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2643 if (dni_len > dn_len - i - 2 ||
2644 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2645 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2646 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2647 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002648 mbedtls_ssl_send_alert_message(
2649 ssl,
2650 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2652 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002653 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 MBEDTLS_SSL_DEBUG_MSG(3,
2655 ("DN hint: %.*s",
2656 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2657 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002658 }
2659#endif
2660
Paul Bakker926af752012-11-23 13:38:07 +01002661exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002666#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002668MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002669static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002670{
Janos Follath865b3eb2019-12-16 11:46:15 +00002671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2676 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2677 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2681 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2682 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2686 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2687 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2688 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2689 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2690 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 }
2692
2693 ssl->state++;
2694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2697 mbedtls_ssl_recv_flight_completed(ssl);
2698 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002699#endif
2700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002704}
2705
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002706MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002707static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002708{
Janos Follath865b3eb2019-12-16 11:46:15 +00002709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002710
2711 size_t header_len;
2712 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002713 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002714 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 /*
2721 * DHM key exchange -- send G^X mod P
2722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002726 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2729 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2730 &ssl->out_msg[header_len], content_len,
2731 ssl->conf->f_rng, ssl->conf->p_rng);
2732 if (ret != 0) {
2733 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2734 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 }
2736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2738 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2741 ssl->handshake->premaster,
2742 MBEDTLS_PREMASTER_SIZE,
2743 &ssl->handshake->pmslen,
2744 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2745 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2746 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 }
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2750 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002752#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2753 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2754 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2755 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002757 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2758 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002760#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002761 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2762 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002763 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002764
2765 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2766
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002767 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002770
Hanno Becker4a63ed42019-01-08 11:39:35 +00002771 /*
2772 * Generate EC private key for ECDHE exchange.
2773 */
2774
Hanno Becker4a63ed42019-01-08 11:39:35 +00002775 /* The master secret is obtained from the shared ECDH secret by
2776 * applying the TLS 1.2 PRF with a specific salt and label. While
2777 * the PSA Crypto API encourages combining key agreement schemes
2778 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2779 * yet support the provisioning of salt + label to the KDF.
2780 * For the time being, we therefore need to split the computation
2781 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002782 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2784 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2785 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2786 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002787
2788 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 status = psa_generate_key(&key_attributes,
2790 &handshake->ecdh_psa_privkey);
2791 if (status != PSA_SUCCESS) {
2792 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2793 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002794
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002795 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002796 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002797 * but we just need to add a length byte before that. */
2798 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2799 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002801 size_t own_pubkey_len;
2802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2804 own_pubkey, own_pubkey_max_len,
2805 &own_pubkey_len);
2806 if (status != PSA_SUCCESS) {
2807 psa_destroy_key(handshake->ecdh_psa_privkey);
Andrzej Kureka0237f82022-02-24 13:24:52 -05002808 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002810 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002811
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002812 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2813 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002814
Hanno Becker4a63ed42019-01-08 11:39:35 +00002815 /* The ECDH secret is the premaster secret used for key derivation. */
2816
Janos Follathdf3b0892019-08-08 11:12:24 +01002817 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2819 handshake->ecdh_psa_privkey,
2820 handshake->ecdh_psa_peerkey,
2821 handshake->ecdh_psa_peerkey_len,
2822 ssl->handshake->premaster,
2823 sizeof(ssl->handshake->premaster),
2824 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002825
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Ronald Croncf56a0a2020-08-04 09:51:30 +02002827 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2830 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2831 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002832#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002833 /*
2834 * ECDH key exchange -- send client public value
2835 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002836 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002837
Gilles Peskineeccd8882020-03-10 12:19:08 +01002838#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 if (ssl->handshake->ecrs_enabled) {
2840 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002841 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002845 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002846#endif
2847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2849 &content_len,
2850 &ssl->out_msg[header_len], 1000,
2851 ssl->conf->f_rng, ssl->conf->p_rng);
2852 if (ret != 0) {
2853 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002854#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002856 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002858#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002860 }
2861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2863 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002864
Gilles Peskineeccd8882020-03-10 12:19:08 +01002865#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002867 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002868 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002869 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002870
2871ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002873 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002875#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2877 &ssl->handshake->pmslen,
2878 ssl->handshake->premaster,
2879 MBEDTLS_MPI_MAX_SIZE,
2880 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2881 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002882#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002884 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002886#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002888 }
2889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2891 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002892#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2895 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2896 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2897 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002898#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2899 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2902 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2903 psa_key_attributes_t key_attributes;
2904
2905 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2906
2907 /*
2908 * opaque psk_identity<0..2^16-1>;
2909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002911 /* We don't offer PSK suites if we don't have a PSK,
2912 * and we check that the server's choice is among the
2913 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2915 }
Neil Armstrong868af822022-03-09 10:26:25 +01002916
Neil Armstrongfc834f22022-03-23 17:54:38 +01002917 /* uint16 to store content length */
2918 const size_t content_len_size = 2;
2919
Neil Armstrong868af822022-03-09 10:26:25 +01002920 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 if (header_len + content_len_size + ssl->conf->psk_identity_len
2923 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2924 MBEDTLS_SSL_DEBUG_MSG(1,
2925 ("psk identity too long or SSL buffer too short"));
2926 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002927 }
2928
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002929 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2932 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002933 header_len += content_len_size;
2934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 memcpy(p, ssl->conf->psk_identity,
2936 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002937 p += ssl->conf->psk_identity_len;
2938
Neil Armstrong868af822022-03-09 10:26:25 +01002939 header_len += ssl->conf->psk_identity_len;
2940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002942
2943 /*
2944 * Generate EC private key for ECDHE exchange.
2945 */
2946
2947 /* The master secret is obtained from the shared ECDH secret by
2948 * applying the TLS 1.2 PRF with a specific salt and label. While
2949 * the PSA Crypto API encourages combining key agreement schemes
2950 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2951 * yet support the provisioning of salt + label to the KDF.
2952 * For the time being, we therefore need to split the computation
2953 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2954 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2956 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2957 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2958 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002959
2960 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 status = psa_generate_key(&key_attributes,
2962 &handshake->ecdh_psa_privkey);
2963 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002964 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 }
Neil Armstrong868af822022-03-09 10:26:25 +01002966
2967 /* Export the public part of the ECDH private key from PSA.
2968 * The export format is an ECPoint structure as expected by TLS,
2969 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002970 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002971 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002973 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2976 own_pubkey, own_pubkey_max_len,
2977 &own_pubkey_len);
2978 if (status != PSA_SUCCESS) {
2979 psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01002980 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002981 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002982 }
2983
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002984 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002985 content_len = own_pubkey_len + 1;
2986
Neil Armstrong25400452022-03-23 17:44:07 +01002987 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2988 * - a uint16 containing the length (in octets) of the ECDH computation
2989 * - the octet string produced by the ECDH computation
2990 * - a uint16 containing the length (in octets) of the PSK
2991 * - the PSK itself
2992 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002993 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 const unsigned char * const pms_end = pms +
2995 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002996 /* uint16 to store length (in octets) of the ECDH computation */
2997 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002998 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002999
Neil Armstrong25400452022-03-23 17:44:07 +01003000 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 status = psa_raw_key_agreement(PSA_ALG_ECDH,
3002 handshake->ecdh_psa_privkey,
3003 handshake->ecdh_psa_peerkey,
3004 handshake->ecdh_psa_peerkey_len,
3005 pms + zlen_size,
3006 pms_end - (pms + zlen_size),
3007 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01003010 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003013 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003015 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 }
Neil Armstrong868af822022-03-09 10:26:25 +01003017
Neil Armstrong25400452022-03-23 17:44:07 +01003018 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003020 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003022#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3023 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003024#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003026 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003027 * opaque psk_identity<0..2^16-1>;
3028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003030 /* We don't offer PSK suites if we don't have a PSK,
3031 * and we check that the server's choice is among the
3032 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003034 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003035
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003036 header_len = 4;
3037 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3040 MBEDTLS_SSL_DEBUG_MSG(1,
3041 ("psk identity too long or SSL buffer too short"));
3042 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003043 }
3044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3046 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 memcpy(ssl->out_msg + header_len,
3049 ssl->conf->psk_identity,
3050 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003051 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003055 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3060 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3061 &content_len, 2)) != 0) {
3062 return ret;
3063 }
3064 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003068 /*
3069 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 if (header_len + 2 + content_len >
3074 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3075 MBEDTLS_SSL_DEBUG_MSG(1,
3076 ("psk identity or DHM size too long or SSL buffer too short"));
3077 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003078 }
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3081 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3084 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3085 &ssl->out_msg[header_len], content_len,
3086 ssl->conf->f_rng, ssl->conf->p_rng);
3087 if (ret != 0) {
3088 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3089 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003090 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003091
3092#if defined(MBEDTLS_USE_PSA_CRYPTO)
3093 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003095 size_t pms_len;
3096
3097 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3099 pms + 2, pms_end - (pms + 2), &pms_len,
3100 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3101 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3102 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003103 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003105 pms += 2 + pms_len;
3106
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003108#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003111#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3113 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003114 /*
3115 * ClientECDiffieHellmanPublic public;
3116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3118 &content_len,
3119 &ssl->out_msg[header_len],
3120 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3121 ssl->conf->f_rng, ssl->conf->p_rng);
3122 if (ret != 0) {
3123 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3124 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003125 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3128 MBEDTLS_DEBUG_ECDH_Q);
3129 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003130#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003131 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3133 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003134 }
3135
Neil Armstrong80f6f322022-05-03 17:56:38 +02003136#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3138 ciphersuite_info->key_exchange)) != 0) {
3139 MBEDTLS_SSL_DEBUG_RET(1,
3140 "mbedtls_ssl_psk_derive_premaster", ret);
3141 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003142 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003143#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003145#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003148 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3150 &content_len, 0)) != 0) {
3151 return ret;
3152 }
3153 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003155#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003157 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003158
Neil Armstrongca7d5062022-05-31 14:43:23 +02003159#if defined(MBEDTLS_USE_PSA_CRYPTO)
3160 unsigned char *out_p = ssl->out_msg + header_len;
3161 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3162 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3164 out_p, end_p - out_p, &content_len,
3165 MBEDTLS_ECJPAKE_ROUND_TWO);
3166 if (ret != 0) {
3167 psa_destroy_key(ssl->handshake->psa_pake_password);
3168 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3169 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3170 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003171 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003172#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3174 ssl->out_msg + header_len,
3175 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3176 &content_len,
3177 ssl->conf->f_rng, ssl->conf->p_rng);
3178 if (ret != 0) {
3179 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3180 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003181 }
3182
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3184 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3185 ssl->conf->f_rng, ssl->conf->p_rng);
3186 if (ret != 0) {
3187 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3188 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003189 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003190#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003192#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003193 {
3194 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3196 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003199 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3201 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003202
3203 ssl->state++;
3204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3206 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3207 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 }
3209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003213}
3214
Gilles Peskineeccd8882020-03-10 12:19:08 +01003215#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003216MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003217static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003218{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003219 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003220 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3226 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3227 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003228 }
3229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3231 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003232 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003234 }
3235
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3237 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003238}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003239#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003240MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003241static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003244 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003245 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003246 size_t n = 0, offset = 0;
3247 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003248 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003250 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003251 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003252#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 size_t out_buf_len = ssl->out_buf_len - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003254#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003256#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003257
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003259
Gilles Peskineeccd8882020-03-10 12:19:08 +01003260#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 if (ssl->handshake->ecrs_enabled &&
3262 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003263 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003264 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003265#endif
3266
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3268 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3269 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003270 }
3271
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3273 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003274 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003276 }
3277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (ssl->handshake->client_auth == 0 ||
3279 mbedtls_ssl_own_cert(ssl) == NULL) {
3280 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003281 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 }
3284
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 if (mbedtls_ssl_own_key(ssl) == NULL) {
3286 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3287 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 }
3289
3290 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003291 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003293#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003295 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003297
3298sign:
3299#endif
3300
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003301 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3302 if (0 != ret) {
3303 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3304 return ret;
3305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Ronald Cron90915f22022-03-07 11:11:36 +01003307 /*
3308 * digitally-signed struct {
3309 * opaque handshake_messages[handshake_messages_length];
3310 * };
3311 *
3312 * Taking shortcut here. We assume that the server always allows the
3313 * PRF Hash function and has sent it in the allowed signature
3314 * algorithms list received in the Certificate Request message.
3315 *
3316 * Until we encounter a server that does not, we will take this
3317 * shortcut.
3318 *
3319 * Reason: Otherwise we should have running hashes for SHA512 and
3320 * SHA224 in order to satisfy 'weird' needs from the server
3321 * side.
3322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003324 md_alg = MBEDTLS_MD_SHA384;
3325 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003327 md_alg = MBEDTLS_MD_SHA256;
3328 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003329 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003331
3332 /* Info from md_alg will be used instead */
3333 hashlen = 0;
3334 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003335
Gilles Peskineeccd8882020-03-10 12:19:08 +01003336#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003338 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003340#endif
3341
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3343 md_alg, hash_start, hashlen,
3344 ssl->out_msg + 6 + offset,
3345 out_buf_len - 6 - offset,
3346 &n,
3347 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3348 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003349#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003351 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003353#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003355 }
Paul Bakker926af752012-11-23 13:38:07 +01003356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003358
Paul Bakker1ef83d62012-04-11 12:09:53 +00003359 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3361 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
3363 ssl->state++;
3364
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3366 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3367 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003368 }
3369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003373}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003374#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003377MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003378static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003379{
Janos Follath865b3eb2019-12-16 11:46:15 +00003380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003381 uint32_t lifetime;
3382 size_t ticket_len;
3383 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003384 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3389 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3390 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003391 }
3392
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3394 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003395 mbedtls_ssl_send_alert_message(
3396 ssl,
3397 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3399 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003400 }
3401
3402 /*
3403 * struct {
3404 * uint32 ticket_lifetime_hint;
3405 * opaque ticket<0..2^16-1>;
3406 * } NewSessionTicket;
3407 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003408 * 0 . 3 ticket_lifetime_hint
3409 * 4 . 5 ticket_len (n)
3410 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003411 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3413 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3414 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3415 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3416 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3417 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003418 }
3419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 lifetime = (((uint32_t) msg[0]) << 24) | (msg[1] << 16) |
3423 (msg[2] << 8) | (msg[3]);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003424
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 ticket_len = (msg[4] << 8) | (msg[5]);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3428 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3429 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3430 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3431 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003432 }
3433
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003435
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003436 /* We're not waiting for a NewSessionTicket message any more */
3437 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003439
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003440 /*
3441 * Zero-length ticket means the server changed his mind and doesn't want
3442 * to send a ticket after all, so just forget it
3443 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 if (ticket_len == 0) {
3445 return 0;
3446 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003447
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 if (ssl->session != NULL && ssl->session->ticket != NULL) {
3449 mbedtls_platform_zeroize(ssl->session->ticket,
3450 ssl->session->ticket_len);
3451 mbedtls_free(ssl->session->ticket);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003452 ssl->session->ticket = NULL;
3453 ssl->session->ticket_len = 0;
3454 }
3455
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 mbedtls_platform_zeroize(ssl->session_negotiate->ticket,
3457 ssl->session_negotiate->ticket_len);
3458 mbedtls_free(ssl->session_negotiate->ticket);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003459 ssl->session_negotiate->ticket = NULL;
3460 ssl->session_negotiate->ticket_len = 0;
3461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3463 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3464 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3465 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3466 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003467 }
3468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003470
3471 ssl->session_negotiate->ticket = ticket;
3472 ssl->session_negotiate->ticket_len = ticket_len;
3473 ssl->session_negotiate->ticket_lifetime = lifetime;
3474
3475 /*
3476 * RFC 5077 section 3.4:
3477 * "If the client receives a session ticket from the server, then it
3478 * discards any Session ID that was sent in the ServerHello."
3479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003481 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003482
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003484
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003488
Paul Bakker5121ce52009-01-03 21:22:43 +00003489/*
Paul Bakker1961b702013-01-25 14:49:24 +01003490 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003492int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003493{
3494 int ret = 0;
3495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003497 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3500 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003501 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003502 }
3503#endif
3504
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 case MBEDTLS_SSL_HELLO_REQUEST:
3507 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003508 break;
3509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 /*
3511 * ==> ClientHello
3512 */
3513 case MBEDTLS_SSL_CLIENT_HELLO:
3514 ret = mbedtls_ssl_write_client_hello(ssl);
3515 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 /*
3518 * <== ServerHello
3519 * Certificate
3520 * ( ServerKeyExchange )
3521 * ( CertificateRequest )
3522 * ServerHelloDone
3523 */
3524 case MBEDTLS_SSL_SERVER_HELLO:
3525 ret = ssl_parse_server_hello(ssl);
3526 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3529 ret = mbedtls_ssl_parse_certificate(ssl);
3530 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003531
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3533 ret = ssl_parse_server_key_exchange(ssl);
3534 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3537 ret = ssl_parse_certificate_request(ssl);
3538 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3541 ret = ssl_parse_server_hello_done(ssl);
3542 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 /*
3545 * ==> ( Certificate/Alert )
3546 * ClientKeyExchange
3547 * ( CertificateVerify )
3548 * ChangeCipherSpec
3549 * Finished
3550 */
3551 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3552 ret = mbedtls_ssl_write_certificate(ssl);
3553 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003554
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3556 ret = ssl_write_client_key_exchange(ssl);
3557 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003558
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3560 ret = ssl_write_certificate_verify(ssl);
3561 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003562
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3564 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3565 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003566
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 case MBEDTLS_SSL_CLIENT_FINISHED:
3568 ret = mbedtls_ssl_write_finished(ssl);
3569 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003570
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 /*
3572 * <== ( NewSessionTicket )
3573 * ChangeCipherSpec
3574 * Finished
3575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3578 ret = ssl_parse_new_session_ticket(ssl);
3579 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003580#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003581
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3583 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3584 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003585
Gilles Peskine449bd832023-01-11 14:50:10 +01003586 case MBEDTLS_SSL_SERVER_FINISHED:
3587 ret = mbedtls_ssl_parse_finished(ssl);
3588 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003589
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 case MBEDTLS_SSL_FLUSH_BUFFERS:
3591 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3592 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3593 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003594
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3596 mbedtls_ssl_handshake_wrapup(ssl);
3597 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003598
Gilles Peskine449bd832023-01-11 14:50:10 +01003599 default:
3600 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3601 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003603
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003605}
Jerry Yuc5aef882021-12-23 20:15:02 +08003606
Jerry Yufb4b6472022-01-27 15:03:26 +08003607#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */