blob: 63f4240f219e5e60450cab956b4d2f845c1c1644 [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
Hanno Beckerbb89e272019-01-08 12:54:37 +000020#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020021#include "psa_util_internal.h"
Ronald Cron69a63422021-10-18 09:47:58 +020022#include "psa/crypto.h"
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040023#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Andrzej Kurek00644842023-05-30 05:45:00 -040024/* Define a local translating function to save code size by not using too many
25 * arguments in each translating place. */
26static int local_err_translation(psa_status_t status)
27{
28 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040029 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040030 psa_generic_status_to_mbedtls);
31}
32#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040033#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +000034#endif /* MBEDTLS_USE_PSA_CRYPTO */
35
SimonBd5800b72016-04-26 07:43:27 +010036#include <string.h>
37
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020038#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010041#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020046#endif
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020049MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010050static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
51 unsigned char *buf,
52 const unsigned char *end,
53 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010054{
55 unsigned char *p = buf;
56
57 *olen = 0;
58
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010059 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010060 * initial ClientHello, in which case also adding the renegotiation
61 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010062 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
63 return 0;
64 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 MBEDTLS_SSL_DEBUG_MSG(3,
67 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010070
Paul Bakkerd3edc862013-03-20 16:07:17 +010071 /*
72 * Secure renegotiation
73 */
Gilles Peskine449bd832023-01-11 14:50:10 +010074 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010075 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010076
77 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
79 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010082
83 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010088
Valerio Setti7aeec542023-07-05 18:57:21 +020089#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +020090 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010091 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010092
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020093MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010094static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
95 unsigned char *buf,
96 const unsigned char *end,
97 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010098{
99 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100100 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100101
102 *olen = 0;
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 MBEDTLS_SSL_DEBUG_MSG(3,
105 ("client hello, adding supported_point_formats extension"));
106 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100109 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100110
111 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100112 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200113
114 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200117 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100120}
Valerio Setti7aeec542023-07-05 18:57:21 +0200121#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200122 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200123 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100124
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200125#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200126MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100127static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
128 unsigned char *buf,
129 const unsigned char *end,
130 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200131{
Janos Follath865b3eb2019-12-16 11:46:15 +0000132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200133 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100134 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200135
136 *olen = 0;
137
138 /* Skip costly extension if we can't use EC J-PAKE anyway */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
141 return 0;
142 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200143#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
145 return 0;
146 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200147#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 MBEDTLS_SSL_DEBUG_MSG(3,
150 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100155 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200156
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200157 /*
158 * We may need to send ClientHello multiple times for Hello verification.
159 * We don't want to compute fresh values every time (both for performance
160 * and consistency reasons), so cache the extension content.
161 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (ssl->handshake->ecjpake_cache == NULL ||
163 ssl->handshake->ecjpake_cache_len == 0) {
164 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200165
Neil Armstrongca7d5062022-05-31 14:43:23 +0200166#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100167 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 p + 2, end - p - 2, &kkpp_len,
169 MBEDTLS_ECJPAKE_ROUND_ONE);
170 if (ret != 0) {
171 psa_destroy_key(ssl->handshake->psa_pake_password);
172 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
173 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
174 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200175 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200176#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
178 p + 2, end - p - 2, &kkpp_len,
179 ssl->conf->f_rng, ssl->conf->p_rng);
180 if (ret != 0) {
181 MBEDTLS_SSL_DEBUG_RET(1,
182 "mbedtls_ecjpake_write_round_one", ret);
183 return ret;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200184 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200185#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
188 if (ssl->handshake->ecjpake_cache == NULL) {
189 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
190 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200191 }
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200194 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 } else {
196 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200197
198 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200202 }
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100205 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200206
207 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200210}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200211#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000212
Hanno Beckera0e20d02019-05-15 14:03:01 +0100213#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200214MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100215static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
216 unsigned char *buf,
217 const unsigned char *end,
218 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100219{
220 unsigned char *p = buf;
221 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100222
223 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100224 * struct {
225 * opaque cid<0..2^8-1>;
226 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100228
229 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
231 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
232 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100233 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100235
236 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
237 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100239
240 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100242 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100243 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100245 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100246
247 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100249
250 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100253}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100254#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200257MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100258static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
259 unsigned char *buf,
260 const unsigned char *end,
261 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200262{
263 unsigned char *p = buf;
264
Simon Butcher0fc94e92015-09-28 20:52:04 +0100265 *olen = 0;
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
268 return 0;
269 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 MBEDTLS_SSL_DEBUG_MSG(3,
272 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100277 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200278
279 *p++ = 0x00;
280 *p++ = 1;
281
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200282 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200283
284 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200291MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100292static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
293 unsigned char *buf,
294 const unsigned char *end,
295 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100296{
297 unsigned char *p = buf;
298
Simon Butcher0fc94e92015-09-28 20:52:04 +0100299 *olen = 0;
300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
302 return 0;
303 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 MBEDTLS_SSL_DEBUG_MSG(3,
306 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100311 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100312
313 *p++ = 0x00;
314 *p++ = 0x00;
315
316 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100319}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200323MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100324static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
325 unsigned char *buf,
326 const unsigned char *end,
327 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200328{
329 unsigned char *p = buf;
330
Simon Butcher0fc94e92015-09-28 20:52:04 +0100331 *olen = 0;
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
334 return 0;
335 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 MBEDTLS_SSL_DEBUG_MSG(3,
338 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100343 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200344
345 *p++ = 0x00;
346 *p++ = 0x00;
347
348 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200351}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200355MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100356static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
357 unsigned char *buf,
358 const unsigned char *end,
359 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200360{
361 unsigned char *p = buf;
362 size_t tlen = ssl->session_negotiate->ticket_len;
363
Simon Butcher0fc94e92015-09-28 20:52:04 +0100364 *olen = 0;
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
367 return 0;
368 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 MBEDTLS_SSL_DEBUG_MSG(3,
371 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200372
Hanno Becker261602c2017-04-12 14:54:42 +0100373 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100377 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100380 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200381
382 *olen = 4;
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
385 return 0;
386 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 MBEDTLS_SSL_DEBUG_MSG(3,
389 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200392
393 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200398
Ron Eldora9788042018-12-05 11:04:31 +0200399#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200400MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100401static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
402 unsigned char *buf,
403 const unsigned char *end,
404 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100405{
406 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200407 size_t protection_profiles_index = 0, ext_len = 0;
408 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100409
410 *olen = 0;
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
413 (ssl->conf->dtls_srtp_profile_list == NULL) ||
414 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
415 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100416 }
417
Ron Eldora9788042018-12-05 11:04:31 +0200418 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100419 * uint8 SRTPProtectionProfile[2];
420 *
421 * struct {
422 * SRTPProtectionProfiles SRTPProtectionProfiles;
423 * opaque srtp_mki<0..255>;
424 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100425 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100426 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200428 mki_len = ssl->dtls_srtp_info.mki_len;
429 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300430 /* Extension length = 2 bytes for profiles length,
431 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
432 * 1 byte for srtp_mki vector length and the mki_len value
433 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200437
438 /* Check there is room in the buffer for the extension + 4 bytes
439 * - the extension tag (2 bytes)
440 * - the extension length (2 bytes)
441 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100445 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100448 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100449
Ron Eldor3adb9922017-12-21 10:15:08 +0200450 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200451 /* micro-optimization:
452 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
453 * which is lower than 127, so the upper byte of the length is always 0
454 * For the documentation, the more generic code is left in comments
455 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
456 * >> 8 ) & 0xFF );
457 */
458 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300462 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200464 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
466 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
467 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
468 profile_value));
469 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100470 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200472 /*
473 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200474 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 MBEDTLS_SSL_DEBUG_MSG(3,
477 ("client hello, "
478 "illegal DTLS-SRTP protection profile %d",
479 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
480 ));
481 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100482 }
483 }
484
Ron Eldor591f1622018-01-22 12:30:04 +0200485 *p++ = mki_len & 0xFF;
486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (mki_len != 0) {
488 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200489 /*
490 * Increment p to point to the current position.
491 */
492 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
494 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200495 }
496
Ron Eldoref72faf2018-07-12 11:54:20 +0300497 /*
498 * total extension length: extension type (2 bytes)
499 * + extension length (2 bytes)
500 * + protection profile length (2 bytes)
501 * + 2 * number of protection profiles
502 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200503 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300504 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200505 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100508}
509#endif /* MBEDTLS_SSL_DTLS_SRTP */
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
512 unsigned char *buf,
513 const unsigned char *end,
514 int uses_ec,
515 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100516{
517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
518 unsigned char *p = buf;
519 size_t ext_len = 0;
520
521 (void) ssl;
522 (void) end;
523 (void) uses_ec;
524 (void) ret;
525 (void) ext_len;
526
527 *out_len = 0;
528
529 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
530 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
531#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
533 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
534 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100535 }
536 p += ext_len;
537#endif
538
Valerio Setti7aeec542023-07-05 18:57:21 +0200539#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200540 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Ronald Cron12dcdf02022-02-16 15:28:22 +0100541 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (uses_ec) {
543 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
544 &ext_len)) != 0) {
545 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
546 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100547 }
548 p += ext_len;
549 }
550#endif
551
552#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
554 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
555 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100556 }
557 p += ext_len;
558#endif
559
560#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
562 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
563 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100564 }
565 p += ext_len;
566#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
567
568#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
570 &ext_len)) != 0) {
571 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
572 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100573 }
574 p += ext_len;
575#endif
576
577#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
579 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
580 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100581 }
582 p += ext_len;
583#endif
584
585#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
587 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
588 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100589 }
590 p += ext_len;
591#endif
592
593#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
595 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
596 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100597 }
598 p += ext_len;
599#endif
600
601#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
603 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
604 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100605 }
606 p += ext_len;
607#endif
608
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000609 *out_len = (size_t) (p - buf);
Ronald Cron12dcdf02022-02-16 15:28:22 +0100610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100612}
613
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200614MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100615static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
616 const unsigned char *buf,
617 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100621 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000623 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 mbedtls_ct_memcmp(buf + 1,
625 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
626 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
627 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
628 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100629 mbedtls_ssl_send_alert_message(
630 ssl,
631 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
633 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000634 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100637 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (len != 1 || buf[0] != 0x00) {
639 MBEDTLS_SSL_DEBUG_MSG(1,
640 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100641 mbedtls_ssl_send_alert_message(
642 ssl,
643 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
645 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100646 }
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100649 }
Paul Bakker48916f92012-09-16 19:57:18 +0000650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000652}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200655MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100656static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
657 const unsigned char *buf,
658 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200659{
660 /*
661 * server should use the extension only if we did,
662 * and if so the server's value should match ours (and len is always 1)
663 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200665 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 buf[0] != ssl->conf->mfl_code) {
667 MBEDTLS_SSL_DEBUG_MSG(1,
668 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100669 mbedtls_ssl_send_alert_message(
670 ssl,
671 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
673 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200674 }
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000679
Hanno Beckera0e20d02019-05-15 14:03:01 +0100680#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200681MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100682static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
683 const unsigned char *buf,
684 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100685{
686 size_t peer_cid_len;
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100689 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
690 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
692 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
693 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
694 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
695 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100696 }
697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (len == 0) {
699 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
700 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
701 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
702 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100703 }
704
705 peer_cid_len = *buf++;
706 len--;
707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
709 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
710 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
711 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
712 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100713 }
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (len != peer_cid_len) {
716 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
717 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
718 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
719 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100720 }
721
Hanno Becker5a299902019-05-03 12:47:49 +0100722 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100723 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
727 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100730}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100731#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200734MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100735static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
736 const unsigned char *buf,
737 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100738{
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
740 len != 0) {
741 MBEDTLS_SSL_DEBUG_MSG(1,
742 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100743 mbedtls_ssl_send_alert_message(
744 ssl,
745 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
747 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100748 }
749
750 ((void) buf);
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200759MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100760static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
761 const unsigned char *buf,
762 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200763{
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
765 len != 0) {
766 MBEDTLS_SSL_DEBUG_MSG(1,
767 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100768 mbedtls_ssl_send_alert_message(
769 ssl,
770 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
772 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200773 }
774
775 ((void) buf);
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200784MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100785static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
786 const unsigned char *buf,
787 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200788{
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
790 len != 0) {
791 MBEDTLS_SSL_DEBUG_MSG(1,
792 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100793 mbedtls_ssl_send_alert_message(
794 ssl,
795 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
797 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200798 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200799
800 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200801
802 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200805}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200807
Valerio Setti7aeec542023-07-05 18:57:21 +0200808#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200809 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100810 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200811MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100812static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
813 const unsigned char *buf,
814 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200815{
816 size_t list_size;
817 const unsigned char *p;
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (len == 0 || (size_t) (buf[0] + 1) != len) {
820 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
821 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
822 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
823 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200824 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200825 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200826
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200827 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 while (list_size > 0) {
829 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
830 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Valerio Setti7aeec542023-07-05 18:57:21 +0200831#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
832 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200833 ssl->handshake->ecdh_ctx.point_format = p[0];
Valerio Setti7aeec542023-07-05 18:57:21 +0200834#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200835#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
837 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
838 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200839#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
841 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200842 }
843
844 list_size--;
845 p++;
846 }
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
849 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
850 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
851 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200852}
Valerio Setti7aeec542023-07-05 18:57:21 +0200853#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200854 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200855 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200856
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200857#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200858MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100859static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
860 const unsigned char *buf,
861 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200862{
Janos Follath865b3eb2019-12-16 11:46:15 +0000863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (ssl->handshake->ciphersuite_info->key_exchange !=
866 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
867 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
868 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200869 }
870
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200871 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200873 ssl->handshake->ecjpake_cache = NULL;
874 ssl->handshake->ecjpake_cache_len = 0;
875
Neil Armstrongca7d5062022-05-31 14:43:23 +0200876#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if ((ret = mbedtls_psa_ecjpake_read_round(
878 &ssl->handshake->psa_pake_ctx, buf, len,
879 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
880 psa_destroy_key(ssl->handshake->psa_pake_password);
881 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100884 mbedtls_ssl_send_alert_message(
885 ssl,
886 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
888 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200889 }
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 return 0;
892#else
893 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
894 buf, len)) != 0) {
895 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
896 mbedtls_ssl_send_alert_message(
897 ssl,
898 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
899 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
900 return ret;
901 }
902
903 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200904#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200905}
906#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200909MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100910static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
911 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200912{
913 size_t list_len, name_len;
914 const char **p;
915
916 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (ssl->conf->alpn_list == NULL) {
918 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100919 mbedtls_ssl_send_alert_message(
920 ssl,
921 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
923 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200924 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200925
926 /*
927 * opaque ProtocolName<1..2^8-1>;
928 *
929 * struct {
930 * ProtocolName protocol_name_list<2..2^16-1>
931 * } ProtocolNameList;
932 *
933 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
934 */
935
936 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (len < 4) {
938 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
939 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
940 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200941 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200942
Dave Rodgmana3d0f612023-11-03 23:34:02 +0000943 list_len = MBEDTLS_GET_UINT16_BE(buf, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (list_len != len - 2) {
945 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
946 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
947 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200948 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200949
950 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (name_len != list_len - 1) {
952 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
953 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
954 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200955 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200956
957 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
959 if (name_len == strlen(*p) &&
960 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200961 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200963 }
964 }
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
967 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
968 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
969 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200970}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200972
Johan Pascalb62bb512015-12-03 21:56:45 +0100973#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200974MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100975static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
976 const unsigned char *buf,
977 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100978{
Johan Pascal43f94902020-09-22 12:25:52 +0200979 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200980 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100981 uint16_t server_protection_profile_value = 0;
982
983 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
985 (ssl->conf->dtls_srtp_profile_list == NULL) ||
986 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
987 return 0;
988 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100989
Ron Eldora9788042018-12-05 11:04:31 +0200990 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100991 * uint8 SRTPProtectionProfile[2];
992 *
993 * struct {
994 * SRTPProtectionProfiles SRTPProtectionProfiles;
995 * opaque srtp_mki<0..255>;
996 * } UseSRTPData;
997
998 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
999 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001002 mki_len = ssl->dtls_srtp_info.mki_len;
1003 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001004
Ron Eldoref72faf2018-07-12 11:54:20 +03001005 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001006 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1007 * + protection profile (2 bytes)
1008 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001009 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if ((len < 5) || (len != (buf[4] + 5u))) {
1012 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1013 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001014
1015 /*
1016 * get the server protection profile
1017 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001018
1019 /*
1020 * protection profile length must be 0x0002 as we must have only
1021 * one protection profile in server Hello
1022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if ((buf[0] != 0) || (buf[1] != 2)) {
1024 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1025 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001028 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 server_protection_profile_value);
1030 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1031 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1032 mbedtls_ssl_get_srtp_profile_as_string(
1033 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001034 }
1035
Johan Pascal43f94902020-09-22 12:25:52 +02001036 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001037
Johan Pascalb62bb512015-12-03 21:56:45 +01001038 /*
1039 * Check we have the server profile in our list
1040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1042 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001043 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001045 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001047 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001048 }
1049 }
1050
Ron Eldor591f1622018-01-22 12:30:04 +02001051 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1053 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1054 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1055 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001056 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001057
1058 /* If server does not use mki in its reply, make sure the client won't keep
1059 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001061 ssl->dtls_srtp_info.mki_len = 0;
1062 }
1063
Ron Eldoref72faf2018-07-12 11:54:20 +03001064 /*
1065 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001066 * If the client detects a nonzero-length MKI in the server's response
1067 * that is different than the one the client offered, then the client
1068 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1069 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (len > 5 && (buf[4] != mki_len ||
1071 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1072 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1073 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1074 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001075 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001076#if defined(MBEDTLS_DEBUG_C)
1077 if (len > 5) {
1078 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1079 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001080 }
1081#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001083}
1084#endif /* MBEDTLS_SSL_DTLS_SRTP */
1085
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001086/*
1087 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001090MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001091static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001092{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001093 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001095 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001096
1097#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1098 uint8_t cookie_len;
1099#else
1100 uint16_t cookie_len;
1101#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001104
Gilles Peskineb64bf062019-09-27 14:02:44 +02001105 /* Check that there is enough room for:
1106 * - 2 bytes of version
1107 * - 1 byte of cookie_len
1108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1110 MBEDTLS_SSL_DEBUG_MSG(1,
1111 ("incoming HelloVerifyRequest message is too short"));
1112 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1113 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1114 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001115 }
1116
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001117 /*
1118 * struct {
1119 * ProtocolVersion server_version;
1120 * opaque cookie<0..2^8-1>;
1121 * } HelloVerifyRequest;
1122 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1124 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001125 p += 2;
1126
TRodziewicz2d8800e2021-05-13 19:14:19 +02001127 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001128 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1129 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1130 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1133 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1136 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001139 }
1140
1141 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1143 MBEDTLS_SSL_DEBUG_MSG(1,
1144 ("cookie length does not match incoming message size"));
1145 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1146 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1147 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001148 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1154 if (ssl->handshake->cookie == NULL) {
1155 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1156 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001157 }
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001160 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001161
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001162 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001164 ret = mbedtls_ssl_reset_checksum(ssl);
1165 if (0 != ret) {
1166 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1167 return ret;
1168 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001177
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001178MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001179static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001180{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001181 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001182 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001183 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001184 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001185 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001187 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001188#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001189 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001195 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1197 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 }
1199
Hanno Becker79594fd2019-05-08 09:38:41 +01001200 buf = ssl->in_msg;
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001205 ssl->renego_records_seen++;
1206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (ssl->conf->renego_max_records >= 0 &&
1208 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1209 MBEDTLS_SSL_DEBUG_MSG(1,
1210 ("renegotiation requested, but not honored by server"));
1211 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001212 }
1213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 MBEDTLS_SSL_DEBUG_MSG(1,
1215 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001216
1217 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001219 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001223 mbedtls_ssl_send_alert_message(
1224 ssl,
1225 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1227 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 }
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1232 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1233 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1234 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1235 return ssl_parse_hello_verify_request(ssl);
1236 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001237 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001239 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001240 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001241 }
1242 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1246 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1247 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1248 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1249 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1250 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 }
1252
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001253 /*
1254 * 0 . 1 server_version
1255 * 2 . 33 random (maybe including 4 bytes of Unix time)
1256 * 34 . 34 session_id length = n
1257 * 35 . 34+n session_id
1258 * 35+n . 36+n cipher_suite
1259 * 37+n . 37+n compression_method
1260 *
1261 * 38+n . 39+n extensions length (optional)
1262 * 40+n . .. extensions
1263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01001267 ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
1268 ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001269 ssl->session_negotiate->tls_version = ssl->tls_version;
Ronald Cron17ef8df2023-11-22 10:29:42 +01001270 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (ssl->tls_version < ssl->conf->min_tls_version ||
1273 ssl->tls_version > ssl->conf->max_tls_version) {
1274 MBEDTLS_SSL_DEBUG_MSG(1,
1275 (
1276 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1277 (unsigned) ssl->conf->min_tls_version,
1278 (unsigned) ssl->tls_version,
1279 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1282 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001285 }
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1288 ((unsigned long) buf[2] << 24) |
1289 ((unsigned long) buf[3] << 16) |
1290 ((unsigned long) buf[4] << 8) |
1291 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001295 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (n > 32) {
1300 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1301 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1302 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1303 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001304 }
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001307 ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if ((ext_len > 0 && ext_len < 4) ||
1310 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1311 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001312 mbedtls_ssl_send_alert_message(
1313 ssl,
1314 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1316 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001317 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001319 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 } else {
1321 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1322 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1323 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1324 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001327 /* ciphersuite (used later) */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001328 i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35);
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001329
1330 /*
1331 * Read and check compression
1332 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001333 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1336 MBEDTLS_SSL_DEBUG_MSG(1,
1337 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001338 mbedtls_ssl_send_alert_message(
1339 ssl,
1340 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1342 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001343 }
1344
Paul Bakker380da532012-04-18 16:10:25 +00001345 /*
1346 * Initialize update checksum functions
1347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1349 if (ssl->handshake->ciphersuite_info == NULL) {
1350 MBEDTLS_SSL_DEBUG_MSG(1,
1351 ("ciphersuite info for %04x not found", (unsigned int) i));
1352 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1353 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1354 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001355 }
Paul Bakker380da532012-04-18 16:10:25 +00001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1360 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 /*
1363 * Check if the session can be resumed
1364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_SSL_RENEGOTIATION)
1367 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001368#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001369 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001370 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001373 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001376#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001377 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001378 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 memcpy(ssl->session_negotiate->id, buf + 35, n);
1380 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1385 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1388 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1389 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Andrzej Kurek03bac442018-04-25 05:06:07 -04001391 /*
1392 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001393 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 while (1) {
1396 if (ssl->conf->ciphersuite_list[i] == 0) {
1397 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001398 mbedtls_ssl_send_alert_message(
1399 ssl,
1400 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1402 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 }
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (ssl->conf->ciphersuite_list[i++] ==
1406 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
1410
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001411 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 ssl->session_negotiate->ciphersuite);
1413 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1414 ssl->tls_version) != 0) {
1415 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001416 mbedtls_ssl_send_alert_message(
1417 ssl,
1418 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1420 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001421 }
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 MBEDTLS_SSL_DEBUG_MSG(3,
1424 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001425
Gilles Peskineeccd8882020-03-10 12:19:08 +01001426#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1428 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001429 ssl->handshake->ecrs_enabled = 1;
1430 }
1431#endif
1432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1434 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001435 mbedtls_ssl_send_alert_message(
1436 ssl,
1437 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1439 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001442 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 MBEDTLS_SSL_DEBUG_MSG(2,
1445 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1446 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 while (ext_len) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001449 unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
1450 unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
Paul Bakker48916f92012-09-16 19:57:18 +00001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (ext_size + 4 > ext_len) {
1453 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001454 mbedtls_ssl_send_alert_message(
1455 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1457 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001458 }
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 switch (ext_id) {
1461 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1462 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001465#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1468 ext_size)) != 0) {
1469 return ret;
1470 }
Paul Bakker48916f92012-09-16 19:57:18 +00001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1476 MBEDTLS_SSL_DEBUG_MSG(3,
1477 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1480 ext + 4, ext_size)) != 0) {
1481 return ret;
1482 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001486
Hanno Beckera0e20d02019-05-15 14:03:01 +01001487#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 case MBEDTLS_TLS_EXT_CID:
1489 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if ((ret = ssl_parse_cid_ext(ssl,
1492 ext + 4,
1493 ext_size)) != 0) {
1494 return ret;
1495 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001498#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1502 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1505 ext + 4, ext_size)) != 0) {
1506 return ret;
1507 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1514 MBEDTLS_SSL_DEBUG_MSG(3,
1515 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if ((ret = ssl_parse_extended_ms_ext(ssl,
1518 ext + 4, ext_size)) != 0) {
1519 return ret;
1520 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1527 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if ((ret = ssl_parse_session_ticket_ext(ssl,
1530 ext + 4, ext_size)) != 0) {
1531 return ret;
1532 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001536
Valerio Setti7aeec542023-07-05 18:57:21 +02001537#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001538 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001539 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1541 MBEDTLS_SSL_DEBUG_MSG(3,
1542 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1545 ext + 4, ext_size)) != 0) {
1546 return ret;
1547 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001550#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001551 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001552 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001553
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001554#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1556 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1559 ext + 4, ext_size)) != 0) {
1560 return ret;
1561 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001564#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 case MBEDTLS_TLS_EXT_ALPN:
1568 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1571 return ret;
1572 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001576
Johan Pascalb62bb512015-12-03 21:56:45 +01001577#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 case MBEDTLS_TLS_EXT_USE_SRTP:
1579 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1582 return ret;
1583 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001586#endif /* MBEDTLS_SSL_DTLS_SRTP */
1587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 default:
1589 MBEDTLS_SSL_DEBUG_MSG(3,
1590 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001591 }
1592
1593 ext_len -= 4 + ext_size;
1594 ext += 4 + ext_size;
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (ext_len > 0 && ext_len < 4) {
1597 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1598 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001599 }
1600 }
1601
1602 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001603 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1604 * extensions. It sets the transform data for the resumed session which in
1605 * case of DTLS includes the server CID extracted from the CID extension.
1606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (ssl->handshake->resume) {
1608 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1609 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001610 mbedtls_ssl_send_alert_message(
1611 ssl,
1612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1614 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001615 }
1616 }
1617
Paul Bakker48916f92012-09-16 19:57:18 +00001618 /*
1619 * Renegotiation security checks
1620 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001622 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1624 MBEDTLS_SSL_DEBUG_MSG(1,
1625 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001626 handshake_failure = 1;
1627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 renegotiation_info_seen == 0) {
1632 MBEDTLS_SSL_DEBUG_MSG(1,
1633 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001634 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1636 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1637 ssl->conf->allow_legacy_renegotiation ==
1638 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1639 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001640 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1642 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1643 renegotiation_info_seen == 1) {
1644 MBEDTLS_SSL_DEBUG_MSG(1,
1645 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001646 handshake_failure = 1;
1647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001651 mbedtls_ssl_send_alert_message(
1652 ssl,
1653 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1655 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001661}
1662
Valerio Setti48659a12025-01-15 14:22:28 +01001663#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_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_dh_params(mbedtls_ssl_context *ssl,
1666 unsigned char **p,
1667 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001668{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001670 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001671
Paul Bakker29e1f122013-04-16 13:07:56 +02001672 /*
1673 * Ephemeral DH parameters:
1674 *
1675 * struct {
1676 * opaque dh_p<1..2^16-1>;
1677 * opaque dh_g<1..2^16-1>;
1678 * opaque dh_Ys<1..2^16-1>;
1679 * } ServerDHParams;
1680 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1682 p, end)) != 0) {
1683 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1684 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001685 }
1686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1688 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1689 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1690 dhm_actual_bitlen,
1691 ssl->conf->dhm_min_bitlen));
1692 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001693 }
1694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1696 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1697 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001700}
Valerio Setti48659a12025-01-15 14:22:28 +01001701#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001702
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001703#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001704#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1705 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1706 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001707MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001708static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1709 unsigned char **p,
1710 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001711{
1712 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001713 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001714 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001715 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001716 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001717
1718 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001719 * struct {
1720 * ECParameters curve_params;
1721 * ECPoint public;
1722 * } ServerECDHParams;
1723 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001724 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001725 * 2..3 NamedCurve
1726 * 4 ECPoint.len
1727 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001728 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 if (end - *p < 4) {
1730 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1731 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001732
1733 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1735 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1736 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001737
1738 /* Next two bytes are the namedcurve value */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001739 tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
1740 *p += 2;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001741
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001742 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1744 MBEDTLS_SSL_DEBUG_MSG(2,
1745 ("bad server key exchange message (ECDHE curve): %u",
1746 (unsigned) tls_id));
1747 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001748 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001749
Valerio Setti40d9ca92023-01-04 16:08:04 +01001750 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001751 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1753 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001754 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001755 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001756 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001757
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001758 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001759 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if ((size_t) (end - *p) < ecpoint_len) {
1761 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1762 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001763
Gilles Peskinec29df532023-10-02 14:59:26 +02001764 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1766 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001767
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001768 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1769 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001770 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001773}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001774#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1775 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1776 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001777#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001778#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1779 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1780 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1781 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1782 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001783MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001784static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001785{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001786 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001787 mbedtls_ecp_group_id grp_id;
1788#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1789 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1790#else
1791 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1792#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1795 if (tls_id == 0) {
1796 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1797 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001798 }
1799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1801 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1804 return -1;
1805 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1808 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001811}
1812
Andrzej Kurek468c5062022-10-24 10:30:14 -04001813#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1814 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1815 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1816 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1817 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1818
1819#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1820 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1821 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001822MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001823static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1824 unsigned char **p,
1825 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001826{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001828
Paul Bakker29e1f122013-04-16 13:07:56 +02001829 /*
1830 * Ephemeral ECDH parameters:
1831 *
1832 * struct {
1833 * ECParameters curve_params;
1834 * ECPoint public;
1835 * } ServerECDHParams;
1836 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1838 (const unsigned char **) p, end)) != 0) {
1839 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001840#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001842 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001844#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001846 }
1847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 if (ssl_check_server_ecdh_params(ssl) != 0) {
1849 MBEDTLS_SSL_DEBUG_MSG(1,
1850 ("bad server key exchange message (ECDHE curve)"));
1851 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001852 }
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001855}
Gilles Peskine449bd832023-01-11 14:50:10 +01001856#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1857 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001858 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1859#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001860#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001861MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001862static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1863 unsigned char **p,
1864 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001865{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001867 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001868 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001869
1870 /*
1871 * PSK parameters:
1872 *
1873 * opaque psk_identity_hint<0..2^16-1>;
1874 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (end - (*p) < 2) {
1876 MBEDTLS_SSL_DEBUG_MSG(1,
1877 ("bad server key exchange message (psk_identity_hint length)"));
1878 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001879 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001880 len = MBEDTLS_GET_UINT16_BE(*p, 0);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001881 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 if (end - (*p) < len) {
1884 MBEDTLS_SSL_DEBUG_MSG(1,
1885 ("bad server key exchange message (psk_identity_hint length)"));
1886 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001887 }
1888
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001889 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001890 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001891 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001892 * someone needs that feature.
1893 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001894 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001895 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001898}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001899#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001900
Gilles Peskineac767e52024-09-20 18:08:44 +02001901#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001902/*
1903 * Generate a pre-master secret and encrypt it with the server's RSA key
1904 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001905MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001906static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1907 size_t offset, size_t *olen,
1908 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001909{
Janos Follath865b3eb2019-12-16 11:46:15 +00001910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001911 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001912 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1916 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1917 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001918 }
1919
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001920 /*
1921 * Generate (part of) the pre-master as
1922 * struct {
1923 * ProtocolVersion client_version;
1924 * opaque random[46];
1925 * } PreMasterSecret;
1926 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 mbedtls_ssl_write_version(p, ssl->conf->transport,
1928 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1931 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1932 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001933 }
1934
1935 ssl->handshake->pmslen = 48;
1936
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001937#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1938 peer_pk = &ssl->handshake->peer_pubkey;
1939#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001941 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1943 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001944 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001945 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1946#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001947
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001948 /*
1949 * Now write it out, encrypted
1950 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1952 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1953 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001954 }
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if ((ret = mbedtls_pk_encrypt(peer_pk,
1957 p, ssl->handshake->pmslen,
1958 ssl->out_msg + offset + len_bytes, olen,
1959 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1960 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1961 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1962 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001963 }
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 if (len_bytes == 2) {
1966 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001967 *olen += 2;
1968 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001969
Hanno Beckerae553dd2019-02-08 14:06:00 +00001970#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1971 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001973#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001975}
Gilles Peskineac767e52024-09-20 18:08:44 +02001976#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1979 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001980MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001981static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001982{
Janos Follath865b3eb2019-12-16 11:46:15 +00001983 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001985
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001986#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1987 peer_pk = &ssl->handshake->peer_pubkey;
1988#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001990 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1992 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001993 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001994 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1995#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001996
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02001997 /* This is a public key, so it can't be opaque, so can_do() is a good
1998 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2000 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2001 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002002 }
2003
Gilles Peskine7354f1e2024-01-23 11:06:02 +01002004#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti97207782023-05-18 18:59:06 +02002005 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
Gilles Peskine7354f1e2024-01-23 11:06:02 +01002006#endif /* !defined(MBEDTLS_PK_USE_PSA_EC_DATA) */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002007
Przemek Stekielea4000f2022-03-16 09:49:33 +01002008#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002009 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02002010 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Settif9362b72023-11-29 08:42:27 +01002011 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002012
Valerio Setti97207782023-05-18 18:59:06 +02002013 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2015 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002016 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002017
Valerio Setti97207782023-05-18 18:59:06 +02002018 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if (tls_id == 0) {
2020 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002021 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002023 }
2024
Valerio Setti1e868cc2023-01-09 17:30:01 +01002025 /* If the above conversion to TLS ID was fine, then also this one will be,
2026 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02002027 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02002028 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002029
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002030 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002031
Przemek Stekielea4000f2022-03-16 09:49:33 +01002032 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002033#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002034 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2035 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02002036 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002037#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002038 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2040 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002041 ssl->handshake->xxdh_psa_peerkey,
Gilles Peskinec29df532023-10-02 14:59:26 +02002042 sizeof(ssl->handshake->xxdh_psa_peerkey));
Przemek Stekielea4000f2022-03-16 09:49:33 +01002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (ret != 0) {
2045 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2046 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002047 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002048 ssl->handshake->xxdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002049#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2050#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2052 MBEDTLS_ECDH_THEIRS)) != 0) {
2053 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2054 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002055 }
2056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if (ssl_check_server_ecdh_params(ssl) != 0) {
2058 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2059 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002060 }
Valerio Setti97207782023-05-18 18:59:06 +02002061#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002062#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2063 /* We don't need the peer's public key anymore. Free it,
2064 * so that more RAM is available for upcoming expensive
2065 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002067#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2072 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002073
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002075static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002076{
Janos Follath865b3eb2019-12-16 11:46:15 +00002077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002078 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002079 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002080 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2086 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002090 ((void) p);
2091 ((void) end);
2092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2095 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2097 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2098 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2099 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002100 mbedtls_ssl_send_alert_message(
2101 ssl,
2102 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2104 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002105 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002108 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002110 }
2111 ((void) p);
2112 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2114 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002115
Gilles Peskineeccd8882020-03-10 12:19:08 +01002116#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (ssl->handshake->ecrs_enabled &&
2118 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002119 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002120 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002121#endif
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2124 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2125 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2129 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002130 mbedtls_ssl_send_alert_message(
2131 ssl,
2132 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2134 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 }
2136
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002137 /*
Gilles Peskineb3ec1252024-09-20 18:22:04 +02002138 * ServerKeyExchange may be skipped with PSK when the server
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002139 * doesn't use a psk_identity_hint
2140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
Gilles Peskine712e9a12024-09-20 18:11:31 +02002142 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002143 /* Current message is probably either
2144 * CertificateRequest or ServerHelloDone */
2145 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002146 goto exit;
2147 }
2148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 MBEDTLS_SSL_DEBUG_MSG(1,
2150 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002151 mbedtls_ssl_send_alert_message(
2152 ssl,
2153 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 }
2158
Gilles Peskineeccd8882020-03-10 12:19:08 +01002159#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002161 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002163
2164start_processing:
2165#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002167 end = ssl->in_msg + ssl->in_hslen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002168 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002169
Gilles Peskineeccd8882020-03-10 12:19:08 +01002170#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2173 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2174 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002175 mbedtls_ssl_send_alert_message(
2176 ssl,
2177 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2179 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002180 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002181 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002182#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002183
Gilles Peskineac767e52024-09-20 18:08:44 +02002184#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine712e9a12024-09-20 18:11:31 +02002185 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002186 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 } else
Gilles Peskineac767e52024-09-20 18:08:44 +02002188#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Valerio Setti48659a12025-01-15 14:22:28 +01002189#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2190 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2192 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002193 mbedtls_ssl_send_alert_message(
2194 ssl,
2195 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2197 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002198 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 } else
Valerio Setti48659a12025-01-15 14:22:28 +01002200#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002201#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2202 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2207 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2208 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002209 mbedtls_ssl_send_alert_message(
2210 ssl,
2211 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2213 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002214 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2217 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2218 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002219#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002221#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002222 /*
2223 * The first 3 bytes are:
2224 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2225 * [1, 2] elliptic curve's TLS ID
2226 *
2227 * However since we only support secp256r1 for now, we check only
2228 * that TLS ID here
2229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002231 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (exp_tls_id == 0) {
2235 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002236 }
2237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2239 (read_tls_id != exp_tls_id)) {
2240 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002241 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002242
2243 p += 3;
2244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if ((ret = mbedtls_psa_ecjpake_read_round(
2246 &ssl->handshake->psa_pake_ctx, p, end - p,
2247 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2248 psa_destroy_key(ssl->handshake->psa_pake_password);
2249 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002252 mbedtls_ssl_send_alert_message(
2253 ssl,
2254 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2256 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002257 }
2258#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2260 p, end - p);
2261 if (ret != 0) {
2262 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002263 mbedtls_ssl_send_alert_message(
2264 ssl,
2265 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2267 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002268 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002269#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002271#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002272 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2274 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002275 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002276
Gilles Peskineeccd8882020-03-10 12:19:08 +01002277#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002279 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002280 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2283 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002285 size_t params_len = (size_t) (p - params);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002286 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002287 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002290
Jerry Yu693a47a2022-06-23 14:02:28 +08002291#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2292 peer_pk = &ssl->handshake->peer_pubkey;
2293#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002295 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2297 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002298 }
2299 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2300#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2301
Paul Bakker29e1f122013-04-16 13:07:56 +02002302 /*
2303 * Handle the digitally-signed structure
2304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2306 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2307 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2308 sig_alg, &pk_alg, &md_alg) != 0 &&
2309 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2310 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2311 MBEDTLS_SSL_DEBUG_MSG(1,
2312 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002313 mbedtls_ssl_send_alert_message(
2314 ssl,
2315 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2317 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002318 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002319 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2322 MBEDTLS_SSL_DEBUG_MSG(1,
2323 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002324 mbedtls_ssl_send_alert_message(
2325 ssl,
2326 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2328 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002329 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002330
2331 /*
2332 * Read signature
2333 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (p > end - 2) {
2336 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002337 mbedtls_ssl_send_alert_message(
2338 ssl,
2339 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2341 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002342 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002343 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
Paul Bakker1ef83d62012-04-11 12:09:53 +00002344 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if (p != end - sig_len) {
2347 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002348 mbedtls_ssl_send_alert_message(
2349 ssl,
2350 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2352 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002356
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002357 /*
2358 * Compute the hash that has been signed
2359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (md_alg != MBEDTLS_MD_NONE) {
2361 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2362 params, params_len,
2363 md_alg);
2364 if (ret != 0) {
2365 return ret;
2366 }
2367 } else {
2368 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2369 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002370 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002373
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002374 /*
2375 * Verify signature
2376 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2378 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002379 mbedtls_ssl_send_alert_message(
2380 ssl,
2381 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2383 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002384 }
2385
Gilles Peskineeccd8882020-03-10 12:19:08 +01002386#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002388 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002390#endif
2391
Jerry Yu693a47a2022-06-23 14:02:28 +08002392#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002394 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2395 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002396 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002397 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (rsassa_pss_options.expected_salt_len == 0) {
2399 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2400 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2403 peer_pk,
2404 md_alg, hash, hashlen,
2405 p, sig_len);
2406 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002407#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 ret = mbedtls_pk_verify_restartable(peer_pk,
2409 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002412 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002413#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002415#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002417 mbedtls_ssl_send_alert_message(
2418 ssl,
2419 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2421 }
2422 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002423#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002425 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002427#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002429 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002430
2431#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2432 /* We don't need the peer's public key anymore. Free it,
2433 * so that more RAM is available for upcoming expensive
2434 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002436#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002438#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002440exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 ssl->state++;
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002446}
2447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002449MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002450static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002451{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002452 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002453 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2458 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002459 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002461 }
2462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2464 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002465}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002466#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002467MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002468static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002469{
Janos Follath865b3eb2019-12-16 11:46:15 +00002470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002471 unsigned char *buf;
2472 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002473 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002474 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002475 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002476 size_t sig_alg_len;
2477#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 unsigned char *sig_alg;
2479 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002480#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2485 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002486 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002488 }
2489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2491 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2492 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 }
2494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2496 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002497 mbedtls_ssl_send_alert_message(
2498 ssl,
2499 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2501 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002502 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002504 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002505 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2509 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002512 /* Current message is probably the ServerHelloDone */
2513 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002514 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002515 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002516
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002517 /*
2518 * struct {
2519 * ClientCertificateType certificate_types<1..2^8-1>;
2520 * SignatureAndHashAlgorithm
2521 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2522 * DistinguishedName certificate_authorities<0..2^16-1>;
2523 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002524 *
2525 * Since we only support a single certificate on clients, let's just
2526 * ignore all the information that's supposed to help us pick a
2527 * certificate.
2528 *
2529 * We could check that our certificate matches the request, and bail out
2530 * if it doesn't, but it's simpler to just send the certificate anyway,
2531 * and give the server the opportunity to decide if it should terminate
2532 * the connection when it doesn't like our certificate.
2533 *
2534 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2535 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002536 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002537 *
2538 * However, we still minimally parse the message to check it is at least
2539 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002540 */
Paul Bakker926af752012-11-23 13:38:07 +01002541 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002542
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002543 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2545 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2546 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2547 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2548 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002549 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002551 n = cert_type_len;
2552
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002553 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002554 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002555 * * the length of the signature algorithms field (if minor version of
2556 * SSL is 3),
2557 * * distinguished name length otherwise.
2558 * Both reach at most the index:
2559 * ...hdr_len + 2 + n,
2560 * therefore the buffer length at this point must be greater than that
2561 * regardless of the actual code path.
2562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2564 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2565 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2566 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2567 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002568 }
2569
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002570 /* supported_signature_algorithms */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002571 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002572
2573 /*
2574 * The furthest access in buf is in the loop few lines below:
2575 * sig_alg[i + 1],
2576 * where:
2577 * sig_alg = buf + ...hdr_len + 3 + n,
2578 * max(i) = sig_alg_len - 1.
2579 * Therefore the furthest access is:
2580 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2581 * which reduces to:
2582 * buf[...hdr_len + 3 + n + sig_alg_len],
2583 * which is one less than we need the buf to be.
2584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2586 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002587 mbedtls_ssl_send_alert_message(
2588 ssl,
2589 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2591 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002592 }
Paul Bakker926af752012-11-23 13:38:07 +01002593
Ronald Cron90915f22022-03-07 11:11:36 +01002594#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2596 for (size_t i = 0; i < sig_alg_len; i += 2) {
2597 MBEDTLS_SSL_DEBUG_MSG(3,
2598 ("Supported Signature Algorithm found: %02x %02x",
2599 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002600 }
2601#endif
2602
2603 n += 2 + sig_alg_len;
2604
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002605 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002606 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002607
2608 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2610 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2611 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2612 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2613 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002614 }
2615
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002616#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2618 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002619 unsigned char *p = dn + i + 2;
2620 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002621 size_t asn1_len;
2622 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 memset(&name, 0, sizeof(name));
2624 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2625 if (dni_len > dn_len - i - 2 ||
2626 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2627 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2628 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2629 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002630 mbedtls_ssl_send_alert_message(
2631 ssl,
2632 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2634 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002635 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 MBEDTLS_SSL_DEBUG_MSG(3,
2637 ("DN hint: %.*s",
2638 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2639 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002640 }
2641#endif
2642
Paul Bakker926af752012-11-23 13:38:07 +01002643exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002647}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002648#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002650MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002651static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002652{
Janos Follath865b3eb2019-12-16 11:46:15 +00002653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2658 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2659 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2663 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2664 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2668 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2669 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2670 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2671 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2672 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 }
2674
2675 ssl->state++;
2676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2679 mbedtls_ssl_recv_flight_completed(ssl);
2680 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002681#endif
2682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002686}
2687
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002688MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002689static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002690{
Janos Follath865b3eb2019-12-16 11:46:15 +00002691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002692
2693 size_t header_len;
2694 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002695 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002696 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 /*
2703 * DHM key exchange -- send G^X mod P
2704 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002708 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2711 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2712 &ssl->out_msg[header_len], content_len,
2713 ssl->conf->f_rng, ssl->conf->p_rng);
2714 if (ret != 0) {
2715 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2716 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 }
2718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2720 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2723 ssl->handshake->premaster,
2724 MBEDTLS_PREMASTER_SIZE,
2725 &ssl->handshake->pmslen,
2726 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2727 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2728 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 }
2730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2732 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002734#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2735 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2736 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2737 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002739 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2740 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002742#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002743 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2744 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002745 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002746
2747 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2748
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002749 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002752
Hanno Becker4a63ed42019-01-08 11:39:35 +00002753 /*
2754 * Generate EC private key for ECDHE exchange.
2755 */
2756
Hanno Becker4a63ed42019-01-08 11:39:35 +00002757 /* The master secret is obtained from the shared ECDH secret by
2758 * applying the TLS 1.2 PRF with a specific salt and label. While
2759 * the PSA Crypto API encourages combining key agreement schemes
2760 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2761 * yet support the provisioning of salt + label to the KDF.
2762 * For the time being, we therefore need to split the computation
2763 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002764 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2766 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002767 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002768 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002769
2770 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002772 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 if (status != PSA_SUCCESS) {
2774 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2775 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002776
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002777 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002778 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002779 * but we just need to add a length byte before that. */
2780 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2781 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002783 size_t own_pubkey_len;
2784
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002785 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 own_pubkey, own_pubkey_max_len,
2787 &own_pubkey_len);
2788 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002789 psa_destroy_key(handshake->xxdh_psa_privkey);
2790 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002792 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002793
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002794 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2795 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002796
Hanno Becker4a63ed42019-01-08 11:39:35 +00002797 /* The ECDH secret is the premaster secret used for key derivation. */
2798
Janos Follathdf3b0892019-08-08 11:12:24 +01002799 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002801 handshake->xxdh_psa_privkey,
2802 handshake->xxdh_psa_peerkey,
2803 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 ssl->handshake->premaster,
2805 sizeof(ssl->handshake->premaster),
2806 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002807
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002808 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2809 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2812 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2813 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002814#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002815 /*
2816 * ECDH key exchange -- send client public value
2817 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002818 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002819
Gilles Peskineeccd8882020-03-10 12:19:08 +01002820#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 if (ssl->handshake->ecrs_enabled) {
2822 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002823 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002825
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002827 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002828#endif
2829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2831 &content_len,
2832 &ssl->out_msg[header_len], 1000,
2833 ssl->conf->f_rng, ssl->conf->p_rng);
2834 if (ret != 0) {
2835 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002836#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002838 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002840#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002842 }
2843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2845 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002846
Gilles Peskineeccd8882020-03-10 12:19:08 +01002847#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002849 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002850 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002851 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002852
2853ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002855 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002857#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2859 &ssl->handshake->pmslen,
2860 ssl->handshake->premaster,
2861 MBEDTLS_MPI_MAX_SIZE,
2862 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2863 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002864#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002866 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002868#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002870 }
2871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2873 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002874#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2877 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2878 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2879 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002880#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2881 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002883 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2884 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2885 psa_key_attributes_t key_attributes;
2886
2887 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2888
2889 /*
2890 * opaque psk_identity<0..2^16-1>;
2891 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002893 /* We don't offer PSK suites if we don't have a PSK,
2894 * and we check that the server's choice is among the
2895 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2897 }
Neil Armstrong868af822022-03-09 10:26:25 +01002898
Neil Armstrongfc834f22022-03-23 17:54:38 +01002899 /* uint16 to store content length */
2900 const size_t content_len_size = 2;
2901
Neil Armstrong868af822022-03-09 10:26:25 +01002902 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 if (header_len + content_len_size + ssl->conf->psk_identity_len
2905 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2906 MBEDTLS_SSL_DEBUG_MSG(1,
2907 ("psk identity too long or SSL buffer too short"));
2908 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002909 }
2910
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002911 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2914 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002915 header_len += content_len_size;
2916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 memcpy(p, ssl->conf->psk_identity,
2918 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002919 p += ssl->conf->psk_identity_len;
2920
Neil Armstrong868af822022-03-09 10:26:25 +01002921 header_len += ssl->conf->psk_identity_len;
2922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002924
2925 /*
2926 * Generate EC private key for ECDHE exchange.
2927 */
2928
2929 /* The master secret is obtained from the shared ECDH secret by
2930 * applying the TLS 1.2 PRF with a specific salt and label. While
2931 * the PSA Crypto API encourages combining key agreement schemes
2932 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2933 * yet support the provisioning of salt + label to the KDF.
2934 * For the time being, we therefore need to split the computation
2935 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2936 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2938 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002939 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002940 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002941
2942 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002944 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002946 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 }
Neil Armstrong868af822022-03-09 10:26:25 +01002948
2949 /* Export the public part of the ECDH private key from PSA.
2950 * The export format is an ECPoint structure as expected by TLS,
2951 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002952 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002953 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002955 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002956
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002957 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 own_pubkey, own_pubkey_max_len,
2959 &own_pubkey_len);
2960 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002961 psa_destroy_key(handshake->xxdh_psa_privkey);
2962 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002963 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002964 }
2965
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002966 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002967 content_len = own_pubkey_len + 1;
2968
Neil Armstrong25400452022-03-23 17:44:07 +01002969 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2970 * - a uint16 containing the length (in octets) of the ECDH computation
2971 * - the octet string produced by the ECDH computation
2972 * - a uint16 containing the length (in octets) of the PSK
2973 * - the PSK itself
2974 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002975 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 const unsigned char * const pms_end = pms +
2977 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002978 /* uint16 to store length (in octets) of the ECDH computation */
2979 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002980 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002981
Neil Armstrong25400452022-03-23 17:44:07 +01002982 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002984 handshake->xxdh_psa_privkey,
2985 handshake->xxdh_psa_peerkey,
2986 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 pms + zlen_size,
2988 pms_end - (pms + zlen_size),
2989 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01002990
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002991 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2992 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002995 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002997 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 }
Neil Armstrong868af822022-03-09 10:26:25 +01002999
Neil Armstrong25400452022-03-23 17:44:07 +01003000 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003002 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003004#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3005 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003006#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003008 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003009 * opaque psk_identity<0..2^16-1>;
3010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003012 /* We don't offer PSK suites if we don't have a PSK,
3013 * and we check that the server's choice is among the
3014 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003016 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003017
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003018 header_len = 4;
3019 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3022 MBEDTLS_SSL_DEBUG_MSG(1,
3023 ("psk identity too long or SSL buffer too short"));
3024 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003025 }
3026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3028 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 memcpy(ssl->out_msg + header_len,
3031 ssl->conf->psk_identity,
3032 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003033 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003037 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003039#endif
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003040#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3042 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003043 /*
3044 * ClientECDiffieHellmanPublic public;
3045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3047 &content_len,
3048 &ssl->out_msg[header_len],
3049 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3050 ssl->conf->f_rng, ssl->conf->p_rng);
3051 if (ret != 0) {
3052 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3053 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003054 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3057 MBEDTLS_DEBUG_ECDH_Q);
3058 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003059#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003060 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3062 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003063 }
3064
Neil Armstrong80f6f322022-05-03 17:56:38 +02003065#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003067 (mbedtls_key_exchange_type_t) ciphersuite_info->
3068 key_exchange)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 MBEDTLS_SSL_DEBUG_RET(1,
3070 "mbedtls_ssl_psk_derive_premaster", ret);
3071 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003072 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003073#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003075#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003078 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3080 &content_len, 0)) != 0) {
3081 return ret;
3082 }
3083 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003085#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003087 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003088
Neil Armstrongca7d5062022-05-31 14:43:23 +02003089#if defined(MBEDTLS_USE_PSA_CRYPTO)
3090 unsigned char *out_p = ssl->out_msg + header_len;
3091 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3092 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3094 out_p, end_p - out_p, &content_len,
3095 MBEDTLS_ECJPAKE_ROUND_TWO);
3096 if (ret != 0) {
3097 psa_destroy_key(ssl->handshake->psa_pake_password);
3098 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3099 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3100 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003101 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003102#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3104 ssl->out_msg + header_len,
3105 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3106 &content_len,
3107 ssl->conf->f_rng, ssl->conf->p_rng);
3108 if (ret != 0) {
3109 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3110 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003111 }
3112
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3114 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3115 ssl->conf->f_rng, ssl->conf->p_rng);
3116 if (ret != 0) {
3117 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3118 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003119 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003120#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003122#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003123 {
3124 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3126 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003127 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003129 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3131 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
3133 ssl->state++;
3134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3136 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3137 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003138 }
3139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003143}
3144
Gilles Peskineeccd8882020-03-10 12:19:08 +01003145#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003146MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003147static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003148{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003149 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003150 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3156 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3157 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003158 }
3159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3161 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003162 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003164 }
3165
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3167 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003168}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003169#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003170MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003171static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003174 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003175 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003176 size_t n = 0, offset = 0;
3177 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003178 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003180 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003181 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003182#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003183 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003184#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003185 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 +02003186#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003187
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003189
Gilles Peskineeccd8882020-03-10 12:19:08 +01003190#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 if (ssl->handshake->ecrs_enabled &&
3192 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003193 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003194 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003195#endif
3196
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3198 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3199 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003200 }
3201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3203 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003204 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003206 }
3207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 if (ssl->handshake->client_auth == 0 ||
3209 mbedtls_ssl_own_cert(ssl) == NULL) {
3210 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003211 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 }
3214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 if (mbedtls_ssl_own_key(ssl) == NULL) {
3216 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3217 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 }
3219
3220 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003221 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003223#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003225 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003227
3228sign:
3229#endif
3230
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003231 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3232 if (0 != ret) {
3233 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3234 return ret;
3235 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
Ronald Cron90915f22022-03-07 11:11:36 +01003237 /*
3238 * digitally-signed struct {
3239 * opaque handshake_messages[handshake_messages_length];
3240 * };
3241 *
3242 * Taking shortcut here. We assume that the server always allows the
3243 * PRF Hash function and has sent it in the allowed signature
3244 * algorithms list received in the Certificate Request message.
3245 *
3246 * Until we encounter a server that does not, we will take this
3247 * shortcut.
3248 *
3249 * Reason: Otherwise we should have running hashes for SHA512 and
3250 * SHA224 in order to satisfy 'weird' needs from the server
3251 * side.
3252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003254 md_alg = MBEDTLS_MD_SHA384;
3255 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003257 md_alg = MBEDTLS_MD_SHA256;
3258 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003259 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003261
3262 /* Info from md_alg will be used instead */
3263 hashlen = 0;
3264 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003265
Gilles Peskineeccd8882020-03-10 12:19:08 +01003266#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003268 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003270#endif
3271
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3273 md_alg, hash_start, hashlen,
3274 ssl->out_msg + 6 + offset,
3275 out_buf_len - 6 - offset,
3276 &n,
3277 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3278 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003279#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003281 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003283#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003285 }
Paul Bakker926af752012-11-23 13:38:07 +01003286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
Paul Bakker1ef83d62012-04-11 12:09:53 +00003289 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3291 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
3293 ssl->state++;
3294
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3296 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3297 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 }
3299
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003303}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003304#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003307MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003308static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003309{
Janos Follath865b3eb2019-12-16 11:46:15 +00003310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003311 uint32_t lifetime;
3312 size_t ticket_len;
3313 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003314 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003315
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003317
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3319 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3320 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003321 }
3322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3324 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003325 mbedtls_ssl_send_alert_message(
3326 ssl,
3327 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3329 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003330 }
3331
3332 /*
3333 * struct {
3334 * uint32 ticket_lifetime_hint;
3335 * opaque ticket<0..2^16-1>;
3336 * } NewSessionTicket;
3337 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003338 * 0 . 3 ticket_lifetime_hint
3339 * 4 . 5 ticket_len (n)
3340 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3343 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3344 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3345 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3346 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3347 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003348 }
3349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003351
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003352 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003353
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003354 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3357 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3358 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3359 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3360 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003361 }
3362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003364
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003365 /* We're not waiting for a NewSessionTicket message any more */
3366 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003368
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003369 /*
3370 * Zero-length ticket means the server changed his mind and doesn't want
3371 * to send a ticket after all, so just forget it
3372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 if (ticket_len == 0) {
3374 return 0;
3375 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003378 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003380 ssl->session->ticket = NULL;
3381 ssl->session->ticket_len = 0;
3382 }
3383
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003384 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003386 ssl->session_negotiate->ticket = NULL;
3387 ssl->session_negotiate->ticket_len = 0;
3388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3390 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3391 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3392 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3393 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003394 }
3395
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003397
3398 ssl->session_negotiate->ticket = ticket;
3399 ssl->session_negotiate->ticket_len = ticket_len;
3400 ssl->session_negotiate->ticket_lifetime = lifetime;
3401
3402 /*
3403 * RFC 5077 section 3.4:
3404 * "If the client receives a session ticket from the server, then it
3405 * discards any Session ID that was sent in the ServerHello."
3406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003408 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003409
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003415
Paul Bakker5121ce52009-01-03 21:22:43 +00003416/*
Paul Bakker1961b702013-01-25 14:49:24 +01003417 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003419int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003420{
3421 int ret = 0;
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003424 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3427 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003428 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003429 }
3430#endif
3431
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 case MBEDTLS_SSL_HELLO_REQUEST:
3434 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003435 break;
3436
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 /*
3438 * ==> ClientHello
3439 */
3440 case MBEDTLS_SSL_CLIENT_HELLO:
3441 ret = mbedtls_ssl_write_client_hello(ssl);
3442 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003443
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 /*
3445 * <== ServerHello
3446 * Certificate
3447 * ( ServerKeyExchange )
3448 * ( CertificateRequest )
3449 * ServerHelloDone
3450 */
3451 case MBEDTLS_SSL_SERVER_HELLO:
3452 ret = ssl_parse_server_hello(ssl);
3453 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003454
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3456 ret = mbedtls_ssl_parse_certificate(ssl);
3457 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
Gilles Peskine449bd832023-01-11 14:50:10 +01003459 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3460 ret = ssl_parse_server_key_exchange(ssl);
3461 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003462
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3464 ret = ssl_parse_certificate_request(ssl);
3465 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003466
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3468 ret = ssl_parse_server_hello_done(ssl);
3469 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003470
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 /*
3472 * ==> ( Certificate/Alert )
3473 * ClientKeyExchange
3474 * ( CertificateVerify )
3475 * ChangeCipherSpec
3476 * Finished
3477 */
3478 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3479 ret = mbedtls_ssl_write_certificate(ssl);
3480 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003481
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3483 ret = ssl_write_client_key_exchange(ssl);
3484 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3487 ret = ssl_write_certificate_verify(ssl);
3488 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003489
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3491 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3492 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003493
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 case MBEDTLS_SSL_CLIENT_FINISHED:
3495 ret = mbedtls_ssl_write_finished(ssl);
3496 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 /*
3499 * <== ( NewSessionTicket )
3500 * ChangeCipherSpec
3501 * Finished
3502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003504 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3505 ret = ssl_parse_new_session_ticket(ssl);
3506 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003507#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003508
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3510 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3511 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003512
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 case MBEDTLS_SSL_SERVER_FINISHED:
3514 ret = mbedtls_ssl_parse_finished(ssl);
3515 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 case MBEDTLS_SSL_FLUSH_BUFFERS:
3518 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3519 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3520 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003521
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3523 mbedtls_ssl_handshake_wrapup(ssl);
3524 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003525
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 default:
3527 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3528 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3529 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003530
Gilles Peskine449bd832023-01-11 14:50:10 +01003531 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003532}
Jerry Yuc5aef882021-12-23 20:15:02 +08003533
Jerry Yufb4b6472022-01-27 15:03:26 +08003534#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */