blob: 820cab17a816ab6e931c001e4fedad2ae1ba6ab6 [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"
Ben Taylor04b03d72025-07-14 09:46:18 +010022#pragma GCC diagnostic warning "-Wenum-conversion"
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040023#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Andrzej Kurek00644842023-05-30 05:45:00 -040024/* Define a local translating function to save code size by not using too many
25 * arguments in each translating place. */
26static int local_err_translation(psa_status_t status)
27{
28 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040029 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040030 psa_generic_status_to_mbedtls);
31}
32#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040033#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +000034
SimonBd5800b72016-04-26 07:43:27 +010035#include <string.h>
36
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020037#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010040#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020041#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050044#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020045#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020048MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010049static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
50 unsigned char *buf,
51 const unsigned char *end,
52 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010053{
54 unsigned char *p = buf;
55
56 *olen = 0;
57
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010058 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010059 * initial ClientHello, in which case also adding the renegotiation
60 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
62 return 0;
63 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010064
Gilles Peskine449bd832023-01-11 14:50:10 +010065 MBEDTLS_SSL_DEBUG_MSG(3,
66 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010069
Paul Bakkerd3edc862013-03-20 16:07:17 +010070 /*
71 * Secure renegotiation
72 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010074 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010075
76 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010077 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
78 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010081
82 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010087
Valerio Setti7aeec542023-07-05 18:57:21 +020088#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +020089 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010090 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010091
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020092MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010093static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
94 unsigned char *buf,
95 const unsigned char *end,
96 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010097{
98 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +010099 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100100
101 *olen = 0;
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 MBEDTLS_SSL_DEBUG_MSG(3,
104 ("client hello, adding supported_point_formats extension"));
105 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100108 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100109
110 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100111 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200112
113 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100115
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200116 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100119}
Valerio Setti7aeec542023-07-05 18:57:21 +0200120#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200121 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200122 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100123
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200124#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200125MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100126static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
127 unsigned char *buf,
128 const unsigned char *end,
129 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200130{
Janos Follath865b3eb2019-12-16 11:46:15 +0000131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200132 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100133 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200134
135 *olen = 0;
136
137 /* Skip costly extension if we can't use EC J-PAKE anyway */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
139 return 0;
140 }
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 MBEDTLS_SSL_DEBUG_MSG(3,
143 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100148 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200149
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200150 /*
151 * We may need to send ClientHello multiple times for Hello verification.
152 * We don't want to compute fresh values every time (both for performance
153 * and consistency reasons), so cache the extension content.
154 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (ssl->handshake->ecjpake_cache == NULL ||
156 ssl->handshake->ecjpake_cache_len == 0) {
157 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200158
Valerio Setti6b3dab02022-11-17 17:14:54 +0100159 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 p + 2, end - p - 2, &kkpp_len,
161 MBEDTLS_ECJPAKE_ROUND_ONE);
162 if (ret != 0) {
163 psa_destroy_key(ssl->handshake->psa_pake_password);
164 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
165 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
166 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200167 }
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
170 if (ssl->handshake->ecjpake_cache == NULL) {
171 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
172 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200176 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 } else {
178 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200179
180 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200184 }
185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100187 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200188
189 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200192}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200193#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000194
Hanno Beckera0e20d02019-05-15 14:03:01 +0100195#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200196MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100197static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
198 unsigned char *buf,
199 const unsigned char *end,
200 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100201{
202 unsigned char *p = buf;
203 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100204
205 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100206 * struct {
207 * opaque cid<0..2^8-1>;
208 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100210
211 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
213 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
214 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100215 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100217
218 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
219 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100221
222 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100224 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100225 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100227 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100228
229 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100231
232 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100235}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100236#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200239MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100240static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
241 unsigned char *buf,
242 const unsigned char *end,
243 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200244{
245 unsigned char *p = buf;
246
Simon Butcher0fc94e92015-09-28 20:52:04 +0100247 *olen = 0;
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
250 return 0;
251 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_SSL_DEBUG_MSG(3,
254 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100259 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200260
261 *p++ = 0x00;
262 *p++ = 1;
263
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200264 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200265
266 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200273MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100274static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
275 unsigned char *buf,
276 const unsigned char *end,
277 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100278{
279 unsigned char *p = buf;
280
Simon Butcher0fc94e92015-09-28 20:52:04 +0100281 *olen = 0;
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
284 return 0;
285 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 MBEDTLS_SSL_DEBUG_MSG(3,
288 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100293 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100294
295 *p++ = 0x00;
296 *p++ = 0x00;
297
298 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200305MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100306static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
307 unsigned char *buf,
308 const unsigned char *end,
309 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200310{
311 unsigned char *p = buf;
312
Simon Butcher0fc94e92015-09-28 20:52:04 +0100313 *olen = 0;
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
316 return 0;
317 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 MBEDTLS_SSL_DEBUG_MSG(3,
320 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100325 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200326
327 *p++ = 0x00;
328 *p++ = 0x00;
329
330 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200337MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100338static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
339 unsigned char *buf,
340 const unsigned char *end,
341 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200342{
343 unsigned char *p = buf;
344 size_t tlen = ssl->session_negotiate->ticket_len;
345
Simon Butcher0fc94e92015-09-28 20:52:04 +0100346 *olen = 0;
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
349 return 0;
350 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 MBEDTLS_SSL_DEBUG_MSG(3,
353 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200354
Hanno Becker261602c2017-04-12 14:54:42 +0100355 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100359 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100362 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200363
364 *olen = 4;
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
367 return 0;
368 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 MBEDTLS_SSL_DEBUG_MSG(3,
371 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200374
375 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200380
Ron Eldora9788042018-12-05 11:04:31 +0200381#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200382MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100383static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
384 unsigned char *buf,
385 const unsigned char *end,
386 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100387{
388 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200389 size_t protection_profiles_index = 0, ext_len = 0;
390 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100391
392 *olen = 0;
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
395 (ssl->conf->dtls_srtp_profile_list == NULL) ||
396 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
397 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100398 }
399
Ron Eldora9788042018-12-05 11:04:31 +0200400 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100401 * uint8 SRTPProtectionProfile[2];
402 *
403 * struct {
404 * SRTPProtectionProfiles SRTPProtectionProfiles;
405 * opaque srtp_mki<0..255>;
406 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100407 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100408 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200410 mki_len = ssl->dtls_srtp_info.mki_len;
411 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300412 /* Extension length = 2 bytes for profiles length,
413 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
414 * 1 byte for srtp_mki vector length and the mki_len value
415 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200419
420 /* Check there is room in the buffer for the extension + 4 bytes
421 * - the extension tag (2 bytes)
422 * - the extension length (2 bytes)
423 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100427 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100430 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100431
Ron Eldor3adb9922017-12-21 10:15:08 +0200432 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200433 /* micro-optimization:
434 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
435 * which is lower than 127, so the upper byte of the length is always 0
436 * For the documentation, the more generic code is left in comments
437 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
438 * >> 8 ) & 0xFF );
439 */
440 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300444 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200446 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
448 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
449 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
450 profile_value));
451 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100452 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200454 /*
455 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200456 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200457 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 MBEDTLS_SSL_DEBUG_MSG(3,
459 ("client hello, "
460 "illegal DTLS-SRTP protection profile %d",
461 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
462 ));
463 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100464 }
465 }
466
Ron Eldor591f1622018-01-22 12:30:04 +0200467 *p++ = mki_len & 0xFF;
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (mki_len != 0) {
470 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200471 /*
472 * Increment p to point to the current position.
473 */
474 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
476 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200477 }
478
Ron Eldoref72faf2018-07-12 11:54:20 +0300479 /*
480 * total extension length: extension type (2 bytes)
481 * + extension length (2 bytes)
482 * + protection profile length (2 bytes)
483 * + 2 * number of protection profiles
484 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200485 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300486 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200487 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100490}
491#endif /* MBEDTLS_SSL_DTLS_SRTP */
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
494 unsigned char *buf,
495 const unsigned char *end,
496 int uses_ec,
497 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100498{
499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
500 unsigned char *p = buf;
501 size_t ext_len = 0;
502
503 (void) ssl;
504 (void) end;
505 (void) uses_ec;
506 (void) ret;
507 (void) ext_len;
508
509 *out_len = 0;
510
511 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
512 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
513#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
515 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
516 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100517 }
518 p += ext_len;
519#endif
520
Valerio Setti7aeec542023-07-05 18:57:21 +0200521#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200522 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Ronald Cron12dcdf02022-02-16 15:28:22 +0100523 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (uses_ec) {
525 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
526 &ext_len)) != 0) {
527 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
528 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100529 }
530 p += ext_len;
531 }
532#endif
533
534#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
536 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
537 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100538 }
539 p += ext_len;
540#endif
541
542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
544 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
545 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100546 }
547 p += ext_len;
548#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
549
550#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
552 &ext_len)) != 0) {
553 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
554 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100555 }
556 p += ext_len;
557#endif
558
559#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
561 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
562 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100563 }
564 p += ext_len;
565#endif
566
567#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
569 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
570 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100571 }
572 p += ext_len;
573#endif
574
575#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
577 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
578 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100579 }
580 p += ext_len;
581#endif
582
583#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
585 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
586 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100587 }
588 p += ext_len;
589#endif
590
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000591 *out_len = (size_t) (p - buf);
Ronald Cron12dcdf02022-02-16 15:28:22 +0100592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100594}
595
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200596MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100597static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
598 const unsigned char *buf,
599 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100603 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000605 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 mbedtls_ct_memcmp(buf + 1,
607 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
608 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
609 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
610 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100611 mbedtls_ssl_send_alert_message(
612 ssl,
613 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
615 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000616 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100619 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (len != 1 || buf[0] != 0x00) {
621 MBEDTLS_SSL_DEBUG_MSG(1,
622 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100623 mbedtls_ssl_send_alert_message(
624 ssl,
625 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
627 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100628 }
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100631 }
Paul Bakker48916f92012-09-16 19:57:18 +0000632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000634}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200637MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100638static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
639 const unsigned char *buf,
640 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200641{
642 /*
643 * server should use the extension only if we did,
644 * and if so the server's value should match ours (and len is always 1)
645 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200647 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 buf[0] != ssl->conf->mfl_code) {
649 MBEDTLS_SSL_DEBUG_MSG(1,
650 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100651 mbedtls_ssl_send_alert_message(
652 ssl,
653 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
655 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200656 }
657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000661
Hanno Beckera0e20d02019-05-15 14:03:01 +0100662#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200663MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100664static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
665 const unsigned char *buf,
666 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100667{
668 size_t peer_cid_len;
669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100671 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
672 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
674 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
675 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
676 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
677 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100678 }
679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (len == 0) {
681 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
682 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
683 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
684 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100685 }
686
687 peer_cid_len = *buf++;
688 len--;
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
691 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
692 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
693 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
694 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100695 }
696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (len != peer_cid_len) {
698 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
699 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
700 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
701 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100702 }
703
Hanno Becker5a299902019-05-03 12:47:49 +0100704 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100705 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
709 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100712}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100713#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200716MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100717static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
718 const unsigned char *buf,
719 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100720{
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
722 len != 0) {
723 MBEDTLS_SSL_DEBUG_MSG(1,
724 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100725 mbedtls_ssl_send_alert_message(
726 ssl,
727 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
729 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100730 }
731
732 ((void) buf);
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100737}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200741MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100742static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
743 const unsigned char *buf,
744 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200745{
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
747 len != 0) {
748 MBEDTLS_SSL_DEBUG_MSG(1,
749 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100750 mbedtls_ssl_send_alert_message(
751 ssl,
752 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
754 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200755 }
756
757 ((void) buf);
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200766MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100767static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
768 const unsigned char *buf,
769 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200770{
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
772 len != 0) {
773 MBEDTLS_SSL_DEBUG_MSG(1,
774 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100775 mbedtls_ssl_send_alert_message(
776 ssl,
777 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
779 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200780 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200781
782 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200783
784 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200789
Valerio Setti7aeec542023-07-05 18:57:21 +0200790#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200791 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100792 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200793MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100794static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
795 const unsigned char *buf,
796 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200797{
798 size_t list_size;
799 const unsigned char *p;
800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (len == 0 || (size_t) (buf[0] + 1) != len) {
802 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
803 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
804 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
805 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200806 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200807 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200808
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200809 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 while (list_size > 0) {
811 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
812 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
814 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200815 }
816
817 list_size--;
818 p++;
819 }
820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
822 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
823 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
824 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200825}
Valerio Setti7aeec542023-07-05 18:57:21 +0200826#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200827 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200828 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200829
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200830#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200831MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100832static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
833 const unsigned char *buf,
834 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200835{
Janos Follath865b3eb2019-12-16 11:46:15 +0000836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (ssl->handshake->ciphersuite_info->key_exchange !=
839 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
840 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
841 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200842 }
843
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200844 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200846 ssl->handshake->ecjpake_cache = NULL;
847 ssl->handshake->ecjpake_cache_len = 0;
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if ((ret = mbedtls_psa_ecjpake_read_round(
850 &ssl->handshake->psa_pake_ctx, buf, len,
851 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
852 psa_destroy_key(ssl->handshake->psa_pake_password);
853 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100856 mbedtls_ssl_send_alert_message(
857 ssl,
858 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
860 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200861 }
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200864}
865#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200868MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100869static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
870 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200871{
872 size_t list_len, name_len;
Gilles Peskinec4949d12025-05-27 19:45:29 +0200873 const char *const *p;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200874
875 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (ssl->conf->alpn_list == NULL) {
877 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100878 mbedtls_ssl_send_alert_message(
879 ssl,
880 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
882 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200883 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200884
885 /*
886 * opaque ProtocolName<1..2^8-1>;
887 *
888 * struct {
889 * ProtocolName protocol_name_list<2..2^16-1>
890 * } ProtocolNameList;
891 *
892 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
893 */
894
895 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (len < 4) {
897 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
898 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
899 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200900 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200901
Dave Rodgmana3d0f612023-11-03 23:34:02 +0000902 list_len = MBEDTLS_GET_UINT16_BE(buf, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (list_len != len - 2) {
904 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
905 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
906 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200907 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200908
909 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (name_len != list_len - 1) {
911 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
912 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
913 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200914 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200915
916 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
918 if (name_len == strlen(*p) &&
919 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200920 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200922 }
923 }
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
926 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
927 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
928 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200929}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200931
Johan Pascalb62bb512015-12-03 21:56:45 +0100932#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200933MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100934static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
935 const unsigned char *buf,
936 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100937{
Johan Pascal43f94902020-09-22 12:25:52 +0200938 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200939 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100940 uint16_t server_protection_profile_value = 0;
941
942 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
944 (ssl->conf->dtls_srtp_profile_list == NULL) ||
945 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
946 return 0;
947 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100948
Ron Eldora9788042018-12-05 11:04:31 +0200949 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100950 * uint8 SRTPProtectionProfile[2];
951 *
952 * struct {
953 * SRTPProtectionProfiles SRTPProtectionProfiles;
954 * opaque srtp_mki<0..255>;
955 * } UseSRTPData;
956
957 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
958 *
Johan Pascalb62bb512015-12-03 21:56:45 +0100959 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200961 mki_len = ssl->dtls_srtp_info.mki_len;
962 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100963
Ron Eldoref72faf2018-07-12 11:54:20 +0300964 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200965 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
966 * + protection profile (2 bytes)
967 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200968 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +0300969 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if ((len < 5) || (len != (buf[4] + 5u))) {
971 return MBEDTLS_ERR_SSL_DECODE_ERROR;
972 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100973
974 /*
975 * get the server protection profile
976 */
Ron Eldoref72faf2018-07-12 11:54:20 +0300977
978 /*
979 * protection profile length must be 0x0002 as we must have only
980 * one protection profile in server Hello
981 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if ((buf[0] != 0) || (buf[1] != 2)) {
983 return MBEDTLS_ERR_SSL_DECODE_ERROR;
984 }
Ron Eldor089c9fe2018-12-06 17:12:49 +0200985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +0200987 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 server_protection_profile_value);
989 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
990 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
991 mbedtls_ssl_get_srtp_profile_as_string(
992 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +0100993 }
994
Johan Pascal43f94902020-09-22 12:25:52 +0200995 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200996
Johan Pascalb62bb512015-12-03 21:56:45 +0100997 /*
998 * Check we have the server profile in our list
999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1001 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001002 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001004 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001006 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001007 }
1008 }
1009
Ron Eldor591f1622018-01-22 12:30:04 +02001010 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1012 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1013 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1014 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001015 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001016
1017 /* If server does not use mki in its reply, make sure the client won't keep
1018 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001020 ssl->dtls_srtp_info.mki_len = 0;
1021 }
1022
Ron Eldoref72faf2018-07-12 11:54:20 +03001023 /*
1024 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001025 * If the client detects a nonzero-length MKI in the server's response
1026 * that is different than the one the client offered, then the client
1027 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (len > 5 && (buf[4] != mki_len ||
1030 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1031 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1032 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1033 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001034 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001035#if defined(MBEDTLS_DEBUG_C)
1036 if (len > 5) {
1037 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1038 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001039 }
1040#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001042}
1043#endif /* MBEDTLS_SSL_DTLS_SRTP */
1044
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001045/*
1046 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001049MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001050static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001051{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001052 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001054 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001055
1056#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1057 uint8_t cookie_len;
1058#else
1059 uint16_t cookie_len;
1060#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001063
Gilles Peskineb64bf062019-09-27 14:02:44 +02001064 /* Check that there is enough room for:
1065 * - 2 bytes of version
1066 * - 1 byte of cookie_len
1067 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1069 MBEDTLS_SSL_DEBUG_MSG(1,
1070 ("incoming HelloVerifyRequest message is too short"));
1071 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1072 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1073 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001074 }
1075
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001076 /*
1077 * struct {
1078 * ProtocolVersion server_version;
1079 * opaque cookie<0..2^8-1>;
1080 * } HelloVerifyRequest;
1081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1083 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001084 p += 2;
1085
TRodziewicz2d8800e2021-05-13 19:14:19 +02001086 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001087 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1088 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1089 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001090 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1092 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1095 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001098 }
1099
1100 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1102 MBEDTLS_SSL_DEBUG_MSG(1,
1103 ("cookie length does not match incoming message size"));
1104 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1105 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1106 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001107 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1113 if (ssl->handshake->cookie == NULL) {
1114 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1115 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001116 }
1117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001119 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001120
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001121 /* Start over at ClientHello */
Gilles Peskinef670ba52025-03-07 15:09:32 +01001122 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001123 ret = mbedtls_ssl_reset_checksum(ssl);
1124 if (0 != ret) {
1125 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1126 return ret;
1127 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001136
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001137MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001138static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001139{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001140 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001141 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001142 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001143 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001144 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001146 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001147#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001148 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001154 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1156 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 }
1158
Hanno Becker79594fd2019-05-08 09:38:41 +01001159 buf = ssl->in_msg;
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001164 ssl->renego_records_seen++;
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (ssl->conf->renego_max_records >= 0 &&
1167 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1168 MBEDTLS_SSL_DEBUG_MSG(1,
1169 ("renegotiation requested, but not honored by server"));
1170 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001171 }
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 MBEDTLS_SSL_DEBUG_MSG(1,
1174 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001175
1176 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001182 mbedtls_ssl_send_alert_message(
1183 ssl,
1184 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1186 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 }
1188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1191 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1192 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1193 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1194 return ssl_parse_hello_verify_request(ssl);
1195 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001196 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001198 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001199 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001200 }
1201 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1205 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1206 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1207 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1208 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1209 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
1211
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001212 /*
1213 * 0 . 1 server_version
1214 * 2 . 33 random (maybe including 4 bytes of Unix time)
1215 * 34 . 34 session_id length = n
1216 * 35 . 34+n session_id
1217 * 35+n . 36+n cipher_suite
1218 * 37+n . 37+n compression_method
1219 *
1220 * 38+n . 39+n extensions length (optional)
1221 * 40+n . .. extensions
1222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01001226 ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
1227 ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001228 ssl->session_negotiate->tls_version = ssl->tls_version;
Ronald Cron17ef8df2023-11-22 10:29:42 +01001229 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (ssl->tls_version < ssl->conf->min_tls_version ||
1232 ssl->tls_version > ssl->conf->max_tls_version) {
1233 MBEDTLS_SSL_DEBUG_MSG(1,
1234 (
1235 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1236 (unsigned) ssl->conf->min_tls_version,
1237 (unsigned) ssl->tls_version,
1238 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1241 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001244 }
1245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1247 ((unsigned long) buf[2] << 24) |
1248 ((unsigned long) buf[3] << 16) |
1249 ((unsigned long) buf[4] << 8) |
1250 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001254 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (n > 32) {
1259 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1260 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1261 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1262 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001263 }
1264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001266 ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if ((ext_len > 0 && ext_len < 4) ||
1269 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1270 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001271 mbedtls_ssl_send_alert_message(
1272 ssl,
1273 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1275 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001276 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001278 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 } else {
1280 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1281 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1282 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1283 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001285
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001286 /* ciphersuite (used later) */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001287 i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35);
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001288
1289 /*
1290 * Read and check compression
1291 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001292 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1295 MBEDTLS_SSL_DEBUG_MSG(1,
1296 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001297 mbedtls_ssl_send_alert_message(
1298 ssl,
1299 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1301 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001302 }
1303
Paul Bakker380da532012-04-18 16:10:25 +00001304 /*
1305 * Initialize update checksum functions
1306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1308 if (ssl->handshake->ciphersuite_info == NULL) {
1309 MBEDTLS_SSL_DEBUG_MSG(1,
1310 ("ciphersuite info for %04x not found", (unsigned int) i));
1311 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1312 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1313 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001314 }
Paul Bakker380da532012-04-18 16:10:25 +00001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1319 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
1321 /*
1322 * Check if the session can be resumed
1323 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_RENEGOTIATION)
1326 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001327#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001328 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001329 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Gilles Peskinef670ba52025-03-07 15:09:32 +01001331 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker0a597072012-09-25 21:55:46 +00001332 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001335#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001336 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001337 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 memcpy(ssl->session_negotiate->id, buf + 35, n);
1339 } else {
Gilles Peskinef670ba52025-03-07 15:09:32 +01001340 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 }
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1344 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1347 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1348 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
Andrzej Kurek03bac442018-04-25 05:06:07 -04001350 /*
1351 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001352 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 while (1) {
1355 if (ssl->conf->ciphersuite_list[i] == 0) {
1356 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001357 mbedtls_ssl_send_alert_message(
1358 ssl,
1359 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1361 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 }
1363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if (ssl->conf->ciphersuite_list[i++] ==
1365 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 }
1369
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001370 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 ssl->session_negotiate->ciphersuite);
1372 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1373 ssl->tls_version) != 0) {
1374 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001375 mbedtls_ssl_send_alert_message(
1376 ssl,
1377 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1379 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001380 }
1381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 MBEDTLS_SSL_DEBUG_MSG(3,
1383 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001384
Gilles Peskineeccd8882020-03-10 12:19:08 +01001385#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1387 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001388 ssl->handshake->ecrs_enabled = 1;
1389 }
1390#endif
1391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1393 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001394 mbedtls_ssl_send_alert_message(
1395 ssl,
1396 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1398 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 }
1400
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001401 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 MBEDTLS_SSL_DEBUG_MSG(2,
1404 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1405 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 while (ext_len) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001408 unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
1409 unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
Paul Bakker48916f92012-09-16 19:57:18 +00001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (ext_size + 4 > ext_len) {
1412 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001413 mbedtls_ssl_send_alert_message(
1414 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1416 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001417 }
1418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 switch (ext_id) {
1420 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1421 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001424#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1427 ext_size)) != 0) {
1428 return ret;
1429 }
Paul Bakker48916f92012-09-16 19:57:18 +00001430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1435 MBEDTLS_SSL_DEBUG_MSG(3,
1436 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1439 ext + 4, ext_size)) != 0) {
1440 return ret;
1441 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001445
Hanno Beckera0e20d02019-05-15 14:03:01 +01001446#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 case MBEDTLS_TLS_EXT_CID:
1448 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if ((ret = ssl_parse_cid_ext(ssl,
1451 ext + 4,
1452 ext_size)) != 0) {
1453 return ret;
1454 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001457#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1461 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1464 ext + 4, ext_size)) != 0) {
1465 return ret;
1466 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1473 MBEDTLS_SSL_DEBUG_MSG(3,
1474 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if ((ret = ssl_parse_extended_ms_ext(ssl,
1477 ext + 4, ext_size)) != 0) {
1478 return ret;
1479 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1486 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if ((ret = ssl_parse_session_ticket_ext(ssl,
1489 ext + 4, ext_size)) != 0) {
1490 return ret;
1491 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001495
Valerio Setti7aeec542023-07-05 18:57:21 +02001496#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001497 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001498 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1500 MBEDTLS_SSL_DEBUG_MSG(3,
1501 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1504 ext + 4, ext_size)) != 0) {
1505 return ret;
1506 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001509#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001510 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001511 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001512
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001513#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1515 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1518 ext + 4, ext_size)) != 0) {
1519 return ret;
1520 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001523#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 case MBEDTLS_TLS_EXT_ALPN:
1527 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1530 return ret;
1531 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001535
Johan Pascalb62bb512015-12-03 21:56:45 +01001536#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 case MBEDTLS_TLS_EXT_USE_SRTP:
1538 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1541 return ret;
1542 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001545#endif /* MBEDTLS_SSL_DTLS_SRTP */
1546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 default:
1548 MBEDTLS_SSL_DEBUG_MSG(3,
1549 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001550 }
1551
1552 ext_len -= 4 + ext_size;
1553 ext += 4 + ext_size;
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (ext_len > 0 && ext_len < 4) {
1556 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1557 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001558 }
1559 }
1560
1561 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001562 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1563 * extensions. It sets the transform data for the resumed session which in
1564 * case of DTLS includes the server CID extracted from the CID extension.
1565 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (ssl->handshake->resume) {
1567 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1568 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001569 mbedtls_ssl_send_alert_message(
1570 ssl,
1571 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1573 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001574 }
1575 }
1576
Paul Bakker48916f92012-09-16 19:57:18 +00001577 /*
1578 * Renegotiation security checks
1579 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001581 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1583 MBEDTLS_SSL_DEBUG_MSG(1,
1584 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001585 handshake_failure = 1;
1586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 renegotiation_info_seen == 0) {
1591 MBEDTLS_SSL_DEBUG_MSG(1,
1592 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001593 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1595 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1596 ssl->conf->allow_legacy_renegotiation ==
1597 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1598 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001599 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1601 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1602 renegotiation_info_seen == 1) {
1603 MBEDTLS_SSL_DEBUG_MSG(1,
1604 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001605 handshake_failure = 1;
1606 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001610 mbedtls_ssl_send_alert_message(
1611 ssl,
1612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1614 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001615 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001620}
1621
Andrzej Kurek468c5062022-10-24 10:30:14 -04001622#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1623 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1624 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001625MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001626static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1627 unsigned char **p,
1628 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001629{
1630 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001631 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001632 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001633 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001634 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001635
1636 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001637 * struct {
1638 * ECParameters curve_params;
1639 * ECPoint public;
1640 * } ServerECDHParams;
1641 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001642 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001643 * 2..3 NamedCurve
1644 * 4 ECPoint.len
1645 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001646 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 if (end - *p < 4) {
1648 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1649 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001650
1651 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1653 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1654 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001655
1656 /* Next two bytes are the namedcurve value */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001657 tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
1658 *p += 2;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001659
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001660 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1662 MBEDTLS_SSL_DEBUG_MSG(2,
1663 ("bad server key exchange message (ECDHE curve): %u",
1664 (unsigned) tls_id));
1665 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001666 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001667
Valerio Setti40d9ca92023-01-04 16:08:04 +01001668 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001669 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1671 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001672 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001673 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001674 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001675
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001676 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001677 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 if ((size_t) (end - *p) < ecpoint_len) {
1679 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1680 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001681
Gilles Peskinec29df532023-10-02 14:59:26 +02001682 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1684 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001685
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001686 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1687 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001688 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001691}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001692#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1693 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1694 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001695#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001696MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001697static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1698 unsigned char **p,
1699 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001702 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001703 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001704
1705 /*
1706 * PSK parameters:
1707 *
1708 * opaque psk_identity_hint<0..2^16-1>;
1709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 if (end - (*p) < 2) {
1711 MBEDTLS_SSL_DEBUG_MSG(1,
1712 ("bad server key exchange message (psk_identity_hint length)"));
1713 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001714 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001715 len = MBEDTLS_GET_UINT16_BE(*p, 0);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001716 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 if (end - (*p) < len) {
1719 MBEDTLS_SSL_DEBUG_MSG(1,
1720 ("bad server key exchange message (psk_identity_hint length)"));
1721 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001722 }
1723
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001724 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001725 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001726 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001727 * someone needs that feature.
1728 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001729 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001730 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001733}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001734#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1737 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001738MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001739static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001740{
Janos Follath865b3eb2019-12-16 11:46:15 +00001741 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001743
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001744#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1745 peer_pk = &ssl->handshake->peer_pubkey;
1746#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001748 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1750 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001751 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001752 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1753#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001754
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02001755 /* This is a public key, so it can't be opaque, so can_do() is a good
1756 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
1758 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
1759 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001760 }
1761
Valerio Setti2b5d3de2023-01-09 11:04:52 +01001762 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001763 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Settif9362b72023-11-29 08:42:27 +01001764 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01001765
Valerio Setti97207782023-05-18 18:59:06 +02001766 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
1768 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01001769 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01001770
Valerio Setti97207782023-05-18 18:59:06 +02001771 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (tls_id == 0) {
Ari Weiler-Ofek67aa9592025-06-10 16:59:44 +01001773 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not supported",
Valerio Setti97207782023-05-18 18:59:06 +02001774 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01001776 }
1777
Valerio Setti1e868cc2023-01-09 17:30:01 +01001778 /* If the above conversion to TLS ID was fine, then also this one will be,
1779 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001780 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02001781 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01001782
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001783 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01001784
Przemek Stekielea4000f2022-03-16 09:49:33 +01001785 /* Store peer's public key in psa format. */
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001786 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
1787 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02001788 ret = 0;
Hanno Beckerae553dd2019-02-08 14:06:00 +00001789#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1790 /* We don't need the peer's public key anymore. Free it,
1791 * so that more RAM is available for upcoming expensive
1792 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001794#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001797}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1799 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001800
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001801MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001802static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01001803{
Janos Follath865b3eb2019-12-16 11:46:15 +00001804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001805 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00001806 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01001807 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1812 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1814 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
1815 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
1816 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001817 mbedtls_ssl_send_alert_message(
1818 ssl,
1819 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1821 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001822 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01001825 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001827 }
1828 ((void) p);
1829 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1831 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001832
Gilles Peskineeccd8882020-03-10 12:19:08 +01001833#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if (ssl->handshake->ecrs_enabled &&
1835 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02001836 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02001837 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02001838#endif
1839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
1841 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1842 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 }
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
1846 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001847 mbedtls_ssl_send_alert_message(
1848 ssl,
1849 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1851 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 }
1853
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001854 /*
Gilles Peskineb3ec1252024-09-20 18:22:04 +02001855 * ServerKeyExchange may be skipped with PSK when the server
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001856 * doesn't use a psk_identity_hint
1857 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
Gilles Peskine712e9a12024-09-20 18:11:31 +02001859 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001860 /* Current message is probably either
1861 * CertificateRequest or ServerHelloDone */
1862 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02001863 goto exit;
1864 }
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 MBEDTLS_SSL_DEBUG_MSG(1,
1867 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001868 mbedtls_ssl_send_alert_message(
1869 ssl,
1870 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 }
1875
Gilles Peskineeccd8882020-03-10 12:19:08 +01001876#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02001878 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02001880
1881start_processing:
1882#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001884 end = ssl->in_msg + ssl->in_hslen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001885 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001886
Gilles Peskineeccd8882020-03-10 12:19:08 +01001887#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
1890 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
1891 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001892 mbedtls_ssl_send_alert_message(
1893 ssl,
1894 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1896 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001897 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001898 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001899#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001900
Gilles Peskineac767e52024-09-20 18:08:44 +02001901#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine712e9a12024-09-20 18:11:31 +02001902 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001903 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 } else
Gilles Peskineac767e52024-09-20 18:08:44 +02001905#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001906#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1907 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
1912 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
1913 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001914 mbedtls_ssl_send_alert_message(
1915 ssl,
1916 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1918 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01001919 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1922 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1923 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02001924#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Valerio Setti9bed8ec2022-11-17 16:36:19 +01001926 /*
1927 * The first 3 bytes are:
1928 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
1929 * [1, 2] elliptic curve's TLS ID
1930 *
1931 * However since we only support secp256r1 for now, we check only
1932 * that TLS ID here
1933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01001935 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (exp_tls_id == 0) {
1939 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01001940 }
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
1943 (read_tls_id != exp_tls_id)) {
1944 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01001945 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01001946
1947 p += 3;
1948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if ((ret = mbedtls_psa_ecjpake_read_round(
1950 &ssl->handshake->psa_pake_ctx, p, end - p,
1951 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
1952 psa_destroy_key(ssl->handshake->psa_pake_password);
1953 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001956 mbedtls_ssl_send_alert_message(
1957 ssl,
1958 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1960 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001961 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02001963#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001964 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1966 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001967 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001968
Gilles Peskineeccd8882020-03-10 12:19:08 +01001969#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00001971 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001972 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1975 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001977 size_t params_len = (size_t) (p - params);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02001978 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08001979 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00001982
Jerry Yu693a47a2022-06-23 14:02:28 +08001983#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1984 peer_pk = &ssl->handshake->peer_pubkey;
1985#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08001987 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1989 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08001990 }
1991 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1992#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1993
Paul Bakker29e1f122013-04-16 13:07:56 +02001994 /*
1995 * Handle the digitally-signed structure
1996 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1998 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
1999 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2000 sig_alg, &pk_alg, &md_alg) != 0 &&
2001 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2002 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2003 MBEDTLS_SSL_DEBUG_MSG(1,
2004 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002005 mbedtls_ssl_send_alert_message(
2006 ssl,
2007 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2009 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002010 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002011 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2014 MBEDTLS_SSL_DEBUG_MSG(1,
2015 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002016 mbedtls_ssl_send_alert_message(
2017 ssl,
2018 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2020 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002021 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002022
2023 /*
2024 * Read signature
2025 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (p > end - 2) {
2028 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002029 mbedtls_ssl_send_alert_message(
2030 ssl,
2031 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2033 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002034 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002035 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
Paul Bakker1ef83d62012-04-11 12:09:53 +00002036 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (p != end - sig_len) {
2039 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002040 mbedtls_ssl_send_alert_message(
2041 ssl,
2042 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2044 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002048
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002049 /*
2050 * Compute the hash that has been signed
2051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (md_alg != MBEDTLS_MD_NONE) {
2053 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2054 params, params_len,
2055 md_alg);
2056 if (ret != 0) {
2057 return ret;
2058 }
2059 } else {
2060 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2061 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002062 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002065
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002066 /*
2067 * Verify signature
2068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2070 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002071 mbedtls_ssl_send_alert_message(
2072 ssl,
2073 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2075 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002076 }
2077
Gilles Peskineeccd8882020-03-10 12:19:08 +01002078#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002080 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002082#endif
2083
Jerry Yu693a47a2022-06-23 14:02:28 +08002084#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Ben Taylor306ffd32025-07-07 09:41:34 +01002086 ret = mbedtls_pk_verify_new(pk_alg, peer_pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 md_alg, hash, hashlen,
2088 p, sig_len);
2089 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002090#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 ret = mbedtls_pk_verify_restartable(peer_pk,
2092 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002095 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002096#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002098#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002100 mbedtls_ssl_send_alert_message(
2101 ssl,
2102 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2104 }
2105 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002106#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002108 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002110#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002112 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002113
2114#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2115 /* We don't need the peer's public key anymore. Free it,
2116 * so that more RAM is available for upcoming expensive
2117 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002119#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002121#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002123exit:
Gilles Peskinef670ba52025-03-07 15:09:32 +01002124 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002129}
2130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002132MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002133static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002134{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002135 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002136 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002139
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2141 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002142 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002144 }
2145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2147 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002148}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002149#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002151static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002152{
Janos Follath865b3eb2019-12-16 11:46:15 +00002153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002154 unsigned char *buf;
2155 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002156 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002157 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002158 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002159 size_t sig_alg_len;
2160#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 unsigned char *sig_alg;
2162 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002163#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2168 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002169 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002171 }
2172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2174 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2175 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 }
2177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2179 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002180 mbedtls_ssl_send_alert_message(
2181 ssl,
2182 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2184 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Gilles Peskinef670ba52025-03-07 15:09:32 +01002187 mbedtls_ssl_handshake_increment_state(ssl);
Jerry Yufb28b882022-01-28 11:05:58 +08002188 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2192 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002195 /* Current message is probably the ServerHelloDone */
2196 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002197 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002198 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002199
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002200 /*
2201 * struct {
2202 * ClientCertificateType certificate_types<1..2^8-1>;
2203 * SignatureAndHashAlgorithm
2204 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2205 * DistinguishedName certificate_authorities<0..2^16-1>;
2206 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002207 *
2208 * Since we only support a single certificate on clients, let's just
2209 * ignore all the information that's supposed to help us pick a
2210 * certificate.
2211 *
2212 * We could check that our certificate matches the request, and bail out
2213 * if it doesn't, but it's simpler to just send the certificate anyway,
2214 * and give the server the opportunity to decide if it should terminate
2215 * the connection when it doesn't like our certificate.
2216 *
2217 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2218 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002219 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002220 *
2221 * However, we still minimally parse the message to check it is at least
2222 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002223 */
Paul Bakker926af752012-11-23 13:38:07 +01002224 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002225
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002226 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2228 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2229 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2230 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2231 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002232 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002234 n = cert_type_len;
2235
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002236 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002237 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002238 * * the length of the signature algorithms field (if minor version of
2239 * SSL is 3),
2240 * * distinguished name length otherwise.
2241 * Both reach at most the index:
2242 * ...hdr_len + 2 + n,
2243 * therefore the buffer length at this point must be greater than that
2244 * regardless of the actual code path.
2245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2247 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2248 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2249 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2250 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002251 }
2252
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002253 /* supported_signature_algorithms */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002254 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002255
2256 /*
2257 * The furthest access in buf is in the loop few lines below:
2258 * sig_alg[i + 1],
2259 * where:
2260 * sig_alg = buf + ...hdr_len + 3 + n,
2261 * max(i) = sig_alg_len - 1.
2262 * Therefore the furthest access is:
2263 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2264 * which reduces to:
2265 * buf[...hdr_len + 3 + n + sig_alg_len],
2266 * which is one less than we need the buf to be.
2267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2269 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002270 mbedtls_ssl_send_alert_message(
2271 ssl,
2272 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2274 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002275 }
Paul Bakker926af752012-11-23 13:38:07 +01002276
Ronald Cron90915f22022-03-07 11:11:36 +01002277#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2279 for (size_t i = 0; i < sig_alg_len; i += 2) {
2280 MBEDTLS_SSL_DEBUG_MSG(3,
2281 ("Supported Signature Algorithm found: %02x %02x",
2282 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002283 }
2284#endif
2285
2286 n += 2 + sig_alg_len;
2287
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002288 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002289 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002290
2291 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2293 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2294 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2295 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2296 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002297 }
2298
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002299#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2301 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002302 unsigned char *p = dn + i + 2;
2303 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002304 size_t asn1_len;
2305 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 memset(&name, 0, sizeof(name));
2307 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2308 if (dni_len > dn_len - i - 2 ||
2309 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2310 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2311 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2312 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002313 mbedtls_ssl_send_alert_message(
2314 ssl,
2315 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2317 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002318 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 MBEDTLS_SSL_DEBUG_MSG(3,
2320 ("DN hint: %.*s",
2321 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2322 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002323 }
2324#endif
2325
Paul Bakker926af752012-11-23 13:38:07 +01002326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002330}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002331#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002333MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002334static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002335{
Janos Follath865b3eb2019-12-16 11:46:15 +00002336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2341 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2342 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2346 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2347 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2351 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2352 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2353 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2354 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2355 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002356 }
2357
Gilles Peskinef670ba52025-03-07 15:09:32 +01002358 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2362 mbedtls_ssl_recv_flight_completed(ssl);
2363 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002364#endif
2365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002369}
2370
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002371MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002372static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002373{
Janos Follath865b3eb2019-12-16 11:46:15 +00002374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002375
2376 size_t header_len;
2377 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002378 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002379 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002383#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2384 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2385 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2386 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002388 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2389 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Andrzej Kureka0237f82022-02-24 13:24:52 -05002391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2392 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002393 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002394
2395 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2396
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002397 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002400
Hanno Becker4a63ed42019-01-08 11:39:35 +00002401 /*
2402 * Generate EC private key for ECDHE exchange.
2403 */
2404
Hanno Becker4a63ed42019-01-08 11:39:35 +00002405 /* The master secret is obtained from the shared ECDH secret by
2406 * applying the TLS 1.2 PRF with a specific salt and label. While
2407 * the PSA Crypto API encourages combining key agreement schemes
2408 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2409 * yet support the provisioning of salt + label to the KDF.
2410 * For the time being, we therefore need to split the computation
2411 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002412 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2414 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002415 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002416 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002417
2418 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002420 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if (status != PSA_SUCCESS) {
2422 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2423 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002424
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002425 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002426 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002427 * but we just need to add a length byte before that. */
2428 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2429 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002431 size_t own_pubkey_len;
2432
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002433 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 own_pubkey, own_pubkey_max_len,
2435 &own_pubkey_len);
2436 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002437 psa_destroy_key(handshake->xxdh_psa_privkey);
2438 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002440 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002441
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002442 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2443 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002444
Hanno Becker4a63ed42019-01-08 11:39:35 +00002445 /* The ECDH secret is the premaster secret used for key derivation. */
2446
Janos Follathdf3b0892019-08-08 11:12:24 +01002447 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002449 handshake->xxdh_psa_privkey,
2450 handshake->xxdh_psa_peerkey,
2451 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 ssl->handshake->premaster,
2453 sizeof(ssl->handshake->premaster),
2454 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002455
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002456 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2457 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2460 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2461 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2464 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2465 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2466 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardc7403ed2025-01-23 11:24:18 +01002467#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002469 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2470 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2471 psa_key_attributes_t key_attributes;
2472
2473 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2474
2475 /*
2476 * opaque psk_identity<0..2^16-1>;
2477 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002479 /* We don't offer PSK suites if we don't have a PSK,
2480 * and we check that the server's choice is among the
2481 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2483 }
Neil Armstrong868af822022-03-09 10:26:25 +01002484
Neil Armstrongfc834f22022-03-23 17:54:38 +01002485 /* uint16 to store content length */
2486 const size_t content_len_size = 2;
2487
Neil Armstrong868af822022-03-09 10:26:25 +01002488 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if (header_len + content_len_size + ssl->conf->psk_identity_len
2491 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2492 MBEDTLS_SSL_DEBUG_MSG(1,
2493 ("psk identity too long or SSL buffer too short"));
2494 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002495 }
2496
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002497 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2500 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002501 header_len += content_len_size;
2502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 memcpy(p, ssl->conf->psk_identity,
2504 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002505 p += ssl->conf->psk_identity_len;
2506
Neil Armstrong868af822022-03-09 10:26:25 +01002507 header_len += ssl->conf->psk_identity_len;
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002510
2511 /*
2512 * Generate EC private key for ECDHE exchange.
2513 */
2514
2515 /* The master secret is obtained from the shared ECDH secret by
2516 * applying the TLS 1.2 PRF with a specific salt and label. While
2517 * the PSA Crypto API encourages combining key agreement schemes
2518 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2519 * yet support the provisioning of salt + label to the KDF.
2520 * For the time being, we therefore need to split the computation
2521 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2522 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2524 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002525 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002526 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002527
2528 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002530 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002532 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 }
Neil Armstrong868af822022-03-09 10:26:25 +01002534
2535 /* Export the public part of the ECDH private key from PSA.
2536 * The export format is an ECPoint structure as expected by TLS,
2537 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002538 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002539 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002541 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002542
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002543 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 own_pubkey, own_pubkey_max_len,
2545 &own_pubkey_len);
2546 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002547 psa_destroy_key(handshake->xxdh_psa_privkey);
2548 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002549 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002550 }
2551
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002552 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002553 content_len = own_pubkey_len + 1;
2554
Neil Armstrong25400452022-03-23 17:44:07 +01002555 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2556 * - a uint16 containing the length (in octets) of the ECDH computation
2557 * - the octet string produced by the ECDH computation
2558 * - a uint16 containing the length (in octets) of the PSK
2559 * - the PSK itself
2560 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002561 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 const unsigned char * const pms_end = pms +
2563 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002564 /* uint16 to store length (in octets) of the ECDH computation */
2565 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002566 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002567
Neil Armstrong25400452022-03-23 17:44:07 +01002568 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002570 handshake->xxdh_psa_privkey,
2571 handshake->xxdh_psa_peerkey,
2572 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 pms + zlen_size,
2574 pms_end - (pms + zlen_size),
2575 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01002576
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002577 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2578 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002581 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002583 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 }
Neil Armstrong868af822022-03-09 10:26:25 +01002585
Neil Armstrong25400452022-03-23 17:44:07 +01002586 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002588 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 } else
Manuel Pégourié-Gonnardc7403ed2025-01-23 11:24:18 +01002590#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002591#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002593 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002594 * opaque psk_identity<0..2^16-1>;
2595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01002597 /* We don't offer PSK suites if we don't have a PSK,
2598 * and we check that the server's choice is among the
2599 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02002601 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002602
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002603 header_len = 4;
2604 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2607 MBEDTLS_SSL_DEBUG_MSG(1,
2608 ("psk identity too long or SSL buffer too short"));
2609 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002610 }
2611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
2613 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 memcpy(ssl->out_msg + header_len,
2616 ssl->conf->psk_identity,
2617 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002618 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002622 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002624#endif
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002625 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2627 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002628 }
2629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01002631#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002632#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002634 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002635
Neil Armstrongca7d5062022-05-31 14:43:23 +02002636 unsigned char *out_p = ssl->out_msg + header_len;
2637 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
2638 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
2640 out_p, end_p - out_p, &content_len,
2641 MBEDTLS_ECJPAKE_ROUND_TWO);
2642 if (ret != 0) {
2643 psa_destroy_key(ssl->handshake->psa_pake_password);
2644 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2645 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
2646 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002647 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 } else
Gabor Mezeie1e27302025-02-26 18:06:05 +01002649#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002650 {
2651 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2653 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02002654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002656 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2658 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Gilles Peskinef670ba52025-03-07 15:09:32 +01002660 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2663 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2664 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
2666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670}
2671
Gilles Peskineeccd8882020-03-10 12:19:08 +01002672#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002673MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002674static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002675{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002676 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002677 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00002678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2683 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2684 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002685 }
2686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2688 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002689 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02002691 }
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2694 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002695}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002696#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002697MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002698static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002701 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002702 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002703 size_t n = 0, offset = 0;
2704 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002705 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002707 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002708 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02002709#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002710 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02002711#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002712 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02002713#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002716
Gilles Peskineeccd8882020-03-10 12:19:08 +01002717#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 if (ssl->handshake->ecrs_enabled &&
2719 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002720 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002721 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002722#endif
2723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2725 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2726 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002727 }
2728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2730 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002731 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002733 }
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if (ssl->handshake->client_auth == 0 ||
2736 mbedtls_ssl_own_cert(ssl) == NULL) {
2737 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01002738 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 }
2741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 if (mbedtls_ssl_own_key(ssl) == NULL) {
2743 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
2744 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 }
2746
2747 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002748 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002750#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002752 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002754
2755sign:
2756#endif
2757
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002758 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
2759 if (0 != ret) {
2760 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
2761 return ret;
2762 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
Ronald Cron90915f22022-03-07 11:11:36 +01002764 /*
2765 * digitally-signed struct {
2766 * opaque handshake_messages[handshake_messages_length];
2767 * };
2768 *
2769 * Taking shortcut here. We assume that the server always allows the
2770 * PRF Hash function and has sent it in the allowed signature
2771 * algorithms list received in the Certificate Request message.
2772 *
2773 * Until we encounter a server that does not, we will take this
2774 * shortcut.
2775 *
2776 * Reason: Otherwise we should have running hashes for SHA512 and
2777 * SHA224 in order to satisfy 'weird' needs from the server
2778 * side.
2779 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01002781 md_alg = MBEDTLS_MD_SHA384;
2782 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01002784 md_alg = MBEDTLS_MD_SHA256;
2785 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02002786 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01002788
2789 /* Info from md_alg will be used instead */
2790 hashlen = 0;
2791 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002792
Gilles Peskineeccd8882020-03-10 12:19:08 +01002793#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002795 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002797#endif
2798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
2800 md_alg, hash_start, hashlen,
2801 ssl->out_msg + 6 + offset,
2802 out_buf_len - 6 - offset,
2803 &n,
Ben Taylor440cb2a2025-03-05 09:40:08 +00002804 rs_ctx)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002806#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002808 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002810#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002812 }
Paul Bakker926af752012-11-23 13:38:07 +01002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2818 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Gilles Peskinef670ba52025-03-07 15:09:32 +01002820 mbedtls_ssl_handshake_increment_state(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2823 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2824 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 }
2826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002830}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002831#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002834MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002835static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002836{
Janos Follath865b3eb2019-12-16 11:46:15 +00002837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002838 uint32_t lifetime;
2839 size_t ticket_len;
2840 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002841 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2846 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2847 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002848 }
2849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2851 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002852 mbedtls_ssl_send_alert_message(
2853 ssl,
2854 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2856 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002857 }
2858
2859 /*
2860 * struct {
2861 * uint32 ticket_lifetime_hint;
2862 * opaque ticket<0..2^16-1>;
2863 * } NewSessionTicket;
2864 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002865 * 0 . 3 ticket_lifetime_hint
2866 * 4 . 5 ticket_len (n)
2867 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002868 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
2870 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
2871 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
2872 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2873 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2874 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002875 }
2876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002878
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002879 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002880
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002881 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
2884 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
2885 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2886 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2887 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002888 }
2889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002891
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002892 /* We're not waiting for a NewSessionTicket message any more */
2893 ssl->handshake->new_session_ticket = 0;
Gilles Peskinef670ba52025-03-07 15:09:32 +01002894 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002895
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002896 /*
2897 * Zero-length ticket means the server changed his mind and doesn't want
2898 * to send a ticket after all, so just forget it
2899 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 if (ticket_len == 0) {
2901 return 0;
2902 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002905 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00002907 ssl->session->ticket = NULL;
2908 ssl->session->ticket_len = 0;
2909 }
2910
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002911 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002913 ssl->session_negotiate->ticket = NULL;
2914 ssl->session_negotiate->ticket_len = 0;
2915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
2917 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
2918 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2919 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
2920 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002921 }
2922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002924
2925 ssl->session_negotiate->ticket = ticket;
2926 ssl->session_negotiate->ticket_len = ticket_len;
2927 ssl->session_negotiate->ticket_lifetime = lifetime;
2928
2929 /*
2930 * RFC 5077 section 3.4:
2931 * "If the client receives a session ticket from the server, then it
2932 * discards any Session ID that was sent in the ServerHello."
2933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002935 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002942
Paul Bakker5121ce52009-01-03 21:22:43 +00002943/*
Paul Bakker1961b702013-01-25 14:49:24 +01002944 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002946int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002947{
2948 int ret = 0;
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002951 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
2954 ssl->handshake->new_session_ticket != 0) {
Gilles Peskinef670ba52025-03-07 15:09:32 +01002955 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_NEW_SESSION_TICKET);
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002956 }
2957#endif
2958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskinef670ba52025-03-07 15:09:32 +01002961 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 break;
2963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 /*
2965 * ==> ClientHello
2966 */
2967 case MBEDTLS_SSL_CLIENT_HELLO:
2968 ret = mbedtls_ssl_write_client_hello(ssl);
2969 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 /*
2972 * <== ServerHello
2973 * Certificate
2974 * ( ServerKeyExchange )
2975 * ( CertificateRequest )
2976 * ServerHelloDone
2977 */
2978 case MBEDTLS_SSL_SERVER_HELLO:
2979 ret = ssl_parse_server_hello(ssl);
2980 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 case MBEDTLS_SSL_SERVER_CERTIFICATE:
2983 ret = mbedtls_ssl_parse_certificate(ssl);
2984 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
2987 ret = ssl_parse_server_key_exchange(ssl);
2988 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2991 ret = ssl_parse_certificate_request(ssl);
2992 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 case MBEDTLS_SSL_SERVER_HELLO_DONE:
2995 ret = ssl_parse_server_hello_done(ssl);
2996 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 /*
2999 * ==> ( Certificate/Alert )
3000 * ClientKeyExchange
3001 * ( CertificateVerify )
3002 * ChangeCipherSpec
3003 * Finished
3004 */
3005 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3006 ret = mbedtls_ssl_write_certificate(ssl);
3007 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3010 ret = ssl_write_client_key_exchange(ssl);
3011 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3014 ret = ssl_write_certificate_verify(ssl);
3015 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3018 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3019 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 case MBEDTLS_SSL_CLIENT_FINISHED:
3022 ret = mbedtls_ssl_write_finished(ssl);
3023 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 /*
3026 * <== ( NewSessionTicket )
3027 * ChangeCipherSpec
3028 * Finished
3029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3032 ret = ssl_parse_new_session_ticket(ssl);
3033 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003034#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3037 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3038 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 case MBEDTLS_SSL_SERVER_FINISHED:
3041 ret = mbedtls_ssl_parse_finished(ssl);
3042 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 case MBEDTLS_SSL_FLUSH_BUFFERS:
3045 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Gilles Peskinef670ba52025-03-07 15:09:32 +01003046 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3050 mbedtls_ssl_handshake_wrapup(ssl);
3051 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 default:
3054 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3055 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003059}
Jerry Yuc5aef882021-12-23 20:15:02 +08003060
Jerry Yufb4b6472022-01-27 15:03:26 +08003061#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */