blob: 5469850b1019949f6307ca7125b4d4cd33543360 [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"
Janos Follath73c616b2019-12-18 15:07:04 +000017#include "mbedtls/debug.h"
18#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
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
368 return 0;
369 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 MBEDTLS_SSL_DEBUG_MSG(3,
372 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200373
Hanno Becker261602c2017-04-12 14:54:42 +0100374 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100378 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100381 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200382
383 *olen = 4;
384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
386 return 0;
387 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 MBEDTLS_SSL_DEBUG_MSG(3,
390 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200393
394 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200399
Ron Eldora9788042018-12-05 11:04:31 +0200400#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200401MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100402static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
403 unsigned char *buf,
404 const unsigned char *end,
405 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100406{
407 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200408 size_t protection_profiles_index = 0, ext_len = 0;
409 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100410
411 *olen = 0;
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
414 (ssl->conf->dtls_srtp_profile_list == NULL) ||
415 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
416 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100417 }
418
Ron Eldora9788042018-12-05 11:04:31 +0200419 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100420 * uint8 SRTPProtectionProfile[2];
421 *
422 * struct {
423 * SRTPProtectionProfiles SRTPProtectionProfiles;
424 * opaque srtp_mki<0..255>;
425 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100426 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100427 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200429 mki_len = ssl->dtls_srtp_info.mki_len;
430 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300431 /* Extension length = 2 bytes for profiles length,
432 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
433 * 1 byte for srtp_mki vector length and the mki_len value
434 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200438
439 /* Check there is room in the buffer for the extension + 4 bytes
440 * - the extension tag (2 bytes)
441 * - the extension length (2 bytes)
442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100446 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100449 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100450
Ron Eldor3adb9922017-12-21 10:15:08 +0200451 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200452 /* micro-optimization:
453 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
454 * which is lower than 127, so the upper byte of the length is always 0
455 * For the documentation, the more generic code is left in comments
456 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
457 * >> 8 ) & 0xFF );
458 */
459 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300463 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200465 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
467 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
468 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
469 profile_value));
470 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100471 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200473 /*
474 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200475 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200476 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 MBEDTLS_SSL_DEBUG_MSG(3,
478 ("client hello, "
479 "illegal DTLS-SRTP protection profile %d",
480 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
481 ));
482 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100483 }
484 }
485
Ron Eldor591f1622018-01-22 12:30:04 +0200486 *p++ = mki_len & 0xFF;
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (mki_len != 0) {
489 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200490 /*
491 * Increment p to point to the current position.
492 */
493 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
495 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200496 }
497
Ron Eldoref72faf2018-07-12 11:54:20 +0300498 /*
499 * total extension length: extension type (2 bytes)
500 * + extension length (2 bytes)
501 * + protection profile length (2 bytes)
502 * + 2 * number of protection profiles
503 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200504 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300505 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200506 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100509}
510#endif /* MBEDTLS_SSL_DTLS_SRTP */
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
513 unsigned char *buf,
514 const unsigned char *end,
515 int uses_ec,
516 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100517{
518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
519 unsigned char *p = buf;
520 size_t ext_len = 0;
521
522 (void) ssl;
523 (void) end;
524 (void) uses_ec;
525 (void) ret;
526 (void) ext_len;
527
528 *out_len = 0;
529
530 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
531 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
532#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
534 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
535 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100536 }
537 p += ext_len;
538#endif
539
Valerio Setti7aeec542023-07-05 18:57:21 +0200540#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200541 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Ronald Cron12dcdf02022-02-16 15:28:22 +0100542 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (uses_ec) {
544 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
545 &ext_len)) != 0) {
546 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
547 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100548 }
549 p += ext_len;
550 }
551#endif
552
553#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
555 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
556 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100557 }
558 p += ext_len;
559#endif
560
561#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
563 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
564 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100565 }
566 p += ext_len;
567#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
568
569#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
571 &ext_len)) != 0) {
572 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
573 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100574 }
575 p += ext_len;
576#endif
577
578#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
580 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
581 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100582 }
583 p += ext_len;
584#endif
585
586#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
588 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
589 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100590 }
591 p += ext_len;
592#endif
593
594#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
596 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
597 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100598 }
599 p += ext_len;
600#endif
601
602#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
604 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
605 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100606 }
607 p += ext_len;
608#endif
609
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000610 *out_len = (size_t) (p - buf);
Ronald Cron12dcdf02022-02-16 15:28:22 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100613}
614
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200615MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100616static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
617 const unsigned char *buf,
618 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100622 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000624 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 mbedtls_ct_memcmp(buf + 1,
626 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
627 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
628 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
629 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100630 mbedtls_ssl_send_alert_message(
631 ssl,
632 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
634 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000635 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100638 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (len != 1 || buf[0] != 0x00) {
640 MBEDTLS_SSL_DEBUG_MSG(1,
641 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100642 mbedtls_ssl_send_alert_message(
643 ssl,
644 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
646 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100647 }
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100650 }
Paul Bakker48916f92012-09-16 19:57:18 +0000651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000653}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200656MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100657static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
658 const unsigned char *buf,
659 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200660{
661 /*
662 * server should use the extension only if we did,
663 * and if so the server's value should match ours (and len is always 1)
664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200666 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 buf[0] != ssl->conf->mfl_code) {
668 MBEDTLS_SSL_DEBUG_MSG(1,
669 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100670 mbedtls_ssl_send_alert_message(
671 ssl,
672 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
674 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200675 }
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000680
Hanno Beckera0e20d02019-05-15 14:03:01 +0100681#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200682MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100683static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
684 const unsigned char *buf,
685 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100686{
687 size_t peer_cid_len;
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100690 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
691 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
693 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
694 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
695 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
696 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100697 }
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (len == 0) {
700 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
701 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
702 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
703 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100704 }
705
706 peer_cid_len = *buf++;
707 len--;
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
710 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
711 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
712 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
713 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100714 }
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (len != peer_cid_len) {
717 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
718 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
719 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
720 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100721 }
722
Hanno Becker5a299902019-05-03 12:47:49 +0100723 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100724 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
728 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100731}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100732#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200735MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100736static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
737 const unsigned char *buf,
738 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100739{
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
741 len != 0) {
742 MBEDTLS_SSL_DEBUG_MSG(1,
743 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100744 mbedtls_ssl_send_alert_message(
745 ssl,
746 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
748 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100749 }
750
751 ((void) buf);
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200760MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100761static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
762 const unsigned char *buf,
763 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200764{
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
766 len != 0) {
767 MBEDTLS_SSL_DEBUG_MSG(1,
768 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100769 mbedtls_ssl_send_alert_message(
770 ssl,
771 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
773 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200774 }
775
776 ((void) buf);
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200785MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100786static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
787 const unsigned char *buf,
788 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200789{
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
791 len != 0) {
792 MBEDTLS_SSL_DEBUG_MSG(1,
793 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100794 mbedtls_ssl_send_alert_message(
795 ssl,
796 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
798 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200799 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200800
801 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200802
803 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200808
Valerio Setti7aeec542023-07-05 18:57:21 +0200809#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200810 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100811 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200812MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100813static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
814 const unsigned char *buf,
815 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200816{
817 size_t list_size;
818 const unsigned char *p;
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (len == 0 || (size_t) (buf[0] + 1) != len) {
821 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
822 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
823 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
824 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200825 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200826 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200827
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200828 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 while (list_size > 0) {
830 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
831 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Valerio Setti7aeec542023-07-05 18:57:21 +0200832#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
833 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200834 ssl->handshake->ecdh_ctx.point_format = p[0];
Valerio Setti7aeec542023-07-05 18:57:21 +0200835#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200836#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
838 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
839 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200840#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
842 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200843 }
844
845 list_size--;
846 p++;
847 }
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
850 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
851 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
852 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200853}
Valerio Setti7aeec542023-07-05 18:57:21 +0200854#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200855 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200856 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200857
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200858#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200859MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100860static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
861 const unsigned char *buf,
862 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200863{
Janos Follath865b3eb2019-12-16 11:46:15 +0000864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (ssl->handshake->ciphersuite_info->key_exchange !=
867 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
868 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
869 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200870 }
871
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200872 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200874 ssl->handshake->ecjpake_cache = NULL;
875 ssl->handshake->ecjpake_cache_len = 0;
876
Neil Armstrongca7d5062022-05-31 14:43:23 +0200877#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if ((ret = mbedtls_psa_ecjpake_read_round(
879 &ssl->handshake->psa_pake_ctx, buf, len,
880 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
881 psa_destroy_key(ssl->handshake->psa_pake_password);
882 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100885 mbedtls_ssl_send_alert_message(
886 ssl,
887 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
889 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200890 }
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 return 0;
893#else
894 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
895 buf, len)) != 0) {
896 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
897 mbedtls_ssl_send_alert_message(
898 ssl,
899 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
900 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
901 return ret;
902 }
903
904 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200905#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200906}
907#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200910MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100911static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
912 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200913{
914 size_t list_len, name_len;
915 const char **p;
916
917 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (ssl->conf->alpn_list == NULL) {
919 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100920 mbedtls_ssl_send_alert_message(
921 ssl,
922 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
924 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200925 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200926
927 /*
928 * opaque ProtocolName<1..2^8-1>;
929 *
930 * struct {
931 * ProtocolName protocol_name_list<2..2^16-1>
932 * } ProtocolNameList;
933 *
934 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
935 */
936
937 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (len < 4) {
939 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
940 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
941 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200942 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200943
Dave Rodgmana3d0f612023-11-03 23:34:02 +0000944 list_len = MBEDTLS_GET_UINT16_BE(buf, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (list_len != len - 2) {
946 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
947 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
948 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200949 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200950
951 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (name_len != list_len - 1) {
953 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
954 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
955 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200956 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200957
958 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
960 if (name_len == strlen(*p) &&
961 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200962 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200964 }
965 }
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
968 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
969 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
970 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200973
Johan Pascalb62bb512015-12-03 21:56:45 +0100974#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200975MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
977 const unsigned char *buf,
978 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100979{
Johan Pascal43f94902020-09-22 12:25:52 +0200980 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200981 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100982 uint16_t server_protection_profile_value = 0;
983
984 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
986 (ssl->conf->dtls_srtp_profile_list == NULL) ||
987 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
988 return 0;
989 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100990
Ron Eldora9788042018-12-05 11:04:31 +0200991 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100992 * uint8 SRTPProtectionProfile[2];
993 *
994 * struct {
995 * SRTPProtectionProfiles SRTPProtectionProfiles;
996 * opaque srtp_mki<0..255>;
997 * } UseSRTPData;
998
999 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1000 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001003 mki_len = ssl->dtls_srtp_info.mki_len;
1004 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001005
Ron Eldoref72faf2018-07-12 11:54:20 +03001006 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001007 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1008 * + protection profile (2 bytes)
1009 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001010 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if ((len < 5) || (len != (buf[4] + 5u))) {
1013 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1014 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001015
1016 /*
1017 * get the server protection profile
1018 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001019
1020 /*
1021 * protection profile length must be 0x0002 as we must have only
1022 * one protection profile in server Hello
1023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if ((buf[0] != 0) || (buf[1] != 2)) {
1025 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1026 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001029 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 server_protection_profile_value);
1031 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1032 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1033 mbedtls_ssl_get_srtp_profile_as_string(
1034 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001035 }
1036
Johan Pascal43f94902020-09-22 12:25:52 +02001037 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001038
Johan Pascalb62bb512015-12-03 21:56:45 +01001039 /*
1040 * Check we have the server profile in our list
1041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1043 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001044 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001046 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001048 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001049 }
1050 }
1051
Ron Eldor591f1622018-01-22 12:30:04 +02001052 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1054 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1055 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1056 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001057 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001058
1059 /* If server does not use mki in its reply, make sure the client won't keep
1060 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001062 ssl->dtls_srtp_info.mki_len = 0;
1063 }
1064
Ron Eldoref72faf2018-07-12 11:54:20 +03001065 /*
1066 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001067 * If the client detects a nonzero-length MKI in the server's response
1068 * that is different than the one the client offered, then the client
1069 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (len > 5 && (buf[4] != mki_len ||
1072 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1073 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1074 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1075 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001077#if defined(MBEDTLS_DEBUG_C)
1078 if (len > 5) {
1079 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1080 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001081 }
1082#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001084}
1085#endif /* MBEDTLS_SSL_DTLS_SRTP */
1086
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001087/*
1088 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001092static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001093{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001094 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001096 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001097
1098#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1099 uint8_t cookie_len;
1100#else
1101 uint16_t cookie_len;
1102#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001105
Gilles Peskineb64bf062019-09-27 14:02:44 +02001106 /* Check that there is enough room for:
1107 * - 2 bytes of version
1108 * - 1 byte of cookie_len
1109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1111 MBEDTLS_SSL_DEBUG_MSG(1,
1112 ("incoming HelloVerifyRequest message is too short"));
1113 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1114 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1115 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001116 }
1117
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001118 /*
1119 * struct {
1120 * ProtocolVersion server_version;
1121 * opaque cookie<0..2^8-1>;
1122 * } HelloVerifyRequest;
1123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1125 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001126 p += 2;
1127
TRodziewicz2d8800e2021-05-13 19:14:19 +02001128 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001129 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1130 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1131 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1134 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1137 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001140 }
1141
1142 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1144 MBEDTLS_SSL_DEBUG_MSG(1,
1145 ("cookie length does not match incoming message size"));
1146 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1147 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1148 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001149 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1155 if (ssl->handshake->cookie == NULL) {
1156 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1157 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001158 }
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001161 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001162
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001163 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001165 ret = mbedtls_ssl_reset_checksum(ssl);
1166 if (0 != ret) {
1167 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1168 return ret;
1169 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001179MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001180static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001181{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001182 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001183 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001184 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001185 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001186 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001188 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001189#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001190 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001196 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1198 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
1200
Hanno Becker79594fd2019-05-08 09:38:41 +01001201 buf = ssl->in_msg;
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001206 ssl->renego_records_seen++;
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (ssl->conf->renego_max_records >= 0 &&
1209 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1210 MBEDTLS_SSL_DEBUG_MSG(1,
1211 ("renegotiation requested, but not honored by server"));
1212 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001213 }
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 MBEDTLS_SSL_DEBUG_MSG(1,
1216 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001217
1218 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001224 mbedtls_ssl_send_alert_message(
1225 ssl,
1226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1228 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 }
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1233 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1234 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1235 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1236 return ssl_parse_hello_verify_request(ssl);
1237 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001238 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001240 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001241 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001242 }
1243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1247 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1248 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1249 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1250 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1251 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 }
1253
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001254 /*
1255 * 0 . 1 server_version
1256 * 2 . 33 random (maybe including 4 bytes of Unix time)
1257 * 34 . 34 session_id length = n
1258 * 35 . 34+n session_id
1259 * 35+n . 36+n cipher_suite
1260 * 37+n . 37+n compression_method
1261 *
1262 * 38+n . 39+n extensions length (optional)
1263 * 40+n . .. extensions
1264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01001268 ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
1269 ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001270 ssl->session_negotiate->tls_version = ssl->tls_version;
Ronald Cron17ef8df2023-11-22 10:29:42 +01001271 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (ssl->tls_version < ssl->conf->min_tls_version ||
1274 ssl->tls_version > ssl->conf->max_tls_version) {
1275 MBEDTLS_SSL_DEBUG_MSG(1,
1276 (
1277 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1278 (unsigned) ssl->conf->min_tls_version,
1279 (unsigned) ssl->tls_version,
1280 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1283 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001286 }
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1289 ((unsigned long) buf[2] << 24) |
1290 ((unsigned long) buf[3] << 16) |
1291 ((unsigned long) buf[4] << 8) |
1292 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001296 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (n > 32) {
1301 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1302 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1303 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1304 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001305 }
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001308 ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if ((ext_len > 0 && ext_len < 4) ||
1311 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1312 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001313 mbedtls_ssl_send_alert_message(
1314 ssl,
1315 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1317 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001318 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001320 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 } else {
1322 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1323 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1324 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1325 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001328 /* ciphersuite (used later) */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001329 i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35);
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001330
1331 /*
1332 * Read and check compression
1333 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001334 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1337 MBEDTLS_SSL_DEBUG_MSG(1,
1338 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001339 mbedtls_ssl_send_alert_message(
1340 ssl,
1341 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1343 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001344 }
1345
Paul Bakker380da532012-04-18 16:10:25 +00001346 /*
1347 * Initialize update checksum functions
1348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1350 if (ssl->handshake->ciphersuite_info == NULL) {
1351 MBEDTLS_SSL_DEBUG_MSG(1,
1352 ("ciphersuite info for %04x not found", (unsigned int) i));
1353 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1354 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1355 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001356 }
Paul Bakker380da532012-04-18 16:10:25 +00001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1361 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
1363 /*
1364 * Check if the session can be resumed
1365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_SSL_RENEGOTIATION)
1368 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001369#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001370 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001371 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001374 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001377#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001378 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001379 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 memcpy(ssl->session_negotiate->id, buf + 35, n);
1381 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 }
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1386 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1389 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1390 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Andrzej Kurek03bac442018-04-25 05:06:07 -04001392 /*
1393 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001394 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 while (1) {
1397 if (ssl->conf->ciphersuite_list[i] == 0) {
1398 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001399 mbedtls_ssl_send_alert_message(
1400 ssl,
1401 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1403 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 }
1405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (ssl->conf->ciphersuite_list[i++] ==
1407 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 }
1411
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001412 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 ssl->session_negotiate->ciphersuite);
1414 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1415 ssl->tls_version) != 0) {
1416 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001417 mbedtls_ssl_send_alert_message(
1418 ssl,
1419 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1421 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001422 }
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 MBEDTLS_SSL_DEBUG_MSG(3,
1425 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001426
Gilles Peskineeccd8882020-03-10 12:19:08 +01001427#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1429 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001430 ssl->handshake->ecrs_enabled = 1;
1431 }
1432#endif
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1435 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001436 mbedtls_ssl_send_alert_message(
1437 ssl,
1438 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1440 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 }
1442
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001443 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 MBEDTLS_SSL_DEBUG_MSG(2,
1446 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1447 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 while (ext_len) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001450 unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
1451 unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
Paul Bakker48916f92012-09-16 19:57:18 +00001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (ext_size + 4 > ext_len) {
1454 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001455 mbedtls_ssl_send_alert_message(
1456 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1458 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001459 }
1460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 switch (ext_id) {
1462 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1463 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001466#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1469 ext_size)) != 0) {
1470 return ret;
1471 }
Paul Bakker48916f92012-09-16 19:57:18 +00001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1477 MBEDTLS_SSL_DEBUG_MSG(3,
1478 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1481 ext + 4, ext_size)) != 0) {
1482 return ret;
1483 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001487
Hanno Beckera0e20d02019-05-15 14:03:01 +01001488#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 case MBEDTLS_TLS_EXT_CID:
1490 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if ((ret = ssl_parse_cid_ext(ssl,
1493 ext + 4,
1494 ext_size)) != 0) {
1495 return ret;
1496 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001499#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1503 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1506 ext + 4, ext_size)) != 0) {
1507 return ret;
1508 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1515 MBEDTLS_SSL_DEBUG_MSG(3,
1516 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 if ((ret = ssl_parse_extended_ms_ext(ssl,
1519 ext + 4, ext_size)) != 0) {
1520 return ret;
1521 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1528 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if ((ret = ssl_parse_session_ticket_ext(ssl,
1531 ext + 4, ext_size)) != 0) {
1532 return ret;
1533 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001537
Valerio Setti7aeec542023-07-05 18:57:21 +02001538#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001539 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001540 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1542 MBEDTLS_SSL_DEBUG_MSG(3,
1543 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1546 ext + 4, ext_size)) != 0) {
1547 return ret;
1548 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001551#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001552 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001553 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001554
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001555#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1557 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1560 ext + 4, ext_size)) != 0) {
1561 return ret;
1562 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001565#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 case MBEDTLS_TLS_EXT_ALPN:
1569 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1572 return ret;
1573 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001577
Johan Pascalb62bb512015-12-03 21:56:45 +01001578#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 case MBEDTLS_TLS_EXT_USE_SRTP:
1580 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1583 return ret;
1584 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001587#endif /* MBEDTLS_SSL_DTLS_SRTP */
1588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 default:
1590 MBEDTLS_SSL_DEBUG_MSG(3,
1591 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001592 }
1593
1594 ext_len -= 4 + ext_size;
1595 ext += 4 + ext_size;
1596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if (ext_len > 0 && ext_len < 4) {
1598 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1599 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001600 }
1601 }
1602
1603 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001604 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1605 * extensions. It sets the transform data for the resumed session which in
1606 * case of DTLS includes the server CID extracted from the CID extension.
1607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (ssl->handshake->resume) {
1609 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1610 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001611 mbedtls_ssl_send_alert_message(
1612 ssl,
1613 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1615 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001616 }
1617 }
1618
Paul Bakker48916f92012-09-16 19:57:18 +00001619 /*
1620 * Renegotiation security checks
1621 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001623 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1625 MBEDTLS_SSL_DEBUG_MSG(1,
1626 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001627 handshake_failure = 1;
1628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 renegotiation_info_seen == 0) {
1633 MBEDTLS_SSL_DEBUG_MSG(1,
1634 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001635 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1637 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1638 ssl->conf->allow_legacy_renegotiation ==
1639 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1640 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001641 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1643 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1644 renegotiation_info_seen == 1) {
1645 MBEDTLS_SSL_DEBUG_MSG(1,
1646 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001647 handshake_failure = 1;
1648 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001652 mbedtls_ssl_send_alert_message(
1653 ssl,
1654 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1656 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662}
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1665 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001666MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001667static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1668 unsigned char **p,
1669 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001670{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001672 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001673
Paul Bakker29e1f122013-04-16 13:07:56 +02001674 /*
1675 * Ephemeral DH parameters:
1676 *
1677 * struct {
1678 * opaque dh_p<1..2^16-1>;
1679 * opaque dh_g<1..2^16-1>;
1680 * opaque dh_Ys<1..2^16-1>;
1681 * } ServerDHParams;
1682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1684 p, end)) != 0) {
1685 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1686 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001687 }
1688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1690 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1691 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1692 dhm_actual_bitlen,
1693 ssl->conf->dhm_min_bitlen));
1694 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001695 }
1696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1698 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1699 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1704 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001705
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001706#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001707#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1708 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1709 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001710MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001711static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1712 unsigned char **p,
1713 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001714{
1715 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001716 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001717 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001718 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001719 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001720
1721 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001722 * struct {
1723 * ECParameters curve_params;
1724 * ECPoint public;
1725 * } ServerECDHParams;
1726 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001727 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001728 * 2..3 NamedCurve
1729 * 4 ECPoint.len
1730 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001731 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if (end - *p < 4) {
1733 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1734 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001735
1736 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1738 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1739 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001740
1741 /* Next two bytes are the namedcurve value */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001742 tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
1743 *p += 2;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001744
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001745 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1747 MBEDTLS_SSL_DEBUG_MSG(2,
1748 ("bad server key exchange message (ECDHE curve): %u",
1749 (unsigned) tls_id));
1750 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001751 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001752
Valerio Setti40d9ca92023-01-04 16:08:04 +01001753 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001754 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1756 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001757 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001758 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001759 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001760
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001761 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001762 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if ((size_t) (end - *p) < ecpoint_len) {
1764 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1765 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001766
Gilles Peskinec29df532023-10-02 14:59:26 +02001767 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1769 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001770
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001771 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1772 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001773 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001776}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001777#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1778 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1779 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001780#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001781#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1782 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1783 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1784 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1785 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001786MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001787static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001788{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001789 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001790 mbedtls_ecp_group_id grp_id;
1791#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1792 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1793#else
1794 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1795#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1798 if (tls_id == 0) {
1799 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1800 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001801 }
1802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1804 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1807 return -1;
1808 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1811 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001814}
1815
Andrzej Kurek468c5062022-10-24 10:30:14 -04001816#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1817 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1818 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1819 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1820 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1821
1822#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1823 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1824 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001825MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001826static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1827 unsigned char **p,
1828 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001831
Paul Bakker29e1f122013-04-16 13:07:56 +02001832 /*
1833 * Ephemeral ECDH parameters:
1834 *
1835 * struct {
1836 * ECParameters curve_params;
1837 * ECPoint public;
1838 * } ServerECDHParams;
1839 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1841 (const unsigned char **) p, end)) != 0) {
1842 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001843#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001845 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001847#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001849 }
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if (ssl_check_server_ecdh_params(ssl) != 0) {
1852 MBEDTLS_SSL_DEBUG_MSG(1,
1853 ("bad server key exchange message (ECDHE curve)"));
1854 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001855 }
1856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001858}
Gilles Peskine449bd832023-01-11 14:50:10 +01001859#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1860 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001861 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1862#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001863#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001864MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001865static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1866 unsigned char **p,
1867 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001868{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001870 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001871 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001872
1873 /*
1874 * PSK parameters:
1875 *
1876 * opaque psk_identity_hint<0..2^16-1>;
1877 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 if (end - (*p) < 2) {
1879 MBEDTLS_SSL_DEBUG_MSG(1,
1880 ("bad server key exchange message (psk_identity_hint length)"));
1881 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001882 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001883 len = MBEDTLS_GET_UINT16_BE(*p, 0);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001884 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 if (end - (*p) < len) {
1887 MBEDTLS_SSL_DEBUG_MSG(1,
1888 ("bad server key exchange message (psk_identity_hint length)"));
1889 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001890 }
1891
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001892 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001893 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001894 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001895 * someone needs that feature.
1896 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001897 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001898 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001901}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001902#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1905 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001906/*
1907 * Generate a pre-master secret and encrypt it with the server's RSA key
1908 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001909MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001910static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1911 size_t offset, size_t *olen,
1912 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001913{
Janos Follath865b3eb2019-12-16 11:46:15 +00001914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001915 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001916 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1920 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1921 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001922 }
1923
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001924 /*
1925 * Generate (part of) the pre-master as
1926 * struct {
1927 * ProtocolVersion client_version;
1928 * opaque random[46];
1929 * } PreMasterSecret;
1930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 mbedtls_ssl_write_version(p, ssl->conf->transport,
1932 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1935 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1936 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001937 }
1938
1939 ssl->handshake->pmslen = 48;
1940
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001941#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1942 peer_pk = &ssl->handshake->peer_pubkey;
1943#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001945 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1947 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001948 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001949 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1950#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001951
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001952 /*
1953 * Now write it out, encrypted
1954 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1956 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1957 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001958 }
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if ((ret = mbedtls_pk_encrypt(peer_pk,
1961 p, ssl->handshake->pmslen,
1962 ssl->out_msg + offset + len_bytes, olen,
1963 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1964 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1965 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1966 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001967 }
1968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (len_bytes == 2) {
1970 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001971 *olen += 2;
1972 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001973
Hanno Beckerae553dd2019-02-08 14:06:00 +00001974#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1975 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001977#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
1981 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1984 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001985MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001986static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001987{
Janos Follath865b3eb2019-12-16 11:46:15 +00001988 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001990
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001991#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1992 peer_pk = &ssl->handshake->peer_pubkey;
1993#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001995 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1997 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001998 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001999 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2000#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002001
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002002 /* This is a public key, so it can't be opaque, so can_do() is a good
2003 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2005 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2006 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002007 }
2008
Valerio Setti97207782023-05-18 18:59:06 +02002009#if defined(MBEDTLS_ECP_C)
2010 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
2011#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002012
Przemek Stekielea4000f2022-03-16 09:49:33 +01002013#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002014 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02002015 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Settif9362b72023-11-29 08:42:27 +01002016 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002017
Valerio Setti97207782023-05-18 18:59:06 +02002018 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2020 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002021 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002022
Valerio Setti97207782023-05-18 18:59:06 +02002023 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (tls_id == 0) {
2025 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002026 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002028 }
2029
Valerio Setti1e868cc2023-01-09 17:30:01 +01002030 /* If the above conversion to TLS ID was fine, then also this one will be,
2031 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02002032 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02002033 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002034
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002035 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002036
Przemek Stekielea4000f2022-03-16 09:49:33 +01002037 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002038#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002039 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2040 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02002041 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002042#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002043 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2045 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002046 ssl->handshake->xxdh_psa_peerkey,
Gilles Peskinec29df532023-10-02 14:59:26 +02002047 sizeof(ssl->handshake->xxdh_psa_peerkey));
Przemek Stekielea4000f2022-03-16 09:49:33 +01002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (ret != 0) {
2050 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2051 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002052 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002053 ssl->handshake->xxdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002054#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2055#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2057 MBEDTLS_ECDH_THEIRS)) != 0) {
2058 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2059 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002060 }
2061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (ssl_check_server_ecdh_params(ssl) != 0) {
2063 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2064 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002065 }
Valerio Setti97207782023-05-18 18:59:06 +02002066#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002067#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2068 /* We don't need the peer's public key anymore. Free it,
2069 * so that more RAM is available for upcoming expensive
2070 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002072#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2077 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002078
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002079MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002080static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002081{
Janos Follath865b3eb2019-12-16 11:46:15 +00002082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002083 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002084 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002085 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2091 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002095 ((void) p);
2096 ((void) end);
2097#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2100 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2102 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2103 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2104 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002105 mbedtls_ssl_send_alert_message(
2106 ssl,
2107 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2109 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002110 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002113 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002115 }
2116 ((void) p);
2117 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2119 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002120
Gilles Peskineeccd8882020-03-10 12:19:08 +01002121#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (ssl->handshake->ecrs_enabled &&
2123 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002124 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002125 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002126#endif
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2129 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2130 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 }
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2134 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002135 mbedtls_ssl_send_alert_message(
2136 ssl,
2137 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2139 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
2141
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002142 /*
2143 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2144 * doesn't use a psk_identity_hint
2145 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2147 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2148 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002149 /* Current message is probably either
2150 * CertificateRequest or ServerHelloDone */
2151 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002152 goto exit;
2153 }
2154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 MBEDTLS_SSL_DEBUG_MSG(1,
2156 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002157 mbedtls_ssl_send_alert_message(
2158 ssl,
2159 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
2164
Gilles Peskineeccd8882020-03-10 12:19:08 +01002165#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002167 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002169
2170start_processing:
2171#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002173 end = ssl->in_msg + ssl->in_hslen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002174 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002175
Gilles Peskineeccd8882020-03-10 12:19:08 +01002176#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2179 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2181 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2182 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002183 mbedtls_ssl_send_alert_message(
2184 ssl,
2185 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2187 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002188 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002189 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002190#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2193 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2195 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002196 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2199 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2200#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2201 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2203 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2204 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2205 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002206 mbedtls_ssl_send_alert_message(
2207 ssl,
2208 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2210 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2214 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002215#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2216 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2221 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2222 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002223 mbedtls_ssl_send_alert_message(
2224 ssl,
2225 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2227 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002228 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2231 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2232 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002233#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002235#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002236 /*
2237 * The first 3 bytes are:
2238 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2239 * [1, 2] elliptic curve's TLS ID
2240 *
2241 * However since we only support secp256r1 for now, we check only
2242 * that TLS ID here
2243 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002245 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (exp_tls_id == 0) {
2249 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002250 }
2251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2253 (read_tls_id != exp_tls_id)) {
2254 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002255 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002256
2257 p += 3;
2258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if ((ret = mbedtls_psa_ecjpake_read_round(
2260 &ssl->handshake->psa_pake_ctx, p, end - p,
2261 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2262 psa_destroy_key(ssl->handshake->psa_pake_password);
2263 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002266 mbedtls_ssl_send_alert_message(
2267 ssl,
2268 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2270 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002271 }
2272#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2274 p, end - p);
2275 if (ret != 0) {
2276 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002277 mbedtls_ssl_send_alert_message(
2278 ssl,
2279 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2281 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002282 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002283#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002285#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002286 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2288 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002289 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002290
Gilles Peskineeccd8882020-03-10 12:19:08 +01002291#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002293 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002294 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2297 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002299 size_t params_len = (size_t) (p - params);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002300 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002301 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002304
Jerry Yu693a47a2022-06-23 14:02:28 +08002305#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2306 peer_pk = &ssl->handshake->peer_pubkey;
2307#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002309 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2311 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002312 }
2313 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2314#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2315
Paul Bakker29e1f122013-04-16 13:07:56 +02002316 /*
2317 * Handle the digitally-signed structure
2318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2320 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2321 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2322 sig_alg, &pk_alg, &md_alg) != 0 &&
2323 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2324 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2325 MBEDTLS_SSL_DEBUG_MSG(1,
2326 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002327 mbedtls_ssl_send_alert_message(
2328 ssl,
2329 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2331 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002332 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002333 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2336 MBEDTLS_SSL_DEBUG_MSG(1,
2337 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002338 mbedtls_ssl_send_alert_message(
2339 ssl,
2340 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2342 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002343 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002344
2345 /*
2346 * Read signature
2347 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (p > end - 2) {
2350 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002351 mbedtls_ssl_send_alert_message(
2352 ssl,
2353 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2355 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002356 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002357 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
Paul Bakker1ef83d62012-04-11 12:09:53 +00002358 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (p != end - sig_len) {
2361 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002362 mbedtls_ssl_send_alert_message(
2363 ssl,
2364 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2366 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002370
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002371 /*
2372 * Compute the hash that has been signed
2373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (md_alg != MBEDTLS_MD_NONE) {
2375 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2376 params, params_len,
2377 md_alg);
2378 if (ret != 0) {
2379 return ret;
2380 }
2381 } else {
2382 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2383 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002384 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002387
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002388 /*
2389 * Verify signature
2390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2392 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002393 mbedtls_ssl_send_alert_message(
2394 ssl,
2395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2397 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002398 }
2399
Gilles Peskineeccd8882020-03-10 12:19:08 +01002400#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002402 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002404#endif
2405
Jerry Yu693a47a2022-06-23 14:02:28 +08002406#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002408 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2409 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002410 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002411 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 if (rsassa_pss_options.expected_salt_len == 0) {
2413 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2414 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2417 peer_pk,
2418 md_alg, hash, hashlen,
2419 p, sig_len);
2420 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002421#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 ret = mbedtls_pk_verify_restartable(peer_pk,
2423 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002426 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002427#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002429#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002431 mbedtls_ssl_send_alert_message(
2432 ssl,
2433 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2435 }
2436 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002437#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002439 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002441#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002443 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002444
2445#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2446 /* We don't need the peer's public key anymore. Free it,
2447 * so that more RAM is available for upcoming expensive
2448 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002450#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002452#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002454exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 ssl->state++;
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002460}
2461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002463MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002464static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002465{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002466 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002467 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2472 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002473 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002475 }
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2478 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002479}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002480#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002481MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002482static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002483{
Janos Follath865b3eb2019-12-16 11:46:15 +00002484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002485 unsigned char *buf;
2486 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002487 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002488 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002489 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002490 size_t sig_alg_len;
2491#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 unsigned char *sig_alg;
2493 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002494#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2499 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002500 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002502 }
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2505 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2506 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 }
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2510 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002511 mbedtls_ssl_send_alert_message(
2512 ssl,
2513 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2515 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002518 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002519 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2523 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002526 /* Current message is probably the ServerHelloDone */
2527 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002528 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002529 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002530
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002531 /*
2532 * struct {
2533 * ClientCertificateType certificate_types<1..2^8-1>;
2534 * SignatureAndHashAlgorithm
2535 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2536 * DistinguishedName certificate_authorities<0..2^16-1>;
2537 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002538 *
2539 * Since we only support a single certificate on clients, let's just
2540 * ignore all the information that's supposed to help us pick a
2541 * certificate.
2542 *
2543 * We could check that our certificate matches the request, and bail out
2544 * if it doesn't, but it's simpler to just send the certificate anyway,
2545 * and give the server the opportunity to decide if it should terminate
2546 * the connection when it doesn't like our certificate.
2547 *
2548 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2549 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002550 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002551 *
2552 * However, we still minimally parse the message to check it is at least
2553 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002554 */
Paul Bakker926af752012-11-23 13:38:07 +01002555 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002556
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002557 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2559 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2560 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2561 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2562 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002563 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002565 n = cert_type_len;
2566
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002567 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002568 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002569 * * the length of the signature algorithms field (if minor version of
2570 * SSL is 3),
2571 * * distinguished name length otherwise.
2572 * Both reach at most the index:
2573 * ...hdr_len + 2 + n,
2574 * therefore the buffer length at this point must be greater than that
2575 * regardless of the actual code path.
2576 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2578 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2579 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2580 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2581 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002582 }
2583
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002584 /* supported_signature_algorithms */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002585 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002586
2587 /*
2588 * The furthest access in buf is in the loop few lines below:
2589 * sig_alg[i + 1],
2590 * where:
2591 * sig_alg = buf + ...hdr_len + 3 + n,
2592 * max(i) = sig_alg_len - 1.
2593 * Therefore the furthest access is:
2594 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2595 * which reduces to:
2596 * buf[...hdr_len + 3 + n + sig_alg_len],
2597 * which is one less than we need the buf to be.
2598 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2600 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002601 mbedtls_ssl_send_alert_message(
2602 ssl,
2603 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2605 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002606 }
Paul Bakker926af752012-11-23 13:38:07 +01002607
Ronald Cron90915f22022-03-07 11:11:36 +01002608#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2610 for (size_t i = 0; i < sig_alg_len; i += 2) {
2611 MBEDTLS_SSL_DEBUG_MSG(3,
2612 ("Supported Signature Algorithm found: %02x %02x",
2613 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002614 }
2615#endif
2616
2617 n += 2 + sig_alg_len;
2618
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002619 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002620 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002621
2622 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2624 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2625 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2626 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2627 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002628 }
2629
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002630#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2632 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002633 unsigned char *p = dn + i + 2;
2634 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002635 size_t asn1_len;
2636 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 memset(&name, 0, sizeof(name));
2638 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2639 if (dni_len > dn_len - i - 2 ||
2640 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2641 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2642 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2643 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002644 mbedtls_ssl_send_alert_message(
2645 ssl,
2646 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2648 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002649 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 MBEDTLS_SSL_DEBUG_MSG(3,
2651 ("DN hint: %.*s",
2652 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2653 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002654 }
2655#endif
2656
Paul Bakker926af752012-11-23 13:38:07 +01002657exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002661}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002662#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002664MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002665static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002666{
Janos Follath865b3eb2019-12-16 11:46:15 +00002667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2672 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2673 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2677 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2678 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2682 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2683 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2684 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2685 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2686 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 }
2688
2689 ssl->state++;
2690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2693 mbedtls_ssl_recv_flight_completed(ssl);
2694 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002695#endif
2696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700}
2701
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002702MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002703static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002704{
Janos Follath865b3eb2019-12-16 11:46:15 +00002705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002706
2707 size_t header_len;
2708 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002709 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002710 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 /*
2717 * DHM key exchange -- send G^X mod P
2718 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002722 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2725 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2726 &ssl->out_msg[header_len], content_len,
2727 ssl->conf->f_rng, ssl->conf->p_rng);
2728 if (ret != 0) {
2729 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2730 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 }
2732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2734 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2737 ssl->handshake->premaster,
2738 MBEDTLS_PREMASTER_SIZE,
2739 &ssl->handshake->pmslen,
2740 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2741 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2742 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 }
2744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2746 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002748#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2749 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2750 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2751 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002753 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2754 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002756#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002757 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2758 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002759 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002760
2761 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2762
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002763 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002766
Hanno Becker4a63ed42019-01-08 11:39:35 +00002767 /*
2768 * Generate EC private key for ECDHE exchange.
2769 */
2770
Hanno Becker4a63ed42019-01-08 11:39:35 +00002771 /* The master secret is obtained from the shared ECDH secret by
2772 * applying the TLS 1.2 PRF with a specific salt and label. While
2773 * the PSA Crypto API encourages combining key agreement schemes
2774 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2775 * yet support the provisioning of salt + label to the KDF.
2776 * For the time being, we therefore need to split the computation
2777 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002778 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2780 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002781 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002782 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002783
2784 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002786 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 if (status != PSA_SUCCESS) {
2788 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2789 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002790
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002791 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002792 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002793 * but we just need to add a length byte before that. */
2794 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2795 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002797 size_t own_pubkey_len;
2798
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002799 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 own_pubkey, own_pubkey_max_len,
2801 &own_pubkey_len);
2802 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002803 psa_destroy_key(handshake->xxdh_psa_privkey);
2804 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002806 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002807
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002808 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2809 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002810
Hanno Becker4a63ed42019-01-08 11:39:35 +00002811 /* The ECDH secret is the premaster secret used for key derivation. */
2812
Janos Follathdf3b0892019-08-08 11:12:24 +01002813 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002815 handshake->xxdh_psa_privkey,
2816 handshake->xxdh_psa_peerkey,
2817 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 ssl->handshake->premaster,
2819 sizeof(ssl->handshake->premaster),
2820 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002821
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002822 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2823 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2826 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2827 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002828#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002829 /*
2830 * ECDH key exchange -- send client public value
2831 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002832 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002833
Gilles Peskineeccd8882020-03-10 12:19:08 +01002834#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 if (ssl->handshake->ecrs_enabled) {
2836 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002837 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002841 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002842#endif
2843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2845 &content_len,
2846 &ssl->out_msg[header_len], 1000,
2847 ssl->conf->f_rng, ssl->conf->p_rng);
2848 if (ret != 0) {
2849 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002850#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002852 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002854#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002856 }
2857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2859 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002860
Gilles Peskineeccd8882020-03-10 12:19:08 +01002861#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002863 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002864 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002865 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002866
2867ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002869 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002871#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2873 &ssl->handshake->pmslen,
2874 ssl->handshake->premaster,
2875 MBEDTLS_MPI_MAX_SIZE,
2876 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2877 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002878#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002880 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002882#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002884 }
2885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2887 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002888#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2891 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2892 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2893 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002894#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2895 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2898 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2899 psa_key_attributes_t key_attributes;
2900
2901 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2902
2903 /*
2904 * opaque psk_identity<0..2^16-1>;
2905 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002907 /* We don't offer PSK suites if we don't have a PSK,
2908 * and we check that the server's choice is among the
2909 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2911 }
Neil Armstrong868af822022-03-09 10:26:25 +01002912
Neil Armstrongfc834f22022-03-23 17:54:38 +01002913 /* uint16 to store content length */
2914 const size_t content_len_size = 2;
2915
Neil Armstrong868af822022-03-09 10:26:25 +01002916 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 if (header_len + content_len_size + ssl->conf->psk_identity_len
2919 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2920 MBEDTLS_SSL_DEBUG_MSG(1,
2921 ("psk identity too long or SSL buffer too short"));
2922 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002923 }
2924
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002925 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2928 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002929 header_len += content_len_size;
2930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 memcpy(p, ssl->conf->psk_identity,
2932 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002933 p += ssl->conf->psk_identity_len;
2934
Neil Armstrong868af822022-03-09 10:26:25 +01002935 header_len += ssl->conf->psk_identity_len;
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002938
2939 /*
2940 * Generate EC private key for ECDHE exchange.
2941 */
2942
2943 /* The master secret is obtained from the shared ECDH secret by
2944 * applying the TLS 1.2 PRF with a specific salt and label. While
2945 * the PSA Crypto API encourages combining key agreement schemes
2946 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2947 * yet support the provisioning of salt + label to the KDF.
2948 * For the time being, we therefore need to split the computation
2949 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2950 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2952 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002953 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002954 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002955
2956 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002958 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002960 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 }
Neil Armstrong868af822022-03-09 10:26:25 +01002962
2963 /* Export the public part of the ECDH private key from PSA.
2964 * The export format is an ECPoint structure as expected by TLS,
2965 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002966 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002967 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002969 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002970
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002971 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 own_pubkey, own_pubkey_max_len,
2973 &own_pubkey_len);
2974 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002975 psa_destroy_key(handshake->xxdh_psa_privkey);
2976 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002977 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002978 }
2979
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002980 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002981 content_len = own_pubkey_len + 1;
2982
Neil Armstrong25400452022-03-23 17:44:07 +01002983 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2984 * - a uint16 containing the length (in octets) of the ECDH computation
2985 * - the octet string produced by the ECDH computation
2986 * - a uint16 containing the length (in octets) of the PSK
2987 * - the PSK itself
2988 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002989 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 const unsigned char * const pms_end = pms +
2991 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002992 /* uint16 to store length (in octets) of the ECDH computation */
2993 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002994 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002995
Neil Armstrong25400452022-03-23 17:44:07 +01002996 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002998 handshake->xxdh_psa_privkey,
2999 handshake->xxdh_psa_peerkey,
3000 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 pms + zlen_size,
3002 pms_end - (pms + zlen_size),
3003 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003004
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02003005 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
3006 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003009 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003011 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 }
Neil Armstrong868af822022-03-09 10:26:25 +01003013
Neil Armstrong25400452022-03-23 17:44:07 +01003014 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003016 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003018#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3019 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003020#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003022 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003023 * opaque psk_identity<0..2^16-1>;
3024 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003026 /* We don't offer PSK suites if we don't have a PSK,
3027 * and we check that the server's choice is among the
3028 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003030 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003031
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003032 header_len = 4;
3033 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3036 MBEDTLS_SSL_DEBUG_MSG(1,
3037 ("psk identity too long or SSL buffer too short"));
3038 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003039 }
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3042 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 memcpy(ssl->out_msg + header_len,
3045 ssl->conf->psk_identity,
3046 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003047 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003051 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3056 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3057 &content_len, 2)) != 0) {
3058 return ret;
3059 }
3060 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003064 /*
3065 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 if (header_len + 2 + content_len >
3070 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3071 MBEDTLS_SSL_DEBUG_MSG(1,
3072 ("psk identity or DHM size too long or SSL buffer too short"));
3073 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003074 }
3075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3077 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003078
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3080 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3081 &ssl->out_msg[header_len], content_len,
3082 ssl->conf->f_rng, ssl->conf->p_rng);
3083 if (ret != 0) {
3084 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3085 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003086 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003087
3088#if defined(MBEDTLS_USE_PSA_CRYPTO)
3089 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003091 size_t pms_len;
3092
3093 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3095 pms + 2, pms_end - (pms + 2), &pms_len,
3096 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3097 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3098 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003099 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003101 pms += 2 + pms_len;
3102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003104#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003107#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3109 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003110 /*
3111 * ClientECDiffieHellmanPublic public;
3112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3114 &content_len,
3115 &ssl->out_msg[header_len],
3116 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3117 ssl->conf->f_rng, ssl->conf->p_rng);
3118 if (ret != 0) {
3119 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3120 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003121 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3124 MBEDTLS_DEBUG_ECDH_Q);
3125 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003126#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003127 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3129 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003130 }
3131
Neil Armstrong80f6f322022-05-03 17:56:38 +02003132#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003134 (mbedtls_key_exchange_type_t) ciphersuite_info->
3135 key_exchange)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 MBEDTLS_SSL_DEBUG_RET(1,
3137 "mbedtls_ssl_psk_derive_premaster", ret);
3138 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003139 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003140#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003142#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003145 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3147 &content_len, 0)) != 0) {
3148 return ret;
3149 }
3150 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003152#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003154 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003155
Neil Armstrongca7d5062022-05-31 14:43:23 +02003156#if defined(MBEDTLS_USE_PSA_CRYPTO)
3157 unsigned char *out_p = ssl->out_msg + header_len;
3158 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3159 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3161 out_p, end_p - out_p, &content_len,
3162 MBEDTLS_ECJPAKE_ROUND_TWO);
3163 if (ret != 0) {
3164 psa_destroy_key(ssl->handshake->psa_pake_password);
3165 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3166 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3167 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003168 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003169#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3171 ssl->out_msg + header_len,
3172 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3173 &content_len,
3174 ssl->conf->f_rng, ssl->conf->p_rng);
3175 if (ret != 0) {
3176 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3177 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003178 }
3179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3181 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3182 ssl->conf->f_rng, ssl->conf->p_rng);
3183 if (ret != 0) {
3184 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3185 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003186 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003187#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003189#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003190 {
3191 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3193 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003194 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003196 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3198 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003199
3200 ssl->state++;
3201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3203 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3204 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 }
3206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003210}
3211
Gilles Peskineeccd8882020-03-10 12:19:08 +01003212#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003213MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003214static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003215{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003216 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003217 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003219
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3223 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3224 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003225 }
3226
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3228 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003229 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003231 }
3232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3234 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003235}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003236#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003237MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003238static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003241 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003242 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003243 size_t n = 0, offset = 0;
3244 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003245 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003247 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003248 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003249#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003250 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003251#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003252 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 +02003253#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003254
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003256
Gilles Peskineeccd8882020-03-10 12:19:08 +01003257#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 if (ssl->handshake->ecrs_enabled &&
3259 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003260 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003261 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003262#endif
3263
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3265 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3266 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003267 }
3268
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3270 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003271 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003273 }
3274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 if (ssl->handshake->client_auth == 0 ||
3276 mbedtls_ssl_own_cert(ssl) == NULL) {
3277 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003278 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 }
3281
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 if (mbedtls_ssl_own_key(ssl) == NULL) {
3283 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3284 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 }
3286
3287 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003288 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003290#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003292 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003294
3295sign:
3296#endif
3297
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003298 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3299 if (0 != ret) {
3300 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3301 return ret;
3302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
Ronald Cron90915f22022-03-07 11:11:36 +01003304 /*
3305 * digitally-signed struct {
3306 * opaque handshake_messages[handshake_messages_length];
3307 * };
3308 *
3309 * Taking shortcut here. We assume that the server always allows the
3310 * PRF Hash function and has sent it in the allowed signature
3311 * algorithms list received in the Certificate Request message.
3312 *
3313 * Until we encounter a server that does not, we will take this
3314 * shortcut.
3315 *
3316 * Reason: Otherwise we should have running hashes for SHA512 and
3317 * SHA224 in order to satisfy 'weird' needs from the server
3318 * side.
3319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003321 md_alg = MBEDTLS_MD_SHA384;
3322 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003324 md_alg = MBEDTLS_MD_SHA256;
3325 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003326 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003328
3329 /* Info from md_alg will be used instead */
3330 hashlen = 0;
3331 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003332
Gilles Peskineeccd8882020-03-10 12:19:08 +01003333#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003335 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003337#endif
3338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3340 md_alg, hash_start, hashlen,
3341 ssl->out_msg + 6 + offset,
3342 out_buf_len - 6 - offset,
3343 &n,
3344 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3345 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003346#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003348 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003350#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003352 }
Paul Bakker926af752012-11-23 13:38:07 +01003353
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
Paul Bakker1ef83d62012-04-11 12:09:53 +00003356 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3358 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003359
3360 ssl->state++;
3361
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3363 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3364 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003365 }
3366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003370}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003371#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003374MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003375static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003376{
Janos Follath865b3eb2019-12-16 11:46:15 +00003377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003378 uint32_t lifetime;
3379 size_t ticket_len;
3380 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003381 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003382
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3386 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3387 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003388 }
3389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3391 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003392 mbedtls_ssl_send_alert_message(
3393 ssl,
3394 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3396 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003397 }
3398
3399 /*
3400 * struct {
3401 * uint32 ticket_lifetime_hint;
3402 * opaque ticket<0..2^16-1>;
3403 * } NewSessionTicket;
3404 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003405 * 0 . 3 ticket_lifetime_hint
3406 * 4 . 5 ticket_len (n)
3407 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003408 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3410 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3411 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3412 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3413 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3414 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003415 }
3416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003418
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003419 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003420
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003421 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3424 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3425 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3426 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3427 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003428 }
3429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003431
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003432 /* We're not waiting for a NewSessionTicket message any more */
3433 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003435
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003436 /*
3437 * Zero-length ticket means the server changed his mind and doesn't want
3438 * to send a ticket after all, so just forget it
3439 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 if (ticket_len == 0) {
3441 return 0;
3442 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003443
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003445 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003447 ssl->session->ticket = NULL;
3448 ssl->session->ticket_len = 0;
3449 }
3450
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003451 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003453 ssl->session_negotiate->ticket = NULL;
3454 ssl->session_negotiate->ticket_len = 0;
3455
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3457 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3458 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3459 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3460 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003461 }
3462
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003464
3465 ssl->session_negotiate->ticket = ticket;
3466 ssl->session_negotiate->ticket_len = ticket_len;
3467 ssl->session_negotiate->ticket_lifetime = lifetime;
3468
3469 /*
3470 * RFC 5077 section 3.4:
3471 * "If the client receives a session ticket from the server, then it
3472 * discards any Session ID that was sent in the ServerHello."
3473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003475 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003476
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003478
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003482
Paul Bakker5121ce52009-01-03 21:22:43 +00003483/*
Paul Bakker1961b702013-01-25 14:49:24 +01003484 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003486int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003487{
3488 int ret = 0;
3489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003491 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3494 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003495 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003496 }
3497#endif
3498
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 case MBEDTLS_SSL_HELLO_REQUEST:
3501 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 break;
3503
Gilles Peskine449bd832023-01-11 14:50:10 +01003504 /*
3505 * ==> ClientHello
3506 */
3507 case MBEDTLS_SSL_CLIENT_HELLO:
3508 ret = mbedtls_ssl_write_client_hello(ssl);
3509 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003510
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 /*
3512 * <== ServerHello
3513 * Certificate
3514 * ( ServerKeyExchange )
3515 * ( CertificateRequest )
3516 * ServerHelloDone
3517 */
3518 case MBEDTLS_SSL_SERVER_HELLO:
3519 ret = ssl_parse_server_hello(ssl);
3520 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003521
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3523 ret = mbedtls_ssl_parse_certificate(ssl);
3524 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003525
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3527 ret = ssl_parse_server_key_exchange(ssl);
3528 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3531 ret = ssl_parse_certificate_request(ssl);
3532 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003533
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3535 ret = ssl_parse_server_hello_done(ssl);
3536 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003537
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 /*
3539 * ==> ( Certificate/Alert )
3540 * ClientKeyExchange
3541 * ( CertificateVerify )
3542 * ChangeCipherSpec
3543 * Finished
3544 */
3545 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3546 ret = mbedtls_ssl_write_certificate(ssl);
3547 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003548
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3550 ret = ssl_write_client_key_exchange(ssl);
3551 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3554 ret = ssl_write_certificate_verify(ssl);
3555 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003556
Gilles Peskine449bd832023-01-11 14:50:10 +01003557 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3558 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3559 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 case MBEDTLS_SSL_CLIENT_FINISHED:
3562 ret = mbedtls_ssl_write_finished(ssl);
3563 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003564
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 /*
3566 * <== ( NewSessionTicket )
3567 * ChangeCipherSpec
3568 * Finished
3569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3572 ret = ssl_parse_new_session_ticket(ssl);
3573 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003574#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003575
Gilles Peskine449bd832023-01-11 14:50:10 +01003576 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3577 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3578 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003579
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 case MBEDTLS_SSL_SERVER_FINISHED:
3581 ret = mbedtls_ssl_parse_finished(ssl);
3582 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003583
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 case MBEDTLS_SSL_FLUSH_BUFFERS:
3585 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3586 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3587 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003588
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3590 mbedtls_ssl_handshake_wrapup(ssl);
3591 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003592
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 default:
3594 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3595 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003597
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003599}
Jerry Yuc5aef882021-12-23 20:15:02 +08003600
Jerry Yufb4b6472022-01-27 15:03:26 +08003601#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */