blob: 8591103a0358418e4d7163381dcc4c2b041c394c [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1664 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001665MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001666static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1667 unsigned char **p,
1668 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001671 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001672
Paul Bakker29e1f122013-04-16 13:07:56 +02001673 /*
1674 * Ephemeral DH parameters:
1675 *
1676 * struct {
1677 * opaque dh_p<1..2^16-1>;
1678 * opaque dh_g<1..2^16-1>;
1679 * opaque dh_Ys<1..2^16-1>;
1680 * } ServerDHParams;
1681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1683 p, end)) != 0) {
1684 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1685 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001686 }
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1689 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1690 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1691 dhm_actual_bitlen,
1692 ssl->conf->dhm_min_bitlen));
1693 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001694 }
1695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1697 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1698 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1703 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001704
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001705#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001706#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1707 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1708 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001709MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001710static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1711 unsigned char **p,
1712 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001713{
1714 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001715 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001716 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001717 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001718 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001719
1720 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001721 * struct {
1722 * ECParameters curve_params;
1723 * ECPoint public;
1724 * } ServerECDHParams;
1725 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001726 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001727 * 2..3 NamedCurve
1728 * 4 ECPoint.len
1729 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001730 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if (end - *p < 4) {
1732 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1733 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001734
1735 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1737 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1738 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001739
1740 /* Next two bytes are the namedcurve value */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001741 tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
1742 *p += 2;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001743
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001744 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1746 MBEDTLS_SSL_DEBUG_MSG(2,
1747 ("bad server key exchange message (ECDHE curve): %u",
1748 (unsigned) tls_id));
1749 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001750 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001751
Valerio Setti40d9ca92023-01-04 16:08:04 +01001752 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001753 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1755 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001756 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001757 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001758 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001759
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001760 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001761 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if ((size_t) (end - *p) < ecpoint_len) {
1763 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1764 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001765
Gilles Peskinec29df532023-10-02 14:59:26 +02001766 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1768 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001769
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001770 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1771 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001772 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001775}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001776#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1777 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1778 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001779#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001780#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1781 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1782 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1783 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1784 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001785MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001786static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001787{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001788 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001789 mbedtls_ecp_group_id grp_id;
1790#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1791 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1792#else
1793 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1794#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1797 if (tls_id == 0) {
1798 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1799 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001800 }
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1803 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1806 return -1;
1807 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1810 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001813}
1814
Andrzej Kurek468c5062022-10-24 10:30:14 -04001815#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1816 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1817 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1818 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1819 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1820
1821#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1822 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1823 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001824MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001825static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1826 unsigned char **p,
1827 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001828{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001830
Paul Bakker29e1f122013-04-16 13:07:56 +02001831 /*
1832 * Ephemeral ECDH parameters:
1833 *
1834 * struct {
1835 * ECParameters curve_params;
1836 * ECPoint public;
1837 * } ServerECDHParams;
1838 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1840 (const unsigned char **) p, end)) != 0) {
1841 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001842#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001844 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001846#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001848 }
1849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 if (ssl_check_server_ecdh_params(ssl) != 0) {
1851 MBEDTLS_SSL_DEBUG_MSG(1,
1852 ("bad server key exchange message (ECDHE curve)"));
1853 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001854 }
1855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001857}
Gilles Peskine449bd832023-01-11 14:50:10 +01001858#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1859 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001860 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1861#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001862#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001863MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001864static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1865 unsigned char **p,
1866 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001869 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001870 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001871
1872 /*
1873 * PSK parameters:
1874 *
1875 * opaque psk_identity_hint<0..2^16-1>;
1876 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if (end - (*p) < 2) {
1878 MBEDTLS_SSL_DEBUG_MSG(1,
1879 ("bad server key exchange message (psk_identity_hint length)"));
1880 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001881 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00001882 len = MBEDTLS_GET_UINT16_BE(*p, 0);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001883 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (end - (*p) < len) {
1886 MBEDTLS_SSL_DEBUG_MSG(1,
1887 ("bad server key exchange message (psk_identity_hint length)"));
1888 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001889 }
1890
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001891 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001892 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001893 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001894 * someone needs that feature.
1895 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001896 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001897 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001900}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001901#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001902
Gilles Peskineac767e52024-09-20 18:08:44 +02001903#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001904/*
1905 * Generate a pre-master secret and encrypt it with the server's RSA key
1906 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001907MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001908static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1909 size_t offset, size_t *olen,
1910 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001911{
Janos Follath865b3eb2019-12-16 11:46:15 +00001912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001913 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001914 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1918 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1919 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001920 }
1921
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001922 /*
1923 * Generate (part of) the pre-master as
1924 * struct {
1925 * ProtocolVersion client_version;
1926 * opaque random[46];
1927 * } PreMasterSecret;
1928 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 mbedtls_ssl_write_version(p, ssl->conf->transport,
1930 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1933 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1934 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001935 }
1936
1937 ssl->handshake->pmslen = 48;
1938
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001939#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1940 peer_pk = &ssl->handshake->peer_pubkey;
1941#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001943 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1945 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001946 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001947 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1948#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001949
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001950 /*
1951 * Now write it out, encrypted
1952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1954 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1955 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001956 }
1957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if ((ret = mbedtls_pk_encrypt(peer_pk,
1959 p, ssl->handshake->pmslen,
1960 ssl->out_msg + offset + len_bytes, olen,
1961 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1962 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1963 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1964 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001965 }
1966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 if (len_bytes == 2) {
1968 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001969 *olen += 2;
1970 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001971
Hanno Beckerae553dd2019-02-08 14:06:00 +00001972#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1973 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001975#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001977}
Gilles Peskineac767e52024-09-20 18:08:44 +02001978#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1981 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001982MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001983static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001984{
Janos Follath865b3eb2019-12-16 11:46:15 +00001985 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001987
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001988#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1989 peer_pk = &ssl->handshake->peer_pubkey;
1990#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001992 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1994 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001995 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001996 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1997#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001998
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02001999 /* This is a public key, so it can't be opaque, so can_do() is a good
2000 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2002 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2003 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002004 }
2005
Gilles Peskine7354f1e2024-01-23 11:06:02 +01002006#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti97207782023-05-18 18:59:06 +02002007 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
Gilles Peskine7354f1e2024-01-23 11:06:02 +01002008#endif /* !defined(MBEDTLS_PK_USE_PSA_EC_DATA) */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002009
Przemek Stekielea4000f2022-03-16 09:49:33 +01002010#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002011 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02002012 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Settif9362b72023-11-29 08:42:27 +01002013 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002014
Valerio Setti97207782023-05-18 18:59:06 +02002015 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2017 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002018 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002019
Valerio Setti97207782023-05-18 18:59:06 +02002020 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (tls_id == 0) {
2022 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002023 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002025 }
2026
Valerio Setti1e868cc2023-01-09 17:30:01 +01002027 /* If the above conversion to TLS ID was fine, then also this one will be,
2028 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02002029 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02002030 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002031
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002032 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002033
Przemek Stekielea4000f2022-03-16 09:49:33 +01002034 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002035#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002036 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2037 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02002038 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002039#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002040 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2042 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002043 ssl->handshake->xxdh_psa_peerkey,
Gilles Peskinec29df532023-10-02 14:59:26 +02002044 sizeof(ssl->handshake->xxdh_psa_peerkey));
Przemek Stekielea4000f2022-03-16 09:49:33 +01002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (ret != 0) {
2047 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2048 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002049 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002050 ssl->handshake->xxdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002051#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2052#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2054 MBEDTLS_ECDH_THEIRS)) != 0) {
2055 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2056 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002057 }
2058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (ssl_check_server_ecdh_params(ssl) != 0) {
2060 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2061 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002062 }
Valerio Setti97207782023-05-18 18:59:06 +02002063#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002064#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2065 /* We don't need the peer's public key anymore. Free it,
2066 * so that more RAM is available for upcoming expensive
2067 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002069#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2074 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002075
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002076MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002077static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002078{
Janos Follath865b3eb2019-12-16 11:46:15 +00002079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002080 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002081 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002082 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2088 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002092 ((void) p);
2093 ((void) end);
2094#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2097 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2099 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2100 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2101 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002102 mbedtls_ssl_send_alert_message(
2103 ssl,
2104 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2106 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002107 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002110 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002112 }
2113 ((void) p);
2114 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2116 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002117
Gilles Peskineeccd8882020-03-10 12:19:08 +01002118#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (ssl->handshake->ecrs_enabled &&
2120 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002121 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002122 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002123#endif
2124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2126 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2127 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
2129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2131 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002132 mbedtls_ssl_send_alert_message(
2133 ssl,
2134 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2136 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 }
2138
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002139 /*
2140 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2141 * doesn't use a psk_identity_hint
2142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2144 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2145 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002146 /* Current message is probably either
2147 * CertificateRequest or ServerHelloDone */
2148 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002149 goto exit;
2150 }
2151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 MBEDTLS_SSL_DEBUG_MSG(1,
2153 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002154 mbedtls_ssl_send_alert_message(
2155 ssl,
2156 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 }
2161
Gilles Peskineeccd8882020-03-10 12:19:08 +01002162#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002164 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002166
2167start_processing:
2168#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002170 end = ssl->in_msg + ssl->in_hslen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002171 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002172
Gilles Peskineeccd8882020-03-10 12:19:08 +01002173#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2176 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2178 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2179 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002180 mbedtls_ssl_send_alert_message(
2181 ssl,
2182 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2184 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002185 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002186 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002187#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002188
Gilles Peskineac767e52024-09-20 18:08:44 +02002189#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2191 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002192 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 } else
Gilles Peskineac767e52024-09-20 18:08:44 +02002194#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2196 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2198 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2199 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2200 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002201 mbedtls_ssl_send_alert_message(
2202 ssl,
2203 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2205 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002206 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2209 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002210#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2211 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2216 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2217 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002218 mbedtls_ssl_send_alert_message(
2219 ssl,
2220 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2222 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002223 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2226 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2227 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002228#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002230#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002231 /*
2232 * The first 3 bytes are:
2233 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2234 * [1, 2] elliptic curve's TLS ID
2235 *
2236 * However since we only support secp256r1 for now, we check only
2237 * that TLS ID here
2238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002240 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 if (exp_tls_id == 0) {
2244 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002245 }
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2248 (read_tls_id != exp_tls_id)) {
2249 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002250 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002251
2252 p += 3;
2253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if ((ret = mbedtls_psa_ecjpake_read_round(
2255 &ssl->handshake->psa_pake_ctx, p, end - p,
2256 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2257 psa_destroy_key(ssl->handshake->psa_pake_password);
2258 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002261 mbedtls_ssl_send_alert_message(
2262 ssl,
2263 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2265 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002266 }
2267#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2269 p, end - p);
2270 if (ret != 0) {
2271 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002272 mbedtls_ssl_send_alert_message(
2273 ssl,
2274 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2276 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002277 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002278#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002280#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002281 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2283 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002284 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002285
Gilles Peskineeccd8882020-03-10 12:19:08 +01002286#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002288 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002289 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2292 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002294 size_t params_len = (size_t) (p - params);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002295 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002296 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002299
Jerry Yu693a47a2022-06-23 14:02:28 +08002300#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2301 peer_pk = &ssl->handshake->peer_pubkey;
2302#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002304 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2306 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002307 }
2308 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2309#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2310
Paul Bakker29e1f122013-04-16 13:07:56 +02002311 /*
2312 * Handle the digitally-signed structure
2313 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2315 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2316 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2317 sig_alg, &pk_alg, &md_alg) != 0 &&
2318 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2319 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2320 MBEDTLS_SSL_DEBUG_MSG(1,
2321 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002322 mbedtls_ssl_send_alert_message(
2323 ssl,
2324 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2326 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002327 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002328 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2331 MBEDTLS_SSL_DEBUG_MSG(1,
2332 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002333 mbedtls_ssl_send_alert_message(
2334 ssl,
2335 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2337 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002338 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002339
2340 /*
2341 * Read signature
2342 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (p > end - 2) {
2345 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002346 mbedtls_ssl_send_alert_message(
2347 ssl,
2348 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2350 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002351 }
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002352 sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
Paul Bakker1ef83d62012-04-11 12:09:53 +00002353 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (p != end - sig_len) {
2356 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002357 mbedtls_ssl_send_alert_message(
2358 ssl,
2359 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2361 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002362 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002365
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002366 /*
2367 * Compute the hash that has been signed
2368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (md_alg != MBEDTLS_MD_NONE) {
2370 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2371 params, params_len,
2372 md_alg);
2373 if (ret != 0) {
2374 return ret;
2375 }
2376 } else {
2377 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2378 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002379 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002382
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002383 /*
2384 * Verify signature
2385 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2387 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002388 mbedtls_ssl_send_alert_message(
2389 ssl,
2390 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2392 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002393 }
2394
Gilles Peskineeccd8882020-03-10 12:19:08 +01002395#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002397 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002399#endif
2400
Jerry Yu693a47a2022-06-23 14:02:28 +08002401#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002403 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2404 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002405 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002406 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (rsassa_pss_options.expected_salt_len == 0) {
2408 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2409 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2412 peer_pk,
2413 md_alg, hash, hashlen,
2414 p, sig_len);
2415 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002416#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 ret = mbedtls_pk_verify_restartable(peer_pk,
2418 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002421 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002422#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002424#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002426 mbedtls_ssl_send_alert_message(
2427 ssl,
2428 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2430 }
2431 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002432#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002434 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002436#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002438 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002439
2440#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2441 /* We don't need the peer's public key anymore. Free it,
2442 * so that more RAM is available for upcoming expensive
2443 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002445#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002447#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002449exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 ssl->state++;
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002455}
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002458MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002459static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002460{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002461 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002462 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2467 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002468 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002470 }
2471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2473 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002474}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002475#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002476MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002477static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002478{
Janos Follath865b3eb2019-12-16 11:46:15 +00002479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002480 unsigned char *buf;
2481 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002482 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002483 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002484 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002485 size_t sig_alg_len;
2486#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 unsigned char *sig_alg;
2488 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002489#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2494 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002495 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002497 }
2498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2500 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2501 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 }
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2505 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002506 mbedtls_ssl_send_alert_message(
2507 ssl,
2508 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2510 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002513 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002514 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2518 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002521 /* Current message is probably the ServerHelloDone */
2522 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002523 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002524 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002525
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002526 /*
2527 * struct {
2528 * ClientCertificateType certificate_types<1..2^8-1>;
2529 * SignatureAndHashAlgorithm
2530 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2531 * DistinguishedName certificate_authorities<0..2^16-1>;
2532 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002533 *
2534 * Since we only support a single certificate on clients, let's just
2535 * ignore all the information that's supposed to help us pick a
2536 * certificate.
2537 *
2538 * We could check that our certificate matches the request, and bail out
2539 * if it doesn't, but it's simpler to just send the certificate anyway,
2540 * and give the server the opportunity to decide if it should terminate
2541 * the connection when it doesn't like our certificate.
2542 *
2543 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2544 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002545 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002546 *
2547 * However, we still minimally parse the message to check it is at least
2548 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002549 */
Paul Bakker926af752012-11-23 13:38:07 +01002550 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002551
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002552 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2554 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2555 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2556 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2557 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002558 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002560 n = cert_type_len;
2561
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002562 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002563 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002564 * * the length of the signature algorithms field (if minor version of
2565 * SSL is 3),
2566 * * distinguished name length otherwise.
2567 * Both reach at most the index:
2568 * ...hdr_len + 2 + n,
2569 * therefore the buffer length at this point must be greater than that
2570 * regardless of the actual code path.
2571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2573 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2574 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2575 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2576 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002577 }
2578
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002579 /* supported_signature_algorithms */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002580 sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Ronald Cron90915f22022-03-07 11:11:36 +01002581
2582 /*
2583 * The furthest access in buf is in the loop few lines below:
2584 * sig_alg[i + 1],
2585 * where:
2586 * sig_alg = buf + ...hdr_len + 3 + n,
2587 * max(i) = sig_alg_len - 1.
2588 * Therefore the furthest access is:
2589 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2590 * which reduces to:
2591 * buf[...hdr_len + 3 + n + sig_alg_len],
2592 * which is one less than we need the buf to be.
2593 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2595 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002596 mbedtls_ssl_send_alert_message(
2597 ssl,
2598 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2600 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002601 }
Paul Bakker926af752012-11-23 13:38:07 +01002602
Ronald Cron90915f22022-03-07 11:11:36 +01002603#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2605 for (size_t i = 0; i < sig_alg_len; i += 2) {
2606 MBEDTLS_SSL_DEBUG_MSG(3,
2607 ("Supported Signature Algorithm found: %02x %02x",
2608 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002609 }
2610#endif
2611
2612 n += 2 + sig_alg_len;
2613
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002614 /* certificate_authorities */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00002615 dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
Paul Bakker926af752012-11-23 13:38:07 +01002616
2617 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2619 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2620 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2621 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2622 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002623 }
2624
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002625#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2627 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002628 unsigned char *p = dn + i + 2;
2629 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002630 size_t asn1_len;
2631 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 memset(&name, 0, sizeof(name));
2633 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2634 if (dni_len > dn_len - i - 2 ||
2635 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2636 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2637 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2638 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002639 mbedtls_ssl_send_alert_message(
2640 ssl,
2641 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2643 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002644 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 MBEDTLS_SSL_DEBUG_MSG(3,
2646 ("DN hint: %.*s",
2647 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2648 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002649 }
2650#endif
2651
Paul Bakker926af752012-11-23 13:38:07 +01002652exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002656}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002657#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002659MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002660static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002661{
Janos Follath865b3eb2019-12-16 11:46:15 +00002662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2667 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2668 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2672 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2673 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2677 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2678 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2679 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2680 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2681 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 }
2683
2684 ssl->state++;
2685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2688 mbedtls_ssl_recv_flight_completed(ssl);
2689 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002690#endif
2691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002695}
2696
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002697MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002698static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002699{
Janos Follath865b3eb2019-12-16 11:46:15 +00002700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002701
2702 size_t header_len;
2703 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002704 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002705 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 /*
2712 * DHM key exchange -- send G^X mod P
2713 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002717 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2720 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2721 &ssl->out_msg[header_len], content_len,
2722 ssl->conf->f_rng, ssl->conf->p_rng);
2723 if (ret != 0) {
2724 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2725 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2729 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2732 ssl->handshake->premaster,
2733 MBEDTLS_PREMASTER_SIZE,
2734 &ssl->handshake->pmslen,
2735 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2736 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2737 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 }
2739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2741 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002743#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2744 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2745 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2746 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002748 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2749 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002751#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2753 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002754 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002755
2756 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2757
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002758 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002759
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002761
Hanno Becker4a63ed42019-01-08 11:39:35 +00002762 /*
2763 * Generate EC private key for ECDHE exchange.
2764 */
2765
Hanno Becker4a63ed42019-01-08 11:39:35 +00002766 /* The master secret is obtained from the shared ECDH secret by
2767 * applying the TLS 1.2 PRF with a specific salt and label. While
2768 * the PSA Crypto API encourages combining key agreement schemes
2769 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2770 * yet support the provisioning of salt + label to the KDF.
2771 * For the time being, we therefore need to split the computation
2772 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002773 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2775 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002776 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002777 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002778
2779 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002781 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if (status != PSA_SUCCESS) {
2783 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2784 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002785
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002786 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002787 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002788 * but we just need to add a length byte before that. */
2789 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2790 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002792 size_t own_pubkey_len;
2793
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002794 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 own_pubkey, own_pubkey_max_len,
2796 &own_pubkey_len);
2797 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002798 psa_destroy_key(handshake->xxdh_psa_privkey);
2799 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002801 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002802
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002803 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2804 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002805
Hanno Becker4a63ed42019-01-08 11:39:35 +00002806 /* The ECDH secret is the premaster secret used for key derivation. */
2807
Janos Follathdf3b0892019-08-08 11:12:24 +01002808 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002810 handshake->xxdh_psa_privkey,
2811 handshake->xxdh_psa_peerkey,
2812 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 ssl->handshake->premaster,
2814 sizeof(ssl->handshake->premaster),
2815 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002816
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002817 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2818 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2821 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2822 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002823#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002824 /*
2825 * ECDH key exchange -- send client public value
2826 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002827 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002828
Gilles Peskineeccd8882020-03-10 12:19:08 +01002829#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 if (ssl->handshake->ecrs_enabled) {
2831 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002832 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002836 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002837#endif
2838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2840 &content_len,
2841 &ssl->out_msg[header_len], 1000,
2842 ssl->conf->f_rng, ssl->conf->p_rng);
2843 if (ret != 0) {
2844 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002845#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002847 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002849#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002851 }
2852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2854 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002855
Gilles Peskineeccd8882020-03-10 12:19:08 +01002856#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002858 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002859 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002860 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002861
2862ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002864 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002866#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2868 &ssl->handshake->pmslen,
2869 ssl->handshake->premaster,
2870 MBEDTLS_MPI_MAX_SIZE,
2871 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2872 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002873#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002875 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002877#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002879 }
2880
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2882 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002883#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2886 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2887 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2888 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002889#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2890 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002892 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2893 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2894 psa_key_attributes_t key_attributes;
2895
2896 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2897
2898 /*
2899 * opaque psk_identity<0..2^16-1>;
2900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002902 /* We don't offer PSK suites if we don't have a PSK,
2903 * and we check that the server's choice is among the
2904 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2906 }
Neil Armstrong868af822022-03-09 10:26:25 +01002907
Neil Armstrongfc834f22022-03-23 17:54:38 +01002908 /* uint16 to store content length */
2909 const size_t content_len_size = 2;
2910
Neil Armstrong868af822022-03-09 10:26:25 +01002911 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 if (header_len + content_len_size + ssl->conf->psk_identity_len
2914 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2915 MBEDTLS_SSL_DEBUG_MSG(1,
2916 ("psk identity too long or SSL buffer too short"));
2917 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002918 }
2919
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002920 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2923 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002924 header_len += content_len_size;
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 memcpy(p, ssl->conf->psk_identity,
2927 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002928 p += ssl->conf->psk_identity_len;
2929
Neil Armstrong868af822022-03-09 10:26:25 +01002930 header_len += ssl->conf->psk_identity_len;
2931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002933
2934 /*
2935 * Generate EC private key for ECDHE exchange.
2936 */
2937
2938 /* The master secret is obtained from the shared ECDH secret by
2939 * applying the TLS 1.2 PRF with a specific salt and label. While
2940 * the PSA Crypto API encourages combining key agreement schemes
2941 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2942 * yet support the provisioning of salt + label to the KDF.
2943 * For the time being, we therefore need to split the computation
2944 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2945 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2947 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002948 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002949 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002950
2951 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002953 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002955 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 }
Neil Armstrong868af822022-03-09 10:26:25 +01002957
2958 /* Export the public part of the ECDH private key from PSA.
2959 * The export format is an ECPoint structure as expected by TLS,
2960 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002961 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002962 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002964 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002965
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002966 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 own_pubkey, own_pubkey_max_len,
2968 &own_pubkey_len);
2969 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002970 psa_destroy_key(handshake->xxdh_psa_privkey);
2971 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002972 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002973 }
2974
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002975 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002976 content_len = own_pubkey_len + 1;
2977
Neil Armstrong25400452022-03-23 17:44:07 +01002978 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2979 * - a uint16 containing the length (in octets) of the ECDH computation
2980 * - the octet string produced by the ECDH computation
2981 * - a uint16 containing the length (in octets) of the PSK
2982 * - the PSK itself
2983 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002984 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 const unsigned char * const pms_end = pms +
2986 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002987 /* uint16 to store length (in octets) of the ECDH computation */
2988 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002989 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002990
Neil Armstrong25400452022-03-23 17:44:07 +01002991 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002993 handshake->xxdh_psa_privkey,
2994 handshake->xxdh_psa_peerkey,
2995 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 pms + zlen_size,
2997 pms_end - (pms + zlen_size),
2998 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01002999
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02003000 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
3001 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003004 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003006 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 }
Neil Armstrong868af822022-03-09 10:26:25 +01003008
Neil Armstrong25400452022-03-23 17:44:07 +01003009 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003011 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003013#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3014 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003015#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003017 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003018 * opaque psk_identity<0..2^16-1>;
3019 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003021 /* We don't offer PSK suites if we don't have a PSK,
3022 * and we check that the server's choice is among the
3023 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003025 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003026
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003027 header_len = 4;
3028 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3031 MBEDTLS_SSL_DEBUG_MSG(1,
3032 ("psk identity too long or SSL buffer too short"));
3033 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003034 }
3035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3037 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 memcpy(ssl->out_msg + header_len,
3040 ssl->conf->psk_identity,
3041 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003042 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003046 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003051 /*
3052 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 if (header_len + 2 + content_len >
3057 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3058 MBEDTLS_SSL_DEBUG_MSG(1,
3059 ("psk identity or DHM size too long or SSL buffer too short"));
3060 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003061 }
3062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3064 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3067 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3068 &ssl->out_msg[header_len], content_len,
3069 ssl->conf->f_rng, ssl->conf->p_rng);
3070 if (ret != 0) {
3071 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3072 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003073 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003074
3075#if defined(MBEDTLS_USE_PSA_CRYPTO)
3076 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003078 size_t pms_len;
3079
3080 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3082 pms + 2, pms_end - (pms + 2), &pms_len,
3083 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3084 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3085 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003086 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003088 pms += 2 + pms_len;
3089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003091#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003094#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3096 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003097 /*
3098 * ClientECDiffieHellmanPublic public;
3099 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3101 &content_len,
3102 &ssl->out_msg[header_len],
3103 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3104 ssl->conf->f_rng, ssl->conf->p_rng);
3105 if (ret != 0) {
3106 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3107 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003108 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3111 MBEDTLS_DEBUG_ECDH_Q);
3112 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003113#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003114 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3116 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003117 }
3118
Neil Armstrong80f6f322022-05-03 17:56:38 +02003119#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003121 (mbedtls_key_exchange_type_t) ciphersuite_info->
3122 key_exchange)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 MBEDTLS_SSL_DEBUG_RET(1,
3124 "mbedtls_ssl_psk_derive_premaster", ret);
3125 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003126 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003127#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003129#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003132 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3134 &content_len, 0)) != 0) {
3135 return ret;
3136 }
3137 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003139#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003141 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003142
Neil Armstrongca7d5062022-05-31 14:43:23 +02003143#if defined(MBEDTLS_USE_PSA_CRYPTO)
3144 unsigned char *out_p = ssl->out_msg + header_len;
3145 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3146 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3148 out_p, end_p - out_p, &content_len,
3149 MBEDTLS_ECJPAKE_ROUND_TWO);
3150 if (ret != 0) {
3151 psa_destroy_key(ssl->handshake->psa_pake_password);
3152 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3153 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3154 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003155 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003156#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3158 ssl->out_msg + header_len,
3159 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3160 &content_len,
3161 ssl->conf->f_rng, ssl->conf->p_rng);
3162 if (ret != 0) {
3163 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3164 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003165 }
3166
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3168 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3169 ssl->conf->f_rng, ssl->conf->p_rng);
3170 if (ret != 0) {
3171 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3172 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003173 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003174#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003176#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003177 {
3178 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3180 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003181 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003183 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3185 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
3187 ssl->state++;
3188
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3190 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3191 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 }
3193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003197}
3198
Gilles Peskineeccd8882020-03-10 12:19:08 +01003199#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003200MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003201static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003202{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003203 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003204 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3210 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3211 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003212 }
3213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3215 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003216 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003218 }
3219
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3221 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003222}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003223#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003224MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003225static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003228 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003229 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003230 size_t n = 0, offset = 0;
3231 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003232 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003234 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003235 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003236#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003237 size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003238#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003239 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 +02003240#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003243
Gilles Peskineeccd8882020-03-10 12:19:08 +01003244#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 if (ssl->handshake->ecrs_enabled &&
3246 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003247 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003248 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003249#endif
3250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3252 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3253 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003254 }
3255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3257 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003260 }
3261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 if (ssl->handshake->client_auth == 0 ||
3263 mbedtls_ssl_own_cert(ssl) == NULL) {
3264 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003265 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 }
3268
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 if (mbedtls_ssl_own_key(ssl) == NULL) {
3270 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3271 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003272 }
3273
3274 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003275 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003277#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003279 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003281
3282sign:
3283#endif
3284
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003285 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3286 if (0 != ret) {
3287 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3288 return ret;
3289 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Ronald Cron90915f22022-03-07 11:11:36 +01003291 /*
3292 * digitally-signed struct {
3293 * opaque handshake_messages[handshake_messages_length];
3294 * };
3295 *
3296 * Taking shortcut here. We assume that the server always allows the
3297 * PRF Hash function and has sent it in the allowed signature
3298 * algorithms list received in the Certificate Request message.
3299 *
3300 * Until we encounter a server that does not, we will take this
3301 * shortcut.
3302 *
3303 * Reason: Otherwise we should have running hashes for SHA512 and
3304 * SHA224 in order to satisfy 'weird' needs from the server
3305 * side.
3306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003308 md_alg = MBEDTLS_MD_SHA384;
3309 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003311 md_alg = MBEDTLS_MD_SHA256;
3312 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003313 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003315
3316 /* Info from md_alg will be used instead */
3317 hashlen = 0;
3318 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003319
Gilles Peskineeccd8882020-03-10 12:19:08 +01003320#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003322 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003324#endif
3325
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3327 md_alg, hash_start, hashlen,
3328 ssl->out_msg + 6 + offset,
3329 out_buf_len - 6 - offset,
3330 &n,
3331 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3332 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003333#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003335 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003337#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003339 }
Paul Bakker926af752012-11-23 13:38:07 +01003340
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003342
Paul Bakker1ef83d62012-04-11 12:09:53 +00003343 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3345 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003346
3347 ssl->state++;
3348
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3350 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3351 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 }
3353
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003357}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003358#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003361MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003362static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003363{
Janos Follath865b3eb2019-12-16 11:46:15 +00003364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003365 uint32_t lifetime;
3366 size_t ticket_len;
3367 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003368 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3373 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3374 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003375 }
3376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3378 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003379 mbedtls_ssl_send_alert_message(
3380 ssl,
3381 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3383 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003384 }
3385
3386 /*
3387 * struct {
3388 * uint32 ticket_lifetime_hint;
3389 * opaque ticket<0..2^16-1>;
3390 * } NewSessionTicket;
3391 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003392 * 0 . 3 ticket_lifetime_hint
3393 * 4 . 5 ticket_len (n)
3394 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3397 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3398 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3399 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3400 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3401 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003402 }
3403
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003405
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003406 lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003407
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003408 ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003409
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3411 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3412 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3413 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3414 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003415 }
3416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003418
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003419 /* We're not waiting for a NewSessionTicket message any more */
3420 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003422
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003423 /*
3424 * Zero-length ticket means the server changed his mind and doesn't want
3425 * to send a ticket after all, so just forget it
3426 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 if (ticket_len == 0) {
3428 return 0;
3429 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003430
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003432 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003434 ssl->session->ticket = NULL;
3435 ssl->session->ticket_len = 0;
3436 }
3437
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003438 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003440 ssl->session_negotiate->ticket = NULL;
3441 ssl->session_negotiate->ticket_len = 0;
3442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3444 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3445 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3446 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3447 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003448 }
3449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003451
3452 ssl->session_negotiate->ticket = ticket;
3453 ssl->session_negotiate->ticket_len = ticket_len;
3454 ssl->session_negotiate->ticket_lifetime = lifetime;
3455
3456 /*
3457 * RFC 5077 section 3.4:
3458 * "If the client receives a session ticket from the server, then it
3459 * discards any Session ID that was sent in the ServerHello."
3460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003462 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003463
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003465
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003469
Paul Bakker5121ce52009-01-03 21:22:43 +00003470/*
Paul Bakker1961b702013-01-25 14:49:24 +01003471 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003473int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003474{
3475 int ret = 0;
3476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003478 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3481 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003482 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003483 }
3484#endif
3485
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 case MBEDTLS_SSL_HELLO_REQUEST:
3488 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 break;
3490
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 /*
3492 * ==> ClientHello
3493 */
3494 case MBEDTLS_SSL_CLIENT_HELLO:
3495 ret = mbedtls_ssl_write_client_hello(ssl);
3496 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 /*
3499 * <== ServerHello
3500 * Certificate
3501 * ( ServerKeyExchange )
3502 * ( CertificateRequest )
3503 * ServerHelloDone
3504 */
3505 case MBEDTLS_SSL_SERVER_HELLO:
3506 ret = ssl_parse_server_hello(ssl);
3507 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3510 ret = mbedtls_ssl_parse_certificate(ssl);
3511 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003512
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3514 ret = ssl_parse_server_key_exchange(ssl);
3515 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3518 ret = ssl_parse_certificate_request(ssl);
3519 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3522 ret = ssl_parse_server_hello_done(ssl);
3523 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 /*
3526 * ==> ( Certificate/Alert )
3527 * ClientKeyExchange
3528 * ( CertificateVerify )
3529 * ChangeCipherSpec
3530 * Finished
3531 */
3532 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3533 ret = mbedtls_ssl_write_certificate(ssl);
3534 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3537 ret = ssl_write_client_key_exchange(ssl);
3538 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3541 ret = ssl_write_certificate_verify(ssl);
3542 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3545 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3546 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 case MBEDTLS_SSL_CLIENT_FINISHED:
3549 ret = mbedtls_ssl_write_finished(ssl);
3550 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003551
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 /*
3553 * <== ( NewSessionTicket )
3554 * ChangeCipherSpec
3555 * Finished
3556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3559 ret = ssl_parse_new_session_ticket(ssl);
3560 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003561#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003562
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3564 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3565 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003566
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 case MBEDTLS_SSL_SERVER_FINISHED:
3568 ret = mbedtls_ssl_parse_finished(ssl);
3569 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003570
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 case MBEDTLS_SSL_FLUSH_BUFFERS:
3572 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3573 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3574 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003575
Gilles Peskine449bd832023-01-11 14:50:10 +01003576 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3577 mbedtls_ssl_handshake_wrapup(ssl);
3578 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003579
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 default:
3581 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3582 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003584
Gilles Peskine449bd832023-01-11 14:50:10 +01003585 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003586}
Jerry Yuc5aef882021-12-23 20:15:02 +08003587
Jerry Yufb4b6472022-01-27 15:03:26 +08003588#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */