blob: 27bbafa06ec679f35c33e658b5830f7fc32c85f0 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020025
SimonBd5800b72016-04-26 07:43:27 +010026#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010027#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000028#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000029#include "mbedtls/debug.h"
30#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020031#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010032
Hanno Beckerbb89e272019-01-08 12:54:37 +000033#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020034#include "psa_util_internal.h"
Ronald Cron69a63422021-10-18 09:47:58 +020035#include "psa/crypto.h"
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040036#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Andrzej Kurek00644842023-05-30 05:45:00 -040037/* Define a local translating function to save code size by not using too many
38 * arguments in each translating place. */
39static int local_err_translation(psa_status_t status)
40{
41 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040042 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040043 psa_generic_status_to_mbedtls);
44}
45#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040046#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +000047#endif /* MBEDTLS_USE_PSA_CRYPTO */
48
SimonBd5800b72016-04-26 07:43:27 +010049#include <string.h>
50
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020051#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010054#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020055#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050058#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020059#endif
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010063static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
64 unsigned char *buf,
65 const unsigned char *end,
66 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010067{
68 unsigned char *p = buf;
69
70 *olen = 0;
71
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010072 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010073 * initial ClientHello, in which case also adding the renegotiation
74 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
76 return 0;
77 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 MBEDTLS_SSL_DEBUG_MSG(3,
80 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010083
Paul Bakkerd3edc862013-03-20 16:07:17 +010084 /*
85 * Secure renegotiation
86 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010088 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010089
90 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
92 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010095
96 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100101
Valerio Setti7aeec542023-07-05 18:57:21 +0200102#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200103 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100104 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100105
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
108 unsigned char *buf,
109 const unsigned char *end,
110 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100111{
112 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100113 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100114
115 *olen = 0;
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 MBEDTLS_SSL_DEBUG_MSG(3,
118 ("client hello, adding supported_point_formats extension"));
119 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100122 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100123
124 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100125 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200126
127 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100129
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200130 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100133}
Valerio Setti7aeec542023-07-05 18:57:21 +0200134#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200135 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200136 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100137
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200138#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200139MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100140static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
141 unsigned char *buf,
142 const unsigned char *end,
143 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200144{
Janos Follath865b3eb2019-12-16 11:46:15 +0000145 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200146 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100147 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200148
149 *olen = 0;
150
151 /* Skip costly extension if we can't use EC J-PAKE anyway */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200152#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
154 return 0;
155 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200156#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
158 return 0;
159 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200160#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 MBEDTLS_SSL_DEBUG_MSG(3,
163 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100168 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200169
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200170 /*
171 * We may need to send ClientHello multiple times for Hello verification.
172 * We don't want to compute fresh values every time (both for performance
173 * and consistency reasons), so cache the extension content.
174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (ssl->handshake->ecjpake_cache == NULL ||
176 ssl->handshake->ecjpake_cache_len == 0) {
177 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200178
Neil Armstrongca7d5062022-05-31 14:43:23 +0200179#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100180 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 p + 2, end - p - 2, &kkpp_len,
182 MBEDTLS_ECJPAKE_ROUND_ONE);
183 if (ret != 0) {
184 psa_destroy_key(ssl->handshake->psa_pake_password);
185 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
186 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
187 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200188 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200189#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
191 p + 2, end - p - 2, &kkpp_len,
192 ssl->conf->f_rng, ssl->conf->p_rng);
193 if (ret != 0) {
194 MBEDTLS_SSL_DEBUG_RET(1,
195 "mbedtls_ecjpake_write_round_one", ret);
196 return ret;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200197 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200198#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
201 if (ssl->handshake->ecjpake_cache == NULL) {
202 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
203 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200204 }
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200207 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 } else {
209 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200210
211 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200215 }
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100218 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200219
220 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200223}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200224#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000225
Hanno Beckera0e20d02019-05-15 14:03:01 +0100226#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200227MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100228static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
229 unsigned char *buf,
230 const unsigned char *end,
231 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100232{
233 unsigned char *p = buf;
234 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100235
236 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100237 * struct {
238 * opaque cid<0..2^8-1>;
239 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100241
242 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
244 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
245 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100246 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100248
249 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
250 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100252
253 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100255 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100256 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100258 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100259
260 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100262
263 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100266}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100267#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200270MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100271static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
272 unsigned char *buf,
273 const unsigned char *end,
274 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200275{
276 unsigned char *p = buf;
277
Simon Butcher0fc94e92015-09-28 20:52:04 +0100278 *olen = 0;
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
281 return 0;
282 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 MBEDTLS_SSL_DEBUG_MSG(3,
285 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100290 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200291
292 *p++ = 0x00;
293 *p++ = 1;
294
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200295 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200296
297 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200304MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100305static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
306 unsigned char *buf,
307 const unsigned char *end,
308 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100309{
310 unsigned char *p = buf;
311
Simon Butcher0fc94e92015-09-28 20:52:04 +0100312 *olen = 0;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
315 return 0;
316 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 MBEDTLS_SSL_DEBUG_MSG(3,
319 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100324 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100325
326 *p++ = 0x00;
327 *p++ = 0x00;
328
329 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200336MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100337static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
338 unsigned char *buf,
339 const unsigned char *end,
340 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200341{
342 unsigned char *p = buf;
343
Simon Butcher0fc94e92015-09-28 20:52:04 +0100344 *olen = 0;
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
347 return 0;
348 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 MBEDTLS_SSL_DEBUG_MSG(3,
351 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100356 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200357
358 *p++ = 0x00;
359 *p++ = 0x00;
360
361 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200368MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100369static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
370 unsigned char *buf,
371 const unsigned char *end,
372 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200373{
374 unsigned char *p = buf;
375 size_t tlen = ssl->session_negotiate->ticket_len;
376
Simon Butcher0fc94e92015-09-28 20:52:04 +0100377 *olen = 0;
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
380 return 0;
381 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 MBEDTLS_SSL_DEBUG_MSG(3,
384 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200385
Hanno Becker261602c2017-04-12 14:54:42 +0100386 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100390 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100393 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200394
395 *olen = 4;
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
398 return 0;
399 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 MBEDTLS_SSL_DEBUG_MSG(3,
402 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200405
406 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200411
Ron Eldora9788042018-12-05 11:04:31 +0200412#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200413MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100414static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
415 unsigned char *buf,
416 const unsigned char *end,
417 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100418{
419 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200420 size_t protection_profiles_index = 0, ext_len = 0;
421 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100422
423 *olen = 0;
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
426 (ssl->conf->dtls_srtp_profile_list == NULL) ||
427 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
428 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100429 }
430
Ron Eldora9788042018-12-05 11:04:31 +0200431 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100432 * uint8 SRTPProtectionProfile[2];
433 *
434 * struct {
435 * SRTPProtectionProfiles SRTPProtectionProfiles;
436 * opaque srtp_mki<0..255>;
437 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100438 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100439 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200441 mki_len = ssl->dtls_srtp_info.mki_len;
442 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300443 /* Extension length = 2 bytes for profiles length,
444 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
445 * 1 byte for srtp_mki vector length and the mki_len value
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200450
451 /* Check there is room in the buffer for the extension + 4 bytes
452 * - the extension tag (2 bytes)
453 * - the extension length (2 bytes)
454 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100458 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100461 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100462
Ron Eldor3adb9922017-12-21 10:15:08 +0200463 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200464 /* micro-optimization:
465 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
466 * which is lower than 127, so the upper byte of the length is always 0
467 * For the documentation, the more generic code is left in comments
468 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
469 * >> 8 ) & 0xFF );
470 */
471 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300475 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200477 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
479 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
480 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
481 profile_value));
482 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100483 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200485 /*
486 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200487 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200488 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 MBEDTLS_SSL_DEBUG_MSG(3,
490 ("client hello, "
491 "illegal DTLS-SRTP protection profile %d",
492 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
493 ));
494 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100495 }
496 }
497
Ron Eldor591f1622018-01-22 12:30:04 +0200498 *p++ = mki_len & 0xFF;
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (mki_len != 0) {
501 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200502 /*
503 * Increment p to point to the current position.
504 */
505 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
507 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200508 }
509
Ron Eldoref72faf2018-07-12 11:54:20 +0300510 /*
511 * total extension length: extension type (2 bytes)
512 * + extension length (2 bytes)
513 * + protection profile length (2 bytes)
514 * + 2 * number of protection profiles
515 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200516 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300517 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200518 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100521}
522#endif /* MBEDTLS_SSL_DTLS_SRTP */
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
525 unsigned char *buf,
526 const unsigned char *end,
527 int uses_ec,
528 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100529{
530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
531 unsigned char *p = buf;
532 size_t ext_len = 0;
533
534 (void) ssl;
535 (void) end;
536 (void) uses_ec;
537 (void) ret;
538 (void) ext_len;
539
540 *out_len = 0;
541
542 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
543 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
544#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
546 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
547 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100548 }
549 p += ext_len;
550#endif
551
Valerio Setti7aeec542023-07-05 18:57:21 +0200552#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200553 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Ronald Cron12dcdf02022-02-16 15:28:22 +0100554 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (uses_ec) {
556 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
557 &ext_len)) != 0) {
558 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
559 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100560 }
561 p += ext_len;
562 }
563#endif
564
565#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
567 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
568 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100569 }
570 p += ext_len;
571#endif
572
573#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
575 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
576 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100577 }
578 p += ext_len;
579#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
580
581#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
583 &ext_len)) != 0) {
584 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
585 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100586 }
587 p += ext_len;
588#endif
589
590#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
592 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
593 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100594 }
595 p += ext_len;
596#endif
597
598#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
600 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
601 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100602 }
603 p += ext_len;
604#endif
605
606#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
608 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
609 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100610 }
611 p += ext_len;
612#endif
613
614#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
616 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
617 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100618 }
619 p += ext_len;
620#endif
621
622 *out_len = p - buf;
623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100625}
626
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200627MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100628static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
629 const unsigned char *buf,
630 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100634 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000636 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 mbedtls_ct_memcmp(buf + 1,
638 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
639 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
640 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
641 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100642 mbedtls_ssl_send_alert_message(
643 ssl,
644 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
646 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000647 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100650 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (len != 1 || buf[0] != 0x00) {
652 MBEDTLS_SSL_DEBUG_MSG(1,
653 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100654 mbedtls_ssl_send_alert_message(
655 ssl,
656 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
658 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100659 }
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100662 }
Paul Bakker48916f92012-09-16 19:57:18 +0000663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000665}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200668MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100669static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
670 const unsigned char *buf,
671 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200672{
673 /*
674 * server should use the extension only if we did,
675 * and if so the server's value should match ours (and len is always 1)
676 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200678 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 buf[0] != ssl->conf->mfl_code) {
680 MBEDTLS_SSL_DEBUG_MSG(1,
681 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100682 mbedtls_ssl_send_alert_message(
683 ssl,
684 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
686 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200687 }
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000692
Hanno Beckera0e20d02019-05-15 14:03:01 +0100693#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200694MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100695static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
696 const unsigned char *buf,
697 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100698{
699 size_t peer_cid_len;
700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100702 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
703 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
705 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
706 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
707 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
708 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100709 }
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (len == 0) {
712 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
713 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
714 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
715 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100716 }
717
718 peer_cid_len = *buf++;
719 len--;
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
722 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
723 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
724 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
725 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100726 }
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (len != peer_cid_len) {
729 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
730 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
731 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
732 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100733 }
734
Hanno Becker5a299902019-05-03 12:47:49 +0100735 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100736 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
740 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100743}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100744#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200747MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100748static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
749 const unsigned char *buf,
750 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100751{
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
753 len != 0) {
754 MBEDTLS_SSL_DEBUG_MSG(1,
755 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100756 mbedtls_ssl_send_alert_message(
757 ssl,
758 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
760 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100761 }
762
763 ((void) buf);
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200772MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100773static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
774 const unsigned char *buf,
775 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200776{
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
778 len != 0) {
779 MBEDTLS_SSL_DEBUG_MSG(1,
780 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100781 mbedtls_ssl_send_alert_message(
782 ssl,
783 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
785 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200786 }
787
788 ((void) buf);
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200797MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100798static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
799 const unsigned char *buf,
800 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200801{
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
803 len != 0) {
804 MBEDTLS_SSL_DEBUG_MSG(1,
805 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100806 mbedtls_ssl_send_alert_message(
807 ssl,
808 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
810 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200811 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200812
813 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200814
815 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200820
Valerio Setti7aeec542023-07-05 18:57:21 +0200821#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200822 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100823 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200824MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100825static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
826 const unsigned char *buf,
827 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200828{
829 size_t list_size;
830 const unsigned char *p;
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (len == 0 || (size_t) (buf[0] + 1) != len) {
833 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
834 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
835 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
836 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200837 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200838 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200839
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200840 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 while (list_size > 0) {
842 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
843 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Valerio Setti7aeec542023-07-05 18:57:21 +0200844#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
845 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200846 ssl->handshake->ecdh_ctx.point_format = p[0];
Valerio Setti7aeec542023-07-05 18:57:21 +0200847#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200848#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
850 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
851 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200852#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
854 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200855 }
856
857 list_size--;
858 p++;
859 }
860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
862 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
863 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
864 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200865}
Valerio Setti7aeec542023-07-05 18:57:21 +0200866#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +0200867 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Valerio Setti45d56f32023-07-13 17:23:20 +0200868 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200869
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200870#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200871MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100872static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
873 const unsigned char *buf,
874 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200875{
Janos Follath865b3eb2019-12-16 11:46:15 +0000876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (ssl->handshake->ciphersuite_info->key_exchange !=
879 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
880 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
881 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200882 }
883
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200884 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200886 ssl->handshake->ecjpake_cache = NULL;
887 ssl->handshake->ecjpake_cache_len = 0;
888
Neil Armstrongca7d5062022-05-31 14:43:23 +0200889#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if ((ret = mbedtls_psa_ecjpake_read_round(
891 &ssl->handshake->psa_pake_ctx, buf, len,
892 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
893 psa_destroy_key(ssl->handshake->psa_pake_password);
894 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100897 mbedtls_ssl_send_alert_message(
898 ssl,
899 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
901 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200902 }
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 return 0;
905#else
906 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
907 buf, len)) != 0) {
908 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
909 mbedtls_ssl_send_alert_message(
910 ssl,
911 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
912 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
913 return ret;
914 }
915
916 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200917#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200918}
919#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200922MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100923static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
924 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200925{
926 size_t list_len, name_len;
927 const char **p;
928
929 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (ssl->conf->alpn_list == NULL) {
931 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100932 mbedtls_ssl_send_alert_message(
933 ssl,
934 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
936 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200937 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200938
939 /*
940 * opaque ProtocolName<1..2^8-1>;
941 *
942 * struct {
943 * ProtocolName protocol_name_list<2..2^16-1>
944 * } ProtocolNameList;
945 *
946 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
947 */
948
949 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (len < 4) {
951 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
952 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
953 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200954 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 list_len = (buf[0] << 8) | buf[1];
957 if (list_len != len - 2) {
958 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
959 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
960 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200961 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200962
963 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (name_len != list_len - 1) {
965 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
966 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
967 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200968 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200969
970 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
972 if (name_len == strlen(*p) &&
973 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200974 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200976 }
977 }
978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
980 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
981 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
982 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200985
Johan Pascalb62bb512015-12-03 21:56:45 +0100986#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200987MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100988static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
989 const unsigned char *buf,
990 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100991{
Johan Pascal43f94902020-09-22 12:25:52 +0200992 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200993 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100994 uint16_t server_protection_profile_value = 0;
995
996 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
998 (ssl->conf->dtls_srtp_profile_list == NULL) ||
999 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
1000 return 0;
1001 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001002
Ron Eldora9788042018-12-05 11:04:31 +02001003 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001004 * uint8 SRTPProtectionProfile[2];
1005 *
1006 * struct {
1007 * SRTPProtectionProfiles SRTPProtectionProfiles;
1008 * opaque srtp_mki<0..255>;
1009 * } UseSRTPData;
1010
1011 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1012 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001015 mki_len = ssl->dtls_srtp_info.mki_len;
1016 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001017
Ron Eldoref72faf2018-07-12 11:54:20 +03001018 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001019 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1020 * + protection profile (2 bytes)
1021 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001022 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if ((len < 5) || (len != (buf[4] + 5u))) {
1025 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1026 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001027
1028 /*
1029 * get the server protection profile
1030 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001031
1032 /*
1033 * protection profile length must be 0x0002 as we must have only
1034 * one protection profile in server Hello
1035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if ((buf[0] != 0) || (buf[1] != 2)) {
1037 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1038 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001041 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 server_protection_profile_value);
1043 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1044 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1045 mbedtls_ssl_get_srtp_profile_as_string(
1046 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001047 }
1048
Johan Pascal43f94902020-09-22 12:25:52 +02001049 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001050
Johan Pascalb62bb512015-12-03 21:56:45 +01001051 /*
1052 * Check we have the server profile in our list
1053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1055 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001056 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001058 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001060 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001061 }
1062 }
1063
Ron Eldor591f1622018-01-22 12:30:04 +02001064 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1066 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1067 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1068 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001069 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001070
1071 /* If server does not use mki in its reply, make sure the client won't keep
1072 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001074 ssl->dtls_srtp_info.mki_len = 0;
1075 }
1076
Ron Eldoref72faf2018-07-12 11:54:20 +03001077 /*
1078 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001079 * If the client detects a nonzero-length MKI in the server's response
1080 * that is different than the one the client offered, then the client
1081 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (len > 5 && (buf[4] != mki_len ||
1084 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1085 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1086 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1087 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001088 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001089#if defined(MBEDTLS_DEBUG_C)
1090 if (len > 5) {
1091 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1092 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001093 }
1094#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001096}
1097#endif /* MBEDTLS_SSL_DTLS_SRTP */
1098
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001099/*
1100 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001103MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001104static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001105{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001106 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001108 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001109
1110#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1111 uint8_t cookie_len;
1112#else
1113 uint16_t cookie_len;
1114#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001117
Gilles Peskineb64bf062019-09-27 14:02:44 +02001118 /* Check that there is enough room for:
1119 * - 2 bytes of version
1120 * - 1 byte of cookie_len
1121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1123 MBEDTLS_SSL_DEBUG_MSG(1,
1124 ("incoming HelloVerifyRequest message is too short"));
1125 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1126 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1127 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001128 }
1129
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001130 /*
1131 * struct {
1132 * ProtocolVersion server_version;
1133 * opaque cookie<0..2^8-1>;
1134 * } HelloVerifyRequest;
1135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1137 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001138 p += 2;
1139
TRodziewicz2d8800e2021-05-13 19:14:19 +02001140 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001141 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1142 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1143 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001144 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1146 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1149 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001152 }
1153
1154 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1156 MBEDTLS_SSL_DEBUG_MSG(1,
1157 ("cookie length does not match incoming message size"));
1158 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1159 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1160 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001161 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1167 if (ssl->handshake->cookie == NULL) {
1168 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1169 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001173 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001174
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001175 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001177 ret = mbedtls_ssl_reset_checksum(ssl);
1178 if (0 != ret) {
1179 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1180 return ret;
1181 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001190
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001191MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001192static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001193{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001194 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001195 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001196 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001197 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001198 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001200 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001201#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001202 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001208 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1210 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 }
1212
Hanno Becker79594fd2019-05-08 09:38:41 +01001213 buf = ssl->in_msg;
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001218 ssl->renego_records_seen++;
1219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (ssl->conf->renego_max_records >= 0 &&
1221 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1222 MBEDTLS_SSL_DEBUG_MSG(1,
1223 ("renegotiation requested, but not honored by server"));
1224 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001225 }
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_DEBUG_MSG(1,
1228 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001229
1230 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001236 mbedtls_ssl_send_alert_message(
1237 ssl,
1238 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1240 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 }
1242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1245 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1246 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1247 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1248 return ssl_parse_hello_verify_request(ssl);
1249 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001250 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001252 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001253 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001254 }
1255 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1259 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1260 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1261 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1262 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1263 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 }
1265
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001266 /*
1267 * 0 . 1 server_version
1268 * 2 . 33 random (maybe including 4 bytes of Unix time)
1269 * 34 . 34 session_id length = n
1270 * 35 . 34+n session_id
1271 * 35+n . 36+n cipher_suite
1272 * 37+n . 37+n compression_method
1273 *
1274 * 38+n . 39+n extensions length (optional)
1275 * 40+n . .. extensions
1276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01001280 ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
1281 ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001282 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (ssl->tls_version < ssl->conf->min_tls_version ||
1285 ssl->tls_version > ssl->conf->max_tls_version) {
1286 MBEDTLS_SSL_DEBUG_MSG(1,
1287 (
1288 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1289 (unsigned) ssl->conf->min_tls_version,
1290 (unsigned) ssl->tls_version,
1291 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1294 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001297 }
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1300 ((unsigned long) buf[2] << 24) |
1301 ((unsigned long) buf[3] << 16) |
1302 ((unsigned long) buf[4] << 8) |
1303 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001307 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (n > 32) {
1312 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1313 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1314 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1315 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001316 }
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
1319 ext_len = ((buf[38 + n] << 8)
1320 | (buf[39 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if ((ext_len > 0 && ext_len < 4) ||
1323 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1324 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001325 mbedtls_ssl_send_alert_message(
1326 ssl,
1327 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1329 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001330 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001332 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 } else {
1334 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1335 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1336 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1337 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001340 /* ciphersuite (used later) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 i = (buf[35 + n] << 8) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001342
1343 /*
1344 * Read and check compression
1345 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001346 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1349 MBEDTLS_SSL_DEBUG_MSG(1,
1350 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001351 mbedtls_ssl_send_alert_message(
1352 ssl,
1353 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1355 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001356 }
1357
Paul Bakker380da532012-04-18 16:10:25 +00001358 /*
1359 * Initialize update checksum functions
1360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1362 if (ssl->handshake->ciphersuite_info == NULL) {
1363 MBEDTLS_SSL_DEBUG_MSG(1,
1364 ("ciphersuite info for %04x not found", (unsigned int) i));
1365 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1366 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1367 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001368 }
Paul Bakker380da532012-04-18 16:10:25 +00001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1373 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
1375 /*
1376 * Check if the session can be resumed
1377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_SSL_RENEGOTIATION)
1380 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001381#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001382 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001383 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001386 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001389#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001390 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001391 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 memcpy(ssl->session_negotiate->id, buf + 35, n);
1393 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 }
1396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1398 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1401 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1402 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Andrzej Kurek03bac442018-04-25 05:06:07 -04001404 /*
1405 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001406 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 while (1) {
1409 if (ssl->conf->ciphersuite_list[i] == 0) {
1410 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001411 mbedtls_ssl_send_alert_message(
1412 ssl,
1413 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1415 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 }
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (ssl->conf->ciphersuite_list[i++] ==
1419 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 }
1423
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001424 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 ssl->session_negotiate->ciphersuite);
1426 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1427 ssl->tls_version) != 0) {
1428 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001429 mbedtls_ssl_send_alert_message(
1430 ssl,
1431 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1433 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001434 }
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 MBEDTLS_SSL_DEBUG_MSG(3,
1437 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001438
Gilles Peskineeccd8882020-03-10 12:19:08 +01001439#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1441 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001442 ssl->handshake->ecrs_enabled = 1;
1443 }
1444#endif
1445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1447 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001448 mbedtls_ssl_send_alert_message(
1449 ssl,
1450 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1452 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 }
1454
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001455 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 MBEDTLS_SSL_DEBUG_MSG(2,
1458 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1459 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 while (ext_len) {
1462 unsigned int ext_id = ((ext[0] << 8)
1463 | (ext[1]));
1464 unsigned int ext_size = ((ext[2] << 8)
1465 | (ext[3]));
Paul Bakker48916f92012-09-16 19:57:18 +00001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (ext_size + 4 > ext_len) {
1468 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001469 mbedtls_ssl_send_alert_message(
1470 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1472 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001473 }
1474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 switch (ext_id) {
1476 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1477 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001480#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1483 ext_size)) != 0) {
1484 return ret;
1485 }
Paul Bakker48916f92012-09-16 19:57:18 +00001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1491 MBEDTLS_SSL_DEBUG_MSG(3,
1492 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1495 ext + 4, ext_size)) != 0) {
1496 return ret;
1497 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001501
Hanno Beckera0e20d02019-05-15 14:03:01 +01001502#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 case MBEDTLS_TLS_EXT_CID:
1504 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if ((ret = ssl_parse_cid_ext(ssl,
1507 ext + 4,
1508 ext_size)) != 0) {
1509 return ret;
1510 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001513#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1517 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1520 ext + 4, ext_size)) != 0) {
1521 return ret;
1522 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1529 MBEDTLS_SSL_DEBUG_MSG(3,
1530 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if ((ret = ssl_parse_extended_ms_ext(ssl,
1533 ext + 4, ext_size)) != 0) {
1534 return ret;
1535 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1542 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if ((ret = ssl_parse_session_ticket_ext(ssl,
1545 ext + 4, ext_size)) != 0) {
1546 return ret;
1547 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001551
Valerio Setti7aeec542023-07-05 18:57:21 +02001552#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +02001553 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02001554 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1556 MBEDTLS_SSL_DEBUG_MSG(3,
1557 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1560 ext + 4, ext_size)) != 0) {
1561 return ret;
1562 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 break;
Valerio Setti45d56f32023-07-13 17:23:20 +02001565#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
Valerio Settie9646ec2023-08-02 20:02:28 +02001566 MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001567 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001568
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001569#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1571 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1574 ext + 4, ext_size)) != 0) {
1575 return ret;
1576 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001579#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 case MBEDTLS_TLS_EXT_ALPN:
1583 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1586 return ret;
1587 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001591
Johan Pascalb62bb512015-12-03 21:56:45 +01001592#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 case MBEDTLS_TLS_EXT_USE_SRTP:
1594 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1597 return ret;
1598 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001601#endif /* MBEDTLS_SSL_DTLS_SRTP */
1602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 default:
1604 MBEDTLS_SSL_DEBUG_MSG(3,
1605 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001606 }
1607
1608 ext_len -= 4 + ext_size;
1609 ext += 4 + ext_size;
1610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (ext_len > 0 && ext_len < 4) {
1612 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1613 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001614 }
1615 }
1616
1617 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001618 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1619 * extensions. It sets the transform data for the resumed session which in
1620 * case of DTLS includes the server CID extracted from the CID extension.
1621 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (ssl->handshake->resume) {
1623 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1624 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001625 mbedtls_ssl_send_alert_message(
1626 ssl,
1627 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1629 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001630 }
1631 }
1632
Paul Bakker48916f92012-09-16 19:57:18 +00001633 /*
1634 * Renegotiation security checks
1635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001637 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1639 MBEDTLS_SSL_DEBUG_MSG(1,
1640 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001641 handshake_failure = 1;
1642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 renegotiation_info_seen == 0) {
1647 MBEDTLS_SSL_DEBUG_MSG(1,
1648 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001649 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1651 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1652 ssl->conf->allow_legacy_renegotiation ==
1653 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1654 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001655 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1657 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1658 renegotiation_info_seen == 1) {
1659 MBEDTLS_SSL_DEBUG_MSG(1,
1660 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001661 handshake_failure = 1;
1662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001666 mbedtls_ssl_send_alert_message(
1667 ssl,
1668 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1670 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676}
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1679 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001680MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001681static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1682 unsigned char **p,
1683 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001684{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001686 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001687
Paul Bakker29e1f122013-04-16 13:07:56 +02001688 /*
1689 * Ephemeral DH parameters:
1690 *
1691 * struct {
1692 * opaque dh_p<1..2^16-1>;
1693 * opaque dh_g<1..2^16-1>;
1694 * opaque dh_Ys<1..2^16-1>;
1695 * } ServerDHParams;
1696 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1698 p, end)) != 0) {
1699 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1700 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001701 }
1702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1704 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1705 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1706 dhm_actual_bitlen,
1707 ssl->conf->dhm_min_bitlen));
1708 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001709 }
1710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1712 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1713 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001716}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1718 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001719
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001720#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001721#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1722 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1723 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001724MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001725static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1726 unsigned char **p,
1727 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001728{
1729 uint16_t tls_id;
Gilles Peskine7910cdd2023-10-02 15:39:13 +02001730 size_t ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001731 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001732 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001733 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001734
1735 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001736 * struct {
1737 * ECParameters curve_params;
1738 * ECPoint public;
1739 * } ServerECDHParams;
1740 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001741 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001742 * 2..3 NamedCurve
1743 * 4 ECPoint.len
1744 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001745 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (end - *p < 4) {
1747 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1748 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001749
1750 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1752 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1753 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001754
1755 /* Next two bytes are the namedcurve value */
1756 tls_id = *(*p)++;
1757 tls_id <<= 8;
1758 tls_id |= *(*p)++;
1759
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001760 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1762 MBEDTLS_SSL_DEBUG_MSG(2,
1763 ("bad server key exchange message (ECDHE curve): %u",
1764 (unsigned) tls_id));
1765 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001766 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001767
Valerio Setti40d9ca92023-01-04 16:08:04 +01001768 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekielda4fba62023-06-02 14:52:28 +02001769 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1771 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001772 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001773 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001774 handshake->xxdh_psa_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001775
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001776 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001777 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if ((size_t) (end - *p) < ecpoint_len) {
1779 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1780 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001781
Gilles Peskinec29df532023-10-02 14:59:26 +02001782 if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1784 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001785
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001786 memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
1787 handshake->xxdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001788 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001791}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001792#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1793 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1794 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001795#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001796#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1797 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1798 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1799 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1800 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001801MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001802static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001803{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001804 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001805 mbedtls_ecp_group_id grp_id;
1806#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1807 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1808#else
1809 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1810#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1813 if (tls_id == 0) {
1814 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1815 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001816 }
1817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1819 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1822 return -1;
1823 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1826 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001829}
1830
Andrzej Kurek468c5062022-10-24 10:30:14 -04001831#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1832 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1833 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1834 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1835 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1836
1837#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1838 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1839 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001840MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001841static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1842 unsigned char **p,
1843 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001846
Paul Bakker29e1f122013-04-16 13:07:56 +02001847 /*
1848 * Ephemeral ECDH parameters:
1849 *
1850 * struct {
1851 * ECParameters curve_params;
1852 * ECPoint public;
1853 * } ServerECDHParams;
1854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1856 (const unsigned char **) p, end)) != 0) {
1857 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001858#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001860 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001862#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001864 }
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 if (ssl_check_server_ecdh_params(ssl) != 0) {
1867 MBEDTLS_SSL_DEBUG_MSG(1,
1868 ("bad server key exchange message (ECDHE curve)"));
1869 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001870 }
1871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001873}
Gilles Peskine449bd832023-01-11 14:50:10 +01001874#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1875 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001876 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1877#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001878#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001879MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001880static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1881 unsigned char **p,
1882 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001883{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001885 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001886 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001887
1888 /*
1889 * PSK parameters:
1890 *
1891 * opaque psk_identity_hint<0..2^16-1>;
1892 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 if (end - (*p) < 2) {
1894 MBEDTLS_SSL_DEBUG_MSG(1,
1895 ("bad server key exchange message (psk_identity_hint length)"));
1896 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001897 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001898 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001899 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (end - (*p) < len) {
1902 MBEDTLS_SSL_DEBUG_MSG(1,
1903 ("bad server key exchange message (psk_identity_hint length)"));
1904 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001905 }
1906
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001907 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001908 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001909 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001910 * someone needs that feature.
1911 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001912 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001913 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001916}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001917#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1920 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001921/*
1922 * Generate a pre-master secret and encrypt it with the server's RSA key
1923 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001924MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001925static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1926 size_t offset, size_t *olen,
1927 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001928{
Janos Follath865b3eb2019-12-16 11:46:15 +00001929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001930 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001931 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1935 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1936 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001937 }
1938
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001939 /*
1940 * Generate (part of) the pre-master as
1941 * struct {
1942 * ProtocolVersion client_version;
1943 * opaque random[46];
1944 * } PreMasterSecret;
1945 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 mbedtls_ssl_write_version(p, ssl->conf->transport,
1947 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1950 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1951 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001952 }
1953
1954 ssl->handshake->pmslen = 48;
1955
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001956#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1957 peer_pk = &ssl->handshake->peer_pubkey;
1958#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001960 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1962 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001963 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001964 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1965#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001966
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001967 /*
1968 * Now write it out, encrypted
1969 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1971 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1972 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001973 }
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 if ((ret = mbedtls_pk_encrypt(peer_pk,
1976 p, ssl->handshake->pmslen,
1977 ssl->out_msg + offset + len_bytes, olen,
1978 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1979 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1980 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1981 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001982 }
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (len_bytes == 2) {
1985 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001986 *olen += 2;
1987 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001988
Hanno Beckerae553dd2019-02-08 14:06:00 +00001989#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1990 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001992#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001994}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
1996 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1999 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002000MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002001static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002002{
Janos Follath865b3eb2019-12-16 11:46:15 +00002003 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002005
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002006#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2007 peer_pk = &ssl->handshake->peer_pubkey;
2008#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00002010 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2012 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002013 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002014 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2015#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002016
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002017 /* This is a public key, so it can't be opaque, so can_do() is a good
2018 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2020 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2021 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002022 }
2023
Valerio Setti97207782023-05-18 18:59:06 +02002024#if defined(MBEDTLS_ECP_C)
2025 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
2026#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002027
Przemek Stekielea4000f2022-03-16 09:49:33 +01002028#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002029 uint16_t tls_id = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02002030 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
Valerio Setti97207782023-05-18 18:59:06 +02002031 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002032
Valerio Setti97207782023-05-18 18:59:06 +02002033 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2035 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002036 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002037
Valerio Setti97207782023-05-18 18:59:06 +02002038 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (tls_id == 0) {
2040 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002041 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002043 }
2044
Valerio Setti1e868cc2023-01-09 17:30:01 +01002045 /* If the above conversion to TLS ID was fine, then also this one will be,
2046 so there is no need to check the return value here */
Przemek Stekielda4fba62023-06-02 14:52:28 +02002047 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
Valerio Settiea59c432023-07-25 11:14:03 +02002048 &ssl->handshake->xxdh_psa_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002049
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002050 ssl->handshake->xxdh_psa_type = key_type;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002051
Przemek Stekielea4000f2022-03-16 09:49:33 +01002052 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002053#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002054 memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2055 ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
Valerio Settid7ca3952023-05-17 15:36:18 +02002056 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002057#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002058 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2060 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002061 ssl->handshake->xxdh_psa_peerkey,
Gilles Peskinec29df532023-10-02 14:59:26 +02002062 sizeof(ssl->handshake->xxdh_psa_peerkey));
Przemek Stekielea4000f2022-03-16 09:49:33 +01002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (ret != 0) {
2065 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2066 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002067 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002068 ssl->handshake->xxdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002069#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2070#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2072 MBEDTLS_ECDH_THEIRS)) != 0) {
2073 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2074 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002075 }
2076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (ssl_check_server_ecdh_params(ssl) != 0) {
2078 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2079 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002080 }
Valerio Setti97207782023-05-18 18:59:06 +02002081#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002082#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2083 /* We don't need the peer's public key anymore. Free it,
2084 * so that more RAM is available for upcoming expensive
2085 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002087#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2092 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002093
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002094MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002095static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002096{
Janos Follath865b3eb2019-12-16 11:46:15 +00002097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002098 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002099 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002100 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2106 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002110 ((void) p);
2111 ((void) end);
2112#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2115 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2117 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2118 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2119 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002120 mbedtls_ssl_send_alert_message(
2121 ssl,
2122 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2124 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002125 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002128 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002130 }
2131 ((void) p);
2132 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2134 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002135
Gilles Peskineeccd8882020-03-10 12:19:08 +01002136#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (ssl->handshake->ecrs_enabled &&
2138 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002139 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002140 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002141#endif
2142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2144 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2145 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 }
2147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2149 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002150 mbedtls_ssl_send_alert_message(
2151 ssl,
2152 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2154 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 }
2156
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002157 /*
2158 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2159 * doesn't use a psk_identity_hint
2160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2162 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2163 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002164 /* Current message is probably either
2165 * CertificateRequest or ServerHelloDone */
2166 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002167 goto exit;
2168 }
2169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 MBEDTLS_SSL_DEBUG_MSG(1,
2171 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002172 mbedtls_ssl_send_alert_message(
2173 ssl,
2174 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 }
2179
Gilles Peskineeccd8882020-03-10 12:19:08 +01002180#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002182 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002184
2185start_processing:
2186#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002188 end = ssl->in_msg + ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, end - p);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002190
Gilles Peskineeccd8882020-03-10 12:19:08 +01002191#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2194 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2196 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2197 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002198 mbedtls_ssl_send_alert_message(
2199 ssl,
2200 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2202 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002203 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002204 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002205#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2208 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2210 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002211 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2214 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2215#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2216 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2218 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2219 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2220 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002221 mbedtls_ssl_send_alert_message(
2222 ssl,
2223 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2225 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2229 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002230#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2231 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2236 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2237 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002238 mbedtls_ssl_send_alert_message(
2239 ssl,
2240 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2242 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002243 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2246 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2247 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002248#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002250#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002251 /*
2252 * The first 3 bytes are:
2253 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2254 * [1, 2] elliptic curve's TLS ID
2255 *
2256 * However since we only support secp256r1 for now, we check only
2257 * that TLS ID here
2258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002260 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (exp_tls_id == 0) {
2264 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002265 }
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2268 (read_tls_id != exp_tls_id)) {
2269 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002270 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002271
2272 p += 3;
2273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if ((ret = mbedtls_psa_ecjpake_read_round(
2275 &ssl->handshake->psa_pake_ctx, p, end - p,
2276 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2277 psa_destroy_key(ssl->handshake->psa_pake_password);
2278 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002281 mbedtls_ssl_send_alert_message(
2282 ssl,
2283 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2285 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002286 }
2287#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2289 p, end - p);
2290 if (ret != 0) {
2291 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002292 mbedtls_ssl_send_alert_message(
2293 ssl,
2294 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2296 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002297 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002298#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002300#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002301 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2303 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002304 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002305
Gilles Peskineeccd8882020-03-10 12:19:08 +01002306#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002308 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002309 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2312 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002314 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002315 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002316 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002319
Jerry Yu693a47a2022-06-23 14:02:28 +08002320#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2321 peer_pk = &ssl->handshake->peer_pubkey;
2322#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002324 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2326 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002327 }
2328 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2329#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2330
Paul Bakker29e1f122013-04-16 13:07:56 +02002331 /*
2332 * Handle the digitally-signed structure
2333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2335 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2336 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2337 sig_alg, &pk_alg, &md_alg) != 0 &&
2338 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2339 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2340 MBEDTLS_SSL_DEBUG_MSG(1,
2341 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002342 mbedtls_ssl_send_alert_message(
2343 ssl,
2344 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2346 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002347 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002348 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2351 MBEDTLS_SSL_DEBUG_MSG(1,
2352 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002353 mbedtls_ssl_send_alert_message(
2354 ssl,
2355 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2357 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002358 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002359
2360 /*
2361 * Read signature
2362 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (p > end - 2) {
2365 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002366 mbedtls_ssl_send_alert_message(
2367 ssl,
2368 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2370 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002371 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 sig_len = (p[0] << 8) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002373 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 if (p != end - sig_len) {
2376 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002377 mbedtls_ssl_send_alert_message(
2378 ssl,
2379 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2381 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002385
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002386 /*
2387 * Compute the hash that has been signed
2388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 if (md_alg != MBEDTLS_MD_NONE) {
2390 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2391 params, params_len,
2392 md_alg);
2393 if (ret != 0) {
2394 return ret;
2395 }
2396 } else {
2397 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2398 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002399 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002402
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002403 /*
2404 * Verify signature
2405 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2407 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002408 mbedtls_ssl_send_alert_message(
2409 ssl,
2410 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2412 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002413 }
2414
Gilles Peskineeccd8882020-03-10 12:19:08 +01002415#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002417 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002419#endif
2420
Jerry Yu693a47a2022-06-23 14:02:28 +08002421#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002423 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2424 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002425 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002426 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (rsassa_pss_options.expected_salt_len == 0) {
2428 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2429 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2432 peer_pk,
2433 md_alg, hash, hashlen,
2434 p, sig_len);
2435 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002436#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 ret = mbedtls_pk_verify_restartable(peer_pk,
2438 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002441 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002442#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002444#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002446 mbedtls_ssl_send_alert_message(
2447 ssl,
2448 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2450 }
2451 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002452#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002454 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002456#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002458 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002459
2460#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2461 /* We don't need the peer's public key anymore. Free it,
2462 * so that more RAM is available for upcoming expensive
2463 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002465#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002467#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002469exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 ssl->state++;
2471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002475}
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002478MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002479static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002480{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002481 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002482 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2487 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002488 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002490 }
2491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2493 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002494}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002495#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002496MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002497static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002498{
Janos Follath865b3eb2019-12-16 11:46:15 +00002499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002500 unsigned char *buf;
2501 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002502 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002503 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002504 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002505 size_t sig_alg_len;
2506#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 unsigned char *sig_alg;
2508 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2514 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002515 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002517 }
2518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2520 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2521 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
2523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2525 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002526 mbedtls_ssl_send_alert_message(
2527 ssl,
2528 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2530 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002531 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002533 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002534 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2538 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002541 /* Current message is probably the ServerHelloDone */
2542 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002543 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002544 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002545
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002546 /*
2547 * struct {
2548 * ClientCertificateType certificate_types<1..2^8-1>;
2549 * SignatureAndHashAlgorithm
2550 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2551 * DistinguishedName certificate_authorities<0..2^16-1>;
2552 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002553 *
2554 * Since we only support a single certificate on clients, let's just
2555 * ignore all the information that's supposed to help us pick a
2556 * certificate.
2557 *
2558 * We could check that our certificate matches the request, and bail out
2559 * if it doesn't, but it's simpler to just send the certificate anyway,
2560 * and give the server the opportunity to decide if it should terminate
2561 * the connection when it doesn't like our certificate.
2562 *
2563 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2564 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002565 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002566 *
2567 * However, we still minimally parse the message to check it is at least
2568 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002569 */
Paul Bakker926af752012-11-23 13:38:07 +01002570 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002571
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002572 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2574 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2575 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2576 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2577 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002578 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002580 n = cert_type_len;
2581
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002582 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002583 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002584 * * the length of the signature algorithms field (if minor version of
2585 * SSL is 3),
2586 * * distinguished name length otherwise.
2587 * Both reach at most the index:
2588 * ...hdr_len + 2 + n,
2589 * therefore the buffer length at this point must be greater than that
2590 * regardless of the actual code path.
2591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2593 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2594 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2595 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2596 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002597 }
2598
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002599 /* supported_signature_algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 sig_alg_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2601 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Ronald Cron90915f22022-03-07 11:11:36 +01002602
2603 /*
2604 * The furthest access in buf is in the loop few lines below:
2605 * sig_alg[i + 1],
2606 * where:
2607 * sig_alg = buf + ...hdr_len + 3 + n,
2608 * max(i) = sig_alg_len - 1.
2609 * Therefore the furthest access is:
2610 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2611 * which reduces to:
2612 * buf[...hdr_len + 3 + n + sig_alg_len],
2613 * which is one less than we need the buf to be.
2614 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2616 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002617 mbedtls_ssl_send_alert_message(
2618 ssl,
2619 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2621 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002622 }
Paul Bakker926af752012-11-23 13:38:07 +01002623
Ronald Cron90915f22022-03-07 11:11:36 +01002624#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2626 for (size_t i = 0; i < sig_alg_len; i += 2) {
2627 MBEDTLS_SSL_DEBUG_MSG(3,
2628 ("Supported Signature Algorithm found: %02x %02x",
2629 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002630 }
2631#endif
2632
2633 n += 2 + sig_alg_len;
2634
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002635 /* certificate_authorities */
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 dn_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2637 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Paul Bakker926af752012-11-23 13:38:07 +01002638
2639 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2641 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2642 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2643 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2644 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002645 }
2646
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002647#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2649 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002650 unsigned char *p = dn + i + 2;
2651 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002652 size_t asn1_len;
2653 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 memset(&name, 0, sizeof(name));
2655 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2656 if (dni_len > dn_len - i - 2 ||
2657 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2658 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2659 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2660 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002661 mbedtls_ssl_send_alert_message(
2662 ssl,
2663 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2665 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002666 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 MBEDTLS_SSL_DEBUG_MSG(3,
2668 ("DN hint: %.*s",
2669 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2670 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002671 }
2672#endif
2673
Paul Bakker926af752012-11-23 13:38:07 +01002674exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002678}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002679#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002681MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002682static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002683{
Janos Follath865b3eb2019-12-16 11:46:15 +00002684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2689 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2690 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2694 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2695 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2699 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2700 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2701 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2702 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2703 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 }
2705
2706 ssl->state++;
2707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2710 mbedtls_ssl_recv_flight_completed(ssl);
2711 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002712#endif
2713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002717}
2718
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002719MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002720static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002721{
Janos Follath865b3eb2019-12-16 11:46:15 +00002722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002723
2724 size_t header_len;
2725 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002726 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002727 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 /*
2734 * DHM key exchange -- send G^X mod P
2735 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002739 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2742 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2743 &ssl->out_msg[header_len], content_len,
2744 ssl->conf->f_rng, ssl->conf->p_rng);
2745 if (ret != 0) {
2746 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2747 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 }
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2751 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2754 ssl->handshake->premaster,
2755 MBEDTLS_PREMASTER_SIZE,
2756 &ssl->handshake->pmslen,
2757 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2758 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2759 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 }
2761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2763 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002765#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2766 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2767 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2768 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002770 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2771 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002773#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2775 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002776 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002777
2778 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2779
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002780 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002783
Hanno Becker4a63ed42019-01-08 11:39:35 +00002784 /*
2785 * Generate EC private key for ECDHE exchange.
2786 */
2787
Hanno Becker4a63ed42019-01-08 11:39:35 +00002788 /* The master secret is obtained from the shared ECDH secret by
2789 * applying the TLS 1.2 PRF with a specific salt and label. While
2790 * the PSA Crypto API encourages combining key agreement schemes
2791 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2792 * yet support the provisioning of salt + label to the KDF.
2793 * For the time being, we therefore need to split the computation
2794 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002795 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2797 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002798 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002799 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002800
2801 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002803 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 if (status != PSA_SUCCESS) {
2805 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2806 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002807
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002808 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002809 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002810 * but we just need to add a length byte before that. */
2811 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2812 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002814 size_t own_pubkey_len;
2815
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002816 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 own_pubkey, own_pubkey_max_len,
2818 &own_pubkey_len);
2819 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002820 psa_destroy_key(handshake->xxdh_psa_privkey);
2821 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002823 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002824
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002825 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2826 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002827
Hanno Becker4a63ed42019-01-08 11:39:35 +00002828 /* The ECDH secret is the premaster secret used for key derivation. */
2829
Janos Follathdf3b0892019-08-08 11:12:24 +01002830 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002832 handshake->xxdh_psa_privkey,
2833 handshake->xxdh_psa_peerkey,
2834 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 ssl->handshake->premaster,
2836 sizeof(ssl->handshake->premaster),
2837 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002838
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002839 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
2840 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2843 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2844 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002845#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002846 /*
2847 * ECDH key exchange -- send client public value
2848 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002849 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002850
Gilles Peskineeccd8882020-03-10 12:19:08 +01002851#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (ssl->handshake->ecrs_enabled) {
2853 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002854 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002858 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002859#endif
2860
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2862 &content_len,
2863 &ssl->out_msg[header_len], 1000,
2864 ssl->conf->f_rng, ssl->conf->p_rng);
2865 if (ret != 0) {
2866 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002867#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002869 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002871#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002873 }
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2876 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002877
Gilles Peskineeccd8882020-03-10 12:19:08 +01002878#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002880 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002881 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002882 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002883
2884ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002886 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002888#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2890 &ssl->handshake->pmslen,
2891 ssl->handshake->premaster,
2892 MBEDTLS_MPI_MAX_SIZE,
2893 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2894 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002895#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002897 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002899#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002901 }
2902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2904 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002905#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2908 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2909 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2910 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002911#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2912 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002914 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2915 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2916 psa_key_attributes_t key_attributes;
2917
2918 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2919
2920 /*
2921 * opaque psk_identity<0..2^16-1>;
2922 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002924 /* We don't offer PSK suites if we don't have a PSK,
2925 * and we check that the server's choice is among the
2926 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2928 }
Neil Armstrong868af822022-03-09 10:26:25 +01002929
Neil Armstrongfc834f22022-03-23 17:54:38 +01002930 /* uint16 to store content length */
2931 const size_t content_len_size = 2;
2932
Neil Armstrong868af822022-03-09 10:26:25 +01002933 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (header_len + content_len_size + ssl->conf->psk_identity_len
2936 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2937 MBEDTLS_SSL_DEBUG_MSG(1,
2938 ("psk identity too long or SSL buffer too short"));
2939 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002940 }
2941
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002942 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2945 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002946 header_len += content_len_size;
2947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 memcpy(p, ssl->conf->psk_identity,
2949 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002950 p += ssl->conf->psk_identity_len;
2951
Neil Armstrong868af822022-03-09 10:26:25 +01002952 header_len += ssl->conf->psk_identity_len;
2953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002955
2956 /*
2957 * Generate EC private key for ECDHE exchange.
2958 */
2959
2960 /* The master secret is obtained from the shared ECDH secret by
2961 * applying the TLS 1.2 PRF with a specific salt and label. While
2962 * the PSA Crypto API encourages combining key agreement schemes
2963 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2964 * yet support the provisioning of salt + label to the KDF.
2965 * For the time being, we therefore need to split the computation
2966 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2967 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2969 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002970 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02002971 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002972
2973 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002975 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002977 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 }
Neil Armstrong868af822022-03-09 10:26:25 +01002979
2980 /* Export the public part of the ECDH private key from PSA.
2981 * The export format is an ECPoint structure as expected by TLS,
2982 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002983 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002984 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002986 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002987
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002988 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 own_pubkey, own_pubkey_max_len,
2990 &own_pubkey_len);
2991 if (status != PSA_SUCCESS) {
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02002992 psa_destroy_key(handshake->xxdh_psa_privkey);
2993 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002994 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002995 }
2996
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002997 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002998 content_len = own_pubkey_len + 1;
2999
Neil Armstrong25400452022-03-23 17:44:07 +01003000 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3001 * - a uint16 containing the length (in octets) of the ECDH computation
3002 * - the octet string produced by the ECDH computation
3003 * - a uint16 containing the length (in octets) of the PSK
3004 * - the PSK itself
3005 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003006 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 const unsigned char * const pms_end = pms +
3008 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003009 /* uint16 to store length (in octets) of the ECDH computation */
3010 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003011 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003012
Neil Armstrong25400452022-03-23 17:44:07 +01003013 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 status = psa_raw_key_agreement(PSA_ALG_ECDH,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02003015 handshake->xxdh_psa_privkey,
3016 handshake->xxdh_psa_peerkey,
3017 handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 pms + zlen_size,
3019 pms_end - (pms + zlen_size),
3020 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003021
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02003022 destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
3023 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong868af822022-03-09 10:26:25 +01003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003026 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003028 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 }
Neil Armstrong868af822022-03-09 10:26:25 +01003030
Neil Armstrong25400452022-03-23 17:44:07 +01003031 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003033 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003035#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3036 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003037#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003039 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003040 * opaque psk_identity<0..2^16-1>;
3041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003043 /* We don't offer PSK suites if we don't have a PSK,
3044 * and we check that the server's choice is among the
3045 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003047 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003048
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003049 header_len = 4;
3050 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3053 MBEDTLS_SSL_DEBUG_MSG(1,
3054 ("psk identity too long or SSL buffer too short"));
3055 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003056 }
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3059 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 memcpy(ssl->out_msg + header_len,
3062 ssl->conf->psk_identity,
3063 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003064 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003068 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3073 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3074 &content_len, 2)) != 0) {
3075 return ret;
3076 }
3077 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003081 /*
3082 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 if (header_len + 2 + content_len >
3087 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3088 MBEDTLS_SSL_DEBUG_MSG(1,
3089 ("psk identity or DHM size too long or SSL buffer too short"));
3090 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003091 }
3092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3094 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3097 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3098 &ssl->out_msg[header_len], content_len,
3099 ssl->conf->f_rng, ssl->conf->p_rng);
3100 if (ret != 0) {
3101 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3102 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003103 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003104
3105#if defined(MBEDTLS_USE_PSA_CRYPTO)
3106 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003108 size_t pms_len;
3109
3110 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3112 pms + 2, pms_end - (pms + 2), &pms_len,
3113 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3114 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3115 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003118 pms += 2 + pms_len;
3119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003121#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003124#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3126 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003127 /*
3128 * ClientECDiffieHellmanPublic public;
3129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3131 &content_len,
3132 &ssl->out_msg[header_len],
3133 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3134 ssl->conf->f_rng, ssl->conf->p_rng);
3135 if (ret != 0) {
3136 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3137 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003138 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3141 MBEDTLS_DEBUG_ECDH_Q);
3142 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003143#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003144 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3146 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003147 }
3148
Neil Armstrong80f6f322022-05-03 17:56:38 +02003149#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003151 (mbedtls_key_exchange_type_t) ciphersuite_info->
3152 key_exchange)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 MBEDTLS_SSL_DEBUG_RET(1,
3154 "mbedtls_ssl_psk_derive_premaster", ret);
3155 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003156 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003157#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003159#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003162 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3164 &content_len, 0)) != 0) {
3165 return ret;
3166 }
3167 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003169#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003171 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003172
Neil Armstrongca7d5062022-05-31 14:43:23 +02003173#if defined(MBEDTLS_USE_PSA_CRYPTO)
3174 unsigned char *out_p = ssl->out_msg + header_len;
3175 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3176 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3178 out_p, end_p - out_p, &content_len,
3179 MBEDTLS_ECJPAKE_ROUND_TWO);
3180 if (ret != 0) {
3181 psa_destroy_key(ssl->handshake->psa_pake_password);
3182 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3183 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3184 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003185 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003186#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3188 ssl->out_msg + header_len,
3189 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3190 &content_len,
3191 ssl->conf->f_rng, ssl->conf->p_rng);
3192 if (ret != 0) {
3193 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3194 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003195 }
3196
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3198 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3199 ssl->conf->f_rng, ssl->conf->p_rng);
3200 if (ret != 0) {
3201 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3202 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003203 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003204#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003206#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003207 {
3208 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3210 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003211 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003212
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003213 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3215 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003216
3217 ssl->state++;
3218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3220 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3221 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 }
3223
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003227}
3228
Gilles Peskineeccd8882020-03-10 12:19:08 +01003229#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003230MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003231static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003232{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003233 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003234 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3240 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3241 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003242 }
3243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3245 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003246 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003248 }
3249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3251 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003252}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003253#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003254MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003255static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003258 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003259 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003260 size_t n = 0, offset = 0;
3261 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003262 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003264 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003265 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003266#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 size_t out_buf_len = ssl->out_buf_len - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003268#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003270#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003271
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003273
Gilles Peskineeccd8882020-03-10 12:19:08 +01003274#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 if (ssl->handshake->ecrs_enabled &&
3276 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003277 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003278 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003279#endif
3280
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3282 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3283 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003284 }
3285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3287 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003288 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003290 }
3291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 if (ssl->handshake->client_auth == 0 ||
3293 mbedtls_ssl_own_cert(ssl) == NULL) {
3294 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003295 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 }
3298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 if (mbedtls_ssl_own_key(ssl) == NULL) {
3300 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3301 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 }
3303
3304 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003305 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003307#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003309 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003311
3312sign:
3313#endif
3314
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003315 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3316 if (0 != ret) {
3317 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3318 return ret;
3319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003320
Ronald Cron90915f22022-03-07 11:11:36 +01003321 /*
3322 * digitally-signed struct {
3323 * opaque handshake_messages[handshake_messages_length];
3324 * };
3325 *
3326 * Taking shortcut here. We assume that the server always allows the
3327 * PRF Hash function and has sent it in the allowed signature
3328 * algorithms list received in the Certificate Request message.
3329 *
3330 * Until we encounter a server that does not, we will take this
3331 * shortcut.
3332 *
3333 * Reason: Otherwise we should have running hashes for SHA512 and
3334 * SHA224 in order to satisfy 'weird' needs from the server
3335 * side.
3336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003338 md_alg = MBEDTLS_MD_SHA384;
3339 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003341 md_alg = MBEDTLS_MD_SHA256;
3342 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003343 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003345
3346 /* Info from md_alg will be used instead */
3347 hashlen = 0;
3348 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003349
Gilles Peskineeccd8882020-03-10 12:19:08 +01003350#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003352 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003354#endif
3355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3357 md_alg, hash_start, hashlen,
3358 ssl->out_msg + 6 + offset,
3359 out_buf_len - 6 - offset,
3360 &n,
3361 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3362 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003363#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003365 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003367#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003369 }
Paul Bakker926af752012-11-23 13:38:07 +01003370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003372
Paul Bakker1ef83d62012-04-11 12:09:53 +00003373 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3375 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003376
3377 ssl->state++;
3378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3380 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3381 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003382 }
3383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003387}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003388#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003391MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003392static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003393{
Janos Follath865b3eb2019-12-16 11:46:15 +00003394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003395 uint32_t lifetime;
3396 size_t ticket_len;
3397 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003398 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003399
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3403 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3404 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003405 }
3406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3408 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003409 mbedtls_ssl_send_alert_message(
3410 ssl,
3411 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3413 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003414 }
3415
3416 /*
3417 * struct {
3418 * uint32 ticket_lifetime_hint;
3419 * opaque ticket<0..2^16-1>;
3420 * } NewSessionTicket;
3421 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003422 * 0 . 3 ticket_lifetime_hint
3423 * 4 . 5 ticket_len (n)
3424 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003425 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3427 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3428 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3429 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3430 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3431 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003432 }
3433
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 lifetime = (((uint32_t) msg[0]) << 24) | (msg[1] << 16) |
3437 (msg[2] << 8) | (msg[3]);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003438
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 ticket_len = (msg[4] << 8) | (msg[5]);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003440
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3442 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3443 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3444 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3445 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003446 }
3447
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003449
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003450 /* We're not waiting for a NewSessionTicket message any more */
3451 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003453
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003454 /*
3455 * Zero-length ticket means the server changed his mind and doesn't want
3456 * to send a ticket after all, so just forget it
3457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 if (ticket_len == 0) {
3459 return 0;
3460 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 if (ssl->session != NULL && ssl->session->ticket != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003463 mbedtls_zeroize_and_free(ssl->session->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 ssl->session->ticket_len);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003465 ssl->session->ticket = NULL;
3466 ssl->session->ticket_len = 0;
3467 }
3468
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003469 mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 ssl->session_negotiate->ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003471 ssl->session_negotiate->ticket = NULL;
3472 ssl->session_negotiate->ticket_len = 0;
3473
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3475 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3476 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3477 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3478 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003479 }
3480
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003482
3483 ssl->session_negotiate->ticket = ticket;
3484 ssl->session_negotiate->ticket_len = ticket_len;
3485 ssl->session_negotiate->ticket_lifetime = lifetime;
3486
3487 /*
3488 * RFC 5077 section 3.4:
3489 * "If the client receives a session ticket from the server, then it
3490 * discards any Session ID that was sent in the ServerHello."
3491 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003493 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003494
Gilles Peskine449bd832023-01-11 14:50:10 +01003495 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003496
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003500
Paul Bakker5121ce52009-01-03 21:22:43 +00003501/*
Paul Bakker1961b702013-01-25 14:49:24 +01003502 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003503 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003504int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003505{
3506 int ret = 0;
3507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003509 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3512 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003513 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003514 }
3515#endif
3516
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 case MBEDTLS_SSL_HELLO_REQUEST:
3519 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 break;
3521
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 /*
3523 * ==> ClientHello
3524 */
3525 case MBEDTLS_SSL_CLIENT_HELLO:
3526 ret = mbedtls_ssl_write_client_hello(ssl);
3527 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 /*
3530 * <== ServerHello
3531 * Certificate
3532 * ( ServerKeyExchange )
3533 * ( CertificateRequest )
3534 * ServerHelloDone
3535 */
3536 case MBEDTLS_SSL_SERVER_HELLO:
3537 ret = ssl_parse_server_hello(ssl);
3538 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3541 ret = mbedtls_ssl_parse_certificate(ssl);
3542 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3545 ret = ssl_parse_server_key_exchange(ssl);
3546 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3549 ret = ssl_parse_certificate_request(ssl);
3550 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003551
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3553 ret = ssl_parse_server_hello_done(ssl);
3554 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003555
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 /*
3557 * ==> ( Certificate/Alert )
3558 * ClientKeyExchange
3559 * ( CertificateVerify )
3560 * ChangeCipherSpec
3561 * Finished
3562 */
3563 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3564 ret = mbedtls_ssl_write_certificate(ssl);
3565 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003566
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3568 ret = ssl_write_client_key_exchange(ssl);
3569 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003570
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3572 ret = ssl_write_certificate_verify(ssl);
3573 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003574
Gilles Peskine449bd832023-01-11 14:50:10 +01003575 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3576 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3577 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003578
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 case MBEDTLS_SSL_CLIENT_FINISHED:
3580 ret = mbedtls_ssl_write_finished(ssl);
3581 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003582
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 /*
3584 * <== ( NewSessionTicket )
3585 * ChangeCipherSpec
3586 * Finished
3587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3590 ret = ssl_parse_new_session_ticket(ssl);
3591 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003592#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003593
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3595 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3596 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003597
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 case MBEDTLS_SSL_SERVER_FINISHED:
3599 ret = mbedtls_ssl_parse_finished(ssl);
3600 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003601
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 case MBEDTLS_SSL_FLUSH_BUFFERS:
3603 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3604 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3605 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003606
Gilles Peskine449bd832023-01-11 14:50:10 +01003607 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3608 mbedtls_ssl_handshake_wrapup(ssl);
3609 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003610
Gilles Peskine449bd832023-01-11 14:50:10 +01003611 default:
3612 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3613 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003615
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003617}
Jerry Yuc5aef882021-12-23 20:15:02 +08003618
Jerry Yufb4b6472022-01-27 15:03:26 +08003619#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */