blob: 1b1f85e41953260a5c8ec0b8d891e3e20ad49ffd [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS client-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "ssl_misc.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020013
SimonBd5800b72016-04-26 07:43:27 +010014#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010015#include "ssl_client.h"
Valerio Settib4f50762024-01-17 10:24:52 +010016#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000017#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020018#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010019
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020020#include "psa_util_internal.h"
Ronald Cron69a63422021-10-18 09:47:58 +020021#include "psa/crypto.h"
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040022#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Andrzej Kurek00644842023-05-30 05:45:00 -040023/* Define a local translating function to save code size by not using too many
24 * arguments in each translating place. */
25static int local_err_translation(psa_status_t status)
26{
27 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040028 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040029 psa_generic_status_to_mbedtls);
30}
31#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040032#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +000033
SimonBd5800b72016-04-26 07:43:27 +010034#include <string.h>
35
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020036#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010039#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050043#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020047MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010048static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
49 unsigned char *buf,
50 const unsigned char *end,
51 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010052{
53 unsigned char *p = buf;
54
55 *olen = 0;
56
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010057 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010058 * initial ClientHello, in which case also adding the renegotiation
59 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
61 return 0;
62 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 MBEDTLS_SSL_DEBUG_MSG(3,
65 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010068
Paul Bakkerd3edc862013-03-20 16:07:17 +010069 /*
70 * Secure renegotiation
71 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010073 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010074
75 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
77 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010080
81 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010084}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010086
Valerio Setti7aeec542023-07-05 18:57:21 +020087#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +020088 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010089 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010090
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010092static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
93 unsigned char *buf,
94 const unsigned char *end,
95 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010096{
97 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +010098 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +010099
100 *olen = 0;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 MBEDTLS_SSL_DEBUG_MSG(3,
103 ("client hello, adding supported_point_formats extension"));
104 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100107 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100108
109 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100110 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200111
112 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100114
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200115 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118}
Valerio Setti7aeec542023-07-05 18:57:21 +0200119#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200120 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200121 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100122
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200123#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200124MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100125static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
126 unsigned char *buf,
127 const unsigned char *end,
128 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200129{
Janos Follath865b3eb2019-12-16 11:46:15 +0000130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200131 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100132 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200133
134 *olen = 0;
135
136 /* Skip costly extension if we can't use EC J-PAKE anyway */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
138 return 0;
139 }
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 MBEDTLS_SSL_DEBUG_MSG(3,
142 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100147 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200148
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200149 /*
150 * We may need to send ClientHello multiple times for Hello verification.
151 * We don't want to compute fresh values every time (both for performance
152 * and consistency reasons), so cache the extension content.
153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (ssl->handshake->ecjpake_cache == NULL ||
155 ssl->handshake->ecjpake_cache_len == 0) {
156 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200157
Valerio Setti6b3dab02022-11-17 17:14:54 +0100158 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 p + 2, end - p - 2, &kkpp_len,
160 MBEDTLS_ECJPAKE_ROUND_ONE);
161 if (ret != 0) {
162 psa_destroy_key(ssl->handshake->psa_pake_password);
163 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
164 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
165 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200166 }
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
169 if (ssl->handshake->ecjpake_cache == NULL) {
170 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
171 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200172 }
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200175 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 } else {
177 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200178
179 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100186 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200187
188 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200191}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200192#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000193
Hanno Beckera0e20d02019-05-15 14:03:01 +0100194#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200195MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100196static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
197 unsigned char *buf,
198 const unsigned char *end,
199 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100200{
201 unsigned char *p = buf;
202 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100203
204 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100205 * struct {
206 * opaque cid<0..2^8-1>;
207 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100209
210 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
212 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
213 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100214 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100216
217 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
218 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100220
221 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100223 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100224 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100226 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100227
228 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100230
231 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100234}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100235#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200238MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100239static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
240 unsigned char *buf,
241 const unsigned char *end,
242 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200243{
244 unsigned char *p = buf;
245
Simon Butcher0fc94e92015-09-28 20:52:04 +0100246 *olen = 0;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
249 return 0;
250 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 MBEDTLS_SSL_DEBUG_MSG(3,
253 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100258 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200259
260 *p++ = 0x00;
261 *p++ = 1;
262
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200263 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200264
265 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200272MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100273static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
274 unsigned char *buf,
275 const unsigned char *end,
276 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100277{
278 unsigned char *p = buf;
279
Simon Butcher0fc94e92015-09-28 20:52:04 +0100280 *olen = 0;
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
283 return 0;
284 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 MBEDTLS_SSL_DEBUG_MSG(3,
287 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100292 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100293
294 *p++ = 0x00;
295 *p++ = 0x00;
296
297 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200304MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100305static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
306 unsigned char *buf,
307 const unsigned char *end,
308 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200309{
310 unsigned char *p = buf;
311
Simon Butcher0fc94e92015-09-28 20:52:04 +0100312 *olen = 0;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
315 return 0;
316 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 MBEDTLS_SSL_DEBUG_MSG(3,
319 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100324 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200325
326 *p++ = 0x00;
327 *p++ = 0x00;
328
329 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200336MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100337static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
338 unsigned char *buf,
339 const unsigned char *end,
340 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200341{
342 unsigned char *p = buf;
343 size_t tlen = ssl->session_negotiate->ticket_len;
344
Simon Butcher0fc94e92015-09-28 20:52:04 +0100345 *olen = 0;
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
348 return 0;
349 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 MBEDTLS_SSL_DEBUG_MSG(3,
352 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200353
Hanno Becker261602c2017-04-12 14:54:42 +0100354 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100358 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100361 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200362
363 *olen = 4;
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
366 return 0;
367 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 MBEDTLS_SSL_DEBUG_MSG(3,
370 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200373
374 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200379
Ron Eldora9788042018-12-05 11:04:31 +0200380#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200381MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100382static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
383 unsigned char *buf,
384 const unsigned char *end,
385 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100386{
387 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200388 size_t protection_profiles_index = 0, ext_len = 0;
389 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100390
391 *olen = 0;
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
394 (ssl->conf->dtls_srtp_profile_list == NULL) ||
395 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
396 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100397 }
398
Ron Eldora9788042018-12-05 11:04:31 +0200399 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100400 * uint8 SRTPProtectionProfile[2];
401 *
402 * struct {
403 * SRTPProtectionProfiles SRTPProtectionProfiles;
404 * opaque srtp_mki<0..255>;
405 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100406 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200409 mki_len = ssl->dtls_srtp_info.mki_len;
410 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300411 /* Extension length = 2 bytes for profiles length,
412 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
413 * 1 byte for srtp_mki vector length and the mki_len value
414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200418
419 /* Check there is room in the buffer for the extension + 4 bytes
420 * - the extension tag (2 bytes)
421 * - the extension length (2 bytes)
422 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100426 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100429 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100430
Ron Eldor3adb9922017-12-21 10:15:08 +0200431 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200432 /* micro-optimization:
433 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
434 * which is lower than 127, so the upper byte of the length is always 0
435 * For the documentation, the more generic code is left in comments
436 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
437 * >> 8 ) & 0xFF );
438 */
439 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300443 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200445 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
447 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
448 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
449 profile_value));
450 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100451 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200453 /*
454 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200455 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 MBEDTLS_SSL_DEBUG_MSG(3,
458 ("client hello, "
459 "illegal DTLS-SRTP protection profile %d",
460 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
461 ));
462 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100463 }
464 }
465
Ron Eldor591f1622018-01-22 12:30:04 +0200466 *p++ = mki_len & 0xFF;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (mki_len != 0) {
469 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200470 /*
471 * Increment p to point to the current position.
472 */
473 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
475 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200476 }
477
Ron Eldoref72faf2018-07-12 11:54:20 +0300478 /*
479 * total extension length: extension type (2 bytes)
480 * + extension length (2 bytes)
481 * + protection profile length (2 bytes)
482 * + 2 * number of protection profiles
483 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200484 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300485 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200486 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100489}
490#endif /* MBEDTLS_SSL_DTLS_SRTP */
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
493 unsigned char *buf,
494 const unsigned char *end,
495 int uses_ec,
496 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100497{
498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
499 unsigned char *p = buf;
500 size_t ext_len = 0;
501
502 (void) ssl;
503 (void) end;
504 (void) uses_ec;
505 (void) ret;
506 (void) ext_len;
507
508 *out_len = 0;
509
510 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
511 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
512#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
514 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
515 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100516 }
517 p += ext_len;
518#endif
519
Valerio Setti7aeec542023-07-05 18:57:21 +0200520#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200521 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Ronald Cron12dcdf02022-02-16 15:28:22 +0100522 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (uses_ec) {
524 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
525 &ext_len)) != 0) {
526 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
527 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100528 }
529 p += ext_len;
530 }
531#endif
532
533#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
535 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
536 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100537 }
538 p += ext_len;
539#endif
540
541#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
543 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
544 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100545 }
546 p += ext_len;
547#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
548
549#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
551 &ext_len)) != 0) {
552 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
553 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100554 }
555 p += ext_len;
556#endif
557
558#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
560 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
561 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100562 }
563 p += ext_len;
564#endif
565
566#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
568 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
569 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100570 }
571 p += ext_len;
572#endif
573
574#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
576 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
577 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100578 }
579 p += ext_len;
580#endif
581
582#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
584 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
585 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100586 }
587 p += ext_len;
588#endif
589
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000590 *out_len = (size_t) (p - buf);
Ronald Cron12dcdf02022-02-16 15:28:22 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100593}
594
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200595MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100596static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
597 const unsigned char *buf,
598 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100602 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000604 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_ct_memcmp(buf + 1,
606 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
607 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
608 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
609 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100610 mbedtls_ssl_send_alert_message(
611 ssl,
612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
614 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000615 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100618 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (len != 1 || buf[0] != 0x00) {
620 MBEDTLS_SSL_DEBUG_MSG(1,
621 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100622 mbedtls_ssl_send_alert_message(
623 ssl,
624 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
626 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100627 }
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100630 }
Paul Bakker48916f92012-09-16 19:57:18 +0000631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000633}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200636MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100637static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
638 const unsigned char *buf,
639 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200640{
641 /*
642 * server should use the extension only if we did,
643 * and if so the server's value should match ours (and len is always 1)
644 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200646 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 buf[0] != ssl->conf->mfl_code) {
648 MBEDTLS_SSL_DEBUG_MSG(1,
649 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100650 mbedtls_ssl_send_alert_message(
651 ssl,
652 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
654 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200655 }
656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000660
Hanno Beckera0e20d02019-05-15 14:03:01 +0100661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200662MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100663static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
664 const unsigned char *buf,
665 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100666{
667 size_t peer_cid_len;
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100670 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
671 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
673 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
674 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
675 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
676 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (len == 0) {
680 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
681 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
682 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
683 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100684 }
685
686 peer_cid_len = *buf++;
687 len--;
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
690 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
691 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
692 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
693 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100694 }
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (len != peer_cid_len) {
697 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
698 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
699 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
700 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100701 }
702
Hanno Becker5a299902019-05-03 12:47:49 +0100703 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100704 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
708 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100711}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100712#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200715MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100716static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
717 const unsigned char *buf,
718 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100719{
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
721 len != 0) {
722 MBEDTLS_SSL_DEBUG_MSG(1,
723 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100724 mbedtls_ssl_send_alert_message(
725 ssl,
726 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
728 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100729 }
730
731 ((void) buf);
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200740MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100741static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
742 const unsigned char *buf,
743 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200744{
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
746 len != 0) {
747 MBEDTLS_SSL_DEBUG_MSG(1,
748 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100749 mbedtls_ssl_send_alert_message(
750 ssl,
751 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
753 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200754 }
755
756 ((void) buf);
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200765MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100766static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
767 const unsigned char *buf,
768 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200769{
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
771 len != 0) {
772 MBEDTLS_SSL_DEBUG_MSG(1,
773 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100774 mbedtls_ssl_send_alert_message(
775 ssl,
776 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
778 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200779 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200780
781 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200782
783 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200788
Valerio Setti7aeec542023-07-05 18:57:21 +0200789#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200790 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100791 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200792MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100793static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
794 const unsigned char *buf,
795 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200796{
797 size_t list_size;
798 const unsigned char *p;
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (len == 0 || (size_t) (buf[0] + 1) != len) {
801 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
802 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
803 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
804 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200805 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200806 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200807
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200808 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 while (list_size > 0) {
810 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
811 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
813 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200814 }
815
816 list_size--;
817 p++;
818 }
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
821 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
822 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
823 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200824}
Valerio Setti7aeec542023-07-05 18:57:21 +0200825#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200826 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200827 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200828
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200829#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200830MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100831static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
832 const unsigned char *buf,
833 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200834{
Janos Follath865b3eb2019-12-16 11:46:15 +0000835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (ssl->handshake->ciphersuite_info->key_exchange !=
838 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
839 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
840 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200841 }
842
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200843 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200845 ssl->handshake->ecjpake_cache = NULL;
846 ssl->handshake->ecjpake_cache_len = 0;
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if ((ret = mbedtls_psa_ecjpake_read_round(
849 &ssl->handshake->psa_pake_ctx, buf, len,
850 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
851 psa_destroy_key(ssl->handshake->psa_pake_password);
852 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100855 mbedtls_ssl_send_alert_message(
856 ssl,
857 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
859 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200860 }
861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200863}
864#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200867MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100868static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
869 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200870{
871 size_t list_len, name_len;
872 const char **p;
873
874 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (ssl->conf->alpn_list == NULL) {
876 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100877 mbedtls_ssl_send_alert_message(
878 ssl,
879 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
881 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200882 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200883
884 /*
885 * opaque ProtocolName<1..2^8-1>;
886 *
887 * struct {
888 * ProtocolName protocol_name_list<2..2^16-1>
889 * } ProtocolNameList;
890 *
891 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
892 */
893
894 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (len < 4) {
896 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
897 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
898 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200899 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200900
Dave Rodgmana3d0f612023-11-03 23:34:02 +0000901 list_len = MBEDTLS_GET_UINT16_BE(buf, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (list_len != len - 2) {
903 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
904 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
905 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200906 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200907
908 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (name_len != list_len - 1) {
910 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
911 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
912 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200913 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200914
915 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
917 if (name_len == strlen(*p) &&
918 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200919 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200921 }
922 }
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
925 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
926 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
927 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200930
Johan Pascalb62bb512015-12-03 21:56:45 +0100931#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200932MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100933static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
934 const unsigned char *buf,
935 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100936{
Johan Pascal43f94902020-09-22 12:25:52 +0200937 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200938 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100939 uint16_t server_protection_profile_value = 0;
940
941 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
943 (ssl->conf->dtls_srtp_profile_list == NULL) ||
944 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
945 return 0;
946 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100947
Ron Eldora9788042018-12-05 11:04:31 +0200948 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100949 * uint8 SRTPProtectionProfile[2];
950 *
951 * struct {
952 * SRTPProtectionProfiles SRTPProtectionProfiles;
953 * opaque srtp_mki<0..255>;
954 * } UseSRTPData;
955
956 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
957 *
Johan Pascalb62bb512015-12-03 21:56:45 +0100958 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200960 mki_len = ssl->dtls_srtp_info.mki_len;
961 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100962
Ron Eldoref72faf2018-07-12 11:54:20 +0300963 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200964 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
965 * + protection profile (2 bytes)
966 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200967 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +0300968 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if ((len < 5) || (len != (buf[4] + 5u))) {
970 return MBEDTLS_ERR_SSL_DECODE_ERROR;
971 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100972
973 /*
974 * get the server protection profile
975 */
Ron Eldoref72faf2018-07-12 11:54:20 +0300976
977 /*
978 * protection profile length must be 0x0002 as we must have only
979 * one protection profile in server Hello
980 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if ((buf[0] != 0) || (buf[1] != 2)) {
982 return MBEDTLS_ERR_SSL_DECODE_ERROR;
983 }
Ron Eldor089c9fe2018-12-06 17:12:49 +0200984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +0200986 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 server_protection_profile_value);
988 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
989 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
990 mbedtls_ssl_get_srtp_profile_as_string(
991 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +0100992 }
993
Johan Pascal43f94902020-09-22 12:25:52 +0200994 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200995
Johan Pascalb62bb512015-12-03 21:56:45 +0100996 /*
997 * Check we have the server profile in our list
998 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1000 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001001 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001003 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001005 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001006 }
1007 }
1008
Ron Eldor591f1622018-01-22 12:30:04 +02001009 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1011 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1012 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1013 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001014 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001015
1016 /* If server does not use mki in its reply, make sure the client won't keep
1017 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001019 ssl->dtls_srtp_info.mki_len = 0;
1020 }
1021
Ron Eldoref72faf2018-07-12 11:54:20 +03001022 /*
1023 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001024 * If the client detects a nonzero-length MKI in the server's response
1025 * that is different than the one the client offered, then the client
1026 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (len > 5 && (buf[4] != mki_len ||
1029 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1030 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1031 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1032 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001033 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001034#if defined(MBEDTLS_DEBUG_C)
1035 if (len > 5) {
1036 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1037 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001038 }
1039#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001041}
1042#endif /* MBEDTLS_SSL_DTLS_SRTP */
1043
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001044/*
1045 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001048MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001049static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001050{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001051 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001053 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001054
1055#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1056 uint8_t cookie_len;
1057#else
1058 uint16_t cookie_len;
1059#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001062
Gilles Peskineb64bf062019-09-27 14:02:44 +02001063 /* Check that there is enough room for:
1064 * - 2 bytes of version
1065 * - 1 byte of cookie_len
1066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1068 MBEDTLS_SSL_DEBUG_MSG(1,
1069 ("incoming HelloVerifyRequest message is too short"));
1070 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1071 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1072 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001073 }
1074
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001075 /*
1076 * struct {
1077 * ProtocolVersion server_version;
1078 * opaque cookie<0..2^8-1>;
1079 * } HelloVerifyRequest;
1080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1082 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001083 p += 2;
1084
TRodziewicz2d8800e2021-05-13 19:14:19 +02001085 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001086 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1087 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1088 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1091 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1094 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001097 }
1098
1099 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1101 MBEDTLS_SSL_DEBUG_MSG(1,
1102 ("cookie length does not match incoming message size"));
1103 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1104 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1105 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001106 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1112 if (ssl->handshake->cookie == NULL) {
1113 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1114 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001115 }
1116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001118 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001119
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001120 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001122 ret = mbedtls_ssl_reset_checksum(ssl);
1123 if (0 != ret) {
1124 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1125 return ret;
1126 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001135
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001136MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001137static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001138{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001139 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001140 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001141 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001142 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001143 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001145 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001146#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001147 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001153 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1155 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 }
1157
Hanno Becker79594fd2019-05-08 09:38:41 +01001158 buf = ssl->in_msg;
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001163 ssl->renego_records_seen++;
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (ssl->conf->renego_max_records >= 0 &&
1166 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1167 MBEDTLS_SSL_DEBUG_MSG(1,
1168 ("renegotiation requested, but not honored by server"));
1169 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 MBEDTLS_SSL_DEBUG_MSG(1,
1173 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001174
1175 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001177 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001181 mbedtls_ssl_send_alert_message(
1182 ssl,
1183 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1185 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 }
1187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1190 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1191 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1192 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1193 return ssl_parse_hello_verify_request(ssl);
1194 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001195 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001197 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001198 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001199 }
1200 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1204 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1205 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1206 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1207 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1208 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 }
1210
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001211 /*
1212 * 0 . 1 server_version
1213 * 2 . 33 random (maybe including 4 bytes of Unix time)
1214 * 34 . 34 session_id length = n
1215 * 35 . 34+n session_id
1216 * 35+n . 36+n cipher_suite
1217 * 37+n . 37+n compression_method
1218 *
1219 * 38+n . 39+n extensions length (optional)
1220 * 40+n . .. extensions
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01001225 ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
1226 ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001227 ssl->session_negotiate->tls_version = ssl->tls_version;
Ronald Cron17ef8df2023-11-22 10:29:42 +01001228 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if (ssl->tls_version < ssl->conf->min_tls_version ||
1231 ssl->tls_version > ssl->conf->max_tls_version) {
1232 MBEDTLS_SSL_DEBUG_MSG(1,
1233 (
1234 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1235 (unsigned) ssl->conf->min_tls_version,
1236 (unsigned) ssl->tls_version,
1237 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1240 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001243 }
1244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1246 ((unsigned long) buf[2] << 24) |
1247 ((unsigned long) buf[3] << 16) |
1248 ((unsigned long) buf[4] << 8) |
1249 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001253 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (n > 32) {
1258 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1259 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1260 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1261 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001262 }
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001265 ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if ((ext_len > 0 && ext_len < 4) ||
1268 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1269 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001270 mbedtls_ssl_send_alert_message(
1271 ssl,
1272 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1274 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001275 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001277 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 } else {
1279 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1280 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1281 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1282 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001285 /* ciphersuite (used later) */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001286 i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35);
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001287
1288 /*
1289 * Read and check compression
1290 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001291 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1294 MBEDTLS_SSL_DEBUG_MSG(1,
1295 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001296 mbedtls_ssl_send_alert_message(
1297 ssl,
1298 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1300 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001301 }
1302
Paul Bakker380da532012-04-18 16:10:25 +00001303 /*
1304 * Initialize update checksum functions
1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1307 if (ssl->handshake->ciphersuite_info == NULL) {
1308 MBEDTLS_SSL_DEBUG_MSG(1,
1309 ("ciphersuite info for %04x not found", (unsigned int) i));
1310 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1311 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1312 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001313 }
Paul Bakker380da532012-04-18 16:10:25 +00001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1318 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
1320 /*
1321 * Check if the session can be resumed
1322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_SSL_RENEGOTIATION)
1325 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001326#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001327 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001328 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001331 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001334#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001335 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001336 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 memcpy(ssl->session_negotiate->id, buf + 35, n);
1338 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1343 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1346 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1347 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Andrzej Kurek03bac442018-04-25 05:06:07 -04001349 /*
1350 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001351 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 while (1) {
1354 if (ssl->conf->ciphersuite_list[i] == 0) {
1355 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001356 mbedtls_ssl_send_alert_message(
1357 ssl,
1358 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1360 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (ssl->conf->ciphersuite_list[i++] ==
1364 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 }
1368
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001369 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 ssl->session_negotiate->ciphersuite);
1371 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1372 ssl->tls_version) != 0) {
1373 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001374 mbedtls_ssl_send_alert_message(
1375 ssl,
1376 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1378 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001379 }
1380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 MBEDTLS_SSL_DEBUG_MSG(3,
1382 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001383
Gilles Peskineeccd8882020-03-10 12:19:08 +01001384#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1386 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001387 ssl->handshake->ecrs_enabled = 1;
1388 }
1389#endif
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1392 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001393 mbedtls_ssl_send_alert_message(
1394 ssl,
1395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1397 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 }
1399
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001400 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 MBEDTLS_SSL_DEBUG_MSG(2,
1403 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1404 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 while (ext_len) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001407 unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
1408 unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
Paul Bakker48916f92012-09-16 19:57:18 +00001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if (ext_size + 4 > ext_len) {
1411 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001412 mbedtls_ssl_send_alert_message(
1413 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1415 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001416 }
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 switch (ext_id) {
1419 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1420 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001423#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1426 ext_size)) != 0) {
1427 return ret;
1428 }
Paul Bakker48916f92012-09-16 19:57:18 +00001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1434 MBEDTLS_SSL_DEBUG_MSG(3,
1435 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1438 ext + 4, ext_size)) != 0) {
1439 return ret;
1440 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001444
Hanno Beckera0e20d02019-05-15 14:03:01 +01001445#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 case MBEDTLS_TLS_EXT_CID:
1447 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if ((ret = ssl_parse_cid_ext(ssl,
1450 ext + 4,
1451 ext_size)) != 0) {
1452 return ret;
1453 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001456#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1460 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1463 ext + 4, ext_size)) != 0) {
1464 return ret;
1465 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1472 MBEDTLS_SSL_DEBUG_MSG(3,
1473 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if ((ret = ssl_parse_extended_ms_ext(ssl,
1476 ext + 4, ext_size)) != 0) {
1477 return ret;
1478 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1485 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if ((ret = ssl_parse_session_ticket_ext(ssl,
1488 ext + 4, ext_size)) != 0) {
1489 return ret;
1490 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001494
Valerio Setti7aeec542023-07-05 18:57:21 +02001495#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001496 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001497 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1499 MBEDTLS_SSL_DEBUG_MSG(3,
1500 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1503 ext + 4, ext_size)) != 0) {
1504 return ret;
1505 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001508#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001509 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001510 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001511
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001512#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1514 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1517 ext + 4, ext_size)) != 0) {
1518 return ret;
1519 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001522#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 case MBEDTLS_TLS_EXT_ALPN:
1526 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1529 return ret;
1530 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001534
Johan Pascalb62bb512015-12-03 21:56:45 +01001535#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 case MBEDTLS_TLS_EXT_USE_SRTP:
1537 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1540 return ret;
1541 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001544#endif /* MBEDTLS_SSL_DTLS_SRTP */
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 default:
1547 MBEDTLS_SSL_DEBUG_MSG(3,
1548 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001549 }
1550
1551 ext_len -= 4 + ext_size;
1552 ext += 4 + ext_size;
1553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (ext_len > 0 && ext_len < 4) {
1555 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1556 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001557 }
1558 }
1559
1560 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001561 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1562 * extensions. It sets the transform data for the resumed session which in
1563 * case of DTLS includes the server CID extracted from the CID extension.
1564 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (ssl->handshake->resume) {
1566 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1567 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001568 mbedtls_ssl_send_alert_message(
1569 ssl,
1570 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1572 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001573 }
1574 }
1575
Paul Bakker48916f92012-09-16 19:57:18 +00001576 /*
1577 * Renegotiation security checks
1578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001580 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1582 MBEDTLS_SSL_DEBUG_MSG(1,
1583 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001584 handshake_failure = 1;
1585 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 renegotiation_info_seen == 0) {
1590 MBEDTLS_SSL_DEBUG_MSG(1,
1591 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001592 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1594 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1595 ssl->conf->allow_legacy_renegotiation ==
1596 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1597 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001598 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1600 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1601 renegotiation_info_seen == 1) {
1602 MBEDTLS_SSL_DEBUG_MSG(1,
1603 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001604 handshake_failure = 1;
1605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001609 mbedtls_ssl_send_alert_message(
1610 ssl,
1611 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1613 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001619}
1620
Valerio Setti48659a12025-01-15 14:22:28 +01001621#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001622MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001623static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1624 unsigned char **p,
1625 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001628 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001629
Paul Bakker29e1f122013-04-16 13:07:56 +02001630 /*
1631 * Ephemeral DH parameters:
1632 *
1633 * struct {
1634 * opaque dh_p<1..2^16-1>;
1635 * opaque dh_g<1..2^16-1>;
1636 * opaque dh_Ys<1..2^16-1>;
1637 * } ServerDHParams;
1638 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1640 p, end)) != 0) {
1641 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1642 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001643 }
1644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1646 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1647 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1648 dhm_actual_bitlen,
1649 ssl->conf->dhm_min_bitlen));
1650 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001651 }
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1654 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1655 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001658}
Valerio Setti48659a12025-01-15 14:22:28 +01001659#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001660
Andrzej Kurek468c5062022-10-24 10:30:14 -04001661#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1662 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1663 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001664MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001665static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1666 unsigned char **p,
1667 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001668{
1669 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001670 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001671 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001672 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001673 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001674
1675 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001676 * struct {
1677 * ECParameters curve_params;
1678 * ECPoint public;
1679 * } ServerECDHParams;
1680 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001681 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001682 * 2..3 NamedCurve
1683 * 4 ECPoint.len
1684 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if (end - *p < 4) {
1687 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1688 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001689
1690 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1692 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1693 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001694
1695 /* Next two bytes are the namedcurve value */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001696 tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
1697 *p += 2;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001698
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001699 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1701 MBEDTLS_SSL_DEBUG_MSG(2,
1702 ("bad server key exchange message (ECDHE curve): %u",
1703 (unsigned) tls_id));
1704 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001705 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001706
Valerio Setti40d9ca92023-01-04 16:08:04 +01001707 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001708 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1710 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001711 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001712 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001713 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001714
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001715 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001716 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 if ((size_t) (end - *p) < ecpoint_len) {
1718 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1719 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001720
Gilles Peskinec29df532023-10-02 14:59:26 +02001721 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1723 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001724
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001725 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1726 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001727 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001730}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001731#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1732 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1733 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001734#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001735MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001736static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1737 unsigned char **p,
1738 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001739{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001741 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001742 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001743
1744 /*
1745 * PSK parameters:
1746 *
1747 * opaque psk_identity_hint<0..2^16-1>;
1748 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (end - (*p) < 2) {
1750 MBEDTLS_SSL_DEBUG_MSG(1,
1751 ("bad server key exchange message (psk_identity_hint length)"));
1752 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001753 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001754 len = MBEDTLS_GET_UINT16_BE(*p, 0);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001755 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (end - (*p) < len) {
1758 MBEDTLS_SSL_DEBUG_MSG(1,
1759 ("bad server key exchange message (psk_identity_hint length)"));
1760 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001761 }
1762
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001763 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001764 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001765 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001766 * someone needs that feature.
1767 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001768 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001769 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001772}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001773#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001774
Gilles Peskineac767e52024-09-20 18:08:44 +02001775#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001776/*
1777 * Generate a pre-master secret and encrypt it with the server's RSA key
1778 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001779MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001780static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1781 size_t offset, size_t *olen,
1782 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001783{
Janos Follath865b3eb2019-12-16 11:46:15 +00001784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001785 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001786 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1790 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1791 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001792 }
1793
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001794 /*
1795 * Generate (part of) the pre-master as
1796 * struct {
1797 * ProtocolVersion client_version;
1798 * opaque random[46];
1799 * } PreMasterSecret;
1800 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 mbedtls_ssl_write_version(p, ssl->conf->transport,
1802 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1805 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1806 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001807 }
1808
1809 ssl->handshake->pmslen = 48;
1810
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001811#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1812 peer_pk = &ssl->handshake->peer_pubkey;
1813#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001815 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1817 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001818 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001819 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1820#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001821
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001822 /*
1823 * Now write it out, encrypted
1824 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1826 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1827 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001828 }
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if ((ret = mbedtls_pk_encrypt(peer_pk,
1831 p, ssl->handshake->pmslen,
1832 ssl->out_msg + offset + len_bytes, olen,
1833 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1834 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1835 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1836 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001837 }
1838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if (len_bytes == 2) {
1840 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001841 *olen += 2;
1842 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001843
Hanno Beckerae553dd2019-02-08 14:06:00 +00001844#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1845 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001847#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001849}
Gilles Peskineac767e52024-09-20 18:08:44 +02001850#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1853 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001854MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001855static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001856{
Janos Follath865b3eb2019-12-16 11:46:15 +00001857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001859
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001860#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1861 peer_pk = &ssl->handshake->peer_pubkey;
1862#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001864 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1866 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001867 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001868 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1869#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001870
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02001871 /* This is a public key, so it can't be opaque, so can_do() is a good
1872 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
1874 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
1875 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001876 }
1877
Gilles Peskine7354f1e2024-01-23 11:06:02 +01001878#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti97207782023-05-18 18:59:06 +02001879 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
Gilles Peskine7354f1e2024-01-23 11:06:02 +01001880#endif /* !defined(MBEDTLS_PK_USE_PSA_EC_DATA) */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001881
Valerio Setti2b5d3de2023-01-09 11:04:52 +01001882 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001883 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Settif9362b72023-11-29 08:42:27 +01001884 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01001885
Valerio Setti97207782023-05-18 18:59:06 +02001886 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
1888 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01001889 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01001890
Valerio Setti97207782023-05-18 18:59:06 +02001891 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 if (tls_id == 0) {
1893 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02001894 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01001896 }
1897
Valerio Setti1e868cc2023-01-09 17:30:01 +01001898 /* If the above conversion to TLS ID was fine, then also this one will be,
1899 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001900 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02001901 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01001902
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001903 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01001904
Przemek Stekielea4000f2022-03-16 09:49:33 +01001905 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02001906#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001907 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
1908 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02001909 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02001910#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02001911 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
1913 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001914 ssl->handshake->xxdh_psa_peerkey,
Gilles Peskinec29df532023-10-02 14:59:26 +02001915 sizeof(ssl->handshake->xxdh_psa_peerkey));
Przemek Stekielea4000f2022-03-16 09:49:33 +01001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if (ret != 0) {
1918 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
1919 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01001920 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001921 ssl->handshake->xxdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02001922#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Hanno Beckerae553dd2019-02-08 14:06:00 +00001923#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1924 /* We don't need the peer's public key anymore. Free it,
1925 * so that more RAM is available for upcoming expensive
1926 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001928#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001931}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1933 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001934
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001935MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001936static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01001937{
Janos Follath865b3eb2019-12-16 11:46:15 +00001938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001939 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00001940 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01001941 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
1947 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001951 ((void) p);
1952 ((void) end);
1953#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1956 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1958 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
1959 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
1960 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001961 mbedtls_ssl_send_alert_message(
1962 ssl,
1963 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1965 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001966 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001969 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001971 }
1972 ((void) p);
1973 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1975 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001976
Gilles Peskineeccd8882020-03-10 12:19:08 +01001977#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if (ssl->handshake->ecrs_enabled &&
1979 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02001980 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02001981 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02001982#endif
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
1985 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1986 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 }
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
1990 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001991 mbedtls_ssl_send_alert_message(
1992 ssl,
1993 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1995 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 }
1997
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001998 /*
Gilles Peskineb3ec1252024-09-20 18:22:04 +02001999 * ServerKeyExchange may be skipped with PSK when the server
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002000 * doesn't use a psk_identity_hint
2001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
Gilles Peskine712e9a12024-09-20 18:11:31 +02002003 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002004 /* Current message is probably either
2005 * CertificateRequest or ServerHelloDone */
2006 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002007 goto exit;
2008 }
2009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 MBEDTLS_SSL_DEBUG_MSG(1,
2011 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002012 mbedtls_ssl_send_alert_message(
2013 ssl,
2014 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 }
2019
Gilles Peskineeccd8882020-03-10 12:19:08 +01002020#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002022 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002024
2025start_processing:
2026#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002028 end = ssl->in_msg + ssl->in_hslen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002029 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002030
Gilles Peskineeccd8882020-03-10 12:19:08 +01002031#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2034 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2035 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002036 mbedtls_ssl_send_alert_message(
2037 ssl,
2038 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2040 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002041 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002042 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002043#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002044
Gilles Peskineac767e52024-09-20 18:08:44 +02002045#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine712e9a12024-09-20 18:11:31 +02002046 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002047 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 } else
Gilles Peskineac767e52024-09-20 18:08:44 +02002049#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Valerio Setti48659a12025-01-15 14:22:28 +01002050#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2051 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2053 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002054 mbedtls_ssl_send_alert_message(
2055 ssl,
2056 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2058 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002059 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 } else
Valerio Setti48659a12025-01-15 14:22:28 +01002061#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002062#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2063 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2068 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2069 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002070 mbedtls_ssl_send_alert_message(
2071 ssl,
2072 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2074 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002075 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2078 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2079 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002080#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002082 /*
2083 * The first 3 bytes are:
2084 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2085 * [1, 2] elliptic curve's TLS ID
2086 *
2087 * However since we only support secp256r1 for now, we check only
2088 * that TLS ID here
2089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002091 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (exp_tls_id == 0) {
2095 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002096 }
2097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2099 (read_tls_id != exp_tls_id)) {
2100 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002101 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002102
2103 p += 3;
2104
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if ((ret = mbedtls_psa_ecjpake_read_round(
2106 &ssl->handshake->psa_pake_ctx, p, end - p,
2107 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2108 psa_destroy_key(ssl->handshake->psa_pake_password);
2109 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002112 mbedtls_ssl_send_alert_message(
2113 ssl,
2114 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2116 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002119#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002120 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2122 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002123 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002124
Gilles Peskineeccd8882020-03-10 12:19:08 +01002125#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002127 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002128 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2131 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002133 size_t params_len = (size_t) (p - params);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002134 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002135 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002138
Jerry Yu693a47a2022-06-23 14:02:28 +08002139#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2140 peer_pk = &ssl->handshake->peer_pubkey;
2141#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002143 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2145 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002146 }
2147 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2148#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2149
Paul Bakker29e1f122013-04-16 13:07:56 +02002150 /*
2151 * Handle the digitally-signed structure
2152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2154 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2155 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2156 sig_alg, &pk_alg, &md_alg) != 0 &&
2157 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2158 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2159 MBEDTLS_SSL_DEBUG_MSG(1,
2160 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002161 mbedtls_ssl_send_alert_message(
2162 ssl,
2163 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2165 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002166 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002167 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2170 MBEDTLS_SSL_DEBUG_MSG(1,
2171 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002172 mbedtls_ssl_send_alert_message(
2173 ssl,
2174 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2176 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002177 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002178
2179 /*
2180 * Read signature
2181 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (p > end - 2) {
2184 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002185 mbedtls_ssl_send_alert_message(
2186 ssl,
2187 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2189 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002190 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002191 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
Paul Bakker1ef83d62012-04-11 12:09:53 +00002192 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (p != end - sig_len) {
2195 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002196 mbedtls_ssl_send_alert_message(
2197 ssl,
2198 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2200 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002201 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002204
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002205 /*
2206 * Compute the hash that has been signed
2207 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (md_alg != MBEDTLS_MD_NONE) {
2209 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2210 params, params_len,
2211 md_alg);
2212 if (ret != 0) {
2213 return ret;
2214 }
2215 } else {
2216 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2217 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002218 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002221
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002222 /*
2223 * Verify signature
2224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2226 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002227 mbedtls_ssl_send_alert_message(
2228 ssl,
2229 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2231 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002232 }
2233
Gilles Peskineeccd8882020-03-10 12:19:08 +01002234#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002236 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002238#endif
2239
Jerry Yu693a47a2022-06-23 14:02:28 +08002240#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002242 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2243 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002244 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002245 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (rsassa_pss_options.expected_salt_len == 0) {
2247 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2248 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2251 peer_pk,
2252 md_alg, hash, hashlen,
2253 p, sig_len);
2254 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002255#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 ret = mbedtls_pk_verify_restartable(peer_pk,
2257 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002260 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002261#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002263#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002265 mbedtls_ssl_send_alert_message(
2266 ssl,
2267 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2269 }
2270 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002271#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002273 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002275#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002277 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002278
2279#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2280 /* We don't need the peer's public key anymore. Free it,
2281 * so that more RAM is available for upcoming expensive
2282 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002284#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002286#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002287
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002288exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 ssl->state++;
2290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002294}
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002297MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002298static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002299{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002300 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002301 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2306 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002307 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002309 }
2310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2312 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002313}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002314#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002315MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002316static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002317{
Janos Follath865b3eb2019-12-16 11:46:15 +00002318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002319 unsigned char *buf;
2320 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002321 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002322 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002323 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002324 size_t sig_alg_len;
2325#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 unsigned char *sig_alg;
2327 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002328#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2333 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002334 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002336 }
2337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2339 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2340 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 }
2342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2344 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002345 mbedtls_ssl_send_alert_message(
2346 ssl,
2347 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2349 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002350 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002351
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002352 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002353 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2357 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002360 /* Current message is probably the ServerHelloDone */
2361 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002362 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002363 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002364
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002365 /*
2366 * struct {
2367 * ClientCertificateType certificate_types<1..2^8-1>;
2368 * SignatureAndHashAlgorithm
2369 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2370 * DistinguishedName certificate_authorities<0..2^16-1>;
2371 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002372 *
2373 * Since we only support a single certificate on clients, let's just
2374 * ignore all the information that's supposed to help us pick a
2375 * certificate.
2376 *
2377 * We could check that our certificate matches the request, and bail out
2378 * if it doesn't, but it's simpler to just send the certificate anyway,
2379 * and give the server the opportunity to decide if it should terminate
2380 * the connection when it doesn't like our certificate.
2381 *
2382 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2383 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002384 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002385 *
2386 * However, we still minimally parse the message to check it is at least
2387 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002388 */
Paul Bakker926af752012-11-23 13:38:07 +01002389 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002390
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002391 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2393 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2394 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2395 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2396 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002397 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002399 n = cert_type_len;
2400
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002401 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002402 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002403 * * the length of the signature algorithms field (if minor version of
2404 * SSL is 3),
2405 * * distinguished name length otherwise.
2406 * Both reach at most the index:
2407 * ...hdr_len + 2 + n,
2408 * therefore the buffer length at this point must be greater than that
2409 * regardless of the actual code path.
2410 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2412 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2413 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2414 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2415 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002416 }
2417
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002418 /* supported_signature_algorithms */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002419 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002420
2421 /*
2422 * The furthest access in buf is in the loop few lines below:
2423 * sig_alg[i + 1],
2424 * where:
2425 * sig_alg = buf + ...hdr_len + 3 + n,
2426 * max(i) = sig_alg_len - 1.
2427 * Therefore the furthest access is:
2428 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2429 * which reduces to:
2430 * buf[...hdr_len + 3 + n + sig_alg_len],
2431 * which is one less than we need the buf to be.
2432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2434 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002435 mbedtls_ssl_send_alert_message(
2436 ssl,
2437 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2439 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002440 }
Paul Bakker926af752012-11-23 13:38:07 +01002441
Ronald Cron90915f22022-03-07 11:11:36 +01002442#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2444 for (size_t i = 0; i < sig_alg_len; i += 2) {
2445 MBEDTLS_SSL_DEBUG_MSG(3,
2446 ("Supported Signature Algorithm found: %02x %02x",
2447 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002448 }
2449#endif
2450
2451 n += 2 + sig_alg_len;
2452
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002453 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002454 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002455
2456 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2458 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2459 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2460 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2461 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002462 }
2463
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002464#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2466 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002467 unsigned char *p = dn + i + 2;
2468 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002469 size_t asn1_len;
2470 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 memset(&name, 0, sizeof(name));
2472 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2473 if (dni_len > dn_len - i - 2 ||
2474 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2475 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2476 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2477 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002478 mbedtls_ssl_send_alert_message(
2479 ssl,
2480 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2482 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002483 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 MBEDTLS_SSL_DEBUG_MSG(3,
2485 ("DN hint: %.*s",
2486 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2487 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002488 }
2489#endif
2490
Paul Bakker926af752012-11-23 13:38:07 +01002491exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002495}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002496#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002498MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002499static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002500{
Janos Follath865b3eb2019-12-16 11:46:15 +00002501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2506 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2507 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2511 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2512 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002513 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2516 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2517 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2518 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2519 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2520 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 }
2522
2523 ssl->state++;
2524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2527 mbedtls_ssl_recv_flight_completed(ssl);
2528 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002529#endif
2530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002534}
2535
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002536MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002537static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002538{
Janos Follath865b3eb2019-12-16 11:46:15 +00002539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002540
2541 size_t header_len;
2542 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002543 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002544 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 /*
2551 * DHM key exchange -- send G^X mod P
2552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002556 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2559 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2560 &ssl->out_msg[header_len], content_len,
2561 ssl->conf->f_rng, ssl->conf->p_rng);
2562 if (ret != 0) {
2563 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2564 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 }
2566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2568 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2571 ssl->handshake->premaster,
2572 MBEDTLS_PREMASTER_SIZE,
2573 &ssl->handshake->pmslen,
2574 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2575 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2576 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 }
2578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2580 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002582#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2583 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2584 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2585 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002587 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2588 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Andrzej Kureka0237f82022-02-24 13:24:52 -05002590 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2591 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002592 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002593
2594 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2595
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002596 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002599
Hanno Becker4a63ed42019-01-08 11:39:35 +00002600 /*
2601 * Generate EC private key for ECDHE exchange.
2602 */
2603
Hanno Becker4a63ed42019-01-08 11:39:35 +00002604 /* The master secret is obtained from the shared ECDH secret by
2605 * applying the TLS 1.2 PRF with a specific salt and label. While
2606 * the PSA Crypto API encourages combining key agreement schemes
2607 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2608 * yet support the provisioning of salt + label to the KDF.
2609 * For the time being, we therefore need to split the computation
2610 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002611 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2613 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002614 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002615 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002616
2617 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002619 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (status != PSA_SUCCESS) {
2621 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2622 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002623
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002624 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002625 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002626 * but we just need to add a length byte before that. */
2627 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2628 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002630 size_t own_pubkey_len;
2631
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002632 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 own_pubkey, own_pubkey_max_len,
2634 &own_pubkey_len);
2635 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002636 psa_destroy_key(handshake->xxdh_psa_privkey);
2637 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002639 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002640
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002641 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2642 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002643
Hanno Becker4a63ed42019-01-08 11:39:35 +00002644 /* The ECDH secret is the premaster secret used for key derivation. */
2645
Janos Follathdf3b0892019-08-08 11:12:24 +01002646 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002648 handshake->xxdh_psa_privkey,
2649 handshake->xxdh_psa_peerkey,
2650 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 ssl->handshake->premaster,
2652 sizeof(ssl->handshake->premaster),
2653 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002654
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002655 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2656 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2659 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2660 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2663 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2664 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2665 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardc7403ed2025-01-23 11:24:18 +01002666#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002668 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2669 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2670 psa_key_attributes_t key_attributes;
2671
2672 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2673
2674 /*
2675 * opaque psk_identity<0..2^16-1>;
2676 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002678 /* We don't offer PSK suites if we don't have a PSK,
2679 * and we check that the server's choice is among the
2680 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2682 }
Neil Armstrong868af822022-03-09 10:26:25 +01002683
Neil Armstrongfc834f22022-03-23 17:54:38 +01002684 /* uint16 to store content length */
2685 const size_t content_len_size = 2;
2686
Neil Armstrong868af822022-03-09 10:26:25 +01002687 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if (header_len + content_len_size + ssl->conf->psk_identity_len
2690 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2691 MBEDTLS_SSL_DEBUG_MSG(1,
2692 ("psk identity too long or SSL buffer too short"));
2693 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002694 }
2695
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002696 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2699 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002700 header_len += content_len_size;
2701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 memcpy(p, ssl->conf->psk_identity,
2703 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002704 p += ssl->conf->psk_identity_len;
2705
Neil Armstrong868af822022-03-09 10:26:25 +01002706 header_len += ssl->conf->psk_identity_len;
2707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002709
2710 /*
2711 * Generate EC private key for ECDHE exchange.
2712 */
2713
2714 /* The master secret is obtained from the shared ECDH secret by
2715 * applying the TLS 1.2 PRF with a specific salt and label. While
2716 * the PSA Crypto API encourages combining key agreement schemes
2717 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2718 * yet support the provisioning of salt + label to the KDF.
2719 * For the time being, we therefore need to split the computation
2720 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2721 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2723 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002724 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002725 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002726
2727 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002729 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002731 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 }
Neil Armstrong868af822022-03-09 10:26:25 +01002733
2734 /* Export the public part of the ECDH private key from PSA.
2735 * The export format is an ECPoint structure as expected by TLS,
2736 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002737 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002738 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002740 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002741
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002742 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 own_pubkey, own_pubkey_max_len,
2744 &own_pubkey_len);
2745 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002746 psa_destroy_key(handshake->xxdh_psa_privkey);
2747 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002748 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002749 }
2750
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002751 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002752 content_len = own_pubkey_len + 1;
2753
Neil Armstrong25400452022-03-23 17:44:07 +01002754 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2755 * - a uint16 containing the length (in octets) of the ECDH computation
2756 * - the octet string produced by the ECDH computation
2757 * - a uint16 containing the length (in octets) of the PSK
2758 * - the PSK itself
2759 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002760 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 const unsigned char * const pms_end = pms +
2762 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002763 /* uint16 to store length (in octets) of the ECDH computation */
2764 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002765 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002766
Neil Armstrong25400452022-03-23 17:44:07 +01002767 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002769 handshake->xxdh_psa_privkey,
2770 handshake->xxdh_psa_peerkey,
2771 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 pms + zlen_size,
2773 pms_end - (pms + zlen_size),
2774 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01002775
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002776 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2777 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01002778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002780 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002782 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 }
Neil Armstrong868af822022-03-09 10:26:25 +01002784
Neil Armstrong25400452022-03-23 17:44:07 +01002785 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002787 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 } else
Manuel Pégourié-Gonnardc7403ed2025-01-23 11:24:18 +01002789#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002790#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002792 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002793 * opaque psk_identity<0..2^16-1>;
2794 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01002796 /* We don't offer PSK suites if we don't have a PSK,
2797 * and we check that the server's choice is among the
2798 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02002800 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002801
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002802 header_len = 4;
2803 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2806 MBEDTLS_SSL_DEBUG_MSG(1,
2807 ("psk identity too long or SSL buffer too short"));
2808 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002809 }
2810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
2812 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 memcpy(ssl->out_msg + header_len,
2815 ssl->conf->psk_identity,
2816 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002817 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002821 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002823#endif
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002824 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2826 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002827 }
2828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01002830#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002833 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
2835 &content_len, 0)) != 0) {
2836 return ret;
2837 }
2838 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002840#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002842 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002843
Neil Armstrongca7d5062022-05-31 14:43:23 +02002844 unsigned char *out_p = ssl->out_msg + header_len;
2845 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
2846 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
2848 out_p, end_p - out_p, &content_len,
2849 MBEDTLS_ECJPAKE_ROUND_TWO);
2850 if (ret != 0) {
2851 psa_destroy_key(ssl->handshake->psa_pake_password);
2852 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2853 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
2854 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002855 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002857#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002858 {
2859 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2861 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02002862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002864 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2866 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
2868 ssl->state++;
2869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2871 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2872 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002873 }
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002878}
2879
Gilles Peskineeccd8882020-03-10 12:19:08 +01002880#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002881MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002882static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002883{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002884 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002885 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00002886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2891 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2892 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002893 }
2894
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2896 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02002897 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02002899 }
2900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2902 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002903}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002904#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002905MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002906static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002909 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002910 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002911 size_t n = 0, offset = 0;
2912 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002913 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002915 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002916 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02002917#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002918 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02002919#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002920 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 +02002921#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002924
Gilles Peskineeccd8882020-03-10 12:19:08 +01002925#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 if (ssl->handshake->ecrs_enabled &&
2927 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002928 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002929 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02002930#endif
2931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2933 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2934 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002935 }
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2938 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002939 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002941 }
2942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 if (ssl->handshake->client_auth == 0 ||
2944 mbedtls_ssl_own_cert(ssl) == NULL) {
2945 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02002946 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002948 }
2949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 if (mbedtls_ssl_own_key(ssl) == NULL) {
2951 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
2952 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 }
2954
2955 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002956 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002958#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002960 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002962
2963sign:
2964#endif
2965
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002966 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
2967 if (0 != ret) {
2968 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
2969 return ret;
2970 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Ronald Cron90915f22022-03-07 11:11:36 +01002972 /*
2973 * digitally-signed struct {
2974 * opaque handshake_messages[handshake_messages_length];
2975 * };
2976 *
2977 * Taking shortcut here. We assume that the server always allows the
2978 * PRF Hash function and has sent it in the allowed signature
2979 * algorithms list received in the Certificate Request message.
2980 *
2981 * Until we encounter a server that does not, we will take this
2982 * shortcut.
2983 *
2984 * Reason: Otherwise we should have running hashes for SHA512 and
2985 * SHA224 in order to satisfy 'weird' needs from the server
2986 * side.
2987 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01002989 md_alg = MBEDTLS_MD_SHA384;
2990 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01002992 md_alg = MBEDTLS_MD_SHA256;
2993 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02002994 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01002996
2997 /* Info from md_alg will be used instead */
2998 hashlen = 0;
2999 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000
Gilles Peskineeccd8882020-03-10 12:19:08 +01003001#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003003 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003005#endif
3006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3008 md_alg, hash_start, hashlen,
3009 ssl->out_msg + 6 + offset,
3010 out_buf_len - 6 - offset,
3011 &n,
3012 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3013 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003014#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003016 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003018#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003020 }
Paul Bakker926af752012-11-23 13:38:07 +01003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Paul Bakker1ef83d62012-04-11 12:09:53 +00003024 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3026 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
3028 ssl->state++;
3029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3031 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3032 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 }
3034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003038}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003039#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003042MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003043static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003044{
Janos Follath865b3eb2019-12-16 11:46:15 +00003045 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003046 uint32_t lifetime;
3047 size_t ticket_len;
3048 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003049 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3055 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003056 }
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3059 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003060 mbedtls_ssl_send_alert_message(
3061 ssl,
3062 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3064 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003065 }
3066
3067 /*
3068 * struct {
3069 * uint32 ticket_lifetime_hint;
3070 * opaque ticket<0..2^16-1>;
3071 * } NewSessionTicket;
3072 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003073 * 0 . 3 ticket_lifetime_hint
3074 * 4 . 5 ticket_len (n)
3075 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3078 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3079 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3080 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3081 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3082 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003083 }
3084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003086
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003087 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003088
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003089 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3092 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3093 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3094 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3095 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003096 }
3097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003099
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003100 /* We're not waiting for a NewSessionTicket message any more */
3101 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003103
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003104 /*
3105 * Zero-length ticket means the server changed his mind and doesn't want
3106 * to send a ticket after all, so just forget it
3107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (ticket_len == 0) {
3109 return 0;
3110 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003111
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003113 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003115 ssl->session->ticket = NULL;
3116 ssl->session->ticket_len = 0;
3117 }
3118
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003119 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003121 ssl->session_negotiate->ticket = NULL;
3122 ssl->session_negotiate->ticket_len = 0;
3123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3125 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3126 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3127 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3128 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003129 }
3130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003132
3133 ssl->session_negotiate->ticket = ticket;
3134 ssl->session_negotiate->ticket_len = ticket_len;
3135 ssl->session_negotiate->ticket_lifetime = lifetime;
3136
3137 /*
3138 * RFC 5077 section 3.4:
3139 * "If the client receives a session ticket from the server, then it
3140 * discards any Session ID that was sent in the ServerHello."
3141 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003143 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003146
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003150
Paul Bakker5121ce52009-01-03 21:22:43 +00003151/*
Paul Bakker1961b702013-01-25 14:49:24 +01003152 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003153 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003154int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003155{
3156 int ret = 0;
3157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003159 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3162 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003163 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003164 }
3165#endif
3166
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 case MBEDTLS_SSL_HELLO_REQUEST:
3169 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003170 break;
3171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 /*
3173 * ==> ClientHello
3174 */
3175 case MBEDTLS_SSL_CLIENT_HELLO:
3176 ret = mbedtls_ssl_write_client_hello(ssl);
3177 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 /*
3180 * <== ServerHello
3181 * Certificate
3182 * ( ServerKeyExchange )
3183 * ( CertificateRequest )
3184 * ServerHelloDone
3185 */
3186 case MBEDTLS_SSL_SERVER_HELLO:
3187 ret = ssl_parse_server_hello(ssl);
3188 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3191 ret = mbedtls_ssl_parse_certificate(ssl);
3192 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3195 ret = ssl_parse_server_key_exchange(ssl);
3196 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3199 ret = ssl_parse_certificate_request(ssl);
3200 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3203 ret = ssl_parse_server_hello_done(ssl);
3204 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 /*
3207 * ==> ( Certificate/Alert )
3208 * ClientKeyExchange
3209 * ( CertificateVerify )
3210 * ChangeCipherSpec
3211 * Finished
3212 */
3213 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3214 ret = mbedtls_ssl_write_certificate(ssl);
3215 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003216
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3218 ret = ssl_write_client_key_exchange(ssl);
3219 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3222 ret = ssl_write_certificate_verify(ssl);
3223 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3226 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3227 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 case MBEDTLS_SSL_CLIENT_FINISHED:
3230 ret = mbedtls_ssl_write_finished(ssl);
3231 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 /*
3234 * <== ( NewSessionTicket )
3235 * ChangeCipherSpec
3236 * Finished
3237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3240 ret = ssl_parse_new_session_ticket(ssl);
3241 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003242#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3245 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3246 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003247
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 case MBEDTLS_SSL_SERVER_FINISHED:
3249 ret = mbedtls_ssl_parse_finished(ssl);
3250 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 case MBEDTLS_SSL_FLUSH_BUFFERS:
3253 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3254 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3255 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3258 mbedtls_ssl_handshake_wrapup(ssl);
3259 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003260
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 default:
3262 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3263 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3264 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003265
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003267}
Jerry Yuc5aef882021-12-23 20:15:02 +08003268
Jerry Yufb4b6472022-01-27 15:03:26 +08003269#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */