blob: 65d6dbd1a760d2c6f892b406f0808b2a6c62691e [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020013
SimonBd5800b72016-04-26 07:43:27 +010014#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010015#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000016#include "ssl_misc.h"
Valerio Settib4f50762024-01-17 10:24:52 +010017#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000018#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020019#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010020
Hanno Beckerbb89e272019-01-08 12:54:37 +000021#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020022#include "psa_util_internal.h"
Ronald Cron69a63422021-10-18 09:47:58 +020023#include "psa/crypto.h"
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040024#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Andrzej Kurek00644842023-05-30 05:45:00 -040025/* Define a local translating function to save code size by not using too many
26 * arguments in each translating place. */
27static int local_err_translation(psa_status_t status)
28{
29 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040030 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040031 psa_generic_status_to_mbedtls);
32}
33#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040034#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +000035#endif /* MBEDTLS_USE_PSA_CRYPTO */
36
SimonBd5800b72016-04-26 07:43:27 +010037#include <string.h>
38
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020039#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010042#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050046#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010051static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
52 unsigned char *buf,
53 const unsigned char *end,
54 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010055{
56 unsigned char *p = buf;
57
58 *olen = 0;
59
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010060 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010061 * initial ClientHello, in which case also adding the renegotiation
62 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
64 return 0;
65 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 MBEDTLS_SSL_DEBUG_MSG(3,
68 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010071
Paul Bakkerd3edc862013-03-20 16:07:17 +010072 /*
73 * Secure renegotiation
74 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010076 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010077
78 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010079 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
80 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010083
84 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010089
Valerio Setti7aeec542023-07-05 18:57:21 +020090#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +020091 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010092 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010093
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020094MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010095static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
96 unsigned char *buf,
97 const unsigned char *end,
98 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010099{
100 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100101 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100102
103 *olen = 0;
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 MBEDTLS_SSL_DEBUG_MSG(3,
106 ("client hello, adding supported_point_formats extension"));
107 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100110 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100111
112 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100113 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200114
115 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100117
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200118 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100121}
Valerio Setti7aeec542023-07-05 18:57:21 +0200122#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200123 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200124 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100125
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200126#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200127MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100128static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
129 unsigned char *buf,
130 const unsigned char *end,
131 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200132{
Janos Follath865b3eb2019-12-16 11:46:15 +0000133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200134 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100135 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200136
137 *olen = 0;
138
139 /* Skip costly extension if we can't use EC J-PAKE anyway */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
142 return 0;
143 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200144#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
146 return 0;
147 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200148#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 MBEDTLS_SSL_DEBUG_MSG(3,
151 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100156 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200157
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200158 /*
159 * We may need to send ClientHello multiple times for Hello verification.
160 * We don't want to compute fresh values every time (both for performance
161 * and consistency reasons), so cache the extension content.
162 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (ssl->handshake->ecjpake_cache == NULL ||
164 ssl->handshake->ecjpake_cache_len == 0) {
165 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200166
Neil Armstrongca7d5062022-05-31 14:43:23 +0200167#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100168 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 p + 2, end - p - 2, &kkpp_len,
170 MBEDTLS_ECJPAKE_ROUND_ONE);
171 if (ret != 0) {
172 psa_destroy_key(ssl->handshake->psa_pake_password);
173 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
174 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
175 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200176 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200177#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
179 p + 2, end - p - 2, &kkpp_len,
180 ssl->conf->f_rng, ssl->conf->p_rng);
181 if (ret != 0) {
182 MBEDTLS_SSL_DEBUG_RET(1,
183 "mbedtls_ecjpake_write_round_one", ret);
184 return ret;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200185 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
189 if (ssl->handshake->ecjpake_cache == NULL) {
190 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
191 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200192 }
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200195 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 } else {
197 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200198
199 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200203 }
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100206 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200207
208 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200211}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200212#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000213
Hanno Beckera0e20d02019-05-15 14:03:01 +0100214#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200215MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
217 unsigned char *buf,
218 const unsigned char *end,
219 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100220{
221 unsigned char *p = buf;
222 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100223
224 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100225 * struct {
226 * opaque cid<0..2^8-1>;
227 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100229
230 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
232 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
233 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100234 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100236
237 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
238 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100240
241 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100243 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100244 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100246 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100247
248 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100250
251 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100254}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100255#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200258MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100259static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
260 unsigned char *buf,
261 const unsigned char *end,
262 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200263{
264 unsigned char *p = buf;
265
Simon Butcher0fc94e92015-09-28 20:52:04 +0100266 *olen = 0;
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
269 return 0;
270 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 MBEDTLS_SSL_DEBUG_MSG(3,
273 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100278 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200279
280 *p++ = 0x00;
281 *p++ = 1;
282
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200283 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200284
285 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200292MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100293static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
294 unsigned char *buf,
295 const unsigned char *end,
296 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100297{
298 unsigned char *p = buf;
299
Simon Butcher0fc94e92015-09-28 20:52:04 +0100300 *olen = 0;
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
303 return 0;
304 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 MBEDTLS_SSL_DEBUG_MSG(3,
307 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100312 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100313
314 *p++ = 0x00;
315 *p++ = 0x00;
316
317 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100320}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200324MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100325static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
326 unsigned char *buf,
327 const unsigned char *end,
328 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200329{
330 unsigned char *p = buf;
331
Simon Butcher0fc94e92015-09-28 20:52:04 +0100332 *olen = 0;
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
335 return 0;
336 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 MBEDTLS_SSL_DEBUG_MSG(3,
339 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100344 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200345
346 *p++ = 0x00;
347 *p++ = 0x00;
348
349 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200356MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100357static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
358 unsigned char *buf,
359 const unsigned char *end,
360 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200361{
362 unsigned char *p = buf;
363 size_t tlen = ssl->session_negotiate->ticket_len;
364
Simon Butcher0fc94e92015-09-28 20:52:04 +0100365 *olen = 0;
366
Ronald Crond67f8012024-08-28 07:45:57 +0200367 if (mbedtls_ssl_conf_get_session_tickets(ssl->conf) ==
368 MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return 0;
370 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 MBEDTLS_SSL_DEBUG_MSG(3,
373 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200374
Hanno Becker261602c2017-04-12 14:54:42 +0100375 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100379 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100382 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200383
384 *olen = 4;
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
387 return 0;
388 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 MBEDTLS_SSL_DEBUG_MSG(3,
391 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200394
395 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200400
Ron Eldora9788042018-12-05 11:04:31 +0200401#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100403static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
404 unsigned char *buf,
405 const unsigned char *end,
406 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100407{
408 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200409 size_t protection_profiles_index = 0, ext_len = 0;
410 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100411
412 *olen = 0;
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
415 (ssl->conf->dtls_srtp_profile_list == NULL) ||
416 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
417 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100418 }
419
Ron Eldora9788042018-12-05 11:04:31 +0200420 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100421 * uint8 SRTPProtectionProfile[2];
422 *
423 * struct {
424 * SRTPProtectionProfiles SRTPProtectionProfiles;
425 * opaque srtp_mki<0..255>;
426 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100427 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100428 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200430 mki_len = ssl->dtls_srtp_info.mki_len;
431 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300432 /* Extension length = 2 bytes for profiles length,
433 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
434 * 1 byte for srtp_mki vector length and the mki_len value
435 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200439
440 /* Check there is room in the buffer for the extension + 4 bytes
441 * - the extension tag (2 bytes)
442 * - the extension length (2 bytes)
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100447 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100450 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100451
Ron Eldor3adb9922017-12-21 10:15:08 +0200452 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200453 /* micro-optimization:
454 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
455 * which is lower than 127, so the upper byte of the length is always 0
456 * For the documentation, the more generic code is left in comments
457 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
458 * >> 8 ) & 0xFF );
459 */
460 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300464 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200466 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
468 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
469 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
470 profile_value));
471 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100472 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200474 /*
475 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200476 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200477 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 MBEDTLS_SSL_DEBUG_MSG(3,
479 ("client hello, "
480 "illegal DTLS-SRTP protection profile %d",
481 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
482 ));
483 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100484 }
485 }
486
Ron Eldor591f1622018-01-22 12:30:04 +0200487 *p++ = mki_len & 0xFF;
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (mki_len != 0) {
490 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200491 /*
492 * Increment p to point to the current position.
493 */
494 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
496 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200497 }
498
Ron Eldoref72faf2018-07-12 11:54:20 +0300499 /*
500 * total extension length: extension type (2 bytes)
501 * + extension length (2 bytes)
502 * + protection profile length (2 bytes)
503 * + 2 * number of protection profiles
504 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200505 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300506 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200507 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100510}
511#endif /* MBEDTLS_SSL_DTLS_SRTP */
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
514 unsigned char *buf,
515 const unsigned char *end,
516 int uses_ec,
517 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100518{
519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
520 unsigned char *p = buf;
521 size_t ext_len = 0;
522
523 (void) ssl;
524 (void) end;
525 (void) uses_ec;
526 (void) ret;
527 (void) ext_len;
528
529 *out_len = 0;
530
531 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
532 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
533#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
535 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
536 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100537 }
538 p += ext_len;
539#endif
540
Valerio Setti7aeec542023-07-05 18:57:21 +0200541#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200542 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Ronald Cron12dcdf02022-02-16 15:28:22 +0100543 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if (uses_ec) {
545 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
546 &ext_len)) != 0) {
547 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
548 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100549 }
550 p += ext_len;
551 }
552#endif
553
554#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
556 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
557 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100558 }
559 p += ext_len;
560#endif
561
562#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
564 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
565 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100566 }
567 p += ext_len;
568#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
569
570#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
572 &ext_len)) != 0) {
573 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
574 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100575 }
576 p += ext_len;
577#endif
578
579#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
581 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
582 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100583 }
584 p += ext_len;
585#endif
586
587#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
589 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
590 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100591 }
592 p += ext_len;
593#endif
594
595#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
597 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
598 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100599 }
600 p += ext_len;
601#endif
602
603#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
605 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
606 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100607 }
608 p += ext_len;
609#endif
610
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000611 *out_len = (size_t) (p - buf);
Ronald Cron12dcdf02022-02-16 15:28:22 +0100612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100614}
615
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200616MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100617static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
618 const unsigned char *buf,
619 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100623 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000625 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 mbedtls_ct_memcmp(buf + 1,
627 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
628 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
629 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
630 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100631 mbedtls_ssl_send_alert_message(
632 ssl,
633 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
635 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000636 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100639 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (len != 1 || buf[0] != 0x00) {
641 MBEDTLS_SSL_DEBUG_MSG(1,
642 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100643 mbedtls_ssl_send_alert_message(
644 ssl,
645 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
647 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100648 }
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100651 }
Paul Bakker48916f92012-09-16 19:57:18 +0000652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000654}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200657MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100658static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
659 const unsigned char *buf,
660 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200661{
662 /*
663 * server should use the extension only if we did,
664 * and if so the server's value should match ours (and len is always 1)
665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200667 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 buf[0] != ssl->conf->mfl_code) {
669 MBEDTLS_SSL_DEBUG_MSG(1,
670 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100671 mbedtls_ssl_send_alert_message(
672 ssl,
673 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
675 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200676 }
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200679}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000681
Hanno Beckera0e20d02019-05-15 14:03:01 +0100682#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200683MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100684static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
685 const unsigned char *buf,
686 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100687{
688 size_t peer_cid_len;
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100691 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
692 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
694 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
695 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
696 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
697 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100698 }
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (len == 0) {
701 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
702 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
703 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
704 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100705 }
706
707 peer_cid_len = *buf++;
708 len--;
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
711 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
712 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
713 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
714 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100715 }
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (len != peer_cid_len) {
718 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
719 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
720 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
721 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100722 }
723
Hanno Becker5a299902019-05-03 12:47:49 +0100724 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100725 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
729 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100732}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100733#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200736MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100737static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
738 const unsigned char *buf,
739 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100740{
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
742 len != 0) {
743 MBEDTLS_SSL_DEBUG_MSG(1,
744 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100745 mbedtls_ssl_send_alert_message(
746 ssl,
747 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
749 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100750 }
751
752 ((void) buf);
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200761MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100762static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
763 const unsigned char *buf,
764 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200765{
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
767 len != 0) {
768 MBEDTLS_SSL_DEBUG_MSG(1,
769 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100770 mbedtls_ssl_send_alert_message(
771 ssl,
772 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
774 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200775 }
776
777 ((void) buf);
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200786MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100787static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
788 const unsigned char *buf,
789 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200790{
Ronald Crond67f8012024-08-28 07:45:57 +0200791 if ((mbedtls_ssl_conf_get_session_tickets(ssl->conf) ==
792 MBEDTLS_SSL_SESSION_TICKETS_DISABLED) ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 len != 0) {
794 MBEDTLS_SSL_DEBUG_MSG(1,
795 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100796 mbedtls_ssl_send_alert_message(
797 ssl,
798 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
800 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200801 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200802
803 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200804
805 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200810
Valerio Setti7aeec542023-07-05 18:57:21 +0200811#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200812 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
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 Setti7aeec542023-07-05 18:57:21 +0200834#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
835 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200836 ssl->handshake->ecdh_ctx.point_format = p[0];
Valerio Setti7aeec542023-07-05 18:57:21 +0200837#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200838#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
840 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
841 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200842#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
844 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200845 }
846
847 list_size--;
848 p++;
849 }
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
852 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
853 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
854 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200855}
Valerio Setti7aeec542023-07-05 18:57:21 +0200856#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200857 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200858 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200859
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200860#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200861MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100862static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
863 const unsigned char *buf,
864 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200865{
Janos Follath865b3eb2019-12-16 11:46:15 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (ssl->handshake->ciphersuite_info->key_exchange !=
869 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
870 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
871 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200872 }
873
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200874 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200876 ssl->handshake->ecjpake_cache = NULL;
877 ssl->handshake->ecjpake_cache_len = 0;
878
Neil Armstrongca7d5062022-05-31 14:43:23 +0200879#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if ((ret = mbedtls_psa_ecjpake_read_round(
881 &ssl->handshake->psa_pake_ctx, buf, len,
882 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
883 psa_destroy_key(ssl->handshake->psa_pake_password);
884 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100887 mbedtls_ssl_send_alert_message(
888 ssl,
889 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
891 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200892 }
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 return 0;
895#else
896 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
897 buf, len)) != 0) {
898 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
899 mbedtls_ssl_send_alert_message(
900 ssl,
901 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
902 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
903 return ret;
904 }
905
906 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200907#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200908}
909#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200912MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100913static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
914 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200915{
916 size_t list_len, name_len;
917 const char **p;
918
919 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (ssl->conf->alpn_list == NULL) {
921 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100922 mbedtls_ssl_send_alert_message(
923 ssl,
924 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
926 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200927 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200928
929 /*
930 * opaque ProtocolName<1..2^8-1>;
931 *
932 * struct {
933 * ProtocolName protocol_name_list<2..2^16-1>
934 * } ProtocolNameList;
935 *
936 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
937 */
938
939 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (len < 4) {
941 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
942 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
943 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200944 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200945
Dave Rodgmana3d0f612023-11-03 23:34:02 +0000946 list_len = MBEDTLS_GET_UINT16_BE(buf, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (list_len != len - 2) {
948 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
949 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
950 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200951 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200952
953 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (name_len != list_len - 1) {
955 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
956 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
957 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200958 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200959
960 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
962 if (name_len == strlen(*p) &&
963 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200964 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200966 }
967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
970 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
971 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
972 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200975
Johan Pascalb62bb512015-12-03 21:56:45 +0100976#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200977MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100978static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
979 const unsigned char *buf,
980 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100981{
Johan Pascal43f94902020-09-22 12:25:52 +0200982 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200983 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100984 uint16_t server_protection_profile_value = 0;
985
986 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
988 (ssl->conf->dtls_srtp_profile_list == NULL) ||
989 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
990 return 0;
991 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100992
Ron Eldora9788042018-12-05 11:04:31 +0200993 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100994 * uint8 SRTPProtectionProfile[2];
995 *
996 * struct {
997 * SRTPProtectionProfiles SRTPProtectionProfiles;
998 * opaque srtp_mki<0..255>;
999 * } UseSRTPData;
1000
1001 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1002 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001005 mki_len = ssl->dtls_srtp_info.mki_len;
1006 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001007
Ron Eldoref72faf2018-07-12 11:54:20 +03001008 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001009 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1010 * + protection profile (2 bytes)
1011 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001012 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if ((len < 5) || (len != (buf[4] + 5u))) {
1015 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1016 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001017
1018 /*
1019 * get the server protection profile
1020 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001021
1022 /*
1023 * protection profile length must be 0x0002 as we must have only
1024 * one protection profile in server Hello
1025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if ((buf[0] != 0) || (buf[1] != 2)) {
1027 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1028 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001031 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 server_protection_profile_value);
1033 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1034 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1035 mbedtls_ssl_get_srtp_profile_as_string(
1036 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001037 }
1038
Johan Pascal43f94902020-09-22 12:25:52 +02001039 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001040
Johan Pascalb62bb512015-12-03 21:56:45 +01001041 /*
1042 * Check we have the server profile in our list
1043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1045 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001046 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001048 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001050 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001051 }
1052 }
1053
Ron Eldor591f1622018-01-22 12:30:04 +02001054 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1056 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1057 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1058 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001059 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001060
1061 /* If server does not use mki in its reply, make sure the client won't keep
1062 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001064 ssl->dtls_srtp_info.mki_len = 0;
1065 }
1066
Ron Eldoref72faf2018-07-12 11:54:20 +03001067 /*
1068 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001069 * If the client detects a nonzero-length MKI in the server's response
1070 * that is different than the one the client offered, then the client
1071 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (len > 5 && (buf[4] != mki_len ||
1074 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1075 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1076 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1077 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001078 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001079#if defined(MBEDTLS_DEBUG_C)
1080 if (len > 5) {
1081 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1082 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001083 }
1084#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001086}
1087#endif /* MBEDTLS_SSL_DTLS_SRTP */
1088
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001089/*
1090 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001093MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001094static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001095{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001096 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001098 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001099
1100#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1101 uint8_t cookie_len;
1102#else
1103 uint16_t cookie_len;
1104#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001107
Gilles Peskineb64bf062019-09-27 14:02:44 +02001108 /* Check that there is enough room for:
1109 * - 2 bytes of version
1110 * - 1 byte of cookie_len
1111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1113 MBEDTLS_SSL_DEBUG_MSG(1,
1114 ("incoming HelloVerifyRequest message is too short"));
1115 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1116 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1117 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001118 }
1119
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001120 /*
1121 * struct {
1122 * ProtocolVersion server_version;
1123 * opaque cookie<0..2^8-1>;
1124 * } HelloVerifyRequest;
1125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1127 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001128 p += 2;
1129
TRodziewicz2d8800e2021-05-13 19:14:19 +02001130 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001131 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1132 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1133 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001134 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1136 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1139 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001142 }
1143
1144 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1146 MBEDTLS_SSL_DEBUG_MSG(1,
1147 ("cookie length does not match incoming message size"));
1148 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1149 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1150 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001151 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1157 if (ssl->handshake->cookie == NULL) {
1158 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1159 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001160 }
1161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001163 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001165 /* Start over at ClientHello */
Gilles Peskine49f179d2025-03-07 15:09:32 +01001166 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001167 ret = mbedtls_ssl_reset_checksum(ssl);
1168 if (0 != ret) {
1169 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1170 return ret;
1171 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001180
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001181MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001182static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001183{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001184 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001185 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001186 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001187 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001188 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001190 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001191#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001192 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001198 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1200 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 }
1202
Hanno Becker79594fd2019-05-08 09:38:41 +01001203 buf = ssl->in_msg;
1204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001208 ssl->renego_records_seen++;
1209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if (ssl->conf->renego_max_records >= 0 &&
1211 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1212 MBEDTLS_SSL_DEBUG_MSG(1,
1213 ("renegotiation requested, but not honored by server"));
1214 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001215 }
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 MBEDTLS_SSL_DEBUG_MSG(1,
1218 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001219
1220 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001222 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001226 mbedtls_ssl_send_alert_message(
1227 ssl,
1228 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1230 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 }
1232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1235 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1236 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1237 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1238 return ssl_parse_hello_verify_request(ssl);
1239 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001240 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001242 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001243 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001244 }
1245 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1249 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1250 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1251 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1252 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1253 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 }
1255
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001256 /*
1257 * 0 . 1 server_version
1258 * 2 . 33 random (maybe including 4 bytes of Unix time)
1259 * 34 . 34 session_id length = n
1260 * 35 . 34+n session_id
1261 * 35+n . 36+n cipher_suite
1262 * 37+n . 37+n compression_method
1263 *
1264 * 38+n . 39+n extensions length (optional)
1265 * 40+n . .. extensions
1266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01001270 ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
1271 ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001272 ssl->session_negotiate->tls_version = ssl->tls_version;
Ronald Cron17ef8df2023-11-22 10:29:42 +01001273 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (ssl->tls_version < ssl->conf->min_tls_version ||
1276 ssl->tls_version > ssl->conf->max_tls_version) {
1277 MBEDTLS_SSL_DEBUG_MSG(1,
1278 (
1279 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1280 (unsigned) ssl->conf->min_tls_version,
1281 (unsigned) ssl->tls_version,
1282 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1285 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001288 }
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1291 ((unsigned long) buf[2] << 24) |
1292 ((unsigned long) buf[3] << 16) |
1293 ((unsigned long) buf[4] << 8) |
1294 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001298 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (n > 32) {
1303 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1304 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1305 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1306 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001307 }
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001310 ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if ((ext_len > 0 && ext_len < 4) ||
1313 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1314 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001315 mbedtls_ssl_send_alert_message(
1316 ssl,
1317 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1319 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001320 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001322 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 } else {
1324 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1325 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1326 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1327 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001328 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001330 /* ciphersuite (used later) */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001331 i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35);
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001332
1333 /*
1334 * Read and check compression
1335 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001336 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1339 MBEDTLS_SSL_DEBUG_MSG(1,
1340 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001341 mbedtls_ssl_send_alert_message(
1342 ssl,
1343 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1345 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001346 }
1347
Paul Bakker380da532012-04-18 16:10:25 +00001348 /*
1349 * Initialize update checksum functions
1350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1352 if (ssl->handshake->ciphersuite_info == NULL) {
1353 MBEDTLS_SSL_DEBUG_MSG(1,
1354 ("ciphersuite info for %04x not found", (unsigned int) i));
1355 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1356 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1357 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001358 }
Paul Bakker380da532012-04-18 16:10:25 +00001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1363 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
1365 /*
1366 * Check if the session can be resumed
1367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#if defined(MBEDTLS_SSL_RENEGOTIATION)
1370 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001371#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001372 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001373 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Gilles Peskine49f179d2025-03-07 15:09:32 +01001375 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker0a597072012-09-25 21:55:46 +00001376 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001379#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001380 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001381 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 memcpy(ssl->session_negotiate->id, buf + 35, n);
1383 } else {
Gilles Peskine49f179d2025-03-07 15:09:32 +01001384 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 }
1386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1388 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1391 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1392 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
Andrzej Kurek03bac442018-04-25 05:06:07 -04001394 /*
1395 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001396 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 while (1) {
1399 if (ssl->conf->ciphersuite_list[i] == 0) {
1400 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001401 mbedtls_ssl_send_alert_message(
1402 ssl,
1403 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1405 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 }
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (ssl->conf->ciphersuite_list[i++] ==
1409 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 }
1413
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001414 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 ssl->session_negotiate->ciphersuite);
1416 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1417 ssl->tls_version) != 0) {
1418 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001419 mbedtls_ssl_send_alert_message(
1420 ssl,
1421 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1423 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001424 }
1425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 MBEDTLS_SSL_DEBUG_MSG(3,
1427 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001428
Gilles Peskineeccd8882020-03-10 12:19:08 +01001429#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1431 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001432 ssl->handshake->ecrs_enabled = 1;
1433 }
1434#endif
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1437 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001438 mbedtls_ssl_send_alert_message(
1439 ssl,
1440 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1442 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 }
1444
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001445 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 MBEDTLS_SSL_DEBUG_MSG(2,
1448 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1449 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 while (ext_len) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001452 unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
1453 unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
Paul Bakker48916f92012-09-16 19:57:18 +00001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (ext_size + 4 > ext_len) {
1456 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001457 mbedtls_ssl_send_alert_message(
1458 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1460 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001461 }
1462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 switch (ext_id) {
1464 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1465 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001468#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1471 ext_size)) != 0) {
1472 return ret;
1473 }
Paul Bakker48916f92012-09-16 19:57:18 +00001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1479 MBEDTLS_SSL_DEBUG_MSG(3,
1480 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1483 ext + 4, ext_size)) != 0) {
1484 return ret;
1485 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001489
Hanno Beckera0e20d02019-05-15 14:03:01 +01001490#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 case MBEDTLS_TLS_EXT_CID:
1492 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if ((ret = ssl_parse_cid_ext(ssl,
1495 ext + 4,
1496 ext_size)) != 0) {
1497 return ret;
1498 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001501#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1505 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1508 ext + 4, ext_size)) != 0) {
1509 return ret;
1510 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1517 MBEDTLS_SSL_DEBUG_MSG(3,
1518 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if ((ret = ssl_parse_extended_ms_ext(ssl,
1521 ext + 4, ext_size)) != 0) {
1522 return ret;
1523 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1530 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if ((ret = ssl_parse_session_ticket_ext(ssl,
1533 ext + 4, ext_size)) != 0) {
1534 return ret;
1535 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001539
Valerio Setti7aeec542023-07-05 18:57:21 +02001540#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001541 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001542 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1544 MBEDTLS_SSL_DEBUG_MSG(3,
1545 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1548 ext + 4, ext_size)) != 0) {
1549 return ret;
1550 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001553#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001554 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001555 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001556
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001557#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1559 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1562 ext + 4, ext_size)) != 0) {
1563 return ret;
1564 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001567#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 case MBEDTLS_TLS_EXT_ALPN:
1571 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1574 return ret;
1575 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001579
Johan Pascalb62bb512015-12-03 21:56:45 +01001580#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 case MBEDTLS_TLS_EXT_USE_SRTP:
1582 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1585 return ret;
1586 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001589#endif /* MBEDTLS_SSL_DTLS_SRTP */
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 default:
1592 MBEDTLS_SSL_DEBUG_MSG(3,
1593 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001594 }
1595
1596 ext_len -= 4 + ext_size;
1597 ext += 4 + ext_size;
1598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if (ext_len > 0 && ext_len < 4) {
1600 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1601 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001602 }
1603 }
1604
1605 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001606 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1607 * extensions. It sets the transform data for the resumed session which in
1608 * case of DTLS includes the server CID extracted from the CID extension.
1609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (ssl->handshake->resume) {
1611 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1612 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001613 mbedtls_ssl_send_alert_message(
1614 ssl,
1615 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1617 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001618 }
1619 }
1620
Paul Bakker48916f92012-09-16 19:57:18 +00001621 /*
1622 * Renegotiation security checks
1623 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001625 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1627 MBEDTLS_SSL_DEBUG_MSG(1,
1628 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001629 handshake_failure = 1;
1630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 renegotiation_info_seen == 0) {
1635 MBEDTLS_SSL_DEBUG_MSG(1,
1636 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001637 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1639 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1640 ssl->conf->allow_legacy_renegotiation ==
1641 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1642 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001643 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1645 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1646 renegotiation_info_seen == 1) {
1647 MBEDTLS_SSL_DEBUG_MSG(1,
1648 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001649 handshake_failure = 1;
1650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001654 mbedtls_ssl_send_alert_message(
1655 ssl,
1656 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1658 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001659 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001664}
1665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1667 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001668MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001669static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1670 unsigned char **p,
1671 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001672{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001674 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001675
Paul Bakker29e1f122013-04-16 13:07:56 +02001676 /*
1677 * Ephemeral DH parameters:
1678 *
1679 * struct {
1680 * opaque dh_p<1..2^16-1>;
1681 * opaque dh_g<1..2^16-1>;
1682 * opaque dh_Ys<1..2^16-1>;
1683 * } ServerDHParams;
1684 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1686 p, end)) != 0) {
1687 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1688 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001689 }
1690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1692 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1693 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1694 dhm_actual_bitlen,
1695 ssl->conf->dhm_min_bitlen));
1696 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001697 }
1698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1700 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1701 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1706 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001707
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001708#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001709#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1710 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1711 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001712MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001713static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1714 unsigned char **p,
1715 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001716{
1717 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001718 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001719 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001720 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001721 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001722
1723 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001724 * struct {
1725 * ECParameters curve_params;
1726 * ECPoint public;
1727 * } ServerECDHParams;
1728 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001729 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001730 * 2..3 NamedCurve
1731 * 4 ECPoint.len
1732 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (end - *p < 4) {
1735 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1736 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001737
1738 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1740 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1741 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001742
1743 /* Next two bytes are the namedcurve value */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001744 tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
1745 *p += 2;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001746
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001747 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1749 MBEDTLS_SSL_DEBUG_MSG(2,
1750 ("bad server key exchange message (ECDHE curve): %u",
1751 (unsigned) tls_id));
1752 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001753 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001754
Valerio Setti40d9ca92023-01-04 16:08:04 +01001755 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001756 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1758 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001759 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001760 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001761 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001762
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001763 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001764 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 if ((size_t) (end - *p) < ecpoint_len) {
1766 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1767 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001768
Gilles Peskinec29df532023-10-02 14:59:26 +02001769 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1771 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001772
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001773 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1774 handshake->xxdh_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 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001885 len = MBEDTLS_GET_UINT16_BE(*p, 0);
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) {
Manuel Pégourié-Gonnard32bdf192024-05-23 09:13:21 +02001967 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_encrypt", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 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
Gilles Peskine7354f1e2024-01-23 11:06:02 +01002011#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti97207782023-05-18 18:59:06 +02002012 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
Gilles Peskine7354f1e2024-01-23 11:06:02 +01002013#endif /* !defined(MBEDTLS_PK_USE_PSA_EC_DATA) */
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 Settif9362b72023-11-29 08:42:27 +01002018 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_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) {
Ari Weiler-Ofekfb2460a2025-06-10 16:59:44 +01002027 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not supported",
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,
Valerio Settiea59c432023-07-25 11:14:03 +02002035 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002036
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002037 ssl->handshake->xxdh_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)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002041 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2042 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02002043 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,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002048 ssl->handshake->xxdh_psa_peerkey,
Gilles Peskinec29df532023-10-02 14:59:26 +02002049 sizeof(ssl->handshake->xxdh_psa_peerkey));
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 Stekiel7ac93be2023-07-04 10:02:38 +02002055 ssl->handshake->xxdh_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"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01002094 mbedtls_ssl_handshake_increment_state(ssl);
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"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01002115 mbedtls_ssl_handshake_increment_state(ssl);
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;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002176 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (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;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002296 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002297
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);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002301 size_t params_len = (size_t) (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 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002359 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
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 =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002413 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 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:
Gilles Peskine49f179d2025-03-07 15:09:32 +01002457 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
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"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01002475 mbedtls_ssl_handshake_increment_state(ssl);
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"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01002502 mbedtls_ssl_handshake_increment_state(ssl);
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
Gilles Peskine49f179d2025-03-07 15:09:32 +01002520 mbedtls_ssl_handshake_increment_state(ssl);
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 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002587 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002588
2589 /*
2590 * The furthest access in buf is in the loop few lines below:
2591 * sig_alg[i + 1],
2592 * where:
2593 * sig_alg = buf + ...hdr_len + 3 + n,
2594 * max(i) = sig_alg_len - 1.
2595 * Therefore the furthest access is:
2596 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2597 * which reduces to:
2598 * buf[...hdr_len + 3 + n + sig_alg_len],
2599 * which is one less than we need the buf to be.
2600 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2602 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002603 mbedtls_ssl_send_alert_message(
2604 ssl,
2605 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2607 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002608 }
Paul Bakker926af752012-11-23 13:38:07 +01002609
Ronald Cron90915f22022-03-07 11:11:36 +01002610#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2612 for (size_t i = 0; i < sig_alg_len; i += 2) {
2613 MBEDTLS_SSL_DEBUG_MSG(3,
2614 ("Supported Signature Algorithm found: %02x %02x",
2615 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002616 }
2617#endif
2618
2619 n += 2 + sig_alg_len;
2620
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002621 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002622 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002623
2624 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2626 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2627 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2628 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2629 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002630 }
2631
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002632#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2634 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002635 unsigned char *p = dn + i + 2;
2636 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002637 size_t asn1_len;
2638 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 memset(&name, 0, sizeof(name));
2640 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2641 if (dni_len > dn_len - i - 2 ||
2642 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2643 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2644 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2645 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002646 mbedtls_ssl_send_alert_message(
2647 ssl,
2648 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2650 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002651 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 MBEDTLS_SSL_DEBUG_MSG(3,
2653 ("DN hint: %.*s",
2654 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2655 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002656 }
2657#endif
2658
Paul Bakker926af752012-11-23 13:38:07 +01002659exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002663}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002664#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002666MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002667static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002668{
Janos Follath865b3eb2019-12-16 11:46:15 +00002669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2674 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2675 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2679 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2680 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2684 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2685 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2686 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2687 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2688 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 }
2690
Gilles Peskine49f179d2025-03-07 15:09:32 +01002691 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2695 mbedtls_ssl_recv_flight_completed(ssl);
2696 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002697#endif
2698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002702}
2703
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002704MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002705static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002706{
Janos Follath865b3eb2019-12-16 11:46:15 +00002707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002708
2709 size_t header_len;
2710 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002711 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002712 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 /*
2719 * DHM key exchange -- send G^X mod P
2720 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002724 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2727 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2728 &ssl->out_msg[header_len], content_len,
2729 ssl->conf->f_rng, ssl->conf->p_rng);
2730 if (ret != 0) {
2731 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2732 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2736 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2739 ssl->handshake->premaster,
2740 MBEDTLS_PREMASTER_SIZE,
2741 &ssl->handshake->pmslen,
2742 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2743 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2744 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 }
2746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2748 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002750#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2751 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2752 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2753 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002755 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2756 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002758#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002759 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2760 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002761 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002762
2763 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2764
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002765 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002768
Hanno Becker4a63ed42019-01-08 11:39:35 +00002769 /*
2770 * Generate EC private key for ECDHE exchange.
2771 */
2772
Hanno Becker4a63ed42019-01-08 11:39:35 +00002773 /* The master secret is obtained from the shared ECDH secret by
2774 * applying the TLS 1.2 PRF with a specific salt and label. While
2775 * the PSA Crypto API encourages combining key agreement schemes
2776 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2777 * yet support the provisioning of salt + label to the KDF.
2778 * For the time being, we therefore need to split the computation
2779 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002780 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2782 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002783 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002784 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002785
2786 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002788 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 if (status != PSA_SUCCESS) {
2790 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2791 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002792
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002793 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002794 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002795 * but we just need to add a length byte before that. */
2796 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2797 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002799 size_t own_pubkey_len;
2800
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002801 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 own_pubkey, own_pubkey_max_len,
2803 &own_pubkey_len);
2804 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002805 psa_destroy_key(handshake->xxdh_psa_privkey);
2806 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002808 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002809
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002810 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2811 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002812
Hanno Becker4a63ed42019-01-08 11:39:35 +00002813 /* The ECDH secret is the premaster secret used for key derivation. */
2814
Janos Follathdf3b0892019-08-08 11:12:24 +01002815 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002817 handshake->xxdh_psa_privkey,
2818 handshake->xxdh_psa_peerkey,
2819 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 ssl->handshake->premaster,
2821 sizeof(ssl->handshake->premaster),
2822 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002823
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002824 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2825 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2828 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2829 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002830#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002831 /*
2832 * ECDH key exchange -- send client public value
2833 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002834 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002835
Gilles Peskineeccd8882020-03-10 12:19:08 +01002836#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 if (ssl->handshake->ecrs_enabled) {
2838 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002839 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002843 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002844#endif
2845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2847 &content_len,
2848 &ssl->out_msg[header_len], 1000,
2849 ssl->conf->f_rng, ssl->conf->p_rng);
2850 if (ret != 0) {
2851 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002852#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002854 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002856#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002858 }
2859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2861 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002862
Gilles Peskineeccd8882020-03-10 12:19:08 +01002863#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002865 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002866 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002867 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002868
2869ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002871 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002873#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2875 &ssl->handshake->pmslen,
2876 ssl->handshake->premaster,
2877 MBEDTLS_MPI_MAX_SIZE,
2878 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2879 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002880#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002882 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002884#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002886 }
2887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2889 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002890#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2893 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2894 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2895 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002896#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2897 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2900 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2901 psa_key_attributes_t key_attributes;
2902
2903 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2904
2905 /*
2906 * opaque psk_identity<0..2^16-1>;
2907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002909 /* We don't offer PSK suites if we don't have a PSK,
2910 * and we check that the server's choice is among the
2911 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2913 }
Neil Armstrong868af822022-03-09 10:26:25 +01002914
Neil Armstrongfc834f22022-03-23 17:54:38 +01002915 /* uint16 to store content length */
2916 const size_t content_len_size = 2;
2917
Neil Armstrong868af822022-03-09 10:26:25 +01002918 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 if (header_len + content_len_size + ssl->conf->psk_identity_len
2921 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2922 MBEDTLS_SSL_DEBUG_MSG(1,
2923 ("psk identity too long or SSL buffer too short"));
2924 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002925 }
2926
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002927 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2930 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002931 header_len += content_len_size;
2932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 memcpy(p, ssl->conf->psk_identity,
2934 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002935 p += ssl->conf->psk_identity_len;
2936
Neil Armstrong868af822022-03-09 10:26:25 +01002937 header_len += ssl->conf->psk_identity_len;
2938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002940
2941 /*
2942 * Generate EC private key for ECDHE exchange.
2943 */
2944
2945 /* The master secret is obtained from the shared ECDH secret by
2946 * applying the TLS 1.2 PRF with a specific salt and label. While
2947 * the PSA Crypto API encourages combining key agreement schemes
2948 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2949 * yet support the provisioning of salt + label to the KDF.
2950 * For the time being, we therefore need to split the computation
2951 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2952 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2954 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002955 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002956 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002957
2958 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002960 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002962 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 }
Neil Armstrong868af822022-03-09 10:26:25 +01002964
2965 /* Export the public part of the ECDH private key from PSA.
2966 * The export format is an ECPoint structure as expected by TLS,
2967 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002968 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002969 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002971 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002972
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002973 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 own_pubkey, own_pubkey_max_len,
2975 &own_pubkey_len);
2976 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002977 psa_destroy_key(handshake->xxdh_psa_privkey);
2978 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002979 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002980 }
2981
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002982 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002983 content_len = own_pubkey_len + 1;
2984
Neil Armstrong25400452022-03-23 17:44:07 +01002985 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2986 * - a uint16 containing the length (in octets) of the ECDH computation
2987 * - the octet string produced by the ECDH computation
2988 * - a uint16 containing the length (in octets) of the PSK
2989 * - the PSK itself
2990 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002991 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 const unsigned char * const pms_end = pms +
2993 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002994 /* uint16 to store length (in octets) of the ECDH computation */
2995 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002996 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002997
Neil Armstrong25400452022-03-23 17:44:07 +01002998 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02003000 handshake->xxdh_psa_privkey,
3001 handshake->xxdh_psa_peerkey,
3002 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 pms + zlen_size,
3004 pms_end - (pms + zlen_size),
3005 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003006
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02003007 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
3008 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003011 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003013 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 }
Neil Armstrong868af822022-03-09 10:26:25 +01003015
Neil Armstrong25400452022-03-23 17:44:07 +01003016 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003018 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003020#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3021 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003022#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003024 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003025 * opaque psk_identity<0..2^16-1>;
3026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003028 /* We don't offer PSK suites if we don't have a PSK,
3029 * and we check that the server's choice is among the
3030 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003032 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003033
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003034 header_len = 4;
3035 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3038 MBEDTLS_SSL_DEBUG_MSG(1,
3039 ("psk identity too long or SSL buffer too short"));
3040 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003041 }
3042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3044 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 memcpy(ssl->out_msg + header_len,
3047 ssl->conf->psk_identity,
3048 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003049 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003053 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3058 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3059 &content_len, 2)) != 0) {
3060 return ret;
3061 }
3062 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003066 /*
3067 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (header_len + 2 + content_len >
3072 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3073 MBEDTLS_SSL_DEBUG_MSG(1,
3074 ("psk identity or DHM size too long or SSL buffer too short"));
3075 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003076 }
3077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3079 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3082 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3083 &ssl->out_msg[header_len], content_len,
3084 ssl->conf->f_rng, ssl->conf->p_rng);
3085 if (ret != 0) {
3086 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3087 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003088 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003089
3090#if defined(MBEDTLS_USE_PSA_CRYPTO)
3091 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003093 size_t pms_len;
3094
3095 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3097 pms + 2, pms_end - (pms + 2), &pms_len,
3098 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3099 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3100 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003101 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003103 pms += 2 + pms_len;
3104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003106#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003109#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3111 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003112 /*
3113 * ClientECDiffieHellmanPublic public;
3114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3116 &content_len,
3117 &ssl->out_msg[header_len],
3118 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3119 ssl->conf->f_rng, ssl->conf->p_rng);
3120 if (ret != 0) {
3121 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3122 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003123 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3126 MBEDTLS_DEBUG_ECDH_Q);
3127 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003128#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003129 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3131 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003132 }
3133
Neil Armstrong80f6f322022-05-03 17:56:38 +02003134#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003136 (mbedtls_key_exchange_type_t) ciphersuite_info->
3137 key_exchange)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 MBEDTLS_SSL_DEBUG_RET(1,
3139 "mbedtls_ssl_psk_derive_premaster", ret);
3140 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003141 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003142#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003144#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003147 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3149 &content_len, 0)) != 0) {
3150 return ret;
3151 }
3152 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003154#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003156 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003157
Neil Armstrongca7d5062022-05-31 14:43:23 +02003158#if defined(MBEDTLS_USE_PSA_CRYPTO)
3159 unsigned char *out_p = ssl->out_msg + header_len;
3160 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3161 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3163 out_p, end_p - out_p, &content_len,
3164 MBEDTLS_ECJPAKE_ROUND_TWO);
3165 if (ret != 0) {
3166 psa_destroy_key(ssl->handshake->psa_pake_password);
3167 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3168 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3169 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003170 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003171#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3173 ssl->out_msg + header_len,
3174 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3175 &content_len,
3176 ssl->conf->f_rng, ssl->conf->p_rng);
3177 if (ret != 0) {
3178 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3179 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003180 }
3181
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3183 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3184 ssl->conf->f_rng, ssl->conf->p_rng);
3185 if (ret != 0) {
3186 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3187 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003188 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003189#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003191#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003192 {
3193 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3195 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003196 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003197
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003198 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3200 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Gilles Peskine49f179d2025-03-07 15:09:32 +01003202 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3205 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3206 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 }
3208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003210
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003212}
3213
Gilles Peskineeccd8882020-03-10 12:19:08 +01003214#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003215MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003216static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003217{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003218 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003219 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3225 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3226 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003227 }
3228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3230 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01003231 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003233 }
3234
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3236 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003237}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003238#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003239MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003240static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003243 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003244 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003245 size_t n = 0, offset = 0;
3246 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003247 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003249 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003250 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003251#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003252 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003253#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003254 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003255#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003256
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258
Gilles Peskineeccd8882020-03-10 12:19:08 +01003259#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 if (ssl->handshake->ecrs_enabled &&
3261 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003262 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003263 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003264#endif
3265
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3267 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3268 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003269 }
3270
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3272 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01003273 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003275 }
3276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 if (ssl->handshake->client_auth == 0 ||
3278 mbedtls_ssl_own_cert(ssl) == NULL) {
3279 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01003280 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 }
3283
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 if (mbedtls_ssl_own_key(ssl) == NULL) {
3285 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3286 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 }
3288
3289 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003290 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003292#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003294 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003296
3297sign:
3298#endif
3299
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003300 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3301 if (0 != ret) {
3302 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3303 return ret;
3304 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003305
Ronald Cron90915f22022-03-07 11:11:36 +01003306 /*
3307 * digitally-signed struct {
3308 * opaque handshake_messages[handshake_messages_length];
3309 * };
3310 *
3311 * Taking shortcut here. We assume that the server always allows the
3312 * PRF Hash function and has sent it in the allowed signature
3313 * algorithms list received in the Certificate Request message.
3314 *
3315 * Until we encounter a server that does not, we will take this
3316 * shortcut.
3317 *
3318 * Reason: Otherwise we should have running hashes for SHA512 and
3319 * SHA224 in order to satisfy 'weird' needs from the server
3320 * side.
3321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003323 md_alg = MBEDTLS_MD_SHA384;
3324 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003326 md_alg = MBEDTLS_MD_SHA256;
3327 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003328 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003330
3331 /* Info from md_alg will be used instead */
3332 hashlen = 0;
3333 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003334
Gilles Peskineeccd8882020-03-10 12:19:08 +01003335#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003337 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003339#endif
3340
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3342 md_alg, hash_start, hashlen,
3343 ssl->out_msg + 6 + offset,
3344 out_buf_len - 6 - offset,
3345 &n,
3346 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3347 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003348#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003350 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003352#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003354 }
Paul Bakker926af752012-11-23 13:38:07 +01003355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
Paul Bakker1ef83d62012-04-11 12:09:53 +00003358 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3360 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003361
Gilles Peskine49f179d2025-03-07 15:09:32 +01003362 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3365 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3366 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003367 }
3368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003372}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003373#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003376MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003377static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003378{
Janos Follath865b3eb2019-12-16 11:46:15 +00003379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003380 uint32_t lifetime;
3381 size_t ticket_len;
3382 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003383 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3388 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3389 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003390 }
3391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3393 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003394 mbedtls_ssl_send_alert_message(
3395 ssl,
3396 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3398 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003399 }
3400
3401 /*
3402 * struct {
3403 * uint32 ticket_lifetime_hint;
3404 * opaque ticket<0..2^16-1>;
3405 * } NewSessionTicket;
3406 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003407 * 0 . 3 ticket_lifetime_hint
3408 * 4 . 5 ticket_len (n)
3409 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003410 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3412 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3413 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3414 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3415 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3416 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003417 }
3418
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003420
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003421 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003422
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003423 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003424
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3426 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3427 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3428 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3429 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003430 }
3431
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003433
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003434 /* We're not waiting for a NewSessionTicket message any more */
3435 ssl->handshake->new_session_ticket = 0;
Gilles Peskine49f179d2025-03-07 15:09:32 +01003436 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003437
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003438 /*
3439 * Zero-length ticket means the server changed his mind and doesn't want
3440 * to send a ticket after all, so just forget it
3441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 if (ticket_len == 0) {
3443 return 0;
3444 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003447 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003449 ssl->session->ticket = NULL;
3450 ssl->session->ticket_len = 0;
3451 }
3452
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003453 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003455 ssl->session_negotiate->ticket = NULL;
3456 ssl->session_negotiate->ticket_len = 0;
3457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3459 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3460 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3461 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3462 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003463 }
3464
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003466
3467 ssl->session_negotiate->ticket = ticket;
3468 ssl->session_negotiate->ticket_len = ticket_len;
3469 ssl->session_negotiate->ticket_lifetime = lifetime;
3470
3471 /*
3472 * RFC 5077 section 3.4:
3473 * "If the client receives a session ticket from the server, then it
3474 * discards any Session ID that was sent in the ServerHello."
3475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003477 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003478
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003480
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003484
Paul Bakker5121ce52009-01-03 21:22:43 +00003485/*
Paul Bakker1961b702013-01-25 14:49:24 +01003486 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003488int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003489{
3490 int ret = 0;
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003493 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003495 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3496 ssl->handshake->new_session_ticket != 0) {
Gilles Peskine49f179d2025-03-07 15:09:32 +01003497 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_NEW_SESSION_TICKET);
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003498 }
3499#endif
3500
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine49f179d2025-03-07 15:09:32 +01003503 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Paul Bakker5121ce52009-01-03 21:22:43 +00003504 break;
3505
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 /*
3507 * ==> ClientHello
3508 */
3509 case MBEDTLS_SSL_CLIENT_HELLO:
3510 ret = mbedtls_ssl_write_client_hello(ssl);
3511 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003512
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 /*
3514 * <== ServerHello
3515 * Certificate
3516 * ( ServerKeyExchange )
3517 * ( CertificateRequest )
3518 * ServerHelloDone
3519 */
3520 case MBEDTLS_SSL_SERVER_HELLO:
3521 ret = ssl_parse_server_hello(ssl);
3522 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3525 ret = mbedtls_ssl_parse_certificate(ssl);
3526 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3529 ret = ssl_parse_server_key_exchange(ssl);
3530 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003531
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3533 ret = ssl_parse_certificate_request(ssl);
3534 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3537 ret = ssl_parse_server_hello_done(ssl);
3538 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 /*
3541 * ==> ( Certificate/Alert )
3542 * ClientKeyExchange
3543 * ( CertificateVerify )
3544 * ChangeCipherSpec
3545 * Finished
3546 */
3547 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3548 ret = mbedtls_ssl_write_certificate(ssl);
3549 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003550
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3552 ret = ssl_write_client_key_exchange(ssl);
3553 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003554
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3556 ret = ssl_write_certificate_verify(ssl);
3557 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003558
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3560 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3561 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003562
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 case MBEDTLS_SSL_CLIENT_FINISHED:
3564 ret = mbedtls_ssl_write_finished(ssl);
3565 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003566
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 /*
3568 * <== ( NewSessionTicket )
3569 * ChangeCipherSpec
3570 * Finished
3571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3574 ret = ssl_parse_new_session_ticket(ssl);
3575 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003576#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003577
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3579 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3580 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003581
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 case MBEDTLS_SSL_SERVER_FINISHED:
3583 ret = mbedtls_ssl_parse_finished(ssl);
3584 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003585
Gilles Peskine449bd832023-01-11 14:50:10 +01003586 case MBEDTLS_SSL_FLUSH_BUFFERS:
3587 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01003588 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003590
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3592 mbedtls_ssl_handshake_wrapup(ssl);
3593 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003594
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 default:
3596 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3597 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003599
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003601}
Jerry Yuc5aef882021-12-23 20:15:02 +08003602
Jerry Yufb4b6472022-01-27 15:03:26 +08003603#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */