blob: 08549a8333ad836baf16cf6312ad500665911a51 [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;
Paul Bakker5121ce52009-01-03 21:22:43 +00001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (ssl->tls_version < ssl->conf->min_tls_version ||
1273 ssl->tls_version > ssl->conf->max_tls_version) {
1274 MBEDTLS_SSL_DEBUG_MSG(1,
1275 (
1276 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1277 (unsigned) ssl->conf->min_tls_version,
1278 (unsigned) ssl->tls_version,
1279 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1282 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001285 }
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1288 ((unsigned long) buf[2] << 24) |
1289 ((unsigned long) buf[3] << 16) |
1290 ((unsigned long) buf[4] << 8) |
1291 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001295 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (n > 32) {
1300 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1301 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1302 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1303 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001304 }
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001307 ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if ((ext_len > 0 && ext_len < 4) ||
1310 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1311 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001312 mbedtls_ssl_send_alert_message(
1313 ssl,
1314 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1316 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001317 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001319 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 } else {
1321 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1322 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1323 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1324 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001327 /* ciphersuite (used later) */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001328 i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35);
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001329
1330 /*
1331 * Read and check compression
1332 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001333 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1336 MBEDTLS_SSL_DEBUG_MSG(1,
1337 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001338 mbedtls_ssl_send_alert_message(
1339 ssl,
1340 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1342 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001343 }
1344
Paul Bakker380da532012-04-18 16:10:25 +00001345 /*
1346 * Initialize update checksum functions
1347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1349 if (ssl->handshake->ciphersuite_info == NULL) {
1350 MBEDTLS_SSL_DEBUG_MSG(1,
1351 ("ciphersuite info for %04x not found", (unsigned int) i));
1352 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1353 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1354 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001355 }
Paul Bakker380da532012-04-18 16:10:25 +00001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1360 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 /*
1363 * Check if the session can be resumed
1364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_SSL_RENEGOTIATION)
1367 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001368#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001369 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001370 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001373 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001376#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001377 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001378 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 memcpy(ssl->session_negotiate->id, buf + 35, n);
1380 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1385 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1388 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1389 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Andrzej Kurek03bac442018-04-25 05:06:07 -04001391 /*
1392 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001393 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 while (1) {
1396 if (ssl->conf->ciphersuite_list[i] == 0) {
1397 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001398 mbedtls_ssl_send_alert_message(
1399 ssl,
1400 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1402 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 }
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (ssl->conf->ciphersuite_list[i++] ==
1406 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
1410
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001411 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 ssl->session_negotiate->ciphersuite);
1413 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1414 ssl->tls_version) != 0) {
1415 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001416 mbedtls_ssl_send_alert_message(
1417 ssl,
1418 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1420 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001421 }
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 MBEDTLS_SSL_DEBUG_MSG(3,
1424 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001425
Gilles Peskineeccd8882020-03-10 12:19:08 +01001426#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1428 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001429 ssl->handshake->ecrs_enabled = 1;
1430 }
1431#endif
1432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1434 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001435 mbedtls_ssl_send_alert_message(
1436 ssl,
1437 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1439 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001442 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 MBEDTLS_SSL_DEBUG_MSG(2,
1445 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1446 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 while (ext_len) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001449 unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
1450 unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
Paul Bakker48916f92012-09-16 19:57:18 +00001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (ext_size + 4 > ext_len) {
1453 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001454 mbedtls_ssl_send_alert_message(
1455 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1457 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001458 }
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 switch (ext_id) {
1461 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1462 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001465#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1468 ext_size)) != 0) {
1469 return ret;
1470 }
Paul Bakker48916f92012-09-16 19:57:18 +00001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1476 MBEDTLS_SSL_DEBUG_MSG(3,
1477 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1480 ext + 4, ext_size)) != 0) {
1481 return ret;
1482 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001486
Hanno Beckera0e20d02019-05-15 14:03:01 +01001487#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 case MBEDTLS_TLS_EXT_CID:
1489 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if ((ret = ssl_parse_cid_ext(ssl,
1492 ext + 4,
1493 ext_size)) != 0) {
1494 return ret;
1495 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001498#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1502 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1505 ext + 4, ext_size)) != 0) {
1506 return ret;
1507 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1514 MBEDTLS_SSL_DEBUG_MSG(3,
1515 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if ((ret = ssl_parse_extended_ms_ext(ssl,
1518 ext + 4, ext_size)) != 0) {
1519 return ret;
1520 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1527 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if ((ret = ssl_parse_session_ticket_ext(ssl,
1530 ext + 4, ext_size)) != 0) {
1531 return ret;
1532 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001536
Valerio Setti7aeec542023-07-05 18:57:21 +02001537#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001538 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001539 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1541 MBEDTLS_SSL_DEBUG_MSG(3,
1542 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1545 ext + 4, ext_size)) != 0) {
1546 return ret;
1547 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001550#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001551 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001552 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001553
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001554#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1556 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1559 ext + 4, ext_size)) != 0) {
1560 return ret;
1561 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001564#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 case MBEDTLS_TLS_EXT_ALPN:
1568 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1571 return ret;
1572 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001576
Johan Pascalb62bb512015-12-03 21:56:45 +01001577#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 case MBEDTLS_TLS_EXT_USE_SRTP:
1579 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1582 return ret;
1583 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001586#endif /* MBEDTLS_SSL_DTLS_SRTP */
1587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 default:
1589 MBEDTLS_SSL_DEBUG_MSG(3,
1590 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001591 }
1592
1593 ext_len -= 4 + ext_size;
1594 ext += 4 + ext_size;
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (ext_len > 0 && ext_len < 4) {
1597 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1598 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001599 }
1600 }
1601
1602 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001603 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1604 * extensions. It sets the transform data for the resumed session which in
1605 * case of DTLS includes the server CID extracted from the CID extension.
1606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (ssl->handshake->resume) {
1608 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1609 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001610 mbedtls_ssl_send_alert_message(
1611 ssl,
1612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1614 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001615 }
1616 }
1617
Paul Bakker48916f92012-09-16 19:57:18 +00001618 /*
1619 * Renegotiation security checks
1620 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001622 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1624 MBEDTLS_SSL_DEBUG_MSG(1,
1625 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001626 handshake_failure = 1;
1627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 renegotiation_info_seen == 0) {
1632 MBEDTLS_SSL_DEBUG_MSG(1,
1633 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001634 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1636 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1637 ssl->conf->allow_legacy_renegotiation ==
1638 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1639 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001640 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1642 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1643 renegotiation_info_seen == 1) {
1644 MBEDTLS_SSL_DEBUG_MSG(1,
1645 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001646 handshake_failure = 1;
1647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001651 mbedtls_ssl_send_alert_message(
1652 ssl,
1653 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1655 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001661}
1662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1664 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001665MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001666static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1667 unsigned char **p,
1668 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001671 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001672
Paul Bakker29e1f122013-04-16 13:07:56 +02001673 /*
1674 * Ephemeral DH parameters:
1675 *
1676 * struct {
1677 * opaque dh_p<1..2^16-1>;
1678 * opaque dh_g<1..2^16-1>;
1679 * opaque dh_Ys<1..2^16-1>;
1680 * } ServerDHParams;
1681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1683 p, end)) != 0) {
1684 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1685 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001686 }
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1689 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1690 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1691 dhm_actual_bitlen,
1692 ssl->conf->dhm_min_bitlen));
1693 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001694 }
1695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1697 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1698 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1703 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001704
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001705#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001706#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1707 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1708 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001709MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001710static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1711 unsigned char **p,
1712 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001713{
1714 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001715 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001716 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001717 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001718 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001719
1720 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001721 * struct {
1722 * ECParameters curve_params;
1723 * ECPoint public;
1724 * } ServerECDHParams;
1725 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001726 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001727 * 2..3 NamedCurve
1728 * 4 ECPoint.len
1729 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001730 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if (end - *p < 4) {
1732 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1733 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001734
1735 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1737 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1738 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001739
1740 /* Next two bytes are the namedcurve value */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001741 tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
1742 *p += 2;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001743
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001744 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1746 MBEDTLS_SSL_DEBUG_MSG(2,
1747 ("bad server key exchange message (ECDHE curve): %u",
1748 (unsigned) tls_id));
1749 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001750 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001751
Valerio Setti40d9ca92023-01-04 16:08:04 +01001752 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001753 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1755 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001756 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001757 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001758 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001759
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001760 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001761 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if ((size_t) (end - *p) < ecpoint_len) {
1763 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1764 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001765
Gilles Peskinec29df532023-10-02 14:59:26 +02001766 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1768 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001769
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001770 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1771 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001772 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001775}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001776#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1777 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1778 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001779#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001780#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1781 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1782 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1783 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1784 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001785MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001786static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001787{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001788 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001789 mbedtls_ecp_group_id grp_id;
1790#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1791 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1792#else
1793 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1794#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1797 if (tls_id == 0) {
1798 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1799 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001800 }
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1803 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1806 return -1;
1807 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1810 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001813}
1814
Andrzej Kurek468c5062022-10-24 10:30:14 -04001815#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1816 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1817 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1818 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1819 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1820
1821#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1822 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1823 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001824MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001825static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1826 unsigned char **p,
1827 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001828{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001830
Paul Bakker29e1f122013-04-16 13:07:56 +02001831 /*
1832 * Ephemeral ECDH parameters:
1833 *
1834 * struct {
1835 * ECParameters curve_params;
1836 * ECPoint public;
1837 * } ServerECDHParams;
1838 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1840 (const unsigned char **) p, end)) != 0) {
1841 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001842#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001844 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001846#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001848 }
1849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 if (ssl_check_server_ecdh_params(ssl) != 0) {
1851 MBEDTLS_SSL_DEBUG_MSG(1,
1852 ("bad server key exchange message (ECDHE curve)"));
1853 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001854 }
1855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001857}
Gilles Peskine449bd832023-01-11 14:50:10 +01001858#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1859 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001860 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1861#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001862#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001863MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001864static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1865 unsigned char **p,
1866 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001869 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001870 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001871
1872 /*
1873 * PSK parameters:
1874 *
1875 * opaque psk_identity_hint<0..2^16-1>;
1876 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if (end - (*p) < 2) {
1878 MBEDTLS_SSL_DEBUG_MSG(1,
1879 ("bad server key exchange message (psk_identity_hint length)"));
1880 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001881 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001882 len = MBEDTLS_GET_UINT16_BE(*p, 0);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001883 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (end - (*p) < len) {
1886 MBEDTLS_SSL_DEBUG_MSG(1,
1887 ("bad server key exchange message (psk_identity_hint length)"));
1888 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001889 }
1890
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001891 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001892 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001893 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001894 * someone needs that feature.
1895 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001896 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001897 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001900}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001901#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1904 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001905/*
1906 * Generate a pre-master secret and encrypt it with the server's RSA key
1907 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001908MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001909static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1910 size_t offset, size_t *olen,
1911 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001912{
Janos Follath865b3eb2019-12-16 11:46:15 +00001913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001914 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001915 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1919 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1920 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001921 }
1922
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001923 /*
1924 * Generate (part of) the pre-master as
1925 * struct {
1926 * ProtocolVersion client_version;
1927 * opaque random[46];
1928 * } PreMasterSecret;
1929 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 mbedtls_ssl_write_version(p, ssl->conf->transport,
1931 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1934 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1935 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001936 }
1937
1938 ssl->handshake->pmslen = 48;
1939
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001940#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1941 peer_pk = &ssl->handshake->peer_pubkey;
1942#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001944 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1946 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001947 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001948 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1949#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001950
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001951 /*
1952 * Now write it out, encrypted
1953 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1955 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1956 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001957 }
1958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if ((ret = mbedtls_pk_encrypt(peer_pk,
1960 p, ssl->handshake->pmslen,
1961 ssl->out_msg + offset + len_bytes, olen,
1962 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1963 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1964 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1965 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001966 }
1967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if (len_bytes == 2) {
1969 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001970 *olen += 2;
1971 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001972
Hanno Beckerae553dd2019-02-08 14:06:00 +00001973#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1974 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001976#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
1980 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1983 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001984MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001985static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001986{
Janos Follath865b3eb2019-12-16 11:46:15 +00001987 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001989
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001990#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1991 peer_pk = &ssl->handshake->peer_pubkey;
1992#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001994 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1996 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001997 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001998 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1999#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002000
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002001 /* This is a public key, so it can't be opaque, so can_do() is a good
2002 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2004 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2005 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002006 }
2007
Valerio Setti97207782023-05-18 18:59:06 +02002008#if defined(MBEDTLS_ECP_C)
2009 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
2010#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002011
Przemek Stekielea4000f2022-03-16 09:49:33 +01002012#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002013 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02002014 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti97207782023-05-18 18:59:06 +02002015 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002016
Valerio Setti97207782023-05-18 18:59:06 +02002017 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2019 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002020 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002021
Valerio Setti97207782023-05-18 18:59:06 +02002022 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (tls_id == 0) {
2024 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002025 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002027 }
2028
Valerio Setti1e868cc2023-01-09 17:30:01 +01002029 /* If the above conversion to TLS ID was fine, then also this one will be,
2030 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02002031 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02002032 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002033
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002034 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002035
Przemek Stekielea4000f2022-03-16 09:49:33 +01002036 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002037#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002038 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2039 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02002040 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002041#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002042 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2044 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002045 ssl->handshake->xxdh_psa_peerkey,
Gilles Peskinec29df532023-10-02 14:59:26 +02002046 sizeof(ssl->handshake->xxdh_psa_peerkey));
Przemek Stekielea4000f2022-03-16 09:49:33 +01002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 if (ret != 0) {
2049 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2050 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002051 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002052 ssl->handshake->xxdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002053#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2054#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2056 MBEDTLS_ECDH_THEIRS)) != 0) {
2057 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2058 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002059 }
2060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (ssl_check_server_ecdh_params(ssl) != 0) {
2062 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2063 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002064 }
Valerio Setti97207782023-05-18 18:59:06 +02002065#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002066#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2067 /* We don't need the peer's public key anymore. Free it,
2068 * so that more RAM is available for upcoming expensive
2069 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002071#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2076 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002077
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002078MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002079static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002080{
Janos Follath865b3eb2019-12-16 11:46:15 +00002081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002082 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002083 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002084 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2090 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002094 ((void) p);
2095 ((void) end);
2096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2099 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2101 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2102 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2103 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002104 mbedtls_ssl_send_alert_message(
2105 ssl,
2106 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2108 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002109 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002112 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002114 }
2115 ((void) p);
2116 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2118 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002119
Gilles Peskineeccd8882020-03-10 12:19:08 +01002120#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (ssl->handshake->ecrs_enabled &&
2122 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002123 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002124 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002125#endif
2126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2128 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2129 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 }
2131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2133 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002134 mbedtls_ssl_send_alert_message(
2135 ssl,
2136 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2138 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 }
2140
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002141 /*
2142 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2143 * doesn't use a psk_identity_hint
2144 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2146 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2147 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002148 /* Current message is probably either
2149 * CertificateRequest or ServerHelloDone */
2150 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002151 goto exit;
2152 }
2153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 MBEDTLS_SSL_DEBUG_MSG(1,
2155 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002156 mbedtls_ssl_send_alert_message(
2157 ssl,
2158 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 }
2163
Gilles Peskineeccd8882020-03-10 12:19:08 +01002164#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002166 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002168
2169start_processing:
2170#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002172 end = ssl->in_msg + ssl->in_hslen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002173 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002174
Gilles Peskineeccd8882020-03-10 12:19:08 +01002175#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2178 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2180 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2181 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002182 mbedtls_ssl_send_alert_message(
2183 ssl,
2184 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2186 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002187 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002188 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002189#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2192 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2194 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002195 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2198 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2199#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2200 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2202 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2203 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2204 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002205 mbedtls_ssl_send_alert_message(
2206 ssl,
2207 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2209 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002210 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2213 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002214#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2215 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2220 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2221 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002222 mbedtls_ssl_send_alert_message(
2223 ssl,
2224 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2226 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002227 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2230 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2231 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002232#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002234#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002235 /*
2236 * The first 3 bytes are:
2237 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2238 * [1, 2] elliptic curve's TLS ID
2239 *
2240 * However since we only support secp256r1 for now, we check only
2241 * that TLS ID here
2242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002244 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (exp_tls_id == 0) {
2248 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002249 }
2250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2252 (read_tls_id != exp_tls_id)) {
2253 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002254 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002255
2256 p += 3;
2257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if ((ret = mbedtls_psa_ecjpake_read_round(
2259 &ssl->handshake->psa_pake_ctx, p, end - p,
2260 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2261 psa_destroy_key(ssl->handshake->psa_pake_password);
2262 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002265 mbedtls_ssl_send_alert_message(
2266 ssl,
2267 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2269 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002270 }
2271#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2273 p, end - p);
2274 if (ret != 0) {
2275 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002276 mbedtls_ssl_send_alert_message(
2277 ssl,
2278 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2280 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002281 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002282#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002284#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002285 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2287 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002288 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002289
Gilles Peskineeccd8882020-03-10 12:19:08 +01002290#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002292 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002293 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2296 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002298 size_t params_len = (size_t) (p - params);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002299 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002300 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002303
Jerry Yu693a47a2022-06-23 14:02:28 +08002304#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2305 peer_pk = &ssl->handshake->peer_pubkey;
2306#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002308 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2310 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002311 }
2312 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2313#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2314
Paul Bakker29e1f122013-04-16 13:07:56 +02002315 /*
2316 * Handle the digitally-signed structure
2317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2319 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2320 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2321 sig_alg, &pk_alg, &md_alg) != 0 &&
2322 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2323 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2324 MBEDTLS_SSL_DEBUG_MSG(1,
2325 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002326 mbedtls_ssl_send_alert_message(
2327 ssl,
2328 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2330 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002331 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002332 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2335 MBEDTLS_SSL_DEBUG_MSG(1,
2336 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002337 mbedtls_ssl_send_alert_message(
2338 ssl,
2339 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2341 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002342 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002343
2344 /*
2345 * Read signature
2346 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (p > end - 2) {
2349 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002350 mbedtls_ssl_send_alert_message(
2351 ssl,
2352 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2354 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002355 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002356 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
Paul Bakker1ef83d62012-04-11 12:09:53 +00002357 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (p != end - sig_len) {
2360 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002361 mbedtls_ssl_send_alert_message(
2362 ssl,
2363 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2365 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002369
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002370 /*
2371 * Compute the hash that has been signed
2372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (md_alg != MBEDTLS_MD_NONE) {
2374 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2375 params, params_len,
2376 md_alg);
2377 if (ret != 0) {
2378 return ret;
2379 }
2380 } else {
2381 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2382 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002383 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002386
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002387 /*
2388 * Verify signature
2389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2391 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002392 mbedtls_ssl_send_alert_message(
2393 ssl,
2394 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2396 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002397 }
2398
Gilles Peskineeccd8882020-03-10 12:19:08 +01002399#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002401 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002403#endif
2404
Jerry Yu693a47a2022-06-23 14:02:28 +08002405#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002407 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2408 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002409 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002410 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (rsassa_pss_options.expected_salt_len == 0) {
2412 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2413 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2416 peer_pk,
2417 md_alg, hash, hashlen,
2418 p, sig_len);
2419 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002420#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 ret = mbedtls_pk_verify_restartable(peer_pk,
2422 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002425 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002426#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002428#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002430 mbedtls_ssl_send_alert_message(
2431 ssl,
2432 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2434 }
2435 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002436#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002438 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002440#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002442 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002443
2444#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2445 /* We don't need the peer's public key anymore. Free it,
2446 * so that more RAM is available for upcoming expensive
2447 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002449#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002451#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002453exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 ssl->state++;
2455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002459}
2460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002463static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002464{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002465 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002466 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2471 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002472 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002474 }
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2477 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002478}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002479#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002480MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002481static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002482{
Janos Follath865b3eb2019-12-16 11:46:15 +00002483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002484 unsigned char *buf;
2485 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002486 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002487 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002488 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002489 size_t sig_alg_len;
2490#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 unsigned char *sig_alg;
2492 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002493#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2498 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002499 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002501 }
2502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2504 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2505 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
2507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2509 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002510 mbedtls_ssl_send_alert_message(
2511 ssl,
2512 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2514 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002515 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002517 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002518 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2522 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002525 /* Current message is probably the ServerHelloDone */
2526 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002527 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002528 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002529
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002530 /*
2531 * struct {
2532 * ClientCertificateType certificate_types<1..2^8-1>;
2533 * SignatureAndHashAlgorithm
2534 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2535 * DistinguishedName certificate_authorities<0..2^16-1>;
2536 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002537 *
2538 * Since we only support a single certificate on clients, let's just
2539 * ignore all the information that's supposed to help us pick a
2540 * certificate.
2541 *
2542 * We could check that our certificate matches the request, and bail out
2543 * if it doesn't, but it's simpler to just send the certificate anyway,
2544 * and give the server the opportunity to decide if it should terminate
2545 * the connection when it doesn't like our certificate.
2546 *
2547 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2548 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002549 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002550 *
2551 * However, we still minimally parse the message to check it is at least
2552 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002553 */
Paul Bakker926af752012-11-23 13:38:07 +01002554 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002555
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002556 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2558 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2559 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2560 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2561 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002562 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002564 n = cert_type_len;
2565
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002566 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002567 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002568 * * the length of the signature algorithms field (if minor version of
2569 * SSL is 3),
2570 * * distinguished name length otherwise.
2571 * Both reach at most the index:
2572 * ...hdr_len + 2 + n,
2573 * therefore the buffer length at this point must be greater than that
2574 * regardless of the actual code path.
2575 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2577 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2578 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2579 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2580 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002581 }
2582
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002583 /* supported_signature_algorithms */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002584 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002585
2586 /*
2587 * The furthest access in buf is in the loop few lines below:
2588 * sig_alg[i + 1],
2589 * where:
2590 * sig_alg = buf + ...hdr_len + 3 + n,
2591 * max(i) = sig_alg_len - 1.
2592 * Therefore the furthest access is:
2593 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2594 * which reduces to:
2595 * buf[...hdr_len + 3 + n + sig_alg_len],
2596 * which is one less than we need the buf to be.
2597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2599 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002600 mbedtls_ssl_send_alert_message(
2601 ssl,
2602 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2604 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002605 }
Paul Bakker926af752012-11-23 13:38:07 +01002606
Ronald Cron90915f22022-03-07 11:11:36 +01002607#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2609 for (size_t i = 0; i < sig_alg_len; i += 2) {
2610 MBEDTLS_SSL_DEBUG_MSG(3,
2611 ("Supported Signature Algorithm found: %02x %02x",
2612 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002613 }
2614#endif
2615
2616 n += 2 + sig_alg_len;
2617
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002618 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002619 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002620
2621 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2623 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2624 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2625 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2626 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002627 }
2628
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002629#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2631 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002632 unsigned char *p = dn + i + 2;
2633 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002634 size_t asn1_len;
2635 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 memset(&name, 0, sizeof(name));
2637 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2638 if (dni_len > dn_len - i - 2 ||
2639 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2640 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2641 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2642 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002643 mbedtls_ssl_send_alert_message(
2644 ssl,
2645 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2647 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002648 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 MBEDTLS_SSL_DEBUG_MSG(3,
2650 ("DN hint: %.*s",
2651 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2652 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002653 }
2654#endif
2655
Paul Bakker926af752012-11-23 13:38:07 +01002656exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002661#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002663MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002664static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002665{
Janos Follath865b3eb2019-12-16 11:46:15 +00002666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2671 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2672 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2676 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2677 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2681 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2682 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2683 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2684 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2685 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 }
2687
2688 ssl->state++;
2689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2692 mbedtls_ssl_recv_flight_completed(ssl);
2693 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002694#endif
2695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699}
2700
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002701MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002702static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002703{
Janos Follath865b3eb2019-12-16 11:46:15 +00002704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002705
2706 size_t header_len;
2707 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002708 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002709 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 /*
2716 * DHM key exchange -- send G^X mod P
2717 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002721 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2724 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2725 &ssl->out_msg[header_len], content_len,
2726 ssl->conf->f_rng, ssl->conf->p_rng);
2727 if (ret != 0) {
2728 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2729 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 }
2731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2733 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2736 ssl->handshake->premaster,
2737 MBEDTLS_PREMASTER_SIZE,
2738 &ssl->handshake->pmslen,
2739 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2740 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2741 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 }
2743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2745 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002747#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2748 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2749 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2750 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002752 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2753 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002755#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002756 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2757 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002758 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002759
2760 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2761
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002762 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002765
Hanno Becker4a63ed42019-01-08 11:39:35 +00002766 /*
2767 * Generate EC private key for ECDHE exchange.
2768 */
2769
Hanno Becker4a63ed42019-01-08 11:39:35 +00002770 /* The master secret is obtained from the shared ECDH secret by
2771 * applying the TLS 1.2 PRF with a specific salt and label. While
2772 * the PSA Crypto API encourages combining key agreement schemes
2773 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2774 * yet support the provisioning of salt + label to the KDF.
2775 * For the time being, we therefore need to split the computation
2776 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002777 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2779 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002780 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002781 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002782
2783 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002785 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 if (status != PSA_SUCCESS) {
2787 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2788 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002789
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002790 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002791 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002792 * but we just need to add a length byte before that. */
2793 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2794 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002796 size_t own_pubkey_len;
2797
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002798 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 own_pubkey, own_pubkey_max_len,
2800 &own_pubkey_len);
2801 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002802 psa_destroy_key(handshake->xxdh_psa_privkey);
2803 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002805 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002806
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002807 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2808 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002809
Hanno Becker4a63ed42019-01-08 11:39:35 +00002810 /* The ECDH secret is the premaster secret used for key derivation. */
2811
Janos Follathdf3b0892019-08-08 11:12:24 +01002812 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002814 handshake->xxdh_psa_privkey,
2815 handshake->xxdh_psa_peerkey,
2816 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 ssl->handshake->premaster,
2818 sizeof(ssl->handshake->premaster),
2819 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002820
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002821 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2822 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2825 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2826 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002827#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002828 /*
2829 * ECDH key exchange -- send client public value
2830 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002831 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002832
Gilles Peskineeccd8882020-03-10 12:19:08 +01002833#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 if (ssl->handshake->ecrs_enabled) {
2835 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002836 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002840 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002841#endif
2842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2844 &content_len,
2845 &ssl->out_msg[header_len], 1000,
2846 ssl->conf->f_rng, ssl->conf->p_rng);
2847 if (ret != 0) {
2848 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002849#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002851 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002853#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002855 }
2856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2858 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002859
Gilles Peskineeccd8882020-03-10 12:19:08 +01002860#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002862 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002863 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002864 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002865
2866ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002868 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002870#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2872 &ssl->handshake->pmslen,
2873 ssl->handshake->premaster,
2874 MBEDTLS_MPI_MAX_SIZE,
2875 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2876 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002877#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002879 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002881#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002883 }
2884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2886 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002887#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2890 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2891 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2892 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002893#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2894 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2897 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2898 psa_key_attributes_t key_attributes;
2899
2900 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2901
2902 /*
2903 * opaque psk_identity<0..2^16-1>;
2904 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002906 /* We don't offer PSK suites if we don't have a PSK,
2907 * and we check that the server's choice is among the
2908 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2910 }
Neil Armstrong868af822022-03-09 10:26:25 +01002911
Neil Armstrongfc834f22022-03-23 17:54:38 +01002912 /* uint16 to store content length */
2913 const size_t content_len_size = 2;
2914
Neil Armstrong868af822022-03-09 10:26:25 +01002915 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 if (header_len + content_len_size + ssl->conf->psk_identity_len
2918 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2919 MBEDTLS_SSL_DEBUG_MSG(1,
2920 ("psk identity too long or SSL buffer too short"));
2921 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002922 }
2923
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002924 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2927 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002928 header_len += content_len_size;
2929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 memcpy(p, ssl->conf->psk_identity,
2931 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002932 p += ssl->conf->psk_identity_len;
2933
Neil Armstrong868af822022-03-09 10:26:25 +01002934 header_len += ssl->conf->psk_identity_len;
2935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002937
2938 /*
2939 * Generate EC private key for ECDHE exchange.
2940 */
2941
2942 /* The master secret is obtained from the shared ECDH secret by
2943 * applying the TLS 1.2 PRF with a specific salt and label. While
2944 * the PSA Crypto API encourages combining key agreement schemes
2945 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2946 * yet support the provisioning of salt + label to the KDF.
2947 * For the time being, we therefore need to split the computation
2948 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2949 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2951 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002952 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002953 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002954
2955 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002957 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002959 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 }
Neil Armstrong868af822022-03-09 10:26:25 +01002961
2962 /* Export the public part of the ECDH private key from PSA.
2963 * The export format is an ECPoint structure as expected by TLS,
2964 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002965 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002966 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002968 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002969
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002970 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 own_pubkey, own_pubkey_max_len,
2972 &own_pubkey_len);
2973 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002974 psa_destroy_key(handshake->xxdh_psa_privkey);
2975 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002976 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002977 }
2978
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002979 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002980 content_len = own_pubkey_len + 1;
2981
Neil Armstrong25400452022-03-23 17:44:07 +01002982 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2983 * - a uint16 containing the length (in octets) of the ECDH computation
2984 * - the octet string produced by the ECDH computation
2985 * - a uint16 containing the length (in octets) of the PSK
2986 * - the PSK itself
2987 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002988 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 const unsigned char * const pms_end = pms +
2990 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002991 /* uint16 to store length (in octets) of the ECDH computation */
2992 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002993 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002994
Neil Armstrong25400452022-03-23 17:44:07 +01002995 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002997 handshake->xxdh_psa_privkey,
2998 handshake->xxdh_psa_peerkey,
2999 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 pms + zlen_size,
3001 pms_end - (pms + zlen_size),
3002 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003003
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02003004 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
3005 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003008 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003010 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 }
Neil Armstrong868af822022-03-09 10:26:25 +01003012
Neil Armstrong25400452022-03-23 17:44:07 +01003013 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003015 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003017#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3018 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003019#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003021 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003022 * opaque psk_identity<0..2^16-1>;
3023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003025 /* We don't offer PSK suites if we don't have a PSK,
3026 * and we check that the server's choice is among the
3027 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003029 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003030
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003031 header_len = 4;
3032 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3035 MBEDTLS_SSL_DEBUG_MSG(1,
3036 ("psk identity too long or SSL buffer too short"));
3037 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003038 }
3039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3041 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 memcpy(ssl->out_msg + header_len,
3044 ssl->conf->psk_identity,
3045 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003046 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003050 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003052#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3055 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3056 &content_len, 2)) != 0) {
3057 return ret;
3058 }
3059 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003060#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003063 /*
3064 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3065 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 if (header_len + 2 + content_len >
3069 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3070 MBEDTLS_SSL_DEBUG_MSG(1,
3071 ("psk identity or DHM size too long or SSL buffer too short"));
3072 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003073 }
3074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3076 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3079 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3080 &ssl->out_msg[header_len], content_len,
3081 ssl->conf->f_rng, ssl->conf->p_rng);
3082 if (ret != 0) {
3083 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3084 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003085 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003086
3087#if defined(MBEDTLS_USE_PSA_CRYPTO)
3088 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003090 size_t pms_len;
3091
3092 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3094 pms + 2, pms_end - (pms + 2), &pms_len,
3095 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3096 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3097 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003098 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003100 pms += 2 + pms_len;
3101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003103#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003106#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3108 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003109 /*
3110 * ClientECDiffieHellmanPublic public;
3111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3113 &content_len,
3114 &ssl->out_msg[header_len],
3115 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3116 ssl->conf->f_rng, ssl->conf->p_rng);
3117 if (ret != 0) {
3118 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3119 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003120 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3123 MBEDTLS_DEBUG_ECDH_Q);
3124 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003125#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003126 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3128 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003129 }
3130
Neil Armstrong80f6f322022-05-03 17:56:38 +02003131#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003133 (mbedtls_key_exchange_type_t) ciphersuite_info->
3134 key_exchange)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 MBEDTLS_SSL_DEBUG_RET(1,
3136 "mbedtls_ssl_psk_derive_premaster", ret);
3137 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003138 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003139#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003141#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003144 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3146 &content_len, 0)) != 0) {
3147 return ret;
3148 }
3149 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003151#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003153 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003154
Neil Armstrongca7d5062022-05-31 14:43:23 +02003155#if defined(MBEDTLS_USE_PSA_CRYPTO)
3156 unsigned char *out_p = ssl->out_msg + header_len;
3157 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3158 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3160 out_p, end_p - out_p, &content_len,
3161 MBEDTLS_ECJPAKE_ROUND_TWO);
3162 if (ret != 0) {
3163 psa_destroy_key(ssl->handshake->psa_pake_password);
3164 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3165 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3166 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003167 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003168#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3170 ssl->out_msg + header_len,
3171 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3172 &content_len,
3173 ssl->conf->f_rng, ssl->conf->p_rng);
3174 if (ret != 0) {
3175 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3176 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003177 }
3178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3180 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3181 ssl->conf->f_rng, ssl->conf->p_rng);
3182 if (ret != 0) {
3183 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3184 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003185 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003188#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003189 {
3190 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3192 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003193 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003194
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003195 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3197 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
3199 ssl->state++;
3200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3202 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3203 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 }
3205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003209}
3210
Gilles Peskineeccd8882020-03-10 12:19:08 +01003211#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003212MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003213static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003214{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003215 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003216 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3222 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3223 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003224 }
3225
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3227 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003228 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003230 }
3231
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3233 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003234}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003235#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003236MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003237static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003240 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003241 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003242 size_t n = 0, offset = 0;
3243 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003244 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003246 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003247 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003248#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003249 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003250#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003251 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 +02003252#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003255
Gilles Peskineeccd8882020-03-10 12:19:08 +01003256#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 if (ssl->handshake->ecrs_enabled &&
3258 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003259 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003260 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003261#endif
3262
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3264 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3265 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003266 }
3267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3269 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003270 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003272 }
3273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (ssl->handshake->client_auth == 0 ||
3275 mbedtls_ssl_own_cert(ssl) == NULL) {
3276 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003277 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 }
3280
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 if (mbedtls_ssl_own_key(ssl) == NULL) {
3282 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3283 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 }
3285
3286 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003287 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003289#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003291 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003293
3294sign:
3295#endif
3296
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003297 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3298 if (0 != ret) {
3299 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3300 return ret;
3301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
Ronald Cron90915f22022-03-07 11:11:36 +01003303 /*
3304 * digitally-signed struct {
3305 * opaque handshake_messages[handshake_messages_length];
3306 * };
3307 *
3308 * Taking shortcut here. We assume that the server always allows the
3309 * PRF Hash function and has sent it in the allowed signature
3310 * algorithms list received in the Certificate Request message.
3311 *
3312 * Until we encounter a server that does not, we will take this
3313 * shortcut.
3314 *
3315 * Reason: Otherwise we should have running hashes for SHA512 and
3316 * SHA224 in order to satisfy 'weird' needs from the server
3317 * side.
3318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003320 md_alg = MBEDTLS_MD_SHA384;
3321 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003323 md_alg = MBEDTLS_MD_SHA256;
3324 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003325 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003327
3328 /* Info from md_alg will be used instead */
3329 hashlen = 0;
3330 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003331
Gilles Peskineeccd8882020-03-10 12:19:08 +01003332#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003334 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003336#endif
3337
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3339 md_alg, hash_start, hashlen,
3340 ssl->out_msg + 6 + offset,
3341 out_buf_len - 6 - offset,
3342 &n,
3343 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3344 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003345#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003347 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003349#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003351 }
Paul Bakker926af752012-11-23 13:38:07 +01003352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003354
Paul Bakker1ef83d62012-04-11 12:09:53 +00003355 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3357 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003358
3359 ssl->state++;
3360
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3362 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3363 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 }
3365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003369}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003370#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003373MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003374static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003375{
Janos Follath865b3eb2019-12-16 11:46:15 +00003376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003377 uint32_t lifetime;
3378 size_t ticket_len;
3379 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003380 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3385 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3386 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003387 }
3388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3390 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003391 mbedtls_ssl_send_alert_message(
3392 ssl,
3393 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3395 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003396 }
3397
3398 /*
3399 * struct {
3400 * uint32 ticket_lifetime_hint;
3401 * opaque ticket<0..2^16-1>;
3402 * } NewSessionTicket;
3403 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003404 * 0 . 3 ticket_lifetime_hint
3405 * 4 . 5 ticket_len (n)
3406 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003407 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3409 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3410 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3411 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3412 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3413 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003414 }
3415
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003417
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003418 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003419
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003420 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3423 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3424 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3425 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3426 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003427 }
3428
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003430
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003431 /* We're not waiting for a NewSessionTicket message any more */
3432 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003434
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003435 /*
3436 * Zero-length ticket means the server changed his mind and doesn't want
3437 * to send a ticket after all, so just forget it
3438 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 if (ticket_len == 0) {
3440 return 0;
3441 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003444 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003446 ssl->session->ticket = NULL;
3447 ssl->session->ticket_len = 0;
3448 }
3449
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003450 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003452 ssl->session_negotiate->ticket = NULL;
3453 ssl->session_negotiate->ticket_len = 0;
3454
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3456 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3457 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3458 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3459 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003460 }
3461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003463
3464 ssl->session_negotiate->ticket = ticket;
3465 ssl->session_negotiate->ticket_len = ticket_len;
3466 ssl->session_negotiate->ticket_lifetime = lifetime;
3467
3468 /*
3469 * RFC 5077 section 3.4:
3470 * "If the client receives a session ticket from the server, then it
3471 * discards any Session ID that was sent in the ServerHello."
3472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003474 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003475
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003477
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003481
Paul Bakker5121ce52009-01-03 21:22:43 +00003482/*
Paul Bakker1961b702013-01-25 14:49:24 +01003483 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003485int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003486{
3487 int ret = 0;
3488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003490 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3493 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003494 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003495 }
3496#endif
3497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 case MBEDTLS_SSL_HELLO_REQUEST:
3500 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003501 break;
3502
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 /*
3504 * ==> ClientHello
3505 */
3506 case MBEDTLS_SSL_CLIENT_HELLO:
3507 ret = mbedtls_ssl_write_client_hello(ssl);
3508 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 /*
3511 * <== ServerHello
3512 * Certificate
3513 * ( ServerKeyExchange )
3514 * ( CertificateRequest )
3515 * ServerHelloDone
3516 */
3517 case MBEDTLS_SSL_SERVER_HELLO:
3518 ret = ssl_parse_server_hello(ssl);
3519 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3522 ret = mbedtls_ssl_parse_certificate(ssl);
3523 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3526 ret = ssl_parse_server_key_exchange(ssl);
3527 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3530 ret = ssl_parse_certificate_request(ssl);
3531 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003532
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3534 ret = ssl_parse_server_hello_done(ssl);
3535 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003536
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 /*
3538 * ==> ( Certificate/Alert )
3539 * ClientKeyExchange
3540 * ( CertificateVerify )
3541 * ChangeCipherSpec
3542 * Finished
3543 */
3544 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3545 ret = mbedtls_ssl_write_certificate(ssl);
3546 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3549 ret = ssl_write_client_key_exchange(ssl);
3550 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003551
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3553 ret = ssl_write_certificate_verify(ssl);
3554 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003555
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3557 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3558 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003559
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 case MBEDTLS_SSL_CLIENT_FINISHED:
3561 ret = mbedtls_ssl_write_finished(ssl);
3562 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003563
Gilles Peskine449bd832023-01-11 14:50:10 +01003564 /*
3565 * <== ( NewSessionTicket )
3566 * ChangeCipherSpec
3567 * Finished
3568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3571 ret = ssl_parse_new_session_ticket(ssl);
3572 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003573#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003574
Gilles Peskine449bd832023-01-11 14:50:10 +01003575 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3576 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3577 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003578
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 case MBEDTLS_SSL_SERVER_FINISHED:
3580 ret = mbedtls_ssl_parse_finished(ssl);
3581 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003582
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 case MBEDTLS_SSL_FLUSH_BUFFERS:
3584 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3585 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3586 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3589 mbedtls_ssl_handshake_wrapup(ssl);
3590 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003591
Gilles Peskine449bd832023-01-11 14:50:10 +01003592 default:
3593 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3594 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003596
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003598}
Jerry Yuc5aef882021-12-23 20:15:02 +08003599
Jerry Yufb4b6472022-01-27 15:03:26 +08003600#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */