blob: 2129da122d692a45a87b2ef3eeea9935c81bc17b [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;
Gilles Peskinec4949d12025-05-27 19:45:29 +0200872 const char *const *p;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200873
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) {
Ari Weiler-Ofek67aa9592025-06-10 16:59:44 +01001772 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not supported",
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) {
Ben Taylor1c118a52025-08-05 10:40:08 +01002085 ret = mbedtls_pk_verify_new(pk_alg, peer_pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 md_alg, hash, hashlen,
2087 p, sig_len);
2088 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002089#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 ret = mbedtls_pk_verify_restartable(peer_pk,
2091 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002094 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002095#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002097#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002099 mbedtls_ssl_send_alert_message(
2100 ssl,
2101 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2103 }
2104 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002105#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002107 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002109#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002111 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002112
2113#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2114 /* We don't need the peer's public key anymore. Free it,
2115 * so that more RAM is available for upcoming expensive
2116 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002118#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002120#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002122exit:
Gilles Peskinef670ba52025-03-07 15:09:32 +01002123 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002128}
2129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002131MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002132static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002133{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002134 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002135 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2140 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002141 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002143 }
2144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2146 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002147}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002148#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002149MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002150static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002151{
Janos Follath865b3eb2019-12-16 11:46:15 +00002152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002153 unsigned char *buf;
2154 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002155 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002156 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002157 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002158 size_t sig_alg_len;
2159#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 unsigned char *sig_alg;
2161 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002162#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2167 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002168 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002170 }
2171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2173 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2174 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 }
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2178 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002179 mbedtls_ssl_send_alert_message(
2180 ssl,
2181 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2183 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
Gilles Peskinef670ba52025-03-07 15:09:32 +01002186 mbedtls_ssl_handshake_increment_state(ssl);
Jerry Yufb28b882022-01-28 11:05:58 +08002187 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2191 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002194 /* Current message is probably the ServerHelloDone */
2195 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002196 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002197 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002198
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002199 /*
2200 * struct {
2201 * ClientCertificateType certificate_types<1..2^8-1>;
2202 * SignatureAndHashAlgorithm
2203 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2204 * DistinguishedName certificate_authorities<0..2^16-1>;
2205 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002206 *
2207 * Since we only support a single certificate on clients, let's just
2208 * ignore all the information that's supposed to help us pick a
2209 * certificate.
2210 *
2211 * We could check that our certificate matches the request, and bail out
2212 * if it doesn't, but it's simpler to just send the certificate anyway,
2213 * and give the server the opportunity to decide if it should terminate
2214 * the connection when it doesn't like our certificate.
2215 *
2216 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2217 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002218 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002219 *
2220 * However, we still minimally parse the message to check it is at least
2221 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002222 */
Paul Bakker926af752012-11-23 13:38:07 +01002223 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002224
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002225 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2227 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2228 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2229 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2230 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002231 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002233 n = cert_type_len;
2234
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002235 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002236 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002237 * * the length of the signature algorithms field (if minor version of
2238 * SSL is 3),
2239 * * distinguished name length otherwise.
2240 * Both reach at most the index:
2241 * ...hdr_len + 2 + n,
2242 * therefore the buffer length at this point must be greater than that
2243 * regardless of the actual code path.
2244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2246 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2247 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2248 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2249 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002250 }
2251
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002252 /* supported_signature_algorithms */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002253 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002254
2255 /*
2256 * The furthest access in buf is in the loop few lines below:
2257 * sig_alg[i + 1],
2258 * where:
2259 * sig_alg = buf + ...hdr_len + 3 + n,
2260 * max(i) = sig_alg_len - 1.
2261 * Therefore the furthest access is:
2262 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2263 * which reduces to:
2264 * buf[...hdr_len + 3 + n + sig_alg_len],
2265 * which is one less than we need the buf to be.
2266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2268 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002269 mbedtls_ssl_send_alert_message(
2270 ssl,
2271 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2273 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002274 }
Paul Bakker926af752012-11-23 13:38:07 +01002275
Ronald Cron90915f22022-03-07 11:11:36 +01002276#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2278 for (size_t i = 0; i < sig_alg_len; i += 2) {
2279 MBEDTLS_SSL_DEBUG_MSG(3,
2280 ("Supported Signature Algorithm found: %02x %02x",
2281 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002282 }
2283#endif
2284
2285 n += 2 + sig_alg_len;
2286
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002287 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002288 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002289
2290 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2292 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2293 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2294 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2295 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002296 }
2297
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002298#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2300 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002301 unsigned char *p = dn + i + 2;
2302 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002303 size_t asn1_len;
2304 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 memset(&name, 0, sizeof(name));
2306 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2307 if (dni_len > dn_len - i - 2 ||
2308 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2309 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2310 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2311 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002312 mbedtls_ssl_send_alert_message(
2313 ssl,
2314 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2316 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002317 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_SSL_DEBUG_MSG(3,
2319 ("DN hint: %.*s",
2320 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2321 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002322 }
2323#endif
2324
Paul Bakker926af752012-11-23 13:38:07 +01002325exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002329}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002330#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002332MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002333static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002334{
Janos Follath865b3eb2019-12-16 11:46:15 +00002335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2340 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2341 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2345 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2346 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2350 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2351 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2352 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2353 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2354 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 }
2356
Gilles Peskinef670ba52025-03-07 15:09:32 +01002357 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2361 mbedtls_ssl_recv_flight_completed(ssl);
2362 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002363#endif
2364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002368}
2369
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002370MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002371static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002372{
Janos Follath865b3eb2019-12-16 11:46:15 +00002373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002374
2375 size_t header_len;
2376 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002377 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002378 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002382#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2383 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2384 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2385 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002387 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2388 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Andrzej Kureka0237f82022-02-24 13:24:52 -05002390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2391 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002392 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002393
2394 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2395
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002396 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002399
Hanno Becker4a63ed42019-01-08 11:39:35 +00002400 /*
2401 * Generate EC private key for ECDHE exchange.
2402 */
2403
Hanno Becker4a63ed42019-01-08 11:39:35 +00002404 /* The master secret is obtained from the shared ECDH secret by
2405 * applying the TLS 1.2 PRF with a specific salt and label. While
2406 * the PSA Crypto API encourages combining key agreement schemes
2407 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2408 * yet support the provisioning of salt + label to the KDF.
2409 * For the time being, we therefore need to split the computation
2410 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002411 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2413 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002414 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002415 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002416
2417 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002419 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (status != PSA_SUCCESS) {
2421 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2422 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002423
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002424 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002425 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002426 * but we just need to add a length byte before that. */
2427 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2428 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002430 size_t own_pubkey_len;
2431
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002432 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 own_pubkey, own_pubkey_max_len,
2434 &own_pubkey_len);
2435 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002436 psa_destroy_key(handshake->xxdh_psa_privkey);
2437 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002439 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002440
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002441 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2442 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002443
Hanno Becker4a63ed42019-01-08 11:39:35 +00002444 /* The ECDH secret is the premaster secret used for key derivation. */
2445
Janos Follathdf3b0892019-08-08 11:12:24 +01002446 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002448 handshake->xxdh_psa_privkey,
2449 handshake->xxdh_psa_peerkey,
2450 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 ssl->handshake->premaster,
2452 sizeof(ssl->handshake->premaster),
2453 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002454
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002455 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2456 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2459 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2460 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2463 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2464 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2465 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardc7403ed2025-01-23 11:24:18 +01002466#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2469 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2470 psa_key_attributes_t key_attributes;
2471
2472 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2473
2474 /*
2475 * opaque psk_identity<0..2^16-1>;
2476 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002478 /* We don't offer PSK suites if we don't have a PSK,
2479 * and we check that the server's choice is among the
2480 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2482 }
Neil Armstrong868af822022-03-09 10:26:25 +01002483
Neil Armstrongfc834f22022-03-23 17:54:38 +01002484 /* uint16 to store content length */
2485 const size_t content_len_size = 2;
2486
Neil Armstrong868af822022-03-09 10:26:25 +01002487 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 if (header_len + content_len_size + ssl->conf->psk_identity_len
2490 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2491 MBEDTLS_SSL_DEBUG_MSG(1,
2492 ("psk identity too long or SSL buffer too short"));
2493 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002494 }
2495
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002496 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2499 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002500 header_len += content_len_size;
2501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 memcpy(p, ssl->conf->psk_identity,
2503 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002504 p += ssl->conf->psk_identity_len;
2505
Neil Armstrong868af822022-03-09 10:26:25 +01002506 header_len += ssl->conf->psk_identity_len;
2507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002509
2510 /*
2511 * Generate EC private key for ECDHE exchange.
2512 */
2513
2514 /* The master secret is obtained from the shared ECDH secret by
2515 * applying the TLS 1.2 PRF with a specific salt and label. While
2516 * the PSA Crypto API encourages combining key agreement schemes
2517 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2518 * yet support the provisioning of salt + label to the KDF.
2519 * For the time being, we therefore need to split the computation
2520 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2521 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2523 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002524 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002525 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002526
2527 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002529 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002531 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 }
Neil Armstrong868af822022-03-09 10:26:25 +01002533
2534 /* Export the public part of the ECDH private key from PSA.
2535 * The export format is an ECPoint structure as expected by TLS,
2536 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002537 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002538 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002540 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002541
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002542 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 own_pubkey, own_pubkey_max_len,
2544 &own_pubkey_len);
2545 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002546 psa_destroy_key(handshake->xxdh_psa_privkey);
2547 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002548 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002549 }
2550
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002551 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002552 content_len = own_pubkey_len + 1;
2553
Neil Armstrong25400452022-03-23 17:44:07 +01002554 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2555 * - a uint16 containing the length (in octets) of the ECDH computation
2556 * - the octet string produced by the ECDH computation
2557 * - a uint16 containing the length (in octets) of the PSK
2558 * - the PSK itself
2559 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002560 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 const unsigned char * const pms_end = pms +
2562 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002563 /* uint16 to store length (in octets) of the ECDH computation */
2564 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002565 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002566
Neil Armstrong25400452022-03-23 17:44:07 +01002567 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002569 handshake->xxdh_psa_privkey,
2570 handshake->xxdh_psa_peerkey,
2571 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 pms + zlen_size,
2573 pms_end - (pms + zlen_size),
2574 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01002575
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002576 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2577 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002580 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002582 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 }
Neil Armstrong868af822022-03-09 10:26:25 +01002584
Neil Armstrong25400452022-03-23 17:44:07 +01002585 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002587 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 } else
Manuel Pégourié-Gonnardc7403ed2025-01-23 11:24:18 +01002589#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002590#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002592 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002593 * opaque psk_identity<0..2^16-1>;
2594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01002596 /* We don't offer PSK suites if we don't have a PSK,
2597 * and we check that the server's choice is among the
2598 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02002600 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002601
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002602 header_len = 4;
2603 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2606 MBEDTLS_SSL_DEBUG_MSG(1,
2607 ("psk identity too long or SSL buffer too short"));
2608 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002609 }
2610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
2612 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 memcpy(ssl->out_msg + header_len,
2615 ssl->conf->psk_identity,
2616 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002617 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002621 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002623#endif
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002624 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2626 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002627 }
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01002630#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002631#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002633 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002634
Neil Armstrongca7d5062022-05-31 14:43:23 +02002635 unsigned char *out_p = ssl->out_msg + header_len;
2636 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
2637 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
2639 out_p, end_p - out_p, &content_len,
2640 MBEDTLS_ECJPAKE_ROUND_TWO);
2641 if (ret != 0) {
2642 psa_destroy_key(ssl->handshake->psa_pake_password);
2643 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2644 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
2645 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002646 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 } else
Gabor Mezeie1e27302025-02-26 18:06:05 +01002648#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002649 {
2650 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2652 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02002653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002655 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2657 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Gilles Peskinef670ba52025-03-07 15:09:32 +01002659 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2662 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2663 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 }
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002669}
2670
Gilles Peskineeccd8882020-03-10 12:19:08 +01002671#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002672MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002673static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002674{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002675 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002676 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00002677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2682 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2683 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002684 }
2685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2687 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002688 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02002690 }
2691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2693 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002694}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002695#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002696MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002697static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002700 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002701 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002702 size_t n = 0, offset = 0;
2703 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002704 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002706 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002707 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02002708#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002709 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02002710#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002711 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 +02002712#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002715
Gilles Peskineeccd8882020-03-10 12:19:08 +01002716#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 if (ssl->handshake->ecrs_enabled &&
2718 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002719 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002720 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002721#endif
2722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2724 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2725 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002726 }
2727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2729 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002730 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002732 }
2733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 if (ssl->handshake->client_auth == 0 ||
2735 mbedtls_ssl_own_cert(ssl) == NULL) {
2736 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002737 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 }
2740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 if (mbedtls_ssl_own_key(ssl) == NULL) {
2742 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
2743 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 }
2745
2746 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002747 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002749#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002751 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002753
2754sign:
2755#endif
2756
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002757 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
2758 if (0 != ret) {
2759 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
2760 return ret;
2761 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Ronald Cron90915f22022-03-07 11:11:36 +01002763 /*
2764 * digitally-signed struct {
2765 * opaque handshake_messages[handshake_messages_length];
2766 * };
2767 *
2768 * Taking shortcut here. We assume that the server always allows the
2769 * PRF Hash function and has sent it in the allowed signature
2770 * algorithms list received in the Certificate Request message.
2771 *
2772 * Until we encounter a server that does not, we will take this
2773 * shortcut.
2774 *
2775 * Reason: Otherwise we should have running hashes for SHA512 and
2776 * SHA224 in order to satisfy 'weird' needs from the server
2777 * side.
2778 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01002780 md_alg = MBEDTLS_MD_SHA384;
2781 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01002783 md_alg = MBEDTLS_MD_SHA256;
2784 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02002785 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01002787
2788 /* Info from md_alg will be used instead */
2789 hashlen = 0;
2790 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002791
Gilles Peskineeccd8882020-03-10 12:19:08 +01002792#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002794 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002796#endif
2797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
2799 md_alg, hash_start, hashlen,
2800 ssl->out_msg + 6 + offset,
2801 out_buf_len - 6 - offset,
2802 &n,
Ben Taylor440cb2a2025-03-05 09:40:08 +00002803 rs_ctx)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002805#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002807 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002809#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002811 }
Paul Bakker926af752012-11-23 13:38:07 +01002812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Paul Bakker1ef83d62012-04-11 12:09:53 +00002815 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2817 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002818
Gilles Peskinef670ba52025-03-07 15:09:32 +01002819 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2822 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2823 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 }
2825
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002829}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002830#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002833MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002834static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002835{
Janos Follath865b3eb2019-12-16 11:46:15 +00002836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002837 uint32_t lifetime;
2838 size_t ticket_len;
2839 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002840 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2845 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2846 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002847 }
2848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2850 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002851 mbedtls_ssl_send_alert_message(
2852 ssl,
2853 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2855 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002856 }
2857
2858 /*
2859 * struct {
2860 * uint32 ticket_lifetime_hint;
2861 * opaque ticket<0..2^16-1>;
2862 * } NewSessionTicket;
2863 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002864 * 0 . 3 ticket_lifetime_hint
2865 * 4 . 5 ticket_len (n)
2866 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002867 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
2869 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
2870 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
2871 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2872 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2873 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002874 }
2875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002877
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002878 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002879
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002880 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
2883 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
2884 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2885 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2886 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002887 }
2888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002890
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002891 /* We're not waiting for a NewSessionTicket message any more */
2892 ssl->handshake->new_session_ticket = 0;
Gilles Peskinef670ba52025-03-07 15:09:32 +01002893 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002894
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002895 /*
2896 * Zero-length ticket means the server changed his mind and doesn't want
2897 * to send a ticket after all, so just forget it
2898 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 if (ticket_len == 0) {
2900 return 0;
2901 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002904 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00002906 ssl->session->ticket = NULL;
2907 ssl->session->ticket_len = 0;
2908 }
2909
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002910 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002912 ssl->session_negotiate->ticket = NULL;
2913 ssl->session_negotiate->ticket_len = 0;
2914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
2916 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
2917 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2918 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
2919 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002920 }
2921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002923
2924 ssl->session_negotiate->ticket = ticket;
2925 ssl->session_negotiate->ticket_len = ticket_len;
2926 ssl->session_negotiate->ticket_lifetime = lifetime;
2927
2928 /*
2929 * RFC 5077 section 3.4:
2930 * "If the client receives a session ticket from the server, then it
2931 * discards any Session ID that was sent in the ServerHello."
2932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002934 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002937
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002939}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002941
Paul Bakker5121ce52009-01-03 21:22:43 +00002942/*
Paul Bakker1961b702013-01-25 14:49:24 +01002943 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002945int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002946{
2947 int ret = 0;
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002950 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
2953 ssl->handshake->new_session_ticket != 0) {
Gilles Peskinef670ba52025-03-07 15:09:32 +01002954 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_NEW_SESSION_TICKET);
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002955 }
2956#endif
2957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskinef670ba52025-03-07 15:09:32 +01002960 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 break;
2962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 /*
2964 * ==> ClientHello
2965 */
2966 case MBEDTLS_SSL_CLIENT_HELLO:
2967 ret = mbedtls_ssl_write_client_hello(ssl);
2968 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 /*
2971 * <== ServerHello
2972 * Certificate
2973 * ( ServerKeyExchange )
2974 * ( CertificateRequest )
2975 * ServerHelloDone
2976 */
2977 case MBEDTLS_SSL_SERVER_HELLO:
2978 ret = ssl_parse_server_hello(ssl);
2979 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 case MBEDTLS_SSL_SERVER_CERTIFICATE:
2982 ret = mbedtls_ssl_parse_certificate(ssl);
2983 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
2986 ret = ssl_parse_server_key_exchange(ssl);
2987 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2990 ret = ssl_parse_certificate_request(ssl);
2991 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 case MBEDTLS_SSL_SERVER_HELLO_DONE:
2994 ret = ssl_parse_server_hello_done(ssl);
2995 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 /*
2998 * ==> ( Certificate/Alert )
2999 * ClientKeyExchange
3000 * ( CertificateVerify )
3001 * ChangeCipherSpec
3002 * Finished
3003 */
3004 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3005 ret = mbedtls_ssl_write_certificate(ssl);
3006 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3009 ret = ssl_write_client_key_exchange(ssl);
3010 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3013 ret = ssl_write_certificate_verify(ssl);
3014 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3017 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3018 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 case MBEDTLS_SSL_CLIENT_FINISHED:
3021 ret = mbedtls_ssl_write_finished(ssl);
3022 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 /*
3025 * <== ( NewSessionTicket )
3026 * ChangeCipherSpec
3027 * Finished
3028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3031 ret = ssl_parse_new_session_ticket(ssl);
3032 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003033#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3036 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3037 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 case MBEDTLS_SSL_SERVER_FINISHED:
3040 ret = mbedtls_ssl_parse_finished(ssl);
3041 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 case MBEDTLS_SSL_FLUSH_BUFFERS:
3044 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01003045 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3049 mbedtls_ssl_handshake_wrapup(ssl);
3050 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 default:
3053 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3054 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003058}
Jerry Yuc5aef882021-12-23 20:15:02 +08003059
Jerry Yufb4b6472022-01-27 15:03:26 +08003060#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */