blob: 78297e89e1357f9beae5cb1ab8516934fccaf9f4 [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
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 list_len = (buf[0] << 8) | buf[1];
945 if (list_len != len - 2) {
946 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
947 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
948 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200949 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200950
951 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (name_len != list_len - 1) {
953 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
954 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
955 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200956 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200957
958 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
960 if (name_len == strlen(*p) &&
961 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200962 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200964 }
965 }
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
968 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
969 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
970 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200973
Johan Pascalb62bb512015-12-03 21:56:45 +0100974#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200975MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
977 const unsigned char *buf,
978 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100979{
Johan Pascal43f94902020-09-22 12:25:52 +0200980 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200981 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100982 uint16_t server_protection_profile_value = 0;
983
984 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
986 (ssl->conf->dtls_srtp_profile_list == NULL) ||
987 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
988 return 0;
989 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100990
Ron Eldora9788042018-12-05 11:04:31 +0200991 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100992 * uint8 SRTPProtectionProfile[2];
993 *
994 * struct {
995 * SRTPProtectionProfiles SRTPProtectionProfiles;
996 * opaque srtp_mki<0..255>;
997 * } UseSRTPData;
998
999 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1000 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001003 mki_len = ssl->dtls_srtp_info.mki_len;
1004 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001005
Ron Eldoref72faf2018-07-12 11:54:20 +03001006 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001007 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1008 * + protection profile (2 bytes)
1009 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001010 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if ((len < 5) || (len != (buf[4] + 5u))) {
1013 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1014 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001015
1016 /*
1017 * get the server protection profile
1018 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001019
1020 /*
1021 * protection profile length must be 0x0002 as we must have only
1022 * one protection profile in server Hello
1023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if ((buf[0] != 0) || (buf[1] != 2)) {
1025 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1026 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001029 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 server_protection_profile_value);
1031 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1032 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1033 mbedtls_ssl_get_srtp_profile_as_string(
1034 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001035 }
1036
Johan Pascal43f94902020-09-22 12:25:52 +02001037 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001038
Johan Pascalb62bb512015-12-03 21:56:45 +01001039 /*
1040 * Check we have the server profile in our list
1041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1043 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001044 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001046 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001048 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001049 }
1050 }
1051
Ron Eldor591f1622018-01-22 12:30:04 +02001052 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1054 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1055 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1056 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001057 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001058
1059 /* If server does not use mki in its reply, make sure the client won't keep
1060 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001062 ssl->dtls_srtp_info.mki_len = 0;
1063 }
1064
Ron Eldoref72faf2018-07-12 11:54:20 +03001065 /*
1066 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001067 * If the client detects a nonzero-length MKI in the server's response
1068 * that is different than the one the client offered, then the client
1069 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (len > 5 && (buf[4] != mki_len ||
1072 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1073 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1074 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1075 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001077#if defined(MBEDTLS_DEBUG_C)
1078 if (len > 5) {
1079 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1080 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001081 }
1082#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001084}
1085#endif /* MBEDTLS_SSL_DTLS_SRTP */
1086
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001087/*
1088 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001092static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001093{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001094 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001096 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001097
1098#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1099 uint8_t cookie_len;
1100#else
1101 uint16_t cookie_len;
1102#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001105
Gilles Peskineb64bf062019-09-27 14:02:44 +02001106 /* Check that there is enough room for:
1107 * - 2 bytes of version
1108 * - 1 byte of cookie_len
1109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1111 MBEDTLS_SSL_DEBUG_MSG(1,
1112 ("incoming HelloVerifyRequest message is too short"));
1113 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1114 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1115 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001116 }
1117
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001118 /*
1119 * struct {
1120 * ProtocolVersion server_version;
1121 * opaque cookie<0..2^8-1>;
1122 * } HelloVerifyRequest;
1123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1125 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001126 p += 2;
1127
TRodziewicz2d8800e2021-05-13 19:14:19 +02001128 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001129 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1130 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1131 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1134 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1137 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001140 }
1141
1142 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1144 MBEDTLS_SSL_DEBUG_MSG(1,
1145 ("cookie length does not match incoming message size"));
1146 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1147 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1148 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001149 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1155 if (ssl->handshake->cookie == NULL) {
1156 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1157 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001158 }
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001161 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001162
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001163 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001165 ret = mbedtls_ssl_reset_checksum(ssl);
1166 if (0 != ret) {
1167 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1168 return ret;
1169 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001179MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001180static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001181{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001182 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001183 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001184 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001185 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001186 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001188 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001189#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001190 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001196 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1198 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
1200
Hanno Becker79594fd2019-05-08 09:38:41 +01001201 buf = ssl->in_msg;
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001206 ssl->renego_records_seen++;
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (ssl->conf->renego_max_records >= 0 &&
1209 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1210 MBEDTLS_SSL_DEBUG_MSG(1,
1211 ("renegotiation requested, but not honored by server"));
1212 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001213 }
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 MBEDTLS_SSL_DEBUG_MSG(1,
1216 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001217
1218 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001224 mbedtls_ssl_send_alert_message(
1225 ssl,
1226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1228 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 }
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1233 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1234 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1235 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1236 return ssl_parse_hello_verify_request(ssl);
1237 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001238 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001240 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001241 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001242 }
1243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1247 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1248 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1249 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1250 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1251 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 }
1253
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001254 /*
1255 * 0 . 1 server_version
1256 * 2 . 33 random (maybe including 4 bytes of Unix time)
1257 * 34 . 34 session_id length = n
1258 * 35 . 34+n session_id
1259 * 35+n . 36+n cipher_suite
1260 * 37+n . 37+n compression_method
1261 *
1262 * 38+n . 39+n extensions length (optional)
1263 * 40+n . .. extensions
1264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
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) {
1307 ext_len = ((buf[38 + n] << 8)
1308 | (buf[39 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if ((ext_len > 0 && ext_len < 4) ||
1311 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1312 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001313 mbedtls_ssl_send_alert_message(
1314 ssl,
1315 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1317 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001318 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001320 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 } else {
1322 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1323 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1324 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1325 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001328 /* ciphersuite (used later) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 i = (buf[35 + n] << 8) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001330
1331 /*
1332 * Read and check compression
1333 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001334 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1337 MBEDTLS_SSL_DEBUG_MSG(1,
1338 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001339 mbedtls_ssl_send_alert_message(
1340 ssl,
1341 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1343 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001344 }
1345
Paul Bakker380da532012-04-18 16:10:25 +00001346 /*
1347 * Initialize update checksum functions
1348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1350 if (ssl->handshake->ciphersuite_info == NULL) {
1351 MBEDTLS_SSL_DEBUG_MSG(1,
1352 ("ciphersuite info for %04x not found", (unsigned int) i));
1353 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1354 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1355 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001356 }
Paul Bakker380da532012-04-18 16:10:25 +00001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1361 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
1363 /*
1364 * Check if the session can be resumed
1365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_SSL_RENEGOTIATION)
1368 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001369#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001370 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001371 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001374 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001377#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001378 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001379 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 memcpy(ssl->session_negotiate->id, buf + 35, n);
1381 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 }
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1386 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1389 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1390 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Andrzej Kurek03bac442018-04-25 05:06:07 -04001392 /*
1393 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001394 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 while (1) {
1397 if (ssl->conf->ciphersuite_list[i] == 0) {
1398 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001399 mbedtls_ssl_send_alert_message(
1400 ssl,
1401 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1403 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 }
1405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (ssl->conf->ciphersuite_list[i++] ==
1407 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 }
1411
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001412 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 ssl->session_negotiate->ciphersuite);
1414 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1415 ssl->tls_version) != 0) {
1416 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001417 mbedtls_ssl_send_alert_message(
1418 ssl,
1419 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1421 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001422 }
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 MBEDTLS_SSL_DEBUG_MSG(3,
1425 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001426
Gilles Peskineeccd8882020-03-10 12:19:08 +01001427#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1429 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001430 ssl->handshake->ecrs_enabled = 1;
1431 }
1432#endif
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1435 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001436 mbedtls_ssl_send_alert_message(
1437 ssl,
1438 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1440 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 }
1442
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001443 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 MBEDTLS_SSL_DEBUG_MSG(2,
1446 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1447 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 while (ext_len) {
1450 unsigned int ext_id = ((ext[0] << 8)
1451 | (ext[1]));
1452 unsigned int ext_size = ((ext[2] << 8)
1453 | (ext[3]));
Paul Bakker48916f92012-09-16 19:57:18 +00001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (ext_size + 4 > ext_len) {
1456 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001457 mbedtls_ssl_send_alert_message(
1458 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1460 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001461 }
1462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 switch (ext_id) {
1464 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1465 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001468#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1471 ext_size)) != 0) {
1472 return ret;
1473 }
Paul Bakker48916f92012-09-16 19:57:18 +00001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1479 MBEDTLS_SSL_DEBUG_MSG(3,
1480 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1483 ext + 4, ext_size)) != 0) {
1484 return ret;
1485 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001489
Hanno Beckera0e20d02019-05-15 14:03:01 +01001490#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 case MBEDTLS_TLS_EXT_CID:
1492 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if ((ret = ssl_parse_cid_ext(ssl,
1495 ext + 4,
1496 ext_size)) != 0) {
1497 return ret;
1498 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001501#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1505 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1508 ext + 4, ext_size)) != 0) {
1509 return ret;
1510 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1517 MBEDTLS_SSL_DEBUG_MSG(3,
1518 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if ((ret = ssl_parse_extended_ms_ext(ssl,
1521 ext + 4, ext_size)) != 0) {
1522 return ret;
1523 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1530 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if ((ret = ssl_parse_session_ticket_ext(ssl,
1533 ext + 4, ext_size)) != 0) {
1534 return ret;
1535 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001539
Valerio Setti7aeec542023-07-05 18:57:21 +02001540#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001541 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001542 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1544 MBEDTLS_SSL_DEBUG_MSG(3,
1545 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1548 ext + 4, ext_size)) != 0) {
1549 return ret;
1550 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001553#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001554 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001555 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001556
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001557#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1559 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1562 ext + 4, ext_size)) != 0) {
1563 return ret;
1564 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001567#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 case MBEDTLS_TLS_EXT_ALPN:
1571 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1574 return ret;
1575 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001579
Johan Pascalb62bb512015-12-03 21:56:45 +01001580#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 case MBEDTLS_TLS_EXT_USE_SRTP:
1582 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1585 return ret;
1586 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001589#endif /* MBEDTLS_SSL_DTLS_SRTP */
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 default:
1592 MBEDTLS_SSL_DEBUG_MSG(3,
1593 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001594 }
1595
1596 ext_len -= 4 + ext_size;
1597 ext += 4 + ext_size;
1598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if (ext_len > 0 && ext_len < 4) {
1600 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1601 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001602 }
1603 }
1604
1605 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001606 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1607 * extensions. It sets the transform data for the resumed session which in
1608 * case of DTLS includes the server CID extracted from the CID extension.
1609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (ssl->handshake->resume) {
1611 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1612 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001613 mbedtls_ssl_send_alert_message(
1614 ssl,
1615 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1617 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001618 }
1619 }
1620
Paul Bakker48916f92012-09-16 19:57:18 +00001621 /*
1622 * Renegotiation security checks
1623 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001625 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1627 MBEDTLS_SSL_DEBUG_MSG(1,
1628 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001629 handshake_failure = 1;
1630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 renegotiation_info_seen == 0) {
1635 MBEDTLS_SSL_DEBUG_MSG(1,
1636 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001637 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1639 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1640 ssl->conf->allow_legacy_renegotiation ==
1641 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1642 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001643 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1645 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1646 renegotiation_info_seen == 1) {
1647 MBEDTLS_SSL_DEBUG_MSG(1,
1648 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001649 handshake_failure = 1;
1650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001654 mbedtls_ssl_send_alert_message(
1655 ssl,
1656 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1658 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001659 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001664}
1665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1667 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001668MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001669static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1670 unsigned char **p,
1671 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001672{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001674 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001675
Paul Bakker29e1f122013-04-16 13:07:56 +02001676 /*
1677 * Ephemeral DH parameters:
1678 *
1679 * struct {
1680 * opaque dh_p<1..2^16-1>;
1681 * opaque dh_g<1..2^16-1>;
1682 * opaque dh_Ys<1..2^16-1>;
1683 * } ServerDHParams;
1684 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1686 p, end)) != 0) {
1687 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1688 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001689 }
1690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1692 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1693 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1694 dhm_actual_bitlen,
1695 ssl->conf->dhm_min_bitlen));
1696 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001697 }
1698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1700 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1701 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1706 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001707
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001708#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001709#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1710 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1711 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001712MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001713static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1714 unsigned char **p,
1715 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001716{
1717 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001718 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001719 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001720 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001721 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001722
1723 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001724 * struct {
1725 * ECParameters curve_params;
1726 * ECPoint public;
1727 * } ServerECDHParams;
1728 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001729 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001730 * 2..3 NamedCurve
1731 * 4 ECPoint.len
1732 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (end - *p < 4) {
1735 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1736 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001737
1738 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1740 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1741 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001742
1743 /* Next two bytes are the namedcurve value */
1744 tls_id = *(*p)++;
1745 tls_id <<= 8;
1746 tls_id |= *(*p)++;
1747
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001748 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1750 MBEDTLS_SSL_DEBUG_MSG(2,
1751 ("bad server key exchange message (ECDHE curve): %u",
1752 (unsigned) tls_id));
1753 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001754 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001755
Valerio Setti40d9ca92023-01-04 16:08:04 +01001756 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001757 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1759 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001760 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001761 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001762 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001763
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001764 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001765 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 if ((size_t) (end - *p) < ecpoint_len) {
1767 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1768 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001769
Gilles Peskinec29df532023-10-02 14:59:26 +02001770 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1772 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001773
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001774 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1775 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001776 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001779}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001780#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1781 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1782 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001783#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001784#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1785 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1786 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1787 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1788 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001789MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001790static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001791{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001792 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001793 mbedtls_ecp_group_id grp_id;
1794#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1795 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1796#else
1797 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1798#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1801 if (tls_id == 0) {
1802 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1803 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001804 }
1805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1807 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1810 return -1;
1811 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1814 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001817}
1818
Andrzej Kurek468c5062022-10-24 10:30:14 -04001819#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1820 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1821 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1822 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1823 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1824
1825#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1826 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1827 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001828MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001829static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1830 unsigned char **p,
1831 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001834
Paul Bakker29e1f122013-04-16 13:07:56 +02001835 /*
1836 * Ephemeral ECDH parameters:
1837 *
1838 * struct {
1839 * ECParameters curve_params;
1840 * ECPoint public;
1841 * } ServerECDHParams;
1842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1844 (const unsigned char **) p, end)) != 0) {
1845 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001846#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001848 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001850#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001852 }
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (ssl_check_server_ecdh_params(ssl) != 0) {
1855 MBEDTLS_SSL_DEBUG_MSG(1,
1856 ("bad server key exchange message (ECDHE curve)"));
1857 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001858 }
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001861}
Gilles Peskine449bd832023-01-11 14:50:10 +01001862#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1863 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001864 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1865#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001866#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001867MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001868static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1869 unsigned char **p,
1870 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001871{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001873 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001874 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001875
1876 /*
1877 * PSK parameters:
1878 *
1879 * opaque psk_identity_hint<0..2^16-1>;
1880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 if (end - (*p) < 2) {
1882 MBEDTLS_SSL_DEBUG_MSG(1,
1883 ("bad server key exchange message (psk_identity_hint length)"));
1884 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001885 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001886 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001887 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if (end - (*p) < len) {
1890 MBEDTLS_SSL_DEBUG_MSG(1,
1891 ("bad server key exchange message (psk_identity_hint length)"));
1892 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001893 }
1894
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001895 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001896 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001897 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001898 * someone needs that feature.
1899 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001900 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001901 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001904}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001905#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1908 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001909/*
1910 * Generate a pre-master secret and encrypt it with the server's RSA key
1911 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001912MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001913static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1914 size_t offset, size_t *olen,
1915 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001916{
Janos Follath865b3eb2019-12-16 11:46:15 +00001917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001918 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001919 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1923 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1924 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001925 }
1926
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001927 /*
1928 * Generate (part of) the pre-master as
1929 * struct {
1930 * ProtocolVersion client_version;
1931 * opaque random[46];
1932 * } PreMasterSecret;
1933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 mbedtls_ssl_write_version(p, ssl->conf->transport,
1935 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1938 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1939 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001940 }
1941
1942 ssl->handshake->pmslen = 48;
1943
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001944#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1945 peer_pk = &ssl->handshake->peer_pubkey;
1946#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001948 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1950 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001951 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001952 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1953#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001954
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001955 /*
1956 * Now write it out, encrypted
1957 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1959 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1960 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001961 }
1962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 if ((ret = mbedtls_pk_encrypt(peer_pk,
1964 p, ssl->handshake->pmslen,
1965 ssl->out_msg + offset + len_bytes, olen,
1966 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1967 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1968 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1969 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001970 }
1971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 if (len_bytes == 2) {
1973 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001974 *olen += 2;
1975 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001976
Hanno Beckerae553dd2019-02-08 14:06:00 +00001977#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1978 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001980#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001982}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
1984 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1987 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001988MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001989static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001990{
Janos Follath865b3eb2019-12-16 11:46:15 +00001991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001993
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001994#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1995 peer_pk = &ssl->handshake->peer_pubkey;
1996#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001998 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2000 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002001 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002002 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2003#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002004
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002005 /* This is a public key, so it can't be opaque, so can_do() is a good
2006 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2008 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2009 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002010 }
2011
Valerio Setti97207782023-05-18 18:59:06 +02002012#if defined(MBEDTLS_ECP_C)
2013 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
2014#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002015
Przemek Stekielea4000f2022-03-16 09:49:33 +01002016#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002017 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02002018 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti97207782023-05-18 18:59:06 +02002019 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002020
Valerio Setti97207782023-05-18 18:59:06 +02002021 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2023 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002024 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002025
Valerio Setti97207782023-05-18 18:59:06 +02002026 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (tls_id == 0) {
2028 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002029 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002031 }
2032
Valerio Setti1e868cc2023-01-09 17:30:01 +01002033 /* If the above conversion to TLS ID was fine, then also this one will be,
2034 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02002035 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02002036 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002037
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002038 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002039
Przemek Stekielea4000f2022-03-16 09:49:33 +01002040 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002041#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002042 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2043 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02002044 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002045#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002046 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2048 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002049 ssl->handshake->xxdh_psa_peerkey,
Gilles Peskinec29df532023-10-02 14:59:26 +02002050 sizeof(ssl->handshake->xxdh_psa_peerkey));
Przemek Stekielea4000f2022-03-16 09:49:33 +01002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (ret != 0) {
2053 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2054 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002055 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002056 ssl->handshake->xxdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002057#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2058#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2060 MBEDTLS_ECDH_THEIRS)) != 0) {
2061 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2062 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002063 }
2064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (ssl_check_server_ecdh_params(ssl) != 0) {
2066 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2067 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002068 }
Valerio Setti97207782023-05-18 18:59:06 +02002069#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002070#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2071 /* We don't need the peer's public key anymore. Free it,
2072 * so that more RAM is available for upcoming expensive
2073 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002075#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2080 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002081
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002082MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002083static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002084{
Janos Follath865b3eb2019-12-16 11:46:15 +00002085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002086 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002087 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002088 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2094 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002098 ((void) p);
2099 ((void) end);
2100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2103 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2105 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2106 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2107 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002108 mbedtls_ssl_send_alert_message(
2109 ssl,
2110 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2112 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002113 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002116 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002118 }
2119 ((void) p);
2120 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2122 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002123
Gilles Peskineeccd8882020-03-10 12:19:08 +01002124#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (ssl->handshake->ecrs_enabled &&
2126 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002127 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002128 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002129#endif
2130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2132 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2133 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 }
2135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2137 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002138 mbedtls_ssl_send_alert_message(
2139 ssl,
2140 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2142 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 }
2144
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002145 /*
2146 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2147 * doesn't use a psk_identity_hint
2148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2150 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2151 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002152 /* Current message is probably either
2153 * CertificateRequest or ServerHelloDone */
2154 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002155 goto exit;
2156 }
2157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 MBEDTLS_SSL_DEBUG_MSG(1,
2159 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002160 mbedtls_ssl_send_alert_message(
2161 ssl,
2162 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 }
2167
Gilles Peskineeccd8882020-03-10 12:19:08 +01002168#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002170 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002172
2173start_processing:
2174#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002176 end = ssl->in_msg + ssl->in_hslen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002177 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002178
Gilles Peskineeccd8882020-03-10 12:19:08 +01002179#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2182 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2184 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2185 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002186 mbedtls_ssl_send_alert_message(
2187 ssl,
2188 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2190 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002191 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002192 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002193#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2196 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2198 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002199 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2202 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2203#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2204 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2206 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2207 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2208 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002209 mbedtls_ssl_send_alert_message(
2210 ssl,
2211 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2213 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002214 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2217 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002218#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2219 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2224 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2225 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002226 mbedtls_ssl_send_alert_message(
2227 ssl,
2228 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2230 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002231 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2234 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2235 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002236#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002238#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002239 /*
2240 * The first 3 bytes are:
2241 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2242 * [1, 2] elliptic curve's TLS ID
2243 *
2244 * However since we only support secp256r1 for now, we check only
2245 * that TLS ID here
2246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002248 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if (exp_tls_id == 0) {
2252 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002253 }
2254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2256 (read_tls_id != exp_tls_id)) {
2257 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002258 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002259
2260 p += 3;
2261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if ((ret = mbedtls_psa_ecjpake_read_round(
2263 &ssl->handshake->psa_pake_ctx, p, end - p,
2264 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2265 psa_destroy_key(ssl->handshake->psa_pake_password);
2266 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002269 mbedtls_ssl_send_alert_message(
2270 ssl,
2271 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2273 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002274 }
2275#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2277 p, end - p);
2278 if (ret != 0) {
2279 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002280 mbedtls_ssl_send_alert_message(
2281 ssl,
2282 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2284 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002285 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002286#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002288#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002289 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2291 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002292 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002293
Gilles Peskineeccd8882020-03-10 12:19:08 +01002294#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002296 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002297 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2300 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002302 size_t params_len = (size_t) (p - params);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002303 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002304 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002307
Jerry Yu693a47a2022-06-23 14:02:28 +08002308#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2309 peer_pk = &ssl->handshake->peer_pubkey;
2310#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002312 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2314 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002315 }
2316 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2317#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2318
Paul Bakker29e1f122013-04-16 13:07:56 +02002319 /*
2320 * Handle the digitally-signed structure
2321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2323 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2324 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2325 sig_alg, &pk_alg, &md_alg) != 0 &&
2326 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2327 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2328 MBEDTLS_SSL_DEBUG_MSG(1,
2329 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002330 mbedtls_ssl_send_alert_message(
2331 ssl,
2332 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2334 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002335 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002336 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2339 MBEDTLS_SSL_DEBUG_MSG(1,
2340 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002341 mbedtls_ssl_send_alert_message(
2342 ssl,
2343 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2345 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002346 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002347
2348 /*
2349 * Read signature
2350 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 if (p > end - 2) {
2353 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002354 mbedtls_ssl_send_alert_message(
2355 ssl,
2356 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2358 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002359 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 sig_len = (p[0] << 8) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002361 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 if (p != end - sig_len) {
2364 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002365 mbedtls_ssl_send_alert_message(
2366 ssl,
2367 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2369 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002370 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002373
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002374 /*
2375 * Compute the hash that has been signed
2376 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (md_alg != MBEDTLS_MD_NONE) {
2378 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2379 params, params_len,
2380 md_alg);
2381 if (ret != 0) {
2382 return ret;
2383 }
2384 } else {
2385 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2386 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002387 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002390
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002391 /*
2392 * Verify signature
2393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2395 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002396 mbedtls_ssl_send_alert_message(
2397 ssl,
2398 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2400 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002401 }
2402
Gilles Peskineeccd8882020-03-10 12:19:08 +01002403#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002405 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002407#endif
2408
Jerry Yu693a47a2022-06-23 14:02:28 +08002409#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002411 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2412 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002413 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002414 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (rsassa_pss_options.expected_salt_len == 0) {
2416 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2417 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2420 peer_pk,
2421 md_alg, hash, hashlen,
2422 p, sig_len);
2423 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002424#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 ret = mbedtls_pk_verify_restartable(peer_pk,
2426 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002429 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002430#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002432#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002434 mbedtls_ssl_send_alert_message(
2435 ssl,
2436 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2438 }
2439 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002440#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002442 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002444#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002446 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002447
2448#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2449 /* We don't need the peer's public key anymore. Free it,
2450 * so that more RAM is available for upcoming expensive
2451 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002453#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002455#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002457exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 ssl->state++;
2459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463}
2464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002466MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002467static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002468{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002469 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002470 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2475 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002476 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002478 }
2479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2481 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002482}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002483#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002484MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002485static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002486{
Janos Follath865b3eb2019-12-16 11:46:15 +00002487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002488 unsigned char *buf;
2489 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002490 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002491 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002492 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002493 size_t sig_alg_len;
2494#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 unsigned char *sig_alg;
2496 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2502 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002503 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002505 }
2506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2508 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2509 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 }
2511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2513 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002514 mbedtls_ssl_send_alert_message(
2515 ssl,
2516 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2518 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002519 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002521 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002522 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2526 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002529 /* Current message is probably the ServerHelloDone */
2530 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002531 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002532 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002533
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002534 /*
2535 * struct {
2536 * ClientCertificateType certificate_types<1..2^8-1>;
2537 * SignatureAndHashAlgorithm
2538 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2539 * DistinguishedName certificate_authorities<0..2^16-1>;
2540 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002541 *
2542 * Since we only support a single certificate on clients, let's just
2543 * ignore all the information that's supposed to help us pick a
2544 * certificate.
2545 *
2546 * We could check that our certificate matches the request, and bail out
2547 * if it doesn't, but it's simpler to just send the certificate anyway,
2548 * and give the server the opportunity to decide if it should terminate
2549 * the connection when it doesn't like our certificate.
2550 *
2551 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2552 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002553 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002554 *
2555 * However, we still minimally parse the message to check it is at least
2556 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002557 */
Paul Bakker926af752012-11-23 13:38:07 +01002558 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002559
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002560 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2562 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2563 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2564 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2565 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002566 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002568 n = cert_type_len;
2569
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002570 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002571 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002572 * * the length of the signature algorithms field (if minor version of
2573 * SSL is 3),
2574 * * distinguished name length otherwise.
2575 * Both reach at most the index:
2576 * ...hdr_len + 2 + n,
2577 * therefore the buffer length at this point must be greater than that
2578 * regardless of the actual code path.
2579 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2581 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2582 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2583 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2584 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002585 }
2586
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002587 /* supported_signature_algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 sig_alg_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2589 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Ronald Cron90915f22022-03-07 11:11:36 +01002590
2591 /*
2592 * The furthest access in buf is in the loop few lines below:
2593 * sig_alg[i + 1],
2594 * where:
2595 * sig_alg = buf + ...hdr_len + 3 + n,
2596 * max(i) = sig_alg_len - 1.
2597 * Therefore the furthest access is:
2598 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2599 * which reduces to:
2600 * buf[...hdr_len + 3 + n + sig_alg_len],
2601 * which is one less than we need the buf to be.
2602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2604 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002605 mbedtls_ssl_send_alert_message(
2606 ssl,
2607 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2609 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002610 }
Paul Bakker926af752012-11-23 13:38:07 +01002611
Ronald Cron90915f22022-03-07 11:11:36 +01002612#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2614 for (size_t i = 0; i < sig_alg_len; i += 2) {
2615 MBEDTLS_SSL_DEBUG_MSG(3,
2616 ("Supported Signature Algorithm found: %02x %02x",
2617 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002618 }
2619#endif
2620
2621 n += 2 + sig_alg_len;
2622
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002623 /* certificate_authorities */
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 dn_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2625 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Paul Bakker926af752012-11-23 13:38:07 +01002626
2627 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2629 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2630 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2631 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2632 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002633 }
2634
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002635#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2637 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002638 unsigned char *p = dn + i + 2;
2639 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002640 size_t asn1_len;
2641 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 memset(&name, 0, sizeof(name));
2643 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2644 if (dni_len > dn_len - i - 2 ||
2645 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2646 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2647 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2648 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002649 mbedtls_ssl_send_alert_message(
2650 ssl,
2651 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2653 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002654 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 MBEDTLS_SSL_DEBUG_MSG(3,
2656 ("DN hint: %.*s",
2657 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2658 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002659 }
2660#endif
2661
Paul Bakker926af752012-11-23 13:38:07 +01002662exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002666}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002667#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002669MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002670static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002671{
Janos Follath865b3eb2019-12-16 11:46:15 +00002672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2677 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2678 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2682 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2683 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2687 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2688 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2689 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2690 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2691 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 }
2693
2694 ssl->state++;
2695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2698 mbedtls_ssl_recv_flight_completed(ssl);
2699 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002700#endif
2701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002705}
2706
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002707MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002708static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002709{
Janos Follath865b3eb2019-12-16 11:46:15 +00002710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002711
2712 size_t header_len;
2713 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002714 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002715 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 /*
2722 * DHM key exchange -- send G^X mod P
2723 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002727 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2730 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2731 &ssl->out_msg[header_len], content_len,
2732 ssl->conf->f_rng, ssl->conf->p_rng);
2733 if (ret != 0) {
2734 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2735 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 }
2737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2739 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2742 ssl->handshake->premaster,
2743 MBEDTLS_PREMASTER_SIZE,
2744 &ssl->handshake->pmslen,
2745 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2746 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2747 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 }
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2751 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002753#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2754 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2755 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2756 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002758 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2759 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002761#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002762 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2763 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002764 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002765
2766 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2767
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002768 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002771
Hanno Becker4a63ed42019-01-08 11:39:35 +00002772 /*
2773 * Generate EC private key for ECDHE exchange.
2774 */
2775
Hanno Becker4a63ed42019-01-08 11:39:35 +00002776 /* The master secret is obtained from the shared ECDH secret by
2777 * applying the TLS 1.2 PRF with a specific salt and label. While
2778 * the PSA Crypto API encourages combining key agreement schemes
2779 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2780 * yet support the provisioning of salt + label to the KDF.
2781 * For the time being, we therefore need to split the computation
2782 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002783 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2785 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002786 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002787 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002788
2789 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002791 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if (status != PSA_SUCCESS) {
2793 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2794 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002795
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002796 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002797 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002798 * but we just need to add a length byte before that. */
2799 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2800 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002802 size_t own_pubkey_len;
2803
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002804 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 own_pubkey, own_pubkey_max_len,
2806 &own_pubkey_len);
2807 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002808 psa_destroy_key(handshake->xxdh_psa_privkey);
2809 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002811 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002812
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002813 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2814 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002815
Hanno Becker4a63ed42019-01-08 11:39:35 +00002816 /* The ECDH secret is the premaster secret used for key derivation. */
2817
Janos Follathdf3b0892019-08-08 11:12:24 +01002818 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002820 handshake->xxdh_psa_privkey,
2821 handshake->xxdh_psa_peerkey,
2822 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 ssl->handshake->premaster,
2824 sizeof(ssl->handshake->premaster),
2825 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002826
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002827 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2828 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2831 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2832 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002833#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002834 /*
2835 * ECDH key exchange -- send client public value
2836 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002837 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002838
Gilles Peskineeccd8882020-03-10 12:19:08 +01002839#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 if (ssl->handshake->ecrs_enabled) {
2841 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002842 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002846 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002847#endif
2848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2850 &content_len,
2851 &ssl->out_msg[header_len], 1000,
2852 ssl->conf->f_rng, ssl->conf->p_rng);
2853 if (ret != 0) {
2854 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002855#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002857 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002859#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002861 }
2862
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2864 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002865
Gilles Peskineeccd8882020-03-10 12:19:08 +01002866#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002868 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002869 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002870 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002871
2872ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002874 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002876#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2878 &ssl->handshake->pmslen,
2879 ssl->handshake->premaster,
2880 MBEDTLS_MPI_MAX_SIZE,
2881 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2882 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002883#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002885 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002887#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002889 }
2890
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2892 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002893#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2896 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2897 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2898 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002899#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2900 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2903 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2904 psa_key_attributes_t key_attributes;
2905
2906 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2907
2908 /*
2909 * opaque psk_identity<0..2^16-1>;
2910 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002912 /* We don't offer PSK suites if we don't have a PSK,
2913 * and we check that the server's choice is among the
2914 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2916 }
Neil Armstrong868af822022-03-09 10:26:25 +01002917
Neil Armstrongfc834f22022-03-23 17:54:38 +01002918 /* uint16 to store content length */
2919 const size_t content_len_size = 2;
2920
Neil Armstrong868af822022-03-09 10:26:25 +01002921 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 if (header_len + content_len_size + ssl->conf->psk_identity_len
2924 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2925 MBEDTLS_SSL_DEBUG_MSG(1,
2926 ("psk identity too long or SSL buffer too short"));
2927 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002928 }
2929
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002930 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2933 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002934 header_len += content_len_size;
2935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 memcpy(p, ssl->conf->psk_identity,
2937 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002938 p += ssl->conf->psk_identity_len;
2939
Neil Armstrong868af822022-03-09 10:26:25 +01002940 header_len += ssl->conf->psk_identity_len;
2941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002943
2944 /*
2945 * Generate EC private key for ECDHE exchange.
2946 */
2947
2948 /* The master secret is obtained from the shared ECDH secret by
2949 * applying the TLS 1.2 PRF with a specific salt and label. While
2950 * the PSA Crypto API encourages combining key agreement schemes
2951 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2952 * yet support the provisioning of salt + label to the KDF.
2953 * For the time being, we therefore need to split the computation
2954 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2955 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2957 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002958 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002959 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002960
2961 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002963 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002965 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 }
Neil Armstrong868af822022-03-09 10:26:25 +01002967
2968 /* Export the public part of the ECDH private key from PSA.
2969 * The export format is an ECPoint structure as expected by TLS,
2970 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002971 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002972 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002974 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002975
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002976 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 own_pubkey, own_pubkey_max_len,
2978 &own_pubkey_len);
2979 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002980 psa_destroy_key(handshake->xxdh_psa_privkey);
2981 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002982 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002983 }
2984
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002985 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002986 content_len = own_pubkey_len + 1;
2987
Neil Armstrong25400452022-03-23 17:44:07 +01002988 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2989 * - a uint16 containing the length (in octets) of the ECDH computation
2990 * - the octet string produced by the ECDH computation
2991 * - a uint16 containing the length (in octets) of the PSK
2992 * - the PSK itself
2993 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002994 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 const unsigned char * const pms_end = pms +
2996 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002997 /* uint16 to store length (in octets) of the ECDH computation */
2998 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002999 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003000
Neil Armstrong25400452022-03-23 17:44:07 +01003001 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02003003 handshake->xxdh_psa_privkey,
3004 handshake->xxdh_psa_peerkey,
3005 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 pms + zlen_size,
3007 pms_end - (pms + zlen_size),
3008 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003009
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02003010 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
3011 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003014 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003016 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 }
Neil Armstrong868af822022-03-09 10:26:25 +01003018
Neil Armstrong25400452022-03-23 17:44:07 +01003019 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003021 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003023#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3024 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003025#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003027 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003028 * opaque psk_identity<0..2^16-1>;
3029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003031 /* We don't offer PSK suites if we don't have a PSK,
3032 * and we check that the server's choice is among the
3033 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003035 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003036
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003037 header_len = 4;
3038 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3041 MBEDTLS_SSL_DEBUG_MSG(1,
3042 ("psk identity too long or SSL buffer too short"));
3043 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003044 }
3045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3047 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 memcpy(ssl->out_msg + header_len,
3050 ssl->conf->psk_identity,
3051 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003052 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003056 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3061 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3062 &content_len, 2)) != 0) {
3063 return ret;
3064 }
3065 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003069 /*
3070 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3071 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 if (header_len + 2 + content_len >
3075 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3076 MBEDTLS_SSL_DEBUG_MSG(1,
3077 ("psk identity or DHM size too long or SSL buffer too short"));
3078 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003079 }
3080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3082 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3085 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3086 &ssl->out_msg[header_len], content_len,
3087 ssl->conf->f_rng, ssl->conf->p_rng);
3088 if (ret != 0) {
3089 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3090 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003091 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003092
3093#if defined(MBEDTLS_USE_PSA_CRYPTO)
3094 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003096 size_t pms_len;
3097
3098 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3100 pms + 2, pms_end - (pms + 2), &pms_len,
3101 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3102 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3103 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003104 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003106 pms += 2 + pms_len;
3107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003109#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003112#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3114 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003115 /*
3116 * ClientECDiffieHellmanPublic public;
3117 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3119 &content_len,
3120 &ssl->out_msg[header_len],
3121 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3122 ssl->conf->f_rng, ssl->conf->p_rng);
3123 if (ret != 0) {
3124 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3125 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003126 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003127
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3129 MBEDTLS_DEBUG_ECDH_Q);
3130 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003131#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003132 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3134 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003135 }
3136
Neil Armstrong80f6f322022-05-03 17:56:38 +02003137#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003139 (mbedtls_key_exchange_type_t) ciphersuite_info->
3140 key_exchange)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 MBEDTLS_SSL_DEBUG_RET(1,
3142 "mbedtls_ssl_psk_derive_premaster", ret);
3143 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003144 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003145#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003147#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003150 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3152 &content_len, 0)) != 0) {
3153 return ret;
3154 }
3155 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003157#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003159 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003160
Neil Armstrongca7d5062022-05-31 14:43:23 +02003161#if defined(MBEDTLS_USE_PSA_CRYPTO)
3162 unsigned char *out_p = ssl->out_msg + header_len;
3163 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3164 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3166 out_p, end_p - out_p, &content_len,
3167 MBEDTLS_ECJPAKE_ROUND_TWO);
3168 if (ret != 0) {
3169 psa_destroy_key(ssl->handshake->psa_pake_password);
3170 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3171 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3172 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003173 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003174#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3176 ssl->out_msg + header_len,
3177 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3178 &content_len,
3179 ssl->conf->f_rng, ssl->conf->p_rng);
3180 if (ret != 0) {
3181 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3182 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003183 }
3184
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3186 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3187 ssl->conf->f_rng, ssl->conf->p_rng);
3188 if (ret != 0) {
3189 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3190 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003191 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003192#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003194#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003195 {
3196 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3198 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003201 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3203 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
3205 ssl->state++;
3206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3208 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3209 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 }
3211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215}
3216
Gilles Peskineeccd8882020-03-10 12:19:08 +01003217#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003218MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003219static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003220{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003221 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003222 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003226
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3228 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3229 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003230 }
3231
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3233 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003234 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003236 }
3237
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3239 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003240}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003241#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003242MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003243static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003246 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003247 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003248 size_t n = 0, offset = 0;
3249 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003250 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003252 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003253 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003254#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003255 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003256#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003257 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 +02003258#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003261
Gilles Peskineeccd8882020-03-10 12:19:08 +01003262#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 if (ssl->handshake->ecrs_enabled &&
3264 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003265 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003266 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003267#endif
3268
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3270 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3271 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003272 }
3273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3275 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003276 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003278 }
3279
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 if (ssl->handshake->client_auth == 0 ||
3281 mbedtls_ssl_own_cert(ssl) == NULL) {
3282 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003283 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 }
3286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 if (mbedtls_ssl_own_key(ssl) == NULL) {
3288 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3289 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 }
3291
3292 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003293 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003295#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003297 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003299
3300sign:
3301#endif
3302
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003303 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3304 if (0 != ret) {
3305 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3306 return ret;
3307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003308
Ronald Cron90915f22022-03-07 11:11:36 +01003309 /*
3310 * digitally-signed struct {
3311 * opaque handshake_messages[handshake_messages_length];
3312 * };
3313 *
3314 * Taking shortcut here. We assume that the server always allows the
3315 * PRF Hash function and has sent it in the allowed signature
3316 * algorithms list received in the Certificate Request message.
3317 *
3318 * Until we encounter a server that does not, we will take this
3319 * shortcut.
3320 *
3321 * Reason: Otherwise we should have running hashes for SHA512 and
3322 * SHA224 in order to satisfy 'weird' needs from the server
3323 * side.
3324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003326 md_alg = MBEDTLS_MD_SHA384;
3327 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003329 md_alg = MBEDTLS_MD_SHA256;
3330 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003331 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003333
3334 /* Info from md_alg will be used instead */
3335 hashlen = 0;
3336 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003337
Gilles Peskineeccd8882020-03-10 12:19:08 +01003338#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003340 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003342#endif
3343
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3345 md_alg, hash_start, hashlen,
3346 ssl->out_msg + 6 + offset,
3347 out_buf_len - 6 - offset,
3348 &n,
3349 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3350 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003351#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003353 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003355#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003357 }
Paul Bakker926af752012-11-23 13:38:07 +01003358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003360
Paul Bakker1ef83d62012-04-11 12:09:53 +00003361 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3363 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
3365 ssl->state++;
3366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3368 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3369 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 }
3371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003375}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003376#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003379MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003380static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003381{
Janos Follath865b3eb2019-12-16 11:46:15 +00003382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003383 uint32_t lifetime;
3384 size_t ticket_len;
3385 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003386 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3391 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3392 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003393 }
3394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3396 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003397 mbedtls_ssl_send_alert_message(
3398 ssl,
3399 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3401 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003402 }
3403
3404 /*
3405 * struct {
3406 * uint32 ticket_lifetime_hint;
3407 * opaque ticket<0..2^16-1>;
3408 * } NewSessionTicket;
3409 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003410 * 0 . 3 ticket_lifetime_hint
3411 * 4 . 5 ticket_len (n)
3412 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3415 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3416 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3417 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3418 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3419 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003420 }
3421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 lifetime = (((uint32_t) msg[0]) << 24) | (msg[1] << 16) |
3425 (msg[2] << 8) | (msg[3]);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 ticket_len = (msg[4] << 8) | (msg[5]);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003428
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3430 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3431 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3432 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3433 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003434 }
3435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003437
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003438 /* We're not waiting for a NewSessionTicket message any more */
3439 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003441
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003442 /*
3443 * Zero-length ticket means the server changed his mind and doesn't want
3444 * to send a ticket after all, so just forget it
3445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 if (ticket_len == 0) {
3447 return 0;
3448 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003451 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003453 ssl->session->ticket = NULL;
3454 ssl->session->ticket_len = 0;
3455 }
3456
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003457 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003459 ssl->session_negotiate->ticket = NULL;
3460 ssl->session_negotiate->ticket_len = 0;
3461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3463 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3464 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3465 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3466 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003467 }
3468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003470
3471 ssl->session_negotiate->ticket = ticket;
3472 ssl->session_negotiate->ticket_len = ticket_len;
3473 ssl->session_negotiate->ticket_lifetime = lifetime;
3474
3475 /*
3476 * RFC 5077 section 3.4:
3477 * "If the client receives a session ticket from the server, then it
3478 * discards any Session ID that was sent in the ServerHello."
3479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003481 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003482
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003484
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003488
Paul Bakker5121ce52009-01-03 21:22:43 +00003489/*
Paul Bakker1961b702013-01-25 14:49:24 +01003490 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003492int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003493{
3494 int ret = 0;
3495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003497 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3500 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003501 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003502 }
3503#endif
3504
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 case MBEDTLS_SSL_HELLO_REQUEST:
3507 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003508 break;
3509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 /*
3511 * ==> ClientHello
3512 */
3513 case MBEDTLS_SSL_CLIENT_HELLO:
3514 ret = mbedtls_ssl_write_client_hello(ssl);
3515 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 /*
3518 * <== ServerHello
3519 * Certificate
3520 * ( ServerKeyExchange )
3521 * ( CertificateRequest )
3522 * ServerHelloDone
3523 */
3524 case MBEDTLS_SSL_SERVER_HELLO:
3525 ret = ssl_parse_server_hello(ssl);
3526 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3529 ret = mbedtls_ssl_parse_certificate(ssl);
3530 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003531
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3533 ret = ssl_parse_server_key_exchange(ssl);
3534 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3537 ret = ssl_parse_certificate_request(ssl);
3538 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3541 ret = ssl_parse_server_hello_done(ssl);
3542 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 /*
3545 * ==> ( Certificate/Alert )
3546 * ClientKeyExchange
3547 * ( CertificateVerify )
3548 * ChangeCipherSpec
3549 * Finished
3550 */
3551 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3552 ret = mbedtls_ssl_write_certificate(ssl);
3553 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003554
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3556 ret = ssl_write_client_key_exchange(ssl);
3557 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003558
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3560 ret = ssl_write_certificate_verify(ssl);
3561 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003562
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3564 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3565 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003566
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 case MBEDTLS_SSL_CLIENT_FINISHED:
3568 ret = mbedtls_ssl_write_finished(ssl);
3569 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003570
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 /*
3572 * <== ( NewSessionTicket )
3573 * ChangeCipherSpec
3574 * Finished
3575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3578 ret = ssl_parse_new_session_ticket(ssl);
3579 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003580#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003581
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3583 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3584 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003585
Gilles Peskine449bd832023-01-11 14:50:10 +01003586 case MBEDTLS_SSL_SERVER_FINISHED:
3587 ret = mbedtls_ssl_parse_finished(ssl);
3588 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003589
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 case MBEDTLS_SSL_FLUSH_BUFFERS:
3591 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3592 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3593 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003594
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3596 mbedtls_ssl_handshake_wrapup(ssl);
3597 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003598
Gilles Peskine449bd832023-01-11 14:50:10 +01003599 default:
3600 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3601 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003603
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003605}
Jerry Yuc5aef882021-12-23 20:15:02 +08003606
Jerry Yufb4b6472022-01-27 15:03:26 +08003607#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */