blob: 80b60aeafc93908828e46a2f3f4922df0fd6f83c [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
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "ssl_misc.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"
Valerio Settib4f50762024-01-17 10:24:52 +010016#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000017#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020018#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010019
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020020#include "psa_util_internal.h"
Ronald Cron69a63422021-10-18 09:47:58 +020021#include "psa/crypto.h"
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040022#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Andrzej Kurek00644842023-05-30 05:45:00 -040023/* Define a local translating function to save code size by not using too many
24 * arguments in each translating place. */
25static int local_err_translation(psa_status_t status)
26{
27 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040028 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040029 psa_generic_status_to_mbedtls);
30}
31#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040032#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +000033
SimonBd5800b72016-04-26 07:43:27 +010034#include <string.h>
35
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020036#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010039#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050043#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020047MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010048static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
49 unsigned char *buf,
50 const unsigned char *end,
51 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010052{
53 unsigned char *p = buf;
54
55 *olen = 0;
56
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010057 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010058 * initial ClientHello, in which case also adding the renegotiation
59 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
61 return 0;
62 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 MBEDTLS_SSL_DEBUG_MSG(3,
65 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010068
Paul Bakkerd3edc862013-03-20 16:07:17 +010069 /*
70 * Secure renegotiation
71 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010073 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010074
75 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
77 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010080
81 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010084}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010086
Valerio Setti7aeec542023-07-05 18:57:21 +020087#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +020088 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010089 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010090
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010092static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
93 unsigned char *buf,
94 const unsigned char *end,
95 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010096{
97 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +010098 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +010099
100 *olen = 0;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 MBEDTLS_SSL_DEBUG_MSG(3,
103 ("client hello, adding supported_point_formats extension"));
104 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100107 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100108
109 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100110 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200111
112 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100114
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200115 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118}
Valerio Setti7aeec542023-07-05 18:57:21 +0200119#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200120 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200121 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100122
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200123#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200124MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100125static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
126 unsigned char *buf,
127 const unsigned char *end,
128 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200129{
Janos Follath865b3eb2019-12-16 11:46:15 +0000130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200131 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100132 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200133
134 *olen = 0;
135
136 /* Skip costly extension if we can't use EC J-PAKE anyway */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
138 return 0;
139 }
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 MBEDTLS_SSL_DEBUG_MSG(3,
142 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100147 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200148
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200149 /*
150 * We may need to send ClientHello multiple times for Hello verification.
151 * We don't want to compute fresh values every time (both for performance
152 * and consistency reasons), so cache the extension content.
153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (ssl->handshake->ecjpake_cache == NULL ||
155 ssl->handshake->ecjpake_cache_len == 0) {
156 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200157
Valerio Setti6b3dab02022-11-17 17:14:54 +0100158 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 p + 2, end - p - 2, &kkpp_len,
160 MBEDTLS_ECJPAKE_ROUND_ONE);
161 if (ret != 0) {
162 psa_destroy_key(ssl->handshake->psa_pake_password);
163 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
164 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
165 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200166 }
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
169 if (ssl->handshake->ecjpake_cache == NULL) {
170 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
171 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200172 }
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200175 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 } else {
177 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200178
179 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100186 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200187
188 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200191}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200192#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000193
Hanno Beckera0e20d02019-05-15 14:03:01 +0100194#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200195MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100196static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
197 unsigned char *buf,
198 const unsigned char *end,
199 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100200{
201 unsigned char *p = buf;
202 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100203
204 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100205 * struct {
206 * opaque cid<0..2^8-1>;
207 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100209
210 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
212 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
213 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100214 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100216
217 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
218 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100220
221 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100223 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100224 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100226 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100227
228 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100230
231 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100234}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100235#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200238MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100239static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
240 unsigned char *buf,
241 const unsigned char *end,
242 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200243{
244 unsigned char *p = buf;
245
Simon Butcher0fc94e92015-09-28 20:52:04 +0100246 *olen = 0;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
249 return 0;
250 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 MBEDTLS_SSL_DEBUG_MSG(3,
253 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100258 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200259
260 *p++ = 0x00;
261 *p++ = 1;
262
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200263 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200264
265 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200272MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100273static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
274 unsigned char *buf,
275 const unsigned char *end,
276 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100277{
278 unsigned char *p = buf;
279
Simon Butcher0fc94e92015-09-28 20:52:04 +0100280 *olen = 0;
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
283 return 0;
284 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 MBEDTLS_SSL_DEBUG_MSG(3,
287 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100292 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100293
294 *p++ = 0x00;
295 *p++ = 0x00;
296
297 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200304MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100305static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
306 unsigned char *buf,
307 const unsigned char *end,
308 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200309{
310 unsigned char *p = buf;
311
Simon Butcher0fc94e92015-09-28 20:52:04 +0100312 *olen = 0;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
315 return 0;
316 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 MBEDTLS_SSL_DEBUG_MSG(3,
319 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100324 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200325
326 *p++ = 0x00;
327 *p++ = 0x00;
328
329 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200336MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100337static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
338 unsigned char *buf,
339 const unsigned char *end,
340 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200341{
342 unsigned char *p = buf;
343 size_t tlen = ssl->session_negotiate->ticket_len;
344
Simon Butcher0fc94e92015-09-28 20:52:04 +0100345 *olen = 0;
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
348 return 0;
349 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 MBEDTLS_SSL_DEBUG_MSG(3,
352 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200353
Hanno Becker261602c2017-04-12 14:54:42 +0100354 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100358 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100361 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200362
363 *olen = 4;
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
366 return 0;
367 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 MBEDTLS_SSL_DEBUG_MSG(3,
370 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200373
374 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200379
Ron Eldora9788042018-12-05 11:04:31 +0200380#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200381MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100382static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
383 unsigned char *buf,
384 const unsigned char *end,
385 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100386{
387 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200388 size_t protection_profiles_index = 0, ext_len = 0;
389 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100390
391 *olen = 0;
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
394 (ssl->conf->dtls_srtp_profile_list == NULL) ||
395 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
396 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100397 }
398
Ron Eldora9788042018-12-05 11:04:31 +0200399 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100400 * uint8 SRTPProtectionProfile[2];
401 *
402 * struct {
403 * SRTPProtectionProfiles SRTPProtectionProfiles;
404 * opaque srtp_mki<0..255>;
405 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100406 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200409 mki_len = ssl->dtls_srtp_info.mki_len;
410 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300411 /* Extension length = 2 bytes for profiles length,
412 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
413 * 1 byte for srtp_mki vector length and the mki_len value
414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200418
419 /* Check there is room in the buffer for the extension + 4 bytes
420 * - the extension tag (2 bytes)
421 * - the extension length (2 bytes)
422 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100426 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100429 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100430
Ron Eldor3adb9922017-12-21 10:15:08 +0200431 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200432 /* micro-optimization:
433 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
434 * which is lower than 127, so the upper byte of the length is always 0
435 * For the documentation, the more generic code is left in comments
436 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
437 * >> 8 ) & 0xFF );
438 */
439 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300443 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200445 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
447 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
448 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
449 profile_value));
450 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100451 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200453 /*
454 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200455 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 MBEDTLS_SSL_DEBUG_MSG(3,
458 ("client hello, "
459 "illegal DTLS-SRTP protection profile %d",
460 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
461 ));
462 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100463 }
464 }
465
Ron Eldor591f1622018-01-22 12:30:04 +0200466 *p++ = mki_len & 0xFF;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (mki_len != 0) {
469 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200470 /*
471 * Increment p to point to the current position.
472 */
473 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
475 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200476 }
477
Ron Eldoref72faf2018-07-12 11:54:20 +0300478 /*
479 * total extension length: extension type (2 bytes)
480 * + extension length (2 bytes)
481 * + protection profile length (2 bytes)
482 * + 2 * number of protection profiles
483 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200484 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300485 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200486 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100489}
490#endif /* MBEDTLS_SSL_DTLS_SRTP */
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
493 unsigned char *buf,
494 const unsigned char *end,
495 int uses_ec,
496 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100497{
498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
499 unsigned char *p = buf;
500 size_t ext_len = 0;
501
502 (void) ssl;
503 (void) end;
504 (void) uses_ec;
505 (void) ret;
506 (void) ext_len;
507
508 *out_len = 0;
509
510 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
511 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
512#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
514 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
515 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100516 }
517 p += ext_len;
518#endif
519
Valerio Setti7aeec542023-07-05 18:57:21 +0200520#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200521 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Ronald Cron12dcdf02022-02-16 15:28:22 +0100522 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (uses_ec) {
524 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
525 &ext_len)) != 0) {
526 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
527 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100528 }
529 p += ext_len;
530 }
531#endif
532
533#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
535 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
536 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100537 }
538 p += ext_len;
539#endif
540
541#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
543 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
544 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100545 }
546 p += ext_len;
547#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
548
549#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
551 &ext_len)) != 0) {
552 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
553 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100554 }
555 p += ext_len;
556#endif
557
558#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
560 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
561 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100562 }
563 p += ext_len;
564#endif
565
566#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
568 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
569 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100570 }
571 p += ext_len;
572#endif
573
574#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
576 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
577 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100578 }
579 p += ext_len;
580#endif
581
582#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
584 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
585 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100586 }
587 p += ext_len;
588#endif
589
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000590 *out_len = (size_t) (p - buf);
Ronald Cron12dcdf02022-02-16 15:28:22 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100593}
594
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200595MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100596static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
597 const unsigned char *buf,
598 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100602 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000604 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_ct_memcmp(buf + 1,
606 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
607 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
608 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
609 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100610 mbedtls_ssl_send_alert_message(
611 ssl,
612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
614 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000615 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100618 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (len != 1 || buf[0] != 0x00) {
620 MBEDTLS_SSL_DEBUG_MSG(1,
621 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100622 mbedtls_ssl_send_alert_message(
623 ssl,
624 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
626 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100627 }
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100630 }
Paul Bakker48916f92012-09-16 19:57:18 +0000631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000633}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200636MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100637static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
638 const unsigned char *buf,
639 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200640{
641 /*
642 * server should use the extension only if we did,
643 * and if so the server's value should match ours (and len is always 1)
644 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200646 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 buf[0] != ssl->conf->mfl_code) {
648 MBEDTLS_SSL_DEBUG_MSG(1,
649 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100650 mbedtls_ssl_send_alert_message(
651 ssl,
652 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
654 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200655 }
656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000660
Hanno Beckera0e20d02019-05-15 14:03:01 +0100661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200662MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100663static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
664 const unsigned char *buf,
665 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100666{
667 size_t peer_cid_len;
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100670 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
671 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
673 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
674 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
675 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
676 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (len == 0) {
680 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
681 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
682 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
683 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100684 }
685
686 peer_cid_len = *buf++;
687 len--;
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
690 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
691 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
692 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
693 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100694 }
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (len != peer_cid_len) {
697 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
698 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
699 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
700 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100701 }
702
Hanno Becker5a299902019-05-03 12:47:49 +0100703 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100704 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
708 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100711}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100712#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200715MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100716static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
717 const unsigned char *buf,
718 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100719{
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
721 len != 0) {
722 MBEDTLS_SSL_DEBUG_MSG(1,
723 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100724 mbedtls_ssl_send_alert_message(
725 ssl,
726 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
728 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100729 }
730
731 ((void) buf);
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200740MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100741static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
742 const unsigned char *buf,
743 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200744{
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
746 len != 0) {
747 MBEDTLS_SSL_DEBUG_MSG(1,
748 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100749 mbedtls_ssl_send_alert_message(
750 ssl,
751 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
753 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200754 }
755
756 ((void) buf);
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200765MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100766static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
767 const unsigned char *buf,
768 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200769{
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
771 len != 0) {
772 MBEDTLS_SSL_DEBUG_MSG(1,
773 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100774 mbedtls_ssl_send_alert_message(
775 ssl,
776 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
778 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200779 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200780
781 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200782
783 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200788
Valerio Setti7aeec542023-07-05 18:57:21 +0200789#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200790 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100791 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200792MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100793static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
794 const unsigned char *buf,
795 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200796{
797 size_t list_size;
798 const unsigned char *p;
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (len == 0 || (size_t) (buf[0] + 1) != len) {
801 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
802 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
803 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
804 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200805 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200806 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200807
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200808 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 while (list_size > 0) {
810 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
811 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
813 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200814 }
815
816 list_size--;
817 p++;
818 }
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
821 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
822 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
823 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200824}
Valerio Setti7aeec542023-07-05 18:57:21 +0200825#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200826 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200827 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200828
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200829#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200830MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100831static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
832 const unsigned char *buf,
833 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200834{
Janos Follath865b3eb2019-12-16 11:46:15 +0000835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (ssl->handshake->ciphersuite_info->key_exchange !=
838 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
839 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
840 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200841 }
842
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200843 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200845 ssl->handshake->ecjpake_cache = NULL;
846 ssl->handshake->ecjpake_cache_len = 0;
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if ((ret = mbedtls_psa_ecjpake_read_round(
849 &ssl->handshake->psa_pake_ctx, buf, len,
850 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
851 psa_destroy_key(ssl->handshake->psa_pake_password);
852 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100855 mbedtls_ssl_send_alert_message(
856 ssl,
857 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
859 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200860 }
861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200863}
864#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200867MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100868static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
869 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200870{
871 size_t list_len, name_len;
872 const char **p;
873
874 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (ssl->conf->alpn_list == NULL) {
876 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100877 mbedtls_ssl_send_alert_message(
878 ssl,
879 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
881 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200882 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200883
884 /*
885 * opaque ProtocolName<1..2^8-1>;
886 *
887 * struct {
888 * ProtocolName protocol_name_list<2..2^16-1>
889 * } ProtocolNameList;
890 *
891 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
892 */
893
894 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (len < 4) {
896 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
897 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
898 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200899 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200900
Dave Rodgmana3d0f612023-11-03 23:34:02 +0000901 list_len = MBEDTLS_GET_UINT16_BE(buf, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (list_len != len - 2) {
903 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
904 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
905 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200906 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200907
908 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (name_len != list_len - 1) {
910 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
911 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
912 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200913 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200914
915 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
917 if (name_len == strlen(*p) &&
918 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200919 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200921 }
922 }
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
925 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
926 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
927 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200930
Johan Pascalb62bb512015-12-03 21:56:45 +0100931#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200932MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100933static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
934 const unsigned char *buf,
935 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100936{
Johan Pascal43f94902020-09-22 12:25:52 +0200937 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200938 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100939 uint16_t server_protection_profile_value = 0;
940
941 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
943 (ssl->conf->dtls_srtp_profile_list == NULL) ||
944 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
945 return 0;
946 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100947
Ron Eldora9788042018-12-05 11:04:31 +0200948 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100949 * uint8 SRTPProtectionProfile[2];
950 *
951 * struct {
952 * SRTPProtectionProfiles SRTPProtectionProfiles;
953 * opaque srtp_mki<0..255>;
954 * } UseSRTPData;
955
956 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
957 *
Johan Pascalb62bb512015-12-03 21:56:45 +0100958 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200960 mki_len = ssl->dtls_srtp_info.mki_len;
961 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100962
Ron Eldoref72faf2018-07-12 11:54:20 +0300963 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200964 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
965 * + protection profile (2 bytes)
966 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200967 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +0300968 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if ((len < 5) || (len != (buf[4] + 5u))) {
970 return MBEDTLS_ERR_SSL_DECODE_ERROR;
971 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100972
973 /*
974 * get the server protection profile
975 */
Ron Eldoref72faf2018-07-12 11:54:20 +0300976
977 /*
978 * protection profile length must be 0x0002 as we must have only
979 * one protection profile in server Hello
980 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if ((buf[0] != 0) || (buf[1] != 2)) {
982 return MBEDTLS_ERR_SSL_DECODE_ERROR;
983 }
Ron Eldor089c9fe2018-12-06 17:12:49 +0200984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +0200986 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 server_protection_profile_value);
988 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
989 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
990 mbedtls_ssl_get_srtp_profile_as_string(
991 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +0100992 }
993
Johan Pascal43f94902020-09-22 12:25:52 +0200994 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200995
Johan Pascalb62bb512015-12-03 21:56:45 +0100996 /*
997 * Check we have the server profile in our list
998 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1000 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001001 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001003 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001005 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001006 }
1007 }
1008
Ron Eldor591f1622018-01-22 12:30:04 +02001009 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1011 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1012 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1013 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001014 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001015
1016 /* If server does not use mki in its reply, make sure the client won't keep
1017 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001019 ssl->dtls_srtp_info.mki_len = 0;
1020 }
1021
Ron Eldoref72faf2018-07-12 11:54:20 +03001022 /*
1023 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001024 * If the client detects a nonzero-length MKI in the server's response
1025 * that is different than the one the client offered, then the client
1026 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (len > 5 && (buf[4] != mki_len ||
1029 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1030 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1031 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1032 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001033 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001034#if defined(MBEDTLS_DEBUG_C)
1035 if (len > 5) {
1036 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1037 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001038 }
1039#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001041}
1042#endif /* MBEDTLS_SSL_DTLS_SRTP */
1043
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001044/*
1045 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001048MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001049static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001050{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001051 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001053 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001054
1055#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1056 uint8_t cookie_len;
1057#else
1058 uint16_t cookie_len;
1059#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001062
Gilles Peskineb64bf062019-09-27 14:02:44 +02001063 /* Check that there is enough room for:
1064 * - 2 bytes of version
1065 * - 1 byte of cookie_len
1066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1068 MBEDTLS_SSL_DEBUG_MSG(1,
1069 ("incoming HelloVerifyRequest message is too short"));
1070 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1071 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1072 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001073 }
1074
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001075 /*
1076 * struct {
1077 * ProtocolVersion server_version;
1078 * opaque cookie<0..2^8-1>;
1079 * } HelloVerifyRequest;
1080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1082 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001083 p += 2;
1084
TRodziewicz2d8800e2021-05-13 19:14:19 +02001085 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001086 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1087 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1088 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1091 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1094 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001097 }
1098
1099 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1101 MBEDTLS_SSL_DEBUG_MSG(1,
1102 ("cookie length does not match incoming message size"));
1103 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1104 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1105 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001106 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1112 if (ssl->handshake->cookie == NULL) {
1113 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1114 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001115 }
1116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001118 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001119
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001120 /* Start over at ClientHello */
Gilles Peskinef670ba52025-03-07 15:09:32 +01001121 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001122 ret = mbedtls_ssl_reset_checksum(ssl);
1123 if (0 != ret) {
1124 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1125 return ret;
1126 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001135
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001136MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001137static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001138{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001139 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001140 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001141 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001142 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001143 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001145 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001146#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001147 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001153 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1155 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 }
1157
Hanno Becker79594fd2019-05-08 09:38:41 +01001158 buf = ssl->in_msg;
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001163 ssl->renego_records_seen++;
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (ssl->conf->renego_max_records >= 0 &&
1166 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1167 MBEDTLS_SSL_DEBUG_MSG(1,
1168 ("renegotiation requested, but not honored by server"));
1169 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 MBEDTLS_SSL_DEBUG_MSG(1,
1173 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001174
1175 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001177 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001181 mbedtls_ssl_send_alert_message(
1182 ssl,
1183 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1185 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 }
1187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1190 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1191 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1192 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1193 return ssl_parse_hello_verify_request(ssl);
1194 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001195 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001197 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001198 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001199 }
1200 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1204 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1205 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1206 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1207 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1208 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 }
1210
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001211 /*
1212 * 0 . 1 server_version
1213 * 2 . 33 random (maybe including 4 bytes of Unix time)
1214 * 34 . 34 session_id length = n
1215 * 35 . 34+n session_id
1216 * 35+n . 36+n cipher_suite
1217 * 37+n . 37+n compression_method
1218 *
1219 * 38+n . 39+n extensions length (optional)
1220 * 40+n . .. extensions
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01001225 ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
1226 ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001227 ssl->session_negotiate->tls_version = ssl->tls_version;
Ronald Cron17ef8df2023-11-22 10:29:42 +01001228 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if (ssl->tls_version < ssl->conf->min_tls_version ||
1231 ssl->tls_version > ssl->conf->max_tls_version) {
1232 MBEDTLS_SSL_DEBUG_MSG(1,
1233 (
1234 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1235 (unsigned) ssl->conf->min_tls_version,
1236 (unsigned) ssl->tls_version,
1237 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1240 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001243 }
1244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1246 ((unsigned long) buf[2] << 24) |
1247 ((unsigned long) buf[3] << 16) |
1248 ((unsigned long) buf[4] << 8) |
1249 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001253 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (n > 32) {
1258 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1259 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1260 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1261 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001262 }
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001265 ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if ((ext_len > 0 && ext_len < 4) ||
1268 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1269 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001270 mbedtls_ssl_send_alert_message(
1271 ssl,
1272 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1274 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001275 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001277 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 } else {
1279 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1280 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1281 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1282 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001285 /* ciphersuite (used later) */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001286 i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35);
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001287
1288 /*
1289 * Read and check compression
1290 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001291 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1294 MBEDTLS_SSL_DEBUG_MSG(1,
1295 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001296 mbedtls_ssl_send_alert_message(
1297 ssl,
1298 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1300 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001301 }
1302
Paul Bakker380da532012-04-18 16:10:25 +00001303 /*
1304 * Initialize update checksum functions
1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1307 if (ssl->handshake->ciphersuite_info == NULL) {
1308 MBEDTLS_SSL_DEBUG_MSG(1,
1309 ("ciphersuite info for %04x not found", (unsigned int) i));
1310 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1311 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1312 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001313 }
Paul Bakker380da532012-04-18 16:10:25 +00001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1318 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
1320 /*
1321 * Check if the session can be resumed
1322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_SSL_RENEGOTIATION)
1325 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001326#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001327 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001328 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Gilles Peskinef670ba52025-03-07 15:09:32 +01001330 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker0a597072012-09-25 21:55:46 +00001331 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001334#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001335 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001336 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 memcpy(ssl->session_negotiate->id, buf + 35, n);
1338 } else {
Gilles Peskinef670ba52025-03-07 15:09:32 +01001339 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1343 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1346 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1347 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Andrzej Kurek03bac442018-04-25 05:06:07 -04001349 /*
1350 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001351 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 while (1) {
1354 if (ssl->conf->ciphersuite_list[i] == 0) {
1355 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001356 mbedtls_ssl_send_alert_message(
1357 ssl,
1358 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1360 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (ssl->conf->ciphersuite_list[i++] ==
1364 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 }
1368
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001369 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 ssl->session_negotiate->ciphersuite);
1371 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1372 ssl->tls_version) != 0) {
1373 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001374 mbedtls_ssl_send_alert_message(
1375 ssl,
1376 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1378 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001379 }
1380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 MBEDTLS_SSL_DEBUG_MSG(3,
1382 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001383
Gilles Peskineeccd8882020-03-10 12:19:08 +01001384#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1386 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001387 ssl->handshake->ecrs_enabled = 1;
1388 }
1389#endif
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1392 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001393 mbedtls_ssl_send_alert_message(
1394 ssl,
1395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1397 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 }
1399
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001400 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 MBEDTLS_SSL_DEBUG_MSG(2,
1403 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1404 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 while (ext_len) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001407 unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
1408 unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
Paul Bakker48916f92012-09-16 19:57:18 +00001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if (ext_size + 4 > ext_len) {
1411 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001412 mbedtls_ssl_send_alert_message(
1413 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1415 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001416 }
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 switch (ext_id) {
1419 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1420 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001423#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1426 ext_size)) != 0) {
1427 return ret;
1428 }
Paul Bakker48916f92012-09-16 19:57:18 +00001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1434 MBEDTLS_SSL_DEBUG_MSG(3,
1435 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1438 ext + 4, ext_size)) != 0) {
1439 return ret;
1440 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001444
Hanno Beckera0e20d02019-05-15 14:03:01 +01001445#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 case MBEDTLS_TLS_EXT_CID:
1447 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if ((ret = ssl_parse_cid_ext(ssl,
1450 ext + 4,
1451 ext_size)) != 0) {
1452 return ret;
1453 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001456#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1460 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1463 ext + 4, ext_size)) != 0) {
1464 return ret;
1465 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1472 MBEDTLS_SSL_DEBUG_MSG(3,
1473 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if ((ret = ssl_parse_extended_ms_ext(ssl,
1476 ext + 4, ext_size)) != 0) {
1477 return ret;
1478 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1485 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if ((ret = ssl_parse_session_ticket_ext(ssl,
1488 ext + 4, ext_size)) != 0) {
1489 return ret;
1490 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001494
Valerio Setti7aeec542023-07-05 18:57:21 +02001495#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001496 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001497 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1499 MBEDTLS_SSL_DEBUG_MSG(3,
1500 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1503 ext + 4, ext_size)) != 0) {
1504 return ret;
1505 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001508#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001509 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001510 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001511
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001512#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1514 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1517 ext + 4, ext_size)) != 0) {
1518 return ret;
1519 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001522#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 case MBEDTLS_TLS_EXT_ALPN:
1526 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1529 return ret;
1530 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001534
Johan Pascalb62bb512015-12-03 21:56:45 +01001535#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 case MBEDTLS_TLS_EXT_USE_SRTP:
1537 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1540 return ret;
1541 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001544#endif /* MBEDTLS_SSL_DTLS_SRTP */
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 default:
1547 MBEDTLS_SSL_DEBUG_MSG(3,
1548 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001549 }
1550
1551 ext_len -= 4 + ext_size;
1552 ext += 4 + ext_size;
1553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (ext_len > 0 && ext_len < 4) {
1555 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1556 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001557 }
1558 }
1559
1560 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001561 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1562 * extensions. It sets the transform data for the resumed session which in
1563 * case of DTLS includes the server CID extracted from the CID extension.
1564 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (ssl->handshake->resume) {
1566 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1567 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001568 mbedtls_ssl_send_alert_message(
1569 ssl,
1570 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1572 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001573 }
1574 }
1575
Paul Bakker48916f92012-09-16 19:57:18 +00001576 /*
1577 * Renegotiation security checks
1578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001580 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1582 MBEDTLS_SSL_DEBUG_MSG(1,
1583 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001584 handshake_failure = 1;
1585 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 renegotiation_info_seen == 0) {
1590 MBEDTLS_SSL_DEBUG_MSG(1,
1591 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001592 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1594 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1595 ssl->conf->allow_legacy_renegotiation ==
1596 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1597 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001598 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1600 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1601 renegotiation_info_seen == 1) {
1602 MBEDTLS_SSL_DEBUG_MSG(1,
1603 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001604 handshake_failure = 1;
1605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001609 mbedtls_ssl_send_alert_message(
1610 ssl,
1611 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1613 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001619}
1620
Andrzej Kurek468c5062022-10-24 10:30:14 -04001621#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1622 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1623 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001624MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001625static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1626 unsigned char **p,
1627 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001628{
1629 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001630 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001631 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001632 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001633 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001634
1635 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001636 * struct {
1637 * ECParameters curve_params;
1638 * ECPoint public;
1639 * } ServerECDHParams;
1640 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001641 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001642 * 2..3 NamedCurve
1643 * 4 ECPoint.len
1644 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if (end - *p < 4) {
1647 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1648 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001649
1650 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1652 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1653 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001654
1655 /* Next two bytes are the namedcurve value */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001656 tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
1657 *p += 2;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001658
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001659 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1661 MBEDTLS_SSL_DEBUG_MSG(2,
1662 ("bad server key exchange message (ECDHE curve): %u",
1663 (unsigned) tls_id));
1664 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001665 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001666
Valerio Setti40d9ca92023-01-04 16:08:04 +01001667 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001668 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1670 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001671 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001672 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001673 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001674
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001675 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001676 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if ((size_t) (end - *p) < ecpoint_len) {
1678 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1679 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001680
Gilles Peskinec29df532023-10-02 14:59:26 +02001681 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1683 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001684
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001685 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1686 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001687 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001690}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001691#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1692 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1693 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001694#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001695MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001696static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1697 unsigned char **p,
1698 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001701 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001702 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001703
1704 /*
1705 * PSK parameters:
1706 *
1707 * opaque psk_identity_hint<0..2^16-1>;
1708 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (end - (*p) < 2) {
1710 MBEDTLS_SSL_DEBUG_MSG(1,
1711 ("bad server key exchange message (psk_identity_hint length)"));
1712 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001713 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001714 len = MBEDTLS_GET_UINT16_BE(*p, 0);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001715 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 if (end - (*p) < len) {
1718 MBEDTLS_SSL_DEBUG_MSG(1,
1719 ("bad server key exchange message (psk_identity_hint length)"));
1720 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001721 }
1722
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001723 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001724 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001725 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001726 * someone needs that feature.
1727 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001728 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001729 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001732}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001733#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1736 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001737MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001738static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001739{
Janos Follath865b3eb2019-12-16 11:46:15 +00001740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001742
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001743#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1744 peer_pk = &ssl->handshake->peer_pubkey;
1745#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001747 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1749 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001750 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001751 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1752#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001753
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02001754 /* This is a public key, so it can't be opaque, so can_do() is a good
1755 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
1757 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
1758 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001759 }
1760
Valerio Setti2b5d3de2023-01-09 11:04:52 +01001761 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001762 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Settif9362b72023-11-29 08:42:27 +01001763 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01001764
Valerio Setti97207782023-05-18 18:59:06 +02001765 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
1767 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01001768 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01001769
Valerio Setti97207782023-05-18 18:59:06 +02001770 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (tls_id == 0) {
1772 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02001773 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01001775 }
1776
Valerio Setti1e868cc2023-01-09 17:30:01 +01001777 /* If the above conversion to TLS ID was fine, then also this one will be,
1778 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001779 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02001780 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01001781
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001782 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01001783
Przemek Stekielea4000f2022-03-16 09:49:33 +01001784 /* Store peer's public key in psa format. */
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001785 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
1786 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02001787 ret = 0;
Hanno Beckerae553dd2019-02-08 14:06:00 +00001788#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1789 /* We don't need the peer's public key anymore. Free it,
1790 * so that more RAM is available for upcoming expensive
1791 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001793#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1798 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001799
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001800MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001801static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01001802{
Janos Follath865b3eb2019-12-16 11:46:15 +00001803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001804 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00001805 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01001806 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1811 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1813 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
1814 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
1815 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001816 mbedtls_ssl_send_alert_message(
1817 ssl,
1818 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1820 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001821 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01001824 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001826 }
1827 ((void) p);
1828 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1830 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001831
Gilles Peskineeccd8882020-03-10 12:19:08 +01001832#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 if (ssl->handshake->ecrs_enabled &&
1834 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02001835 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02001836 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02001837#endif
1838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
1840 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1841 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 }
1843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
1845 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001846 mbedtls_ssl_send_alert_message(
1847 ssl,
1848 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1850 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001851 }
1852
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001853 /*
Gilles Peskineb3ec1252024-09-20 18:22:04 +02001854 * ServerKeyExchange may be skipped with PSK when the server
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001855 * doesn't use a psk_identity_hint
1856 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
Gilles Peskine712e9a12024-09-20 18:11:31 +02001858 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001859 /* Current message is probably either
1860 * CertificateRequest or ServerHelloDone */
1861 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02001862 goto exit;
1863 }
1864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 MBEDTLS_SSL_DEBUG_MSG(1,
1866 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001867 mbedtls_ssl_send_alert_message(
1868 ssl,
1869 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 }
1874
Gilles Peskineeccd8882020-03-10 12:19:08 +01001875#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02001877 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02001879
1880start_processing:
1881#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001883 end = ssl->in_msg + ssl->in_hslen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001884 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001885
Gilles Peskineeccd8882020-03-10 12:19:08 +01001886#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
1889 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
1890 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001891 mbedtls_ssl_send_alert_message(
1892 ssl,
1893 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1895 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001896 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001897 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001898#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001899
Gilles Peskineac767e52024-09-20 18:08:44 +02001900#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine712e9a12024-09-20 18:11:31 +02001901 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001902 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 } else
Gilles Peskineac767e52024-09-20 18:08:44 +02001904#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001905#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1906 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
1911 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
1912 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001913 mbedtls_ssl_send_alert_message(
1914 ssl,
1915 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1917 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01001918 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1921 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1922 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02001923#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Valerio Setti9bed8ec2022-11-17 16:36:19 +01001925 /*
1926 * The first 3 bytes are:
1927 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
1928 * [1, 2] elliptic curve's TLS ID
1929 *
1930 * However since we only support secp256r1 for now, we check only
1931 * that TLS ID here
1932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01001934 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01001936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 if (exp_tls_id == 0) {
1938 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01001939 }
1940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
1942 (read_tls_id != exp_tls_id)) {
1943 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01001944 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01001945
1946 p += 3;
1947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if ((ret = mbedtls_psa_ecjpake_read_round(
1949 &ssl->handshake->psa_pake_ctx, p, end - p,
1950 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
1951 psa_destroy_key(ssl->handshake->psa_pake_password);
1952 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001955 mbedtls_ssl_send_alert_message(
1956 ssl,
1957 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1959 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001960 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02001962#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001963 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1965 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001966 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001967
Gilles Peskineeccd8882020-03-10 12:19:08 +01001968#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00001970 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001971 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1974 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001976 size_t params_len = (size_t) (p - params);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02001977 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08001978 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00001981
Jerry Yu693a47a2022-06-23 14:02:28 +08001982#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1983 peer_pk = &ssl->handshake->peer_pubkey;
1984#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08001986 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1988 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08001989 }
1990 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1991#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1992
Paul Bakker29e1f122013-04-16 13:07:56 +02001993 /*
1994 * Handle the digitally-signed structure
1995 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1997 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
1998 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1999 sig_alg, &pk_alg, &md_alg) != 0 &&
2000 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2001 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2002 MBEDTLS_SSL_DEBUG_MSG(1,
2003 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002004 mbedtls_ssl_send_alert_message(
2005 ssl,
2006 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2008 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002009 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002010 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2013 MBEDTLS_SSL_DEBUG_MSG(1,
2014 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002015 mbedtls_ssl_send_alert_message(
2016 ssl,
2017 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2019 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002020 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002021
2022 /*
2023 * Read signature
2024 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (p > end - 2) {
2027 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002028 mbedtls_ssl_send_alert_message(
2029 ssl,
2030 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2032 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002033 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002034 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
Paul Bakker1ef83d62012-04-11 12:09:53 +00002035 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if (p != end - sig_len) {
2038 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002039 mbedtls_ssl_send_alert_message(
2040 ssl,
2041 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2043 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002047
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002048 /*
2049 * Compute the hash that has been signed
2050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 if (md_alg != MBEDTLS_MD_NONE) {
2052 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2053 params, params_len,
2054 md_alg);
2055 if (ret != 0) {
2056 return ret;
2057 }
2058 } else {
2059 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2060 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002061 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002064
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002065 /*
2066 * Verify signature
2067 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2069 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002070 mbedtls_ssl_send_alert_message(
2071 ssl,
2072 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2074 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002075 }
2076
Gilles Peskineeccd8882020-03-10 12:19:08 +01002077#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002079 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002081#endif
2082
Jerry Yu693a47a2022-06-23 14:02:28 +08002083#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Valerio Setti7f6f4e62025-04-23 11:29:51 +02002085 ret = mbedtls_pk_verify_ext(pk_alg, NULL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 peer_pk,
2087 md_alg, hash, hashlen,
2088 p, sig_len);
2089 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002090#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 ret = mbedtls_pk_verify_restartable(peer_pk,
2092 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002095 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002096#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002098#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002100 mbedtls_ssl_send_alert_message(
2101 ssl,
2102 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2104 }
2105 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002106#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002108 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002110#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002112 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002113
2114#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2115 /* We don't need the peer's public key anymore. Free it,
2116 * so that more RAM is available for upcoming expensive
2117 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002119#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002121#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002123exit:
Gilles Peskinef670ba52025-03-07 15:09:32 +01002124 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002129}
2130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002132MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002133static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002134{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002135 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002136 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002139
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2141 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002142 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002144 }
2145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2147 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002148}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002149#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002151static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002152{
Janos Follath865b3eb2019-12-16 11:46:15 +00002153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002154 unsigned char *buf;
2155 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002156 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002157 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002158 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002159 size_t sig_alg_len;
2160#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 unsigned char *sig_alg;
2162 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002163#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2168 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002169 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002171 }
2172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2174 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2175 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 }
2177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2179 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002180 mbedtls_ssl_send_alert_message(
2181 ssl,
2182 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2184 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Gilles Peskinef670ba52025-03-07 15:09:32 +01002187 mbedtls_ssl_handshake_increment_state(ssl);
Jerry Yufb28b882022-01-28 11:05:58 +08002188 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2192 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002195 /* Current message is probably the ServerHelloDone */
2196 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002197 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002198 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002199
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002200 /*
2201 * struct {
2202 * ClientCertificateType certificate_types<1..2^8-1>;
2203 * SignatureAndHashAlgorithm
2204 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2205 * DistinguishedName certificate_authorities<0..2^16-1>;
2206 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002207 *
2208 * Since we only support a single certificate on clients, let's just
2209 * ignore all the information that's supposed to help us pick a
2210 * certificate.
2211 *
2212 * We could check that our certificate matches the request, and bail out
2213 * if it doesn't, but it's simpler to just send the certificate anyway,
2214 * and give the server the opportunity to decide if it should terminate
2215 * the connection when it doesn't like our certificate.
2216 *
2217 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2218 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002219 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002220 *
2221 * However, we still minimally parse the message to check it is at least
2222 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002223 */
Paul Bakker926af752012-11-23 13:38:07 +01002224 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002225
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002226 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2228 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2229 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2230 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2231 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002232 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002234 n = cert_type_len;
2235
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002236 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002237 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002238 * * the length of the signature algorithms field (if minor version of
2239 * SSL is 3),
2240 * * distinguished name length otherwise.
2241 * Both reach at most the index:
2242 * ...hdr_len + 2 + n,
2243 * therefore the buffer length at this point must be greater than that
2244 * regardless of the actual code path.
2245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2247 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2248 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2249 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2250 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002251 }
2252
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002253 /* supported_signature_algorithms */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002254 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002255
2256 /*
2257 * The furthest access in buf is in the loop few lines below:
2258 * sig_alg[i + 1],
2259 * where:
2260 * sig_alg = buf + ...hdr_len + 3 + n,
2261 * max(i) = sig_alg_len - 1.
2262 * Therefore the furthest access is:
2263 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2264 * which reduces to:
2265 * buf[...hdr_len + 3 + n + sig_alg_len],
2266 * which is one less than we need the buf to be.
2267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2269 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002270 mbedtls_ssl_send_alert_message(
2271 ssl,
2272 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2274 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002275 }
Paul Bakker926af752012-11-23 13:38:07 +01002276
Ronald Cron90915f22022-03-07 11:11:36 +01002277#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2279 for (size_t i = 0; i < sig_alg_len; i += 2) {
2280 MBEDTLS_SSL_DEBUG_MSG(3,
2281 ("Supported Signature Algorithm found: %02x %02x",
2282 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002283 }
2284#endif
2285
2286 n += 2 + sig_alg_len;
2287
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002288 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002289 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002290
2291 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2293 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2294 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2295 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2296 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002297 }
2298
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002299#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2301 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002302 unsigned char *p = dn + i + 2;
2303 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002304 size_t asn1_len;
2305 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 memset(&name, 0, sizeof(name));
2307 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2308 if (dni_len > dn_len - i - 2 ||
2309 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2310 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2311 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2312 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002313 mbedtls_ssl_send_alert_message(
2314 ssl,
2315 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2317 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002318 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 MBEDTLS_SSL_DEBUG_MSG(3,
2320 ("DN hint: %.*s",
2321 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2322 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002323 }
2324#endif
2325
Paul Bakker926af752012-11-23 13:38:07 +01002326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002330}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002331#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002333MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002334static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002335{
Janos Follath865b3eb2019-12-16 11:46:15 +00002336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2341 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2342 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2346 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2347 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2351 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2352 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2353 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2354 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2355 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002356 }
2357
Gilles Peskinef670ba52025-03-07 15:09:32 +01002358 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2362 mbedtls_ssl_recv_flight_completed(ssl);
2363 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002364#endif
2365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002369}
2370
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002371MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002372static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002373{
Janos Follath865b3eb2019-12-16 11:46:15 +00002374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002375
2376 size_t header_len;
2377 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002378 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002379 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002383#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2384 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2385 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2386 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002388 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2389 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Andrzej Kureka0237f82022-02-24 13:24:52 -05002391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2392 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002393 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002394
2395 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2396
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002397 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002400
Hanno Becker4a63ed42019-01-08 11:39:35 +00002401 /*
2402 * Generate EC private key for ECDHE exchange.
2403 */
2404
Hanno Becker4a63ed42019-01-08 11:39:35 +00002405 /* The master secret is obtained from the shared ECDH secret by
2406 * applying the TLS 1.2 PRF with a specific salt and label. While
2407 * the PSA Crypto API encourages combining key agreement schemes
2408 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2409 * yet support the provisioning of salt + label to the KDF.
2410 * For the time being, we therefore need to split the computation
2411 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002412 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2414 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002415 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002416 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002417
2418 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002420 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if (status != PSA_SUCCESS) {
2422 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2423 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002424
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002425 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002426 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002427 * but we just need to add a length byte before that. */
2428 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2429 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002431 size_t own_pubkey_len;
2432
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002433 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 own_pubkey, own_pubkey_max_len,
2435 &own_pubkey_len);
2436 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002437 psa_destroy_key(handshake->xxdh_psa_privkey);
2438 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002440 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002441
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002442 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2443 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002444
Hanno Becker4a63ed42019-01-08 11:39:35 +00002445 /* The ECDH secret is the premaster secret used for key derivation. */
2446
Janos Follathdf3b0892019-08-08 11:12:24 +01002447 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002449 handshake->xxdh_psa_privkey,
2450 handshake->xxdh_psa_peerkey,
2451 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 ssl->handshake->premaster,
2453 sizeof(ssl->handshake->premaster),
2454 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002455
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002456 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2457 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2460 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2461 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2464 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2465 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2466 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardc7403ed2025-01-23 11:24:18 +01002467#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002469 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2470 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2471 psa_key_attributes_t key_attributes;
2472
2473 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2474
2475 /*
2476 * opaque psk_identity<0..2^16-1>;
2477 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002479 /* We don't offer PSK suites if we don't have a PSK,
2480 * and we check that the server's choice is among the
2481 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2483 }
Neil Armstrong868af822022-03-09 10:26:25 +01002484
Neil Armstrongfc834f22022-03-23 17:54:38 +01002485 /* uint16 to store content length */
2486 const size_t content_len_size = 2;
2487
Neil Armstrong868af822022-03-09 10:26:25 +01002488 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if (header_len + content_len_size + ssl->conf->psk_identity_len
2491 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2492 MBEDTLS_SSL_DEBUG_MSG(1,
2493 ("psk identity too long or SSL buffer too short"));
2494 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002495 }
2496
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002497 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2500 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002501 header_len += content_len_size;
2502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 memcpy(p, ssl->conf->psk_identity,
2504 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002505 p += ssl->conf->psk_identity_len;
2506
Neil Armstrong868af822022-03-09 10:26:25 +01002507 header_len += ssl->conf->psk_identity_len;
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002510
2511 /*
2512 * Generate EC private key for ECDHE exchange.
2513 */
2514
2515 /* The master secret is obtained from the shared ECDH secret by
2516 * applying the TLS 1.2 PRF with a specific salt and label. While
2517 * the PSA Crypto API encourages combining key agreement schemes
2518 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2519 * yet support the provisioning of salt + label to the KDF.
2520 * For the time being, we therefore need to split the computation
2521 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2522 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2524 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002525 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002526 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002527
2528 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002530 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002532 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 }
Neil Armstrong868af822022-03-09 10:26:25 +01002534
2535 /* Export the public part of the ECDH private key from PSA.
2536 * The export format is an ECPoint structure as expected by TLS,
2537 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002538 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002539 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002541 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002542
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002543 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 own_pubkey, own_pubkey_max_len,
2545 &own_pubkey_len);
2546 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002547 psa_destroy_key(handshake->xxdh_psa_privkey);
2548 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002549 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002550 }
2551
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002552 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002553 content_len = own_pubkey_len + 1;
2554
Neil Armstrong25400452022-03-23 17:44:07 +01002555 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2556 * - a uint16 containing the length (in octets) of the ECDH computation
2557 * - the octet string produced by the ECDH computation
2558 * - a uint16 containing the length (in octets) of the PSK
2559 * - the PSK itself
2560 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002561 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 const unsigned char * const pms_end = pms +
2563 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002564 /* uint16 to store length (in octets) of the ECDH computation */
2565 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002566 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002567
Neil Armstrong25400452022-03-23 17:44:07 +01002568 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002570 handshake->xxdh_psa_privkey,
2571 handshake->xxdh_psa_peerkey,
2572 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 pms + zlen_size,
2574 pms_end - (pms + zlen_size),
2575 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01002576
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002577 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2578 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002581 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002583 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 }
Neil Armstrong868af822022-03-09 10:26:25 +01002585
Neil Armstrong25400452022-03-23 17:44:07 +01002586 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002588 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 } else
Manuel Pégourié-Gonnardc7403ed2025-01-23 11:24:18 +01002590#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002591#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002593 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002594 * opaque psk_identity<0..2^16-1>;
2595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01002597 /* We don't offer PSK suites if we don't have a PSK,
2598 * and we check that the server's choice is among the
2599 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02002601 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002602
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002603 header_len = 4;
2604 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2607 MBEDTLS_SSL_DEBUG_MSG(1,
2608 ("psk identity too long or SSL buffer too short"));
2609 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002610 }
2611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
2613 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 memcpy(ssl->out_msg + header_len,
2616 ssl->conf->psk_identity,
2617 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002618 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002622 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002624#endif
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002625 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2627 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002628 }
2629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01002631#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002632#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002634 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002635
Neil Armstrongca7d5062022-05-31 14:43:23 +02002636 unsigned char *out_p = ssl->out_msg + header_len;
2637 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
2638 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
2640 out_p, end_p - out_p, &content_len,
2641 MBEDTLS_ECJPAKE_ROUND_TWO);
2642 if (ret != 0) {
2643 psa_destroy_key(ssl->handshake->psa_pake_password);
2644 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2645 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
2646 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002647 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 } else
Gabor Mezeie1e27302025-02-26 18:06:05 +01002649#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002650 {
2651 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2653 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02002654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002656 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2658 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Gilles Peskinef670ba52025-03-07 15:09:32 +01002660 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2663 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2664 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
2666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670}
2671
Gilles Peskineeccd8882020-03-10 12:19:08 +01002672#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002673MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002674static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002675{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002676 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002677 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00002678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2683 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2684 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002685 }
2686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2688 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002689 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02002691 }
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2694 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002695}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002696#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002697MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002698static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002701 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002702 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002703 size_t n = 0, offset = 0;
2704 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002705 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002707 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002708 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02002709#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002710 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02002711#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002712 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 +02002713#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002716
Gilles Peskineeccd8882020-03-10 12:19:08 +01002717#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 if (ssl->handshake->ecrs_enabled &&
2719 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002720 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002721 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002722#endif
2723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2725 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2726 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002727 }
2728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2730 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002731 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002733 }
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if (ssl->handshake->client_auth == 0 ||
2736 mbedtls_ssl_own_cert(ssl) == NULL) {
2737 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002738 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 }
2741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 if (mbedtls_ssl_own_key(ssl) == NULL) {
2743 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
2744 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 }
2746
2747 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002748 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002750#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002752 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002754
2755sign:
2756#endif
2757
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002758 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
2759 if (0 != ret) {
2760 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
2761 return ret;
2762 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
Ronald Cron90915f22022-03-07 11:11:36 +01002764 /*
2765 * digitally-signed struct {
2766 * opaque handshake_messages[handshake_messages_length];
2767 * };
2768 *
2769 * Taking shortcut here. We assume that the server always allows the
2770 * PRF Hash function and has sent it in the allowed signature
2771 * algorithms list received in the Certificate Request message.
2772 *
2773 * Until we encounter a server that does not, we will take this
2774 * shortcut.
2775 *
2776 * Reason: Otherwise we should have running hashes for SHA512 and
2777 * SHA224 in order to satisfy 'weird' needs from the server
2778 * side.
2779 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01002781 md_alg = MBEDTLS_MD_SHA384;
2782 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01002784 md_alg = MBEDTLS_MD_SHA256;
2785 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02002786 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01002788
2789 /* Info from md_alg will be used instead */
2790 hashlen = 0;
2791 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002792
Gilles Peskineeccd8882020-03-10 12:19:08 +01002793#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002795 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002797#endif
2798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
2800 md_alg, hash_start, hashlen,
2801 ssl->out_msg + 6 + offset,
2802 out_buf_len - 6 - offset,
2803 &n,
Ben Taylor440cb2a2025-03-05 09:40:08 +00002804 rs_ctx)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002806#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002808 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002810#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002812 }
Paul Bakker926af752012-11-23 13:38:07 +01002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2818 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Gilles Peskinef670ba52025-03-07 15:09:32 +01002820 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2823 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2824 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 }
2826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002830}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002831#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002834MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002835static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002836{
Janos Follath865b3eb2019-12-16 11:46:15 +00002837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002838 uint32_t lifetime;
2839 size_t ticket_len;
2840 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002841 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2846 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2847 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002848 }
2849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2851 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002852 mbedtls_ssl_send_alert_message(
2853 ssl,
2854 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2856 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002857 }
2858
2859 /*
2860 * struct {
2861 * uint32 ticket_lifetime_hint;
2862 * opaque ticket<0..2^16-1>;
2863 * } NewSessionTicket;
2864 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002865 * 0 . 3 ticket_lifetime_hint
2866 * 4 . 5 ticket_len (n)
2867 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002868 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
2870 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
2871 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
2872 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2873 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2874 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002875 }
2876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002878
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002879 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002880
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002881 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
2884 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
2885 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2886 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2887 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002888 }
2889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002891
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002892 /* We're not waiting for a NewSessionTicket message any more */
2893 ssl->handshake->new_session_ticket = 0;
Gilles Peskinef670ba52025-03-07 15:09:32 +01002894 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002895
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002896 /*
2897 * Zero-length ticket means the server changed his mind and doesn't want
2898 * to send a ticket after all, so just forget it
2899 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 if (ticket_len == 0) {
2901 return 0;
2902 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002905 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00002907 ssl->session->ticket = NULL;
2908 ssl->session->ticket_len = 0;
2909 }
2910
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002911 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002913 ssl->session_negotiate->ticket = NULL;
2914 ssl->session_negotiate->ticket_len = 0;
2915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
2917 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
2918 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2919 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
2920 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002921 }
2922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002924
2925 ssl->session_negotiate->ticket = ticket;
2926 ssl->session_negotiate->ticket_len = ticket_len;
2927 ssl->session_negotiate->ticket_lifetime = lifetime;
2928
2929 /*
2930 * RFC 5077 section 3.4:
2931 * "If the client receives a session ticket from the server, then it
2932 * discards any Session ID that was sent in the ServerHello."
2933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002935 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002942
Paul Bakker5121ce52009-01-03 21:22:43 +00002943/*
Paul Bakker1961b702013-01-25 14:49:24 +01002944 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002946int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002947{
2948 int ret = 0;
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002951 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
2954 ssl->handshake->new_session_ticket != 0) {
Gilles Peskinef670ba52025-03-07 15:09:32 +01002955 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_NEW_SESSION_TICKET);
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002956 }
2957#endif
2958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskinef670ba52025-03-07 15:09:32 +01002961 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 break;
2963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 /*
2965 * ==> ClientHello
2966 */
2967 case MBEDTLS_SSL_CLIENT_HELLO:
2968 ret = mbedtls_ssl_write_client_hello(ssl);
2969 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 /*
2972 * <== ServerHello
2973 * Certificate
2974 * ( ServerKeyExchange )
2975 * ( CertificateRequest )
2976 * ServerHelloDone
2977 */
2978 case MBEDTLS_SSL_SERVER_HELLO:
2979 ret = ssl_parse_server_hello(ssl);
2980 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 case MBEDTLS_SSL_SERVER_CERTIFICATE:
2983 ret = mbedtls_ssl_parse_certificate(ssl);
2984 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
2987 ret = ssl_parse_server_key_exchange(ssl);
2988 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2991 ret = ssl_parse_certificate_request(ssl);
2992 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 case MBEDTLS_SSL_SERVER_HELLO_DONE:
2995 ret = ssl_parse_server_hello_done(ssl);
2996 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 /*
2999 * ==> ( Certificate/Alert )
3000 * ClientKeyExchange
3001 * ( CertificateVerify )
3002 * ChangeCipherSpec
3003 * Finished
3004 */
3005 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3006 ret = mbedtls_ssl_write_certificate(ssl);
3007 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3010 ret = ssl_write_client_key_exchange(ssl);
3011 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3014 ret = ssl_write_certificate_verify(ssl);
3015 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3018 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3019 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 case MBEDTLS_SSL_CLIENT_FINISHED:
3022 ret = mbedtls_ssl_write_finished(ssl);
3023 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 /*
3026 * <== ( NewSessionTicket )
3027 * ChangeCipherSpec
3028 * Finished
3029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3032 ret = ssl_parse_new_session_ticket(ssl);
3033 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003034#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3037 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3038 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 case MBEDTLS_SSL_SERVER_FINISHED:
3041 ret = mbedtls_ssl_parse_finished(ssl);
3042 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 case MBEDTLS_SSL_FLUSH_BUFFERS:
3045 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01003046 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3050 mbedtls_ssl_handshake_wrapup(ssl);
3051 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 default:
3054 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3055 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003059}
Jerry Yuc5aef882021-12-23 20:15:02 +08003060
Jerry Yufb4b6472022-01-27 15:03:26 +08003061#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */