blob: a15daf0abc27b599c718bef4b83ea5ba4ac2463a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS client-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "ssl_misc.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020013
SimonBd5800b72016-04-26 07:43:27 +010014#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010015#include "ssl_client.h"
Valerio Settib4f50762024-01-17 10:24:52 +010016#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000017#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020018#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010019
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020020#include "psa_util_internal.h"
Ronald Cron69a63422021-10-18 09:47:58 +020021#include "psa/crypto.h"
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040022#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Andrzej Kurek00644842023-05-30 05:45:00 -040023/* Define a local translating function to save code size by not using too many
24 * arguments in each translating place. */
25static int local_err_translation(psa_status_t status)
26{
27 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040028 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040029 psa_generic_status_to_mbedtls);
30}
31#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040032#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +000033
SimonBd5800b72016-04-26 07:43:27 +010034#include <string.h>
35
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020036#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010039#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050043#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020047MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010048static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
49 unsigned char *buf,
50 const unsigned char *end,
51 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010052{
53 unsigned char *p = buf;
54
55 *olen = 0;
56
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010057 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010058 * initial ClientHello, in which case also adding the renegotiation
59 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
61 return 0;
62 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 MBEDTLS_SSL_DEBUG_MSG(3,
65 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010068
Paul Bakkerd3edc862013-03-20 16:07:17 +010069 /*
70 * Secure renegotiation
71 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010073 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010074
75 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
77 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010080
81 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010084}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010086
Valerio Setti7aeec542023-07-05 18:57:21 +020087#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +020088 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010089 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010090
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010092static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
93 unsigned char *buf,
94 const unsigned char *end,
95 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010096{
97 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +010098 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +010099
100 *olen = 0;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 MBEDTLS_SSL_DEBUG_MSG(3,
103 ("client hello, adding supported_point_formats extension"));
104 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100107 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100108
109 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100110 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200111
112 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100114
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200115 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118}
Valerio Setti7aeec542023-07-05 18:57:21 +0200119#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200120 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200121 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100122
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200123#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200124MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100125static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
126 unsigned char *buf,
127 const unsigned char *end,
128 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200129{
Janos Follath865b3eb2019-12-16 11:46:15 +0000130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200131 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100132 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200133
134 *olen = 0;
135
136 /* Skip costly extension if we can't use EC J-PAKE anyway */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
138 return 0;
139 }
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 MBEDTLS_SSL_DEBUG_MSG(3,
142 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100147 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200148
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200149 /*
150 * We may need to send ClientHello multiple times for Hello verification.
151 * We don't want to compute fresh values every time (both for performance
152 * and consistency reasons), so cache the extension content.
153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (ssl->handshake->ecjpake_cache == NULL ||
155 ssl->handshake->ecjpake_cache_len == 0) {
156 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200157
Valerio Setti6b3dab02022-11-17 17:14:54 +0100158 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 p + 2, end - p - 2, &kkpp_len,
160 MBEDTLS_ECJPAKE_ROUND_ONE);
161 if (ret != 0) {
162 psa_destroy_key(ssl->handshake->psa_pake_password);
163 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
164 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
165 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200166 }
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
169 if (ssl->handshake->ecjpake_cache == NULL) {
170 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
171 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200172 }
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200175 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 } else {
177 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200178
179 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100186 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200187
188 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200191}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200192#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000193
Hanno Beckera0e20d02019-05-15 14:03:01 +0100194#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200195MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100196static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
197 unsigned char *buf,
198 const unsigned char *end,
199 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100200{
201 unsigned char *p = buf;
202 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100203
204 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100205 * struct {
206 * opaque cid<0..2^8-1>;
207 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100209
210 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
212 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
213 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100214 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100216
217 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
218 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100220
221 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100223 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100224 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100226 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100227
228 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100230
231 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100234}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100235#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200238MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100239static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
240 unsigned char *buf,
241 const unsigned char *end,
242 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200243{
244 unsigned char *p = buf;
245
Simon Butcher0fc94e92015-09-28 20:52:04 +0100246 *olen = 0;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
249 return 0;
250 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 MBEDTLS_SSL_DEBUG_MSG(3,
253 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100258 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200259
260 *p++ = 0x00;
261 *p++ = 1;
262
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200263 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200264
265 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200272MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100273static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
274 unsigned char *buf,
275 const unsigned char *end,
276 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100277{
278 unsigned char *p = buf;
279
Simon Butcher0fc94e92015-09-28 20:52:04 +0100280 *olen = 0;
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
283 return 0;
284 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 MBEDTLS_SSL_DEBUG_MSG(3,
287 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100292 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100293
294 *p++ = 0x00;
295 *p++ = 0x00;
296
297 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200304MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100305static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
306 unsigned char *buf,
307 const unsigned char *end,
308 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200309{
310 unsigned char *p = buf;
311
Simon Butcher0fc94e92015-09-28 20:52:04 +0100312 *olen = 0;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
315 return 0;
316 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 MBEDTLS_SSL_DEBUG_MSG(3,
319 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100324 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200325
326 *p++ = 0x00;
327 *p++ = 0x00;
328
329 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200336MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100337static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
338 unsigned char *buf,
339 const unsigned char *end,
340 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200341{
342 unsigned char *p = buf;
343 size_t tlen = ssl->session_negotiate->ticket_len;
344
Simon Butcher0fc94e92015-09-28 20:52:04 +0100345 *olen = 0;
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
348 return 0;
349 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 MBEDTLS_SSL_DEBUG_MSG(3,
352 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200353
Hanno Becker261602c2017-04-12 14:54:42 +0100354 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100358 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100361 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200362
363 *olen = 4;
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
366 return 0;
367 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 MBEDTLS_SSL_DEBUG_MSG(3,
370 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200373
374 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200379
Ron Eldora9788042018-12-05 11:04:31 +0200380#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200381MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100382static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
383 unsigned char *buf,
384 const unsigned char *end,
385 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100386{
387 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200388 size_t protection_profiles_index = 0, ext_len = 0;
389 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100390
391 *olen = 0;
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
394 (ssl->conf->dtls_srtp_profile_list == NULL) ||
395 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
396 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100397 }
398
Ron Eldora9788042018-12-05 11:04:31 +0200399 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100400 * uint8 SRTPProtectionProfile[2];
401 *
402 * struct {
403 * SRTPProtectionProfiles SRTPProtectionProfiles;
404 * opaque srtp_mki<0..255>;
405 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100406 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200409 mki_len = ssl->dtls_srtp_info.mki_len;
410 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300411 /* Extension length = 2 bytes for profiles length,
412 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
413 * 1 byte for srtp_mki vector length and the mki_len value
414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200418
419 /* Check there is room in the buffer for the extension + 4 bytes
420 * - the extension tag (2 bytes)
421 * - the extension length (2 bytes)
422 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100426 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100429 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100430
Ron Eldor3adb9922017-12-21 10:15:08 +0200431 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200432 /* micro-optimization:
433 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
434 * which is lower than 127, so the upper byte of the length is always 0
435 * For the documentation, the more generic code is left in comments
436 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
437 * >> 8 ) & 0xFF );
438 */
439 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300443 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200445 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
447 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
448 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
449 profile_value));
450 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100451 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200453 /*
454 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200455 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 MBEDTLS_SSL_DEBUG_MSG(3,
458 ("client hello, "
459 "illegal DTLS-SRTP protection profile %d",
460 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
461 ));
462 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100463 }
464 }
465
Ron Eldor591f1622018-01-22 12:30:04 +0200466 *p++ = mki_len & 0xFF;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (mki_len != 0) {
469 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200470 /*
471 * Increment p to point to the current position.
472 */
473 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
475 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200476 }
477
Ron Eldoref72faf2018-07-12 11:54:20 +0300478 /*
479 * total extension length: extension type (2 bytes)
480 * + extension length (2 bytes)
481 * + protection profile length (2 bytes)
482 * + 2 * number of protection profiles
483 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200484 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300485 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200486 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100489}
490#endif /* MBEDTLS_SSL_DTLS_SRTP */
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
493 unsigned char *buf,
494 const unsigned char *end,
495 int uses_ec,
496 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100497{
498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
499 unsigned char *p = buf;
500 size_t ext_len = 0;
501
502 (void) ssl;
503 (void) end;
504 (void) uses_ec;
505 (void) ret;
506 (void) ext_len;
507
508 *out_len = 0;
509
510 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
511 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
512#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
514 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
515 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100516 }
517 p += ext_len;
518#endif
519
Valerio Setti7aeec542023-07-05 18:57:21 +0200520#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200521 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Ronald Cron12dcdf02022-02-16 15:28:22 +0100522 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (uses_ec) {
524 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
525 &ext_len)) != 0) {
526 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
527 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100528 }
529 p += ext_len;
530 }
531#endif
532
533#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
535 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
536 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100537 }
538 p += ext_len;
539#endif
540
541#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
543 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
544 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100545 }
546 p += ext_len;
547#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
548
549#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
551 &ext_len)) != 0) {
552 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
553 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100554 }
555 p += ext_len;
556#endif
557
558#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
560 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
561 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100562 }
563 p += ext_len;
564#endif
565
566#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
568 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
569 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100570 }
571 p += ext_len;
572#endif
573
574#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
576 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
577 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100578 }
579 p += ext_len;
580#endif
581
582#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
584 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
585 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100586 }
587 p += ext_len;
588#endif
589
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000590 *out_len = (size_t) (p - buf);
Ronald Cron12dcdf02022-02-16 15:28:22 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100593}
594
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200595MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100596static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
597 const unsigned char *buf,
598 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100602 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000604 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_ct_memcmp(buf + 1,
606 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
607 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
608 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
609 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100610 mbedtls_ssl_send_alert_message(
611 ssl,
612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
614 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000615 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100618 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (len != 1 || buf[0] != 0x00) {
620 MBEDTLS_SSL_DEBUG_MSG(1,
621 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100622 mbedtls_ssl_send_alert_message(
623 ssl,
624 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
626 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100627 }
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100630 }
Paul Bakker48916f92012-09-16 19:57:18 +0000631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000633}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200636MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100637static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
638 const unsigned char *buf,
639 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200640{
641 /*
642 * server should use the extension only if we did,
643 * and if so the server's value should match ours (and len is always 1)
644 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200646 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 buf[0] != ssl->conf->mfl_code) {
648 MBEDTLS_SSL_DEBUG_MSG(1,
649 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100650 mbedtls_ssl_send_alert_message(
651 ssl,
652 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
654 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200655 }
656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000660
Hanno Beckera0e20d02019-05-15 14:03:01 +0100661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200662MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100663static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
664 const unsigned char *buf,
665 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100666{
667 size_t peer_cid_len;
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100670 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
671 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
673 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
674 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
675 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
676 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (len == 0) {
680 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
681 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
682 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
683 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100684 }
685
686 peer_cid_len = *buf++;
687 len--;
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
690 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
691 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
692 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
693 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100694 }
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (len != peer_cid_len) {
697 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
698 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
699 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
700 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100701 }
702
Hanno Becker5a299902019-05-03 12:47:49 +0100703 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100704 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
708 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100711}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100712#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200715MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100716static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
717 const unsigned char *buf,
718 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100719{
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
721 len != 0) {
722 MBEDTLS_SSL_DEBUG_MSG(1,
723 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100724 mbedtls_ssl_send_alert_message(
725 ssl,
726 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
728 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100729 }
730
731 ((void) buf);
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200740MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100741static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
742 const unsigned char *buf,
743 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200744{
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
746 len != 0) {
747 MBEDTLS_SSL_DEBUG_MSG(1,
748 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100749 mbedtls_ssl_send_alert_message(
750 ssl,
751 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
753 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200754 }
755
756 ((void) buf);
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200765MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100766static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
767 const unsigned char *buf,
768 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200769{
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
771 len != 0) {
772 MBEDTLS_SSL_DEBUG_MSG(1,
773 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100774 mbedtls_ssl_send_alert_message(
775 ssl,
776 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
778 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200779 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200780
781 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200782
783 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200788
Valerio Setti7aeec542023-07-05 18:57:21 +0200789#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200790 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100791 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200792MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100793static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
794 const unsigned char *buf,
795 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200796{
797 size_t list_size;
798 const unsigned char *p;
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (len == 0 || (size_t) (buf[0] + 1) != len) {
801 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
802 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
803 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
804 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200805 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200806 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200807
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200808 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 while (list_size > 0) {
810 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
811 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Valerio Setti7aeec542023-07-05 18:57:21 +0200812#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
813 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200814 ssl->handshake->ecdh_ctx.point_format = p[0];
Valerio Setti7aeec542023-07-05 18:57:21 +0200815#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200816#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
818 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
819 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200820#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
822 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200823 }
824
825 list_size--;
826 p++;
827 }
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
830 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
831 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
832 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200833}
Valerio Setti7aeec542023-07-05 18:57:21 +0200834#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200835 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200836 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200837
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200838#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200839MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100840static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
841 const unsigned char *buf,
842 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200843{
Janos Follath865b3eb2019-12-16 11:46:15 +0000844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (ssl->handshake->ciphersuite_info->key_exchange !=
847 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
848 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
849 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200850 }
851
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200852 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200854 ssl->handshake->ecjpake_cache = NULL;
855 ssl->handshake->ecjpake_cache_len = 0;
856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if ((ret = mbedtls_psa_ecjpake_read_round(
858 &ssl->handshake->psa_pake_ctx, buf, len,
859 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
860 psa_destroy_key(ssl->handshake->psa_pake_password);
861 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100864 mbedtls_ssl_send_alert_message(
865 ssl,
866 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
868 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200869 }
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200872}
873#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200876MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100877static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
878 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200879{
880 size_t list_len, name_len;
881 const char **p;
882
883 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (ssl->conf->alpn_list == NULL) {
885 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100886 mbedtls_ssl_send_alert_message(
887 ssl,
888 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
890 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200891 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200892
893 /*
894 * opaque ProtocolName<1..2^8-1>;
895 *
896 * struct {
897 * ProtocolName protocol_name_list<2..2^16-1>
898 * } ProtocolNameList;
899 *
900 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
901 */
902
903 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (len < 4) {
905 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
906 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
907 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200908 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200909
Dave Rodgmana3d0f612023-11-03 23:34:02 +0000910 list_len = MBEDTLS_GET_UINT16_BE(buf, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (list_len != len - 2) {
912 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
913 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
914 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200915 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200916
917 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (name_len != list_len - 1) {
919 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
920 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
921 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200922 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200923
924 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
926 if (name_len == strlen(*p) &&
927 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200928 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200930 }
931 }
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
934 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
935 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
936 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200937}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200939
Johan Pascalb62bb512015-12-03 21:56:45 +0100940#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200941MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100942static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
943 const unsigned char *buf,
944 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100945{
Johan Pascal43f94902020-09-22 12:25:52 +0200946 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200947 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100948 uint16_t server_protection_profile_value = 0;
949
950 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
952 (ssl->conf->dtls_srtp_profile_list == NULL) ||
953 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
954 return 0;
955 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100956
Ron Eldora9788042018-12-05 11:04:31 +0200957 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100958 * uint8 SRTPProtectionProfile[2];
959 *
960 * struct {
961 * SRTPProtectionProfiles SRTPProtectionProfiles;
962 * opaque srtp_mki<0..255>;
963 * } UseSRTPData;
964
965 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
966 *
Johan Pascalb62bb512015-12-03 21:56:45 +0100967 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200969 mki_len = ssl->dtls_srtp_info.mki_len;
970 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100971
Ron Eldoref72faf2018-07-12 11:54:20 +0300972 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200973 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
974 * + protection profile (2 bytes)
975 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200976 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +0300977 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((len < 5) || (len != (buf[4] + 5u))) {
979 return MBEDTLS_ERR_SSL_DECODE_ERROR;
980 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100981
982 /*
983 * get the server protection profile
984 */
Ron Eldoref72faf2018-07-12 11:54:20 +0300985
986 /*
987 * protection profile length must be 0x0002 as we must have only
988 * one protection profile in server Hello
989 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if ((buf[0] != 0) || (buf[1] != 2)) {
991 return MBEDTLS_ERR_SSL_DECODE_ERROR;
992 }
Ron Eldor089c9fe2018-12-06 17:12:49 +0200993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +0200995 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 server_protection_profile_value);
997 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
998 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
999 mbedtls_ssl_get_srtp_profile_as_string(
1000 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001001 }
1002
Johan Pascal43f94902020-09-22 12:25:52 +02001003 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001004
Johan Pascalb62bb512015-12-03 21:56:45 +01001005 /*
1006 * Check we have the server profile in our list
1007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1009 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001010 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001012 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001014 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001015 }
1016 }
1017
Ron Eldor591f1622018-01-22 12:30:04 +02001018 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1020 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1021 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1022 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001023 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001024
1025 /* If server does not use mki in its reply, make sure the client won't keep
1026 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001028 ssl->dtls_srtp_info.mki_len = 0;
1029 }
1030
Ron Eldoref72faf2018-07-12 11:54:20 +03001031 /*
1032 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001033 * If the client detects a nonzero-length MKI in the server's response
1034 * that is different than the one the client offered, then the client
1035 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (len > 5 && (buf[4] != mki_len ||
1038 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1039 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1040 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1041 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001042 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001043#if defined(MBEDTLS_DEBUG_C)
1044 if (len > 5) {
1045 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1046 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001047 }
1048#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001050}
1051#endif /* MBEDTLS_SSL_DTLS_SRTP */
1052
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001053/*
1054 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001057MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001058static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001059{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001060 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001062 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001063
1064#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1065 uint8_t cookie_len;
1066#else
1067 uint16_t cookie_len;
1068#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001071
Gilles Peskineb64bf062019-09-27 14:02:44 +02001072 /* Check that there is enough room for:
1073 * - 2 bytes of version
1074 * - 1 byte of cookie_len
1075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1077 MBEDTLS_SSL_DEBUG_MSG(1,
1078 ("incoming HelloVerifyRequest message is too short"));
1079 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1080 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1081 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001082 }
1083
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001084 /*
1085 * struct {
1086 * ProtocolVersion server_version;
1087 * opaque cookie<0..2^8-1>;
1088 * } HelloVerifyRequest;
1089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1091 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001092 p += 2;
1093
TRodziewicz2d8800e2021-05-13 19:14:19 +02001094 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001095 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1096 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1097 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1100 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1103 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001106 }
1107
1108 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1110 MBEDTLS_SSL_DEBUG_MSG(1,
1111 ("cookie length does not match incoming message size"));
1112 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1113 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1114 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001115 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1121 if (ssl->handshake->cookie == NULL) {
1122 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1123 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001124 }
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001127 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001128
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001129 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001131 ret = mbedtls_ssl_reset_checksum(ssl);
1132 if (0 != ret) {
1133 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1134 return ret;
1135 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001144
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001145MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001146static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001147{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001148 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001149 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001150 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001151 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001152 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001154 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001155#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001156 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001162 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1164 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 }
1166
Hanno Becker79594fd2019-05-08 09:38:41 +01001167 buf = ssl->in_msg;
1168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001172 ssl->renego_records_seen++;
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (ssl->conf->renego_max_records >= 0 &&
1175 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1176 MBEDTLS_SSL_DEBUG_MSG(1,
1177 ("renegotiation requested, but not honored by server"));
1178 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001179 }
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_SSL_DEBUG_MSG(1,
1182 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001183
1184 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001186 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001190 mbedtls_ssl_send_alert_message(
1191 ssl,
1192 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1194 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 }
1196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1199 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1200 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1201 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1202 return ssl_parse_hello_verify_request(ssl);
1203 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001204 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001206 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001207 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001208 }
1209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1213 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1214 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1215 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1216 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1217 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 }
1219
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001220 /*
1221 * 0 . 1 server_version
1222 * 2 . 33 random (maybe including 4 bytes of Unix time)
1223 * 34 . 34 session_id length = n
1224 * 35 . 34+n session_id
1225 * 35+n . 36+n cipher_suite
1226 * 37+n . 37+n compression_method
1227 *
1228 * 38+n . 39+n extensions length (optional)
1229 * 40+n . .. extensions
1230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01001234 ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
1235 ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001236 ssl->session_negotiate->tls_version = ssl->tls_version;
Ronald Cron17ef8df2023-11-22 10:29:42 +01001237 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if (ssl->tls_version < ssl->conf->min_tls_version ||
1240 ssl->tls_version > ssl->conf->max_tls_version) {
1241 MBEDTLS_SSL_DEBUG_MSG(1,
1242 (
1243 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1244 (unsigned) ssl->conf->min_tls_version,
1245 (unsigned) ssl->tls_version,
1246 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1249 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001252 }
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1255 ((unsigned long) buf[2] << 24) |
1256 ((unsigned long) buf[3] << 16) |
1257 ((unsigned long) buf[4] << 8) |
1258 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001261
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001262 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (n > 32) {
1267 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1268 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1269 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1270 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001271 }
1272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001274 ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 if ((ext_len > 0 && ext_len < 4) ||
1277 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1278 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001279 mbedtls_ssl_send_alert_message(
1280 ssl,
1281 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1283 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001284 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001286 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 } else {
1288 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1289 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1290 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1291 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001292 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001294 /* ciphersuite (used later) */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001295 i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35);
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001296
1297 /*
1298 * Read and check compression
1299 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001300 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1303 MBEDTLS_SSL_DEBUG_MSG(1,
1304 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001305 mbedtls_ssl_send_alert_message(
1306 ssl,
1307 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1309 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001310 }
1311
Paul Bakker380da532012-04-18 16:10:25 +00001312 /*
1313 * Initialize update checksum functions
1314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1316 if (ssl->handshake->ciphersuite_info == NULL) {
1317 MBEDTLS_SSL_DEBUG_MSG(1,
1318 ("ciphersuite info for %04x not found", (unsigned int) i));
1319 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1320 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1321 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001322 }
Paul Bakker380da532012-04-18 16:10:25 +00001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1327 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
1329 /*
1330 * Check if the session can be resumed
1331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_SSL_RENEGOTIATION)
1334 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001335#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 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001340 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001343#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001344 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001345 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 memcpy(ssl->session_negotiate->id, buf + 35, n);
1347 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 }
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1352 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1355 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1356 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
Andrzej Kurek03bac442018-04-25 05:06:07 -04001358 /*
1359 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001360 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 while (1) {
1363 if (ssl->conf->ciphersuite_list[i] == 0) {
1364 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001365 mbedtls_ssl_send_alert_message(
1366 ssl,
1367 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1369 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 }
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (ssl->conf->ciphersuite_list[i++] ==
1373 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 }
1377
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001378 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 ssl->session_negotiate->ciphersuite);
1380 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1381 ssl->tls_version) != 0) {
1382 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001383 mbedtls_ssl_send_alert_message(
1384 ssl,
1385 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1387 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001388 }
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 MBEDTLS_SSL_DEBUG_MSG(3,
1391 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001392
Gilles Peskineeccd8882020-03-10 12:19:08 +01001393#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1395 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001396 ssl->handshake->ecrs_enabled = 1;
1397 }
1398#endif
1399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1401 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001402 mbedtls_ssl_send_alert_message(
1403 ssl,
1404 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1406 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 }
1408
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001409 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 MBEDTLS_SSL_DEBUG_MSG(2,
1412 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1413 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 while (ext_len) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001416 unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
1417 unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
Paul Bakker48916f92012-09-16 19:57:18 +00001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (ext_size + 4 > ext_len) {
1420 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001421 mbedtls_ssl_send_alert_message(
1422 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1424 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001425 }
1426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 switch (ext_id) {
1428 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1429 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001432#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1435 ext_size)) != 0) {
1436 return ret;
1437 }
Paul Bakker48916f92012-09-16 19:57:18 +00001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1443 MBEDTLS_SSL_DEBUG_MSG(3,
1444 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1447 ext + 4, ext_size)) != 0) {
1448 return ret;
1449 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001453
Hanno Beckera0e20d02019-05-15 14:03:01 +01001454#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 case MBEDTLS_TLS_EXT_CID:
1456 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if ((ret = ssl_parse_cid_ext(ssl,
1459 ext + 4,
1460 ext_size)) != 0) {
1461 return ret;
1462 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001465#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1469 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1472 ext + 4, ext_size)) != 0) {
1473 return ret;
1474 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1481 MBEDTLS_SSL_DEBUG_MSG(3,
1482 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if ((ret = ssl_parse_extended_ms_ext(ssl,
1485 ext + 4, ext_size)) != 0) {
1486 return ret;
1487 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1494 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if ((ret = ssl_parse_session_ticket_ext(ssl,
1497 ext + 4, ext_size)) != 0) {
1498 return ret;
1499 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001503
Valerio Setti7aeec542023-07-05 18:57:21 +02001504#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001505 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001506 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1508 MBEDTLS_SSL_DEBUG_MSG(3,
1509 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1512 ext + 4, ext_size)) != 0) {
1513 return ret;
1514 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001517#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001518 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001519 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001520
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001521#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1523 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1526 ext + 4, ext_size)) != 0) {
1527 return ret;
1528 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001531#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 case MBEDTLS_TLS_EXT_ALPN:
1535 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1538 return ret;
1539 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001543
Johan Pascalb62bb512015-12-03 21:56:45 +01001544#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 case MBEDTLS_TLS_EXT_USE_SRTP:
1546 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1549 return ret;
1550 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001553#endif /* MBEDTLS_SSL_DTLS_SRTP */
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 default:
1556 MBEDTLS_SSL_DEBUG_MSG(3,
1557 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001558 }
1559
1560 ext_len -= 4 + ext_size;
1561 ext += 4 + ext_size;
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (ext_len > 0 && ext_len < 4) {
1564 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1565 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001566 }
1567 }
1568
1569 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001570 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1571 * extensions. It sets the transform data for the resumed session which in
1572 * case of DTLS includes the server CID extracted from the CID extension.
1573 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (ssl->handshake->resume) {
1575 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1576 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001577 mbedtls_ssl_send_alert_message(
1578 ssl,
1579 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1581 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001582 }
1583 }
1584
Paul Bakker48916f92012-09-16 19:57:18 +00001585 /*
1586 * Renegotiation security checks
1587 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001589 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1591 MBEDTLS_SSL_DEBUG_MSG(1,
1592 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001593 handshake_failure = 1;
1594 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 renegotiation_info_seen == 0) {
1599 MBEDTLS_SSL_DEBUG_MSG(1,
1600 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001601 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1603 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1604 ssl->conf->allow_legacy_renegotiation ==
1605 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1606 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001607 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1609 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1610 renegotiation_info_seen == 1) {
1611 MBEDTLS_SSL_DEBUG_MSG(1,
1612 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001613 handshake_failure = 1;
1614 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001618 mbedtls_ssl_send_alert_message(
1619 ssl,
1620 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1622 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001623 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001628}
1629
Valerio Setti48659a12025-01-15 14:22:28 +01001630#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001631MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001632static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1633 unsigned char **p,
1634 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001637 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001638
Paul Bakker29e1f122013-04-16 13:07:56 +02001639 /*
1640 * Ephemeral DH parameters:
1641 *
1642 * struct {
1643 * opaque dh_p<1..2^16-1>;
1644 * opaque dh_g<1..2^16-1>;
1645 * opaque dh_Ys<1..2^16-1>;
1646 * } ServerDHParams;
1647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1649 p, end)) != 0) {
1650 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1651 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001652 }
1653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1655 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1656 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1657 dhm_actual_bitlen,
1658 ssl->conf->dhm_min_bitlen));
1659 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001660 }
1661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1663 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1664 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001667}
Valerio Setti48659a12025-01-15 14:22:28 +01001668#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001669
Andrzej Kurek468c5062022-10-24 10:30:14 -04001670#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1671 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1672 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001673MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001674static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1675 unsigned char **p,
1676 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001677{
1678 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001679 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001680 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001681 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001682 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001683
1684 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001685 * struct {
1686 * ECParameters curve_params;
1687 * ECPoint public;
1688 * } ServerECDHParams;
1689 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001690 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001691 * 2..3 NamedCurve
1692 * 4 ECPoint.len
1693 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001694 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 if (end - *p < 4) {
1696 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1697 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001698
1699 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1701 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1702 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001703
1704 /* Next two bytes are the namedcurve value */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001705 tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
1706 *p += 2;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001707
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001708 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1710 MBEDTLS_SSL_DEBUG_MSG(2,
1711 ("bad server key exchange message (ECDHE curve): %u",
1712 (unsigned) tls_id));
1713 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001714 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001715
Valerio Setti40d9ca92023-01-04 16:08:04 +01001716 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001717 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1719 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001720 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001721 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001722 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001723
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001724 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001725 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if ((size_t) (end - *p) < ecpoint_len) {
1727 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1728 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001729
Gilles Peskinec29df532023-10-02 14:59:26 +02001730 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1732 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001733
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001734 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1735 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001736 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001739}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001740#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1741 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1742 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001743#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001744MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001745static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1746 unsigned char **p,
1747 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001748{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001750 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001751 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001752
1753 /*
1754 * PSK parameters:
1755 *
1756 * opaque psk_identity_hint<0..2^16-1>;
1757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (end - (*p) < 2) {
1759 MBEDTLS_SSL_DEBUG_MSG(1,
1760 ("bad server key exchange message (psk_identity_hint length)"));
1761 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001762 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001763 len = MBEDTLS_GET_UINT16_BE(*p, 0);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001764 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 if (end - (*p) < len) {
1767 MBEDTLS_SSL_DEBUG_MSG(1,
1768 ("bad server key exchange message (psk_identity_hint length)"));
1769 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001770 }
1771
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001772 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001773 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001774 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001775 * someone needs that feature.
1776 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001777 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001778 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001781}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001782#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001783
Gilles Peskineac767e52024-09-20 18:08:44 +02001784#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001785/*
1786 * Generate a pre-master secret and encrypt it with the server's RSA key
1787 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001789static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1790 size_t offset, size_t *olen,
1791 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001792{
Janos Follath865b3eb2019-12-16 11:46:15 +00001793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001794 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001795 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1799 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1800 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001801 }
1802
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001803 /*
1804 * Generate (part of) the pre-master as
1805 * struct {
1806 * ProtocolVersion client_version;
1807 * opaque random[46];
1808 * } PreMasterSecret;
1809 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 mbedtls_ssl_write_version(p, ssl->conf->transport,
1811 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1814 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1815 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001816 }
1817
1818 ssl->handshake->pmslen = 48;
1819
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001820#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1821 peer_pk = &ssl->handshake->peer_pubkey;
1822#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001824 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1826 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001827 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001828 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1829#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001830
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001831 /*
1832 * Now write it out, encrypted
1833 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1835 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1836 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001837 }
1838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if ((ret = mbedtls_pk_encrypt(peer_pk,
1840 p, ssl->handshake->pmslen,
1841 ssl->out_msg + offset + len_bytes, olen,
1842 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1843 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1844 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1845 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001846 }
1847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 if (len_bytes == 2) {
1849 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001850 *olen += 2;
1851 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001852
Hanno Beckerae553dd2019-02-08 14:06:00 +00001853#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1854 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001856#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001858}
Gilles Peskineac767e52024-09-20 18:08:44 +02001859#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1862 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001863MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001864static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001865{
Janos Follath865b3eb2019-12-16 11:46:15 +00001866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001868
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001869#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1870 peer_pk = &ssl->handshake->peer_pubkey;
1871#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001873 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1875 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001876 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001877 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1878#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001879
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02001880 /* This is a public key, so it can't be opaque, so can_do() is a good
1881 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
1883 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
1884 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001885 }
1886
Gilles Peskine7354f1e2024-01-23 11:06:02 +01001887#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti97207782023-05-18 18:59:06 +02001888 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
Gilles Peskine7354f1e2024-01-23 11:06:02 +01001889#endif /* !defined(MBEDTLS_PK_USE_PSA_EC_DATA) */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001890
Valerio Setti2b5d3de2023-01-09 11:04:52 +01001891 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001892 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Settif9362b72023-11-29 08:42:27 +01001893 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01001894
Valerio Setti97207782023-05-18 18:59:06 +02001895 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
1897 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01001898 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01001899
Valerio Setti97207782023-05-18 18:59:06 +02001900 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (tls_id == 0) {
1902 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02001903 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01001905 }
1906
Valerio Setti1e868cc2023-01-09 17:30:01 +01001907 /* If the above conversion to TLS ID was fine, then also this one will be,
1908 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001909 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02001910 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01001911
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001912 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01001913
Przemek Stekielea4000f2022-03-16 09:49:33 +01001914 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02001915#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001916 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
1917 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02001918 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02001919#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02001920 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
1922 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001923 ssl->handshake->xxdh_psa_peerkey,
Gilles Peskinec29df532023-10-02 14:59:26 +02001924 sizeof(ssl->handshake->xxdh_psa_peerkey));
Przemek Stekielea4000f2022-03-16 09:49:33 +01001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 if (ret != 0) {
1927 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
1928 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01001929 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001930 ssl->handshake->xxdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02001931#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Hanno Beckerae553dd2019-02-08 14:06:00 +00001932#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1933 /* We don't need the peer's public key anymore. Free it,
1934 * so that more RAM is available for upcoming expensive
1935 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001937#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1942 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001943
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001944MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001945static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01001946{
Janos Follath865b3eb2019-12-16 11:46:15 +00001947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001948 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00001949 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01001950 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
1956 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001960 ((void) p);
1961 ((void) end);
1962#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1965 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1967 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
1968 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
1969 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001970 mbedtls_ssl_send_alert_message(
1971 ssl,
1972 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1974 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001975 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001978 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001980 }
1981 ((void) p);
1982 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1984 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001985
Gilles Peskineeccd8882020-03-10 12:19:08 +01001986#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (ssl->handshake->ecrs_enabled &&
1988 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02001989 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02001990 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02001991#endif
1992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
1994 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1995 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 }
1997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
1999 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002000 mbedtls_ssl_send_alert_message(
2001 ssl,
2002 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2004 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 }
2006
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002007 /*
Gilles Peskineb3ec1252024-09-20 18:22:04 +02002008 * ServerKeyExchange may be skipped with PSK when the server
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002009 * doesn't use a psk_identity_hint
2010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
Gilles Peskine712e9a12024-09-20 18:11:31 +02002012 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002013 /* Current message is probably either
2014 * CertificateRequest or ServerHelloDone */
2015 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002016 goto exit;
2017 }
2018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 MBEDTLS_SSL_DEBUG_MSG(1,
2020 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002021 mbedtls_ssl_send_alert_message(
2022 ssl,
2023 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 }
2028
Gilles Peskineeccd8882020-03-10 12:19:08 +01002029#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002031 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002033
2034start_processing:
2035#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002037 end = ssl->in_msg + ssl->in_hslen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002038 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002039
Gilles Peskineeccd8882020-03-10 12:19:08 +01002040#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2043 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2044 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002045 mbedtls_ssl_send_alert_message(
2046 ssl,
2047 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2049 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002050 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002051 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002052#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002053
Gilles Peskineac767e52024-09-20 18:08:44 +02002054#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine712e9a12024-09-20 18:11:31 +02002055 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002056 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 } else
Gilles Peskineac767e52024-09-20 18:08:44 +02002058#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Valerio Setti48659a12025-01-15 14:22:28 +01002059#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2060 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2062 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002063 mbedtls_ssl_send_alert_message(
2064 ssl,
2065 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2067 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002068 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 } else
Valerio Setti48659a12025-01-15 14:22:28 +01002070#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002071#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2072 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2077 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2078 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002079 mbedtls_ssl_send_alert_message(
2080 ssl,
2081 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2083 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002084 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2087 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2088 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002089#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002091 /*
2092 * The first 3 bytes are:
2093 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2094 * [1, 2] elliptic curve's TLS ID
2095 *
2096 * However since we only support secp256r1 for now, we check only
2097 * that TLS ID here
2098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002100 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 if (exp_tls_id == 0) {
2104 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002105 }
2106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2108 (read_tls_id != exp_tls_id)) {
2109 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002110 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002111
2112 p += 3;
2113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if ((ret = mbedtls_psa_ecjpake_read_round(
2115 &ssl->handshake->psa_pake_ctx, p, end - p,
2116 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2117 psa_destroy_key(ssl->handshake->psa_pake_password);
2118 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002121 mbedtls_ssl_send_alert_message(
2122 ssl,
2123 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2125 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002128#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002129 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2131 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002132 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002133
Gilles Peskineeccd8882020-03-10 12:19:08 +01002134#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002136 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002137 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2140 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002142 size_t params_len = (size_t) (p - params);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002143 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002144 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002147
Jerry Yu693a47a2022-06-23 14:02:28 +08002148#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2149 peer_pk = &ssl->handshake->peer_pubkey;
2150#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002152 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2154 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002155 }
2156 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2157#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2158
Paul Bakker29e1f122013-04-16 13:07:56 +02002159 /*
2160 * Handle the digitally-signed structure
2161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2163 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2164 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2165 sig_alg, &pk_alg, &md_alg) != 0 &&
2166 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2167 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2168 MBEDTLS_SSL_DEBUG_MSG(1,
2169 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002170 mbedtls_ssl_send_alert_message(
2171 ssl,
2172 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2174 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002175 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002176 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2179 MBEDTLS_SSL_DEBUG_MSG(1,
2180 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002181 mbedtls_ssl_send_alert_message(
2182 ssl,
2183 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2185 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002186 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002187
2188 /*
2189 * Read signature
2190 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (p > end - 2) {
2193 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002194 mbedtls_ssl_send_alert_message(
2195 ssl,
2196 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2198 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002199 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002200 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
Paul Bakker1ef83d62012-04-11 12:09:53 +00002201 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (p != end - sig_len) {
2204 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002205 mbedtls_ssl_send_alert_message(
2206 ssl,
2207 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2209 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002213
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002214 /*
2215 * Compute the hash that has been signed
2216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (md_alg != MBEDTLS_MD_NONE) {
2218 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2219 params, params_len,
2220 md_alg);
2221 if (ret != 0) {
2222 return ret;
2223 }
2224 } else {
2225 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2226 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002227 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002230
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002231 /*
2232 * Verify signature
2233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2235 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002236 mbedtls_ssl_send_alert_message(
2237 ssl,
2238 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2240 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002241 }
2242
Gilles Peskineeccd8882020-03-10 12:19:08 +01002243#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002245 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002247#endif
2248
Jerry Yu693a47a2022-06-23 14:02:28 +08002249#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002251 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2252 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002253 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002254 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if (rsassa_pss_options.expected_salt_len == 0) {
2256 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2257 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2260 peer_pk,
2261 md_alg, hash, hashlen,
2262 p, sig_len);
2263 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002264#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 ret = mbedtls_pk_verify_restartable(peer_pk,
2266 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002269 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002270#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002272#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002274 mbedtls_ssl_send_alert_message(
2275 ssl,
2276 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2278 }
2279 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002280#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002282 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002284#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002286 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002287
2288#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2289 /* We don't need the peer's public key anymore. Free it,
2290 * so that more RAM is available for upcoming expensive
2291 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002293#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002295#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002297exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 ssl->state++;
2299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002303}
2304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002307static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002308{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002309 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002310 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2315 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002316 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002318 }
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2321 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002322}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002323#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002324MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002325static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002326{
Janos Follath865b3eb2019-12-16 11:46:15 +00002327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002328 unsigned char *buf;
2329 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002330 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002331 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002332 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002333 size_t sig_alg_len;
2334#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 unsigned char *sig_alg;
2336 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2342 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002343 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002345 }
2346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2348 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2349 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 }
2351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2353 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002354 mbedtls_ssl_send_alert_message(
2355 ssl,
2356 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2358 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002359 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002361 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002362 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2366 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002369 /* Current message is probably the ServerHelloDone */
2370 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002371 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002372 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002373
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002374 /*
2375 * struct {
2376 * ClientCertificateType certificate_types<1..2^8-1>;
2377 * SignatureAndHashAlgorithm
2378 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2379 * DistinguishedName certificate_authorities<0..2^16-1>;
2380 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002381 *
2382 * Since we only support a single certificate on clients, let's just
2383 * ignore all the information that's supposed to help us pick a
2384 * certificate.
2385 *
2386 * We could check that our certificate matches the request, and bail out
2387 * if it doesn't, but it's simpler to just send the certificate anyway,
2388 * and give the server the opportunity to decide if it should terminate
2389 * the connection when it doesn't like our certificate.
2390 *
2391 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2392 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002393 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002394 *
2395 * However, we still minimally parse the message to check it is at least
2396 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002397 */
Paul Bakker926af752012-11-23 13:38:07 +01002398 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002399
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002400 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2402 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2403 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2404 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2405 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002406 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002408 n = cert_type_len;
2409
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002410 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002411 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002412 * * the length of the signature algorithms field (if minor version of
2413 * SSL is 3),
2414 * * distinguished name length otherwise.
2415 * Both reach at most the index:
2416 * ...hdr_len + 2 + n,
2417 * therefore the buffer length at this point must be greater than that
2418 * regardless of the actual code path.
2419 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2421 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2422 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2423 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2424 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002425 }
2426
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002427 /* supported_signature_algorithms */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002428 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002429
2430 /*
2431 * The furthest access in buf is in the loop few lines below:
2432 * sig_alg[i + 1],
2433 * where:
2434 * sig_alg = buf + ...hdr_len + 3 + n,
2435 * max(i) = sig_alg_len - 1.
2436 * Therefore the furthest access is:
2437 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2438 * which reduces to:
2439 * buf[...hdr_len + 3 + n + sig_alg_len],
2440 * which is one less than we need the buf to be.
2441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2443 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002444 mbedtls_ssl_send_alert_message(
2445 ssl,
2446 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2448 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002449 }
Paul Bakker926af752012-11-23 13:38:07 +01002450
Ronald Cron90915f22022-03-07 11:11:36 +01002451#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2453 for (size_t i = 0; i < sig_alg_len; i += 2) {
2454 MBEDTLS_SSL_DEBUG_MSG(3,
2455 ("Supported Signature Algorithm found: %02x %02x",
2456 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002457 }
2458#endif
2459
2460 n += 2 + sig_alg_len;
2461
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002462 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002463 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002464
2465 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2467 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2468 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2469 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2470 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002471 }
2472
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002473#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2475 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002476 unsigned char *p = dn + i + 2;
2477 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002478 size_t asn1_len;
2479 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 memset(&name, 0, sizeof(name));
2481 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2482 if (dni_len > dn_len - i - 2 ||
2483 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2484 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2485 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2486 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002487 mbedtls_ssl_send_alert_message(
2488 ssl,
2489 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2491 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002492 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 MBEDTLS_SSL_DEBUG_MSG(3,
2494 ("DN hint: %.*s",
2495 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2496 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002497 }
2498#endif
2499
Paul Bakker926af752012-11-23 13:38:07 +01002500exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002504}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002505#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002506
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002507MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002508static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002509{
Janos Follath865b3eb2019-12-16 11:46:15 +00002510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2515 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2516 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2520 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2521 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002522 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2525 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2526 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2527 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2528 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2529 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 }
2531
2532 ssl->state++;
2533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2536 mbedtls_ssl_recv_flight_completed(ssl);
2537 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002538#endif
2539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002543}
2544
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002545MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002546static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002547{
Janos Follath865b3eb2019-12-16 11:46:15 +00002548 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002549
2550 size_t header_len;
2551 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002552 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002553 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 /*
2560 * DHM key exchange -- send G^X mod P
2561 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002565 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2568 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2569 &ssl->out_msg[header_len], content_len,
2570 ssl->conf->f_rng, ssl->conf->p_rng);
2571 if (ret != 0) {
2572 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2573 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
2575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2577 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2580 ssl->handshake->premaster,
2581 MBEDTLS_PREMASTER_SIZE,
2582 &ssl->handshake->pmslen,
2583 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2584 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2585 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
2587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2589 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002591#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2592 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2593 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2594 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002596 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2597 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Andrzej Kureka0237f82022-02-24 13:24:52 -05002599 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2600 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002601 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002602
2603 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2604
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002605 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002608
Hanno Becker4a63ed42019-01-08 11:39:35 +00002609 /*
2610 * Generate EC private key for ECDHE exchange.
2611 */
2612
Hanno Becker4a63ed42019-01-08 11:39:35 +00002613 /* The master secret is obtained from the shared ECDH secret by
2614 * applying the TLS 1.2 PRF with a specific salt and label. While
2615 * the PSA Crypto API encourages combining key agreement schemes
2616 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2617 * yet support the provisioning of salt + label to the KDF.
2618 * For the time being, we therefore need to split the computation
2619 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002620 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2622 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002623 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002624 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002625
2626 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002628 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (status != PSA_SUCCESS) {
2630 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2631 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002632
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002633 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002634 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002635 * but we just need to add a length byte before that. */
2636 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2637 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002639 size_t own_pubkey_len;
2640
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002641 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 own_pubkey, own_pubkey_max_len,
2643 &own_pubkey_len);
2644 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002645 psa_destroy_key(handshake->xxdh_psa_privkey);
2646 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002648 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002649
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002650 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2651 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002652
Hanno Becker4a63ed42019-01-08 11:39:35 +00002653 /* The ECDH secret is the premaster secret used for key derivation. */
2654
Janos Follathdf3b0892019-08-08 11:12:24 +01002655 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002657 handshake->xxdh_psa_privkey,
2658 handshake->xxdh_psa_peerkey,
2659 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 ssl->handshake->premaster,
2661 sizeof(ssl->handshake->premaster),
2662 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002663
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002664 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2665 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2668 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2669 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2672 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2673 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2674 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002675#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2676 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2679 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2680 psa_key_attributes_t key_attributes;
2681
2682 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2683
2684 /*
2685 * opaque psk_identity<0..2^16-1>;
2686 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002688 /* We don't offer PSK suites if we don't have a PSK,
2689 * and we check that the server's choice is among the
2690 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2692 }
Neil Armstrong868af822022-03-09 10:26:25 +01002693
Neil Armstrongfc834f22022-03-23 17:54:38 +01002694 /* uint16 to store content length */
2695 const size_t content_len_size = 2;
2696
Neil Armstrong868af822022-03-09 10:26:25 +01002697 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 if (header_len + content_len_size + ssl->conf->psk_identity_len
2700 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2701 MBEDTLS_SSL_DEBUG_MSG(1,
2702 ("psk identity too long or SSL buffer too short"));
2703 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002704 }
2705
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002706 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2709 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002710 header_len += content_len_size;
2711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 memcpy(p, ssl->conf->psk_identity,
2713 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002714 p += ssl->conf->psk_identity_len;
2715
Neil Armstrong868af822022-03-09 10:26:25 +01002716 header_len += ssl->conf->psk_identity_len;
2717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002719
2720 /*
2721 * Generate EC private key for ECDHE exchange.
2722 */
2723
2724 /* The master secret is obtained from the shared ECDH secret by
2725 * applying the TLS 1.2 PRF with a specific salt and label. While
2726 * the PSA Crypto API encourages combining key agreement schemes
2727 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2728 * yet support the provisioning of salt + label to the KDF.
2729 * For the time being, we therefore need to split the computation
2730 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2731 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2733 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002734 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002735 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002736
2737 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002739 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002741 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 }
Neil Armstrong868af822022-03-09 10:26:25 +01002743
2744 /* Export the public part of the ECDH private key from PSA.
2745 * The export format is an ECPoint structure as expected by TLS,
2746 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002747 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002748 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002750 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002751
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002752 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 own_pubkey, own_pubkey_max_len,
2754 &own_pubkey_len);
2755 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002756 psa_destroy_key(handshake->xxdh_psa_privkey);
2757 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002758 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002759 }
2760
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002761 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002762 content_len = own_pubkey_len + 1;
2763
Neil Armstrong25400452022-03-23 17:44:07 +01002764 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2765 * - a uint16 containing the length (in octets) of the ECDH computation
2766 * - the octet string produced by the ECDH computation
2767 * - a uint16 containing the length (in octets) of the PSK
2768 * - the PSK itself
2769 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002770 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 const unsigned char * const pms_end = pms +
2772 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002773 /* uint16 to store length (in octets) of the ECDH computation */
2774 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002775 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002776
Neil Armstrong25400452022-03-23 17:44:07 +01002777 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002779 handshake->xxdh_psa_privkey,
2780 handshake->xxdh_psa_peerkey,
2781 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 pms + zlen_size,
2783 pms_end - (pms + zlen_size),
2784 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01002785
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002786 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2787 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01002788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002790 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002792 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 }
Neil Armstrong868af822022-03-09 10:26:25 +01002794
Neil Armstrong25400452022-03-23 17:44:07 +01002795 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002797 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 } else
Neil Armstrong868af822022-03-09 10:26:25 +01002799#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2800 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002801#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002803 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002804 * opaque psk_identity<0..2^16-1>;
2805 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01002807 /* We don't offer PSK suites if we don't have a PSK,
2808 * and we check that the server's choice is among the
2809 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02002811 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002812
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002813 header_len = 4;
2814 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2817 MBEDTLS_SSL_DEBUG_MSG(1,
2818 ("psk identity too long or SSL buffer too short"));
2819 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002820 }
2821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
2823 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 memcpy(ssl->out_msg + header_len,
2826 ssl->conf->psk_identity,
2827 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002828 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002832 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002834#endif
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002835#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2837 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002838 /*
2839 * ClientECDiffieHellmanPublic public;
2840 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2842 &content_len,
2843 &ssl->out_msg[header_len],
2844 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
2845 ssl->conf->f_rng, ssl->conf->p_rng);
2846 if (ret != 0) {
2847 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
2848 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002849 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2852 MBEDTLS_DEBUG_ECDH_Q);
2853 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002854#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002855 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2857 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002858 }
2859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01002861#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002864 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
2866 &content_len, 0)) != 0) {
2867 return ret;
2868 }
2869 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002871#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002873 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002874
Neil Armstrongca7d5062022-05-31 14:43:23 +02002875 unsigned char *out_p = ssl->out_msg + header_len;
2876 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
2877 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
2879 out_p, end_p - out_p, &content_len,
2880 MBEDTLS_ECJPAKE_ROUND_TWO);
2881 if (ret != 0) {
2882 psa_destroy_key(ssl->handshake->psa_pake_password);
2883 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2884 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
2885 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002886 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002888#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002889 {
2890 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2892 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02002893 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002895 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2897 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
2899 ssl->state++;
2900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2902 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2903 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 }
2905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002909}
2910
Gilles Peskineeccd8882020-03-10 12:19:08 +01002911#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002912MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002913static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002914{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002915 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002916 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00002917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2922 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2923 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002924 }
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2927 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02002928 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02002930 }
2931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2933 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002934}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002935#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002936MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002937static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002940 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002941 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002942 size_t n = 0, offset = 0;
2943 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002944 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002946 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002947 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02002948#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002949 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02002950#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002951 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 +02002952#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002955
Gilles Peskineeccd8882020-03-10 12:19:08 +01002956#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (ssl->handshake->ecrs_enabled &&
2958 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002959 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002960 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002961#endif
2962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2964 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2965 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002966 }
2967
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2969 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002970 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002972 }
2973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 if (ssl->handshake->client_auth == 0 ||
2975 mbedtls_ssl_own_cert(ssl) == NULL) {
2976 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02002977 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 }
2980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 if (mbedtls_ssl_own_key(ssl) == NULL) {
2982 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
2983 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002984 }
2985
2986 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002987 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002989#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002991 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002993
2994sign:
2995#endif
2996
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002997 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
2998 if (0 != ret) {
2999 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3000 return ret;
3001 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003002
Ronald Cron90915f22022-03-07 11:11:36 +01003003 /*
3004 * digitally-signed struct {
3005 * opaque handshake_messages[handshake_messages_length];
3006 * };
3007 *
3008 * Taking shortcut here. We assume that the server always allows the
3009 * PRF Hash function and has sent it in the allowed signature
3010 * algorithms list received in the Certificate Request message.
3011 *
3012 * Until we encounter a server that does not, we will take this
3013 * shortcut.
3014 *
3015 * Reason: Otherwise we should have running hashes for SHA512 and
3016 * SHA224 in order to satisfy 'weird' needs from the server
3017 * side.
3018 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003020 md_alg = MBEDTLS_MD_SHA384;
3021 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003023 md_alg = MBEDTLS_MD_SHA256;
3024 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003025 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003027
3028 /* Info from md_alg will be used instead */
3029 hashlen = 0;
3030 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003031
Gilles Peskineeccd8882020-03-10 12:19:08 +01003032#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003034 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003036#endif
3037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3039 md_alg, hash_start, hashlen,
3040 ssl->out_msg + 6 + offset,
3041 out_buf_len - 6 - offset,
3042 &n,
3043 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3044 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003045#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003047 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003049#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003051 }
Paul Bakker926af752012-11-23 13:38:07 +01003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003054
Paul Bakker1ef83d62012-04-11 12:09:53 +00003055 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3057 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
3059 ssl->state++;
3060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3062 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3063 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003064 }
3065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003069}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003070#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003073MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003074static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003075{
Janos Follath865b3eb2019-12-16 11:46:15 +00003076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003077 uint32_t lifetime;
3078 size_t ticket_len;
3079 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003080 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3085 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3086 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003087 }
3088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3090 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003091 mbedtls_ssl_send_alert_message(
3092 ssl,
3093 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3095 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003096 }
3097
3098 /*
3099 * struct {
3100 * uint32 ticket_lifetime_hint;
3101 * opaque ticket<0..2^16-1>;
3102 * } NewSessionTicket;
3103 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003104 * 0 . 3 ticket_lifetime_hint
3105 * 4 . 5 ticket_len (n)
3106 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3109 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3110 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3111 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3112 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3113 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003114 }
3115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003117
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003118 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003119
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003120 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3123 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3124 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3125 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3126 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003127 }
3128
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003130
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003131 /* We're not waiting for a NewSessionTicket message any more */
3132 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003134
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003135 /*
3136 * Zero-length ticket means the server changed his mind and doesn't want
3137 * to send a ticket after all, so just forget it
3138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 if (ticket_len == 0) {
3140 return 0;
3141 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003142
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003144 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003146 ssl->session->ticket = NULL;
3147 ssl->session->ticket_len = 0;
3148 }
3149
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003150 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003152 ssl->session_negotiate->ticket = NULL;
3153 ssl->session_negotiate->ticket_len = 0;
3154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3156 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3157 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3158 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3159 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003160 }
3161
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003163
3164 ssl->session_negotiate->ticket = ticket;
3165 ssl->session_negotiate->ticket_len = ticket_len;
3166 ssl->session_negotiate->ticket_lifetime = lifetime;
3167
3168 /*
3169 * RFC 5077 section 3.4:
3170 * "If the client receives a session ticket from the server, then it
3171 * discards any Session ID that was sent in the ServerHello."
3172 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003174 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003175
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003177
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003181
Paul Bakker5121ce52009-01-03 21:22:43 +00003182/*
Paul Bakker1961b702013-01-25 14:49:24 +01003183 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003185int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003186{
3187 int ret = 0;
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003190 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3193 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003194 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003195 }
3196#endif
3197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 case MBEDTLS_SSL_HELLO_REQUEST:
3200 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 break;
3202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 /*
3204 * ==> ClientHello
3205 */
3206 case MBEDTLS_SSL_CLIENT_HELLO:
3207 ret = mbedtls_ssl_write_client_hello(ssl);
3208 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 /*
3211 * <== ServerHello
3212 * Certificate
3213 * ( ServerKeyExchange )
3214 * ( CertificateRequest )
3215 * ServerHelloDone
3216 */
3217 case MBEDTLS_SSL_SERVER_HELLO:
3218 ret = ssl_parse_server_hello(ssl);
3219 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3222 ret = mbedtls_ssl_parse_certificate(ssl);
3223 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3226 ret = ssl_parse_server_key_exchange(ssl);
3227 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3230 ret = ssl_parse_certificate_request(ssl);
3231 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3234 ret = ssl_parse_server_hello_done(ssl);
3235 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 /*
3238 * ==> ( Certificate/Alert )
3239 * ClientKeyExchange
3240 * ( CertificateVerify )
3241 * ChangeCipherSpec
3242 * Finished
3243 */
3244 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3245 ret = mbedtls_ssl_write_certificate(ssl);
3246 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003247
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3249 ret = ssl_write_client_key_exchange(ssl);
3250 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3253 ret = ssl_write_certificate_verify(ssl);
3254 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3257 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3258 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 case MBEDTLS_SSL_CLIENT_FINISHED:
3261 ret = mbedtls_ssl_write_finished(ssl);
3262 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003263
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 /*
3265 * <== ( NewSessionTicket )
3266 * ChangeCipherSpec
3267 * Finished
3268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3271 ret = ssl_parse_new_session_ticket(ssl);
3272 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003273#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3276 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3277 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 case MBEDTLS_SSL_SERVER_FINISHED:
3280 ret = mbedtls_ssl_parse_finished(ssl);
3281 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 case MBEDTLS_SSL_FLUSH_BUFFERS:
3284 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3285 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3286 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3289 mbedtls_ssl_handshake_wrapup(ssl);
3290 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 default:
3293 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3294 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3295 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003298}
Jerry Yuc5aef882021-12-23 20:15:02 +08003299
Jerry Yufb4b6472022-01-27 15:03:26 +08003300#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */