blob: fc96dae1e20c7821266cf4dafe7a99c27c193ad9 [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)
34#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020035#include "psa/crypto.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050036#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
37 psa_to_ssl_errors, \
38 psa_generic_status_to_mbedtls)
Hanno Beckerbb89e272019-01-08 12:54:37 +000039#endif /* MBEDTLS_USE_PSA_CRYPTO */
40
SimonBd5800b72016-04-26 07:43:27 +010041#include <string.h>
42
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020043#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010046#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050050#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020054MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010055static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
56 unsigned char *buf,
57 const unsigned char *end,
58 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010059{
60 unsigned char *p = buf;
61
62 *olen = 0;
63
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010064 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010065 * initial ClientHello, in which case also adding the renegotiation
66 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
68 return 0;
69 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 MBEDTLS_SSL_DEBUG_MSG(3,
72 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010075
Paul Bakkerd3edc862013-03-20 16:07:17 +010076 /*
77 * Secure renegotiation
78 */
Gilles Peskine449bd832023-01-11 14:50:10 +010079 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010080 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010081
82 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010083 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
84 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010087
88 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010093
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +020094#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010095 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010096
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020097MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
99 unsigned char *buf,
100 const unsigned char *end,
101 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100102{
103 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100104 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100105
106 *olen = 0;
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 MBEDTLS_SSL_DEBUG_MSG(3,
109 ("client hello, adding supported_point_formats extension"));
110 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100113 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100114
115 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200117
118 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100120
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200121 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100124}
Darryl Green11999bb2018-03-13 15:22:58 +0000125#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100126 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100127
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200128#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200129MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100130static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
131 unsigned char *buf,
132 const unsigned char *end,
133 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200134{
Janos Follath865b3eb2019-12-16 11:46:15 +0000135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200136 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100137 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200138
139 *olen = 0;
140
141 /* Skip costly extension if we can't use EC J-PAKE anyway */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200142#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
144 return 0;
145 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200146#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
148 return 0;
149 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200150#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 MBEDTLS_SSL_DEBUG_MSG(3,
153 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100158 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200159
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200160 /*
161 * We may need to send ClientHello multiple times for Hello verification.
162 * We don't want to compute fresh values every time (both for performance
163 * and consistency reasons), so cache the extension content.
164 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (ssl->handshake->ecjpake_cache == NULL ||
166 ssl->handshake->ecjpake_cache_len == 0) {
167 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200168
Neil Armstrongca7d5062022-05-31 14:43:23 +0200169#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100170 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 p + 2, end - p - 2, &kkpp_len,
172 MBEDTLS_ECJPAKE_ROUND_ONE);
173 if (ret != 0) {
174 psa_destroy_key(ssl->handshake->psa_pake_password);
175 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
176 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
177 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200178 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200179#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
181 p + 2, end - p - 2, &kkpp_len,
182 ssl->conf->f_rng, ssl->conf->p_rng);
183 if (ret != 0) {
184 MBEDTLS_SSL_DEBUG_RET(1,
185 "mbedtls_ecjpake_write_round_one", ret);
186 return ret;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200187 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200188#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
191 if (ssl->handshake->ecjpake_cache == NULL) {
192 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
193 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200194 }
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200197 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 } else {
199 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200200
201 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200205 }
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100208 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200209
210 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200213}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200214#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000215
Hanno Beckera0e20d02019-05-15 14:03:01 +0100216#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200217MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100218static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
219 unsigned char *buf,
220 const unsigned char *end,
221 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100222{
223 unsigned char *p = buf;
224 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100225
226 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100227 * struct {
228 * opaque cid<0..2^8-1>;
229 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100231
232 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
234 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
235 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100236 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100238
239 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
240 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100242
243 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100245 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100246 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100248 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100249
250 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100252
253 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100256}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100257#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200260MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100261static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
262 unsigned char *buf,
263 const unsigned char *end,
264 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200265{
266 unsigned char *p = buf;
267
Simon Butcher0fc94e92015-09-28 20:52:04 +0100268 *olen = 0;
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
271 return 0;
272 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 MBEDTLS_SSL_DEBUG_MSG(3,
275 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100280 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200281
282 *p++ = 0x00;
283 *p++ = 1;
284
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200285 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200286
287 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200294MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100295static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
296 unsigned char *buf,
297 const unsigned char *end,
298 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100299{
300 unsigned char *p = buf;
301
Simon Butcher0fc94e92015-09-28 20:52:04 +0100302 *olen = 0;
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
305 return 0;
306 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 MBEDTLS_SSL_DEBUG_MSG(3,
309 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100314 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100315
316 *p++ = 0x00;
317 *p++ = 0x00;
318
319 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200326MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100327static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
328 unsigned char *buf,
329 const unsigned char *end,
330 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200331{
332 unsigned char *p = buf;
333
Simon Butcher0fc94e92015-09-28 20:52:04 +0100334 *olen = 0;
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
337 return 0;
338 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 MBEDTLS_SSL_DEBUG_MSG(3,
341 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100346 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200347
348 *p++ = 0x00;
349 *p++ = 0x00;
350
351 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200358MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100359static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
360 unsigned char *buf,
361 const unsigned char *end,
362 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200363{
364 unsigned char *p = buf;
365 size_t tlen = ssl->session_negotiate->ticket_len;
366
Simon Butcher0fc94e92015-09-28 20:52:04 +0100367 *olen = 0;
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
370 return 0;
371 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 MBEDTLS_SSL_DEBUG_MSG(3,
374 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200375
Hanno Becker261602c2017-04-12 14:54:42 +0100376 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100380 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100383 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200384
385 *olen = 4;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
388 return 0;
389 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 MBEDTLS_SSL_DEBUG_MSG(3,
392 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200395
396 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200401
Ron Eldora9788042018-12-05 11:04:31 +0200402#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200403MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100404static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
405 unsigned char *buf,
406 const unsigned char *end,
407 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100408{
409 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200410 size_t protection_profiles_index = 0, ext_len = 0;
411 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100412
413 *olen = 0;
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
416 (ssl->conf->dtls_srtp_profile_list == NULL) ||
417 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
418 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100419 }
420
Ron Eldora9788042018-12-05 11:04:31 +0200421 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100422 * uint8 SRTPProtectionProfile[2];
423 *
424 * struct {
425 * SRTPProtectionProfiles SRTPProtectionProfiles;
426 * opaque srtp_mki<0..255>;
427 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100428 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100429 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200431 mki_len = ssl->dtls_srtp_info.mki_len;
432 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300433 /* Extension length = 2 bytes for profiles length,
434 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
435 * 1 byte for srtp_mki vector length and the mki_len value
436 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200440
441 /* Check there is room in the buffer for the extension + 4 bytes
442 * - the extension tag (2 bytes)
443 * - the extension length (2 bytes)
444 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100448 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100451 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100452
Ron Eldor3adb9922017-12-21 10:15:08 +0200453 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200454 /* micro-optimization:
455 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
456 * which is lower than 127, so the upper byte of the length is always 0
457 * For the documentation, the more generic code is left in comments
458 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
459 * >> 8 ) & 0xFF );
460 */
461 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300465 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200467 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
469 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
470 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
471 profile_value));
472 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100473 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200475 /*
476 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200477 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 MBEDTLS_SSL_DEBUG_MSG(3,
480 ("client hello, "
481 "illegal DTLS-SRTP protection profile %d",
482 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
483 ));
484 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100485 }
486 }
487
Ron Eldor591f1622018-01-22 12:30:04 +0200488 *p++ = mki_len & 0xFF;
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (mki_len != 0) {
491 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200492 /*
493 * Increment p to point to the current position.
494 */
495 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
497 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200498 }
499
Ron Eldoref72faf2018-07-12 11:54:20 +0300500 /*
501 * total extension length: extension type (2 bytes)
502 * + extension length (2 bytes)
503 * + protection profile length (2 bytes)
504 * + 2 * number of protection profiles
505 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200506 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300507 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200508 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100511}
512#endif /* MBEDTLS_SSL_DTLS_SRTP */
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
515 unsigned char *buf,
516 const unsigned char *end,
517 int uses_ec,
518 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100519{
520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
521 unsigned char *p = buf;
522 size_t ext_len = 0;
523
524 (void) ssl;
525 (void) end;
526 (void) uses_ec;
527 (void) ret;
528 (void) ext_len;
529
530 *out_len = 0;
531
532 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
533 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
534#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
536 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
537 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100538 }
539 p += ext_len;
540#endif
541
542#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
543 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if (uses_ec) {
545 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
546 &ext_len)) != 0) {
547 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
548 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100549 }
550 p += ext_len;
551 }
552#endif
553
554#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
556 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
557 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100558 }
559 p += ext_len;
560#endif
561
562#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
564 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
565 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100566 }
567 p += ext_len;
568#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
569
570#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
572 &ext_len)) != 0) {
573 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
574 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100575 }
576 p += ext_len;
577#endif
578
579#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
581 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
582 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100583 }
584 p += ext_len;
585#endif
586
587#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
589 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
590 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100591 }
592 p += ext_len;
593#endif
594
595#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
597 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
598 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100599 }
600 p += ext_len;
601#endif
602
603#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
605 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
606 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100607 }
608 p += ext_len;
609#endif
610
611 *out_len = p - buf;
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100614}
615
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200616MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100617static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
618 const unsigned char *buf,
619 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100623 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000625 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 mbedtls_ct_memcmp(buf + 1,
627 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
628 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
629 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
630 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100631 mbedtls_ssl_send_alert_message(
632 ssl,
633 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
635 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000636 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100639 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (len != 1 || buf[0] != 0x00) {
641 MBEDTLS_SSL_DEBUG_MSG(1,
642 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100643 mbedtls_ssl_send_alert_message(
644 ssl,
645 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
647 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100648 }
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100651 }
Paul Bakker48916f92012-09-16 19:57:18 +0000652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000654}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200657MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100658static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
659 const unsigned char *buf,
660 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200661{
662 /*
663 * server should use the extension only if we did,
664 * and if so the server's value should match ours (and len is always 1)
665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200667 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 buf[0] != ssl->conf->mfl_code) {
669 MBEDTLS_SSL_DEBUG_MSG(1,
670 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100671 mbedtls_ssl_send_alert_message(
672 ssl,
673 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
675 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200676 }
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200679}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000681
Hanno Beckera0e20d02019-05-15 14:03:01 +0100682#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200683MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100684static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
685 const unsigned char *buf,
686 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100687{
688 size_t peer_cid_len;
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100691 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
692 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
694 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
695 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
696 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
697 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100698 }
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (len == 0) {
701 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
702 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
703 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
704 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100705 }
706
707 peer_cid_len = *buf++;
708 len--;
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
711 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
712 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
713 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
714 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100715 }
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (len != peer_cid_len) {
718 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
719 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
720 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
721 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100722 }
723
Hanno Becker5a299902019-05-03 12:47:49 +0100724 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100725 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
729 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100732}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100733#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200736MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100737static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
738 const unsigned char *buf,
739 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100740{
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
742 len != 0) {
743 MBEDTLS_SSL_DEBUG_MSG(1,
744 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100745 mbedtls_ssl_send_alert_message(
746 ssl,
747 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
749 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100750 }
751
752 ((void) buf);
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200761MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100762static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
763 const unsigned char *buf,
764 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200765{
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
767 len != 0) {
768 MBEDTLS_SSL_DEBUG_MSG(1,
769 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100770 mbedtls_ssl_send_alert_message(
771 ssl,
772 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
774 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200775 }
776
777 ((void) buf);
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200786MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100787static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
788 const unsigned char *buf,
789 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200790{
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
792 len != 0) {
793 MBEDTLS_SSL_DEBUG_MSG(1,
794 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100795 mbedtls_ssl_send_alert_message(
796 ssl,
797 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
799 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200800 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200801
802 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200803
804 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200807}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200809
Robert Cragie136884c2015-10-02 13:34:31 +0100810#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100811 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200812MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100813static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
814 const unsigned char *buf,
815 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200816{
817 size_t list_size;
818 const unsigned char *p;
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (len == 0 || (size_t) (buf[0] + 1) != len) {
821 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
822 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
823 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
824 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200825 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200826 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200827
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200828 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 while (list_size > 0) {
830 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
831 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Valerio Setti46423162023-03-27 14:33:27 +0200832#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200833 ssl->handshake->ecdh_ctx.point_format = p[0];
Valerio Setti77a904c2023-03-24 07:28:49 +0100834#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_ECDH_C */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200835#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
837 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
838 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200839#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
841 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200842 }
843
844 list_size--;
845 p++;
846 }
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
849 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
850 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
851 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200852}
Darryl Green11999bb2018-03-13 15:22:58 +0000853#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100854 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200855
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200856#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200857MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100858static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
859 const unsigned char *buf,
860 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200861{
Janos Follath865b3eb2019-12-16 11:46:15 +0000862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (ssl->handshake->ciphersuite_info->key_exchange !=
865 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
866 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
867 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200868 }
869
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200870 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200872 ssl->handshake->ecjpake_cache = NULL;
873 ssl->handshake->ecjpake_cache_len = 0;
874
Neil Armstrongca7d5062022-05-31 14:43:23 +0200875#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if ((ret = mbedtls_psa_ecjpake_read_round(
877 &ssl->handshake->psa_pake_ctx, buf, len,
878 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
879 psa_destroy_key(ssl->handshake->psa_pake_password);
880 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100883 mbedtls_ssl_send_alert_message(
884 ssl,
885 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
887 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200888 }
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 return 0;
891#else
892 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
893 buf, len)) != 0) {
894 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
895 mbedtls_ssl_send_alert_message(
896 ssl,
897 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
898 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
899 return ret;
900 }
901
902 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200903#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200904}
905#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200908MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100909static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
910 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200911{
912 size_t list_len, name_len;
913 const char **p;
914
915 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (ssl->conf->alpn_list == NULL) {
917 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100918 mbedtls_ssl_send_alert_message(
919 ssl,
920 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
922 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200923 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200924
925 /*
926 * opaque ProtocolName<1..2^8-1>;
927 *
928 * struct {
929 * ProtocolName protocol_name_list<2..2^16-1>
930 * } ProtocolNameList;
931 *
932 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
933 */
934
935 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (len < 4) {
937 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
938 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
939 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200940 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 list_len = (buf[0] << 8) | buf[1];
943 if (list_len != len - 2) {
944 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
945 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
946 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200947 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200948
949 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (name_len != list_len - 1) {
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
956 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
958 if (name_len == strlen(*p) &&
959 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200960 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200962 }
963 }
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
966 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
967 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
968 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200969}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200971
Johan Pascalb62bb512015-12-03 21:56:45 +0100972#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200973MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100974static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
975 const unsigned char *buf,
976 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100977{
Johan Pascal43f94902020-09-22 12:25:52 +0200978 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200979 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100980 uint16_t server_protection_profile_value = 0;
981
982 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
984 (ssl->conf->dtls_srtp_profile_list == NULL) ||
985 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
986 return 0;
987 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100988
Ron Eldora9788042018-12-05 11:04:31 +0200989 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100990 * uint8 SRTPProtectionProfile[2];
991 *
992 * struct {
993 * SRTPProtectionProfiles SRTPProtectionProfiles;
994 * opaque srtp_mki<0..255>;
995 * } UseSRTPData;
996
997 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
998 *
Johan Pascalb62bb512015-12-03 21:56:45 +0100999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001001 mki_len = ssl->dtls_srtp_info.mki_len;
1002 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001003
Ron Eldoref72faf2018-07-12 11:54:20 +03001004 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001005 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1006 * + protection profile (2 bytes)
1007 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001008 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001009 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if ((len < 5) || (len != (buf[4] + 5u))) {
1011 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1012 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001013
1014 /*
1015 * get the server protection profile
1016 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001017
1018 /*
1019 * protection profile length must be 0x0002 as we must have only
1020 * one protection profile in server Hello
1021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((buf[0] != 0) || (buf[1] != 2)) {
1023 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1024 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001027 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 server_protection_profile_value);
1029 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1030 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1031 mbedtls_ssl_get_srtp_profile_as_string(
1032 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001033 }
1034
Johan Pascal43f94902020-09-22 12:25:52 +02001035 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001036
Johan Pascalb62bb512015-12-03 21:56:45 +01001037 /*
1038 * Check we have the server profile in our list
1039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1041 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001042 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001044 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001046 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001047 }
1048 }
1049
Ron Eldor591f1622018-01-22 12:30:04 +02001050 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1052 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1053 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1054 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001055 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001056
1057 /* If server does not use mki in its reply, make sure the client won't keep
1058 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001060 ssl->dtls_srtp_info.mki_len = 0;
1061 }
1062
Ron Eldoref72faf2018-07-12 11:54:20 +03001063 /*
1064 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001065 * If the client detects a nonzero-length MKI in the server's response
1066 * that is different than the one the client offered, then the client
1067 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (len > 5 && (buf[4] != mki_len ||
1070 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1071 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1072 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1073 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001074 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001075#if defined(MBEDTLS_DEBUG_C)
1076 if (len > 5) {
1077 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1078 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001079 }
1080#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001082}
1083#endif /* MBEDTLS_SSL_DTLS_SRTP */
1084
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001085/*
1086 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001089MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001090static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001091{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001092 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001094 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001095
1096#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1097 uint8_t cookie_len;
1098#else
1099 uint16_t cookie_len;
1100#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001103
Gilles Peskineb64bf062019-09-27 14:02:44 +02001104 /* Check that there is enough room for:
1105 * - 2 bytes of version
1106 * - 1 byte of cookie_len
1107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1109 MBEDTLS_SSL_DEBUG_MSG(1,
1110 ("incoming HelloVerifyRequest message is too short"));
1111 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1112 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1113 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001114 }
1115
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001116 /*
1117 * struct {
1118 * ProtocolVersion server_version;
1119 * opaque cookie<0..2^8-1>;
1120 * } HelloVerifyRequest;
1121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1123 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001124 p += 2;
1125
TRodziewicz2d8800e2021-05-13 19:14:19 +02001126 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001127 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1128 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1129 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001130 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1132 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1135 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001138 }
1139
1140 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1142 MBEDTLS_SSL_DEBUG_MSG(1,
1143 ("cookie length does not match incoming message size"));
1144 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1145 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1146 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001147 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1153 if (ssl->handshake->cookie == NULL) {
1154 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1155 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001156 }
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001159 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001160
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001161 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001163 ret = mbedtls_ssl_reset_checksum(ssl);
1164 if (0 != ret) {
1165 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1166 return ret;
1167 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001176
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001177MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001178static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001179{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001180 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001181 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001182 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001183 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001184 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001186 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001187#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001188 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001194 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1196 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 }
1198
Hanno Becker79594fd2019-05-08 09:38:41 +01001199 buf = ssl->in_msg;
1200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001204 ssl->renego_records_seen++;
1205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (ssl->conf->renego_max_records >= 0 &&
1207 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1208 MBEDTLS_SSL_DEBUG_MSG(1,
1209 ("renegotiation requested, but not honored by server"));
1210 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001211 }
1212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 MBEDTLS_SSL_DEBUG_MSG(1,
1214 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001215
1216 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001218 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001222 mbedtls_ssl_send_alert_message(
1223 ssl,
1224 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1226 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 }
1228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1231 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1232 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1233 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1234 return ssl_parse_hello_verify_request(ssl);
1235 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001236 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001238 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001239 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001240 }
1241 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1245 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1246 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1247 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1248 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1249 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 }
1251
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001252 /*
1253 * 0 . 1 server_version
1254 * 2 . 33 random (maybe including 4 bytes of Unix time)
1255 * 34 . 34 session_id length = n
1256 * 35 . 34+n session_id
1257 * 35+n . 36+n cipher_suite
1258 * 37+n . 37+n compression_method
1259 *
1260 * 38+n . 39+n extensions length (optional)
1261 * 40+n . .. extensions
1262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
1266 ssl->tls_version = mbedtls_ssl_read_version(buf, ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001267 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (ssl->tls_version < ssl->conf->min_tls_version ||
1270 ssl->tls_version > ssl->conf->max_tls_version) {
1271 MBEDTLS_SSL_DEBUG_MSG(1,
1272 (
1273 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1274 (unsigned) ssl->conf->min_tls_version,
1275 (unsigned) ssl->tls_version,
1276 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1279 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001282 }
1283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1285 ((unsigned long) buf[2] << 24) |
1286 ((unsigned long) buf[3] << 16) |
1287 ((unsigned long) buf[4] << 8) |
1288 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001292 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (n > 32) {
1297 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1298 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1299 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1300 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001301 }
1302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
1304 ext_len = ((buf[38 + n] << 8)
1305 | (buf[39 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if ((ext_len > 0 && ext_len < 4) ||
1308 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1309 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001310 mbedtls_ssl_send_alert_message(
1311 ssl,
1312 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1314 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001315 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001317 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 } else {
1319 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1320 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1321 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1322 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001323 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001325 /* ciphersuite (used later) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 i = (buf[35 + n] << 8) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001327
1328 /*
1329 * Read and check compression
1330 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001331 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1334 MBEDTLS_SSL_DEBUG_MSG(1,
1335 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001336 mbedtls_ssl_send_alert_message(
1337 ssl,
1338 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1340 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001341 }
1342
Paul Bakker380da532012-04-18 16:10:25 +00001343 /*
1344 * Initialize update checksum functions
1345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1347 if (ssl->handshake->ciphersuite_info == NULL) {
1348 MBEDTLS_SSL_DEBUG_MSG(1,
1349 ("ciphersuite info for %04x not found", (unsigned int) i));
1350 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1351 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1352 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001353 }
Paul Bakker380da532012-04-18 16:10:25 +00001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1358 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
1360 /*
1361 * Check if the session can be resumed
1362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_SSL_RENEGOTIATION)
1365 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001366#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001367 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001368 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001371 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001374#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001375 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001376 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 memcpy(ssl->session_negotiate->id, buf + 35, n);
1378 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 }
1381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1383 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1386 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1387 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Andrzej Kurek03bac442018-04-25 05:06:07 -04001389 /*
1390 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001391 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 while (1) {
1394 if (ssl->conf->ciphersuite_list[i] == 0) {
1395 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001396 mbedtls_ssl_send_alert_message(
1397 ssl,
1398 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1400 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 }
1402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (ssl->conf->ciphersuite_list[i++] ==
1404 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 }
1408
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001409 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 ssl->session_negotiate->ciphersuite);
1411 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1412 ssl->tls_version) != 0) {
1413 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001414 mbedtls_ssl_send_alert_message(
1415 ssl,
1416 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1418 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001419 }
1420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 MBEDTLS_SSL_DEBUG_MSG(3,
1422 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001423
Gilles Peskineeccd8882020-03-10 12:19:08 +01001424#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1426 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001427 ssl->handshake->ecrs_enabled = 1;
1428 }
1429#endif
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1432 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001433 mbedtls_ssl_send_alert_message(
1434 ssl,
1435 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1437 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 }
1439
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001440 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 MBEDTLS_SSL_DEBUG_MSG(2,
1443 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1444 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 while (ext_len) {
1447 unsigned int ext_id = ((ext[0] << 8)
1448 | (ext[1]));
1449 unsigned int ext_size = ((ext[2] << 8)
1450 | (ext[3]));
Paul Bakker48916f92012-09-16 19:57:18 +00001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (ext_size + 4 > ext_len) {
1453 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001454 mbedtls_ssl_send_alert_message(
1455 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1457 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001458 }
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 switch (ext_id) {
1461 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1462 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001465#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1468 ext_size)) != 0) {
1469 return ret;
1470 }
Paul Bakker48916f92012-09-16 19:57:18 +00001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1476 MBEDTLS_SSL_DEBUG_MSG(3,
1477 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1480 ext + 4, ext_size)) != 0) {
1481 return ret;
1482 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001486
Hanno Beckera0e20d02019-05-15 14:03:01 +01001487#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 case MBEDTLS_TLS_EXT_CID:
1489 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if ((ret = ssl_parse_cid_ext(ssl,
1492 ext + 4,
1493 ext_size)) != 0) {
1494 return ret;
1495 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001498#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1502 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1505 ext + 4, ext_size)) != 0) {
1506 return ret;
1507 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1514 MBEDTLS_SSL_DEBUG_MSG(3,
1515 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if ((ret = ssl_parse_extended_ms_ext(ssl,
1518 ext + 4, ext_size)) != 0) {
1519 return ret;
1520 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1527 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if ((ret = ssl_parse_session_ticket_ext(ssl,
1530 ext + 4, ext_size)) != 0) {
1531 return ret;
1532 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001536
Robert Cragie136884c2015-10-02 13:34:31 +01001537#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1539 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1540 MBEDTLS_SSL_DEBUG_MSG(3,
1541 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1544 ext + 4, ext_size)) != 0) {
1545 return ret;
1546 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001549#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1550 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001551
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001552#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1554 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1557 ext + 4, ext_size)) != 0) {
1558 return ret;
1559 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001562#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 case MBEDTLS_TLS_EXT_ALPN:
1566 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1569 return ret;
1570 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001574
Johan Pascalb62bb512015-12-03 21:56:45 +01001575#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 case MBEDTLS_TLS_EXT_USE_SRTP:
1577 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1580 return ret;
1581 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001584#endif /* MBEDTLS_SSL_DTLS_SRTP */
1585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 default:
1587 MBEDTLS_SSL_DEBUG_MSG(3,
1588 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001589 }
1590
1591 ext_len -= 4 + ext_size;
1592 ext += 4 + ext_size;
1593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (ext_len > 0 && ext_len < 4) {
1595 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1596 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001597 }
1598 }
1599
1600 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001601 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1602 * extensions. It sets the transform data for the resumed session which in
1603 * case of DTLS includes the server CID extracted from the CID extension.
1604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 if (ssl->handshake->resume) {
1606 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1607 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001608 mbedtls_ssl_send_alert_message(
1609 ssl,
1610 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1612 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001613 }
1614 }
1615
Paul Bakker48916f92012-09-16 19:57:18 +00001616 /*
1617 * Renegotiation security checks
1618 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001620 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1622 MBEDTLS_SSL_DEBUG_MSG(1,
1623 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001624 handshake_failure = 1;
1625 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 renegotiation_info_seen == 0) {
1630 MBEDTLS_SSL_DEBUG_MSG(1,
1631 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001632 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1634 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1635 ssl->conf->allow_legacy_renegotiation ==
1636 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1637 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001638 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1640 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1641 renegotiation_info_seen == 1) {
1642 MBEDTLS_SSL_DEBUG_MSG(1,
1643 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001644 handshake_failure = 1;
1645 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001649 mbedtls_ssl_send_alert_message(
1650 ssl,
1651 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1653 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001659}
1660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1662 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001663MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001664static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1665 unsigned char **p,
1666 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001669 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001670
Paul Bakker29e1f122013-04-16 13:07:56 +02001671 /*
1672 * Ephemeral DH parameters:
1673 *
1674 * struct {
1675 * opaque dh_p<1..2^16-1>;
1676 * opaque dh_g<1..2^16-1>;
1677 * opaque dh_Ys<1..2^16-1>;
1678 * } ServerDHParams;
1679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1681 p, end)) != 0) {
1682 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1683 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001684 }
1685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1687 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1688 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1689 dhm_actual_bitlen,
1690 ssl->conf->dhm_min_bitlen));
1691 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001692 }
1693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1695 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1696 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1701 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001702
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001703#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001704#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1705 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1706 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001707MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001708static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1709 unsigned char **p,
1710 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001711{
1712 uint16_t tls_id;
1713 uint8_t ecpoint_len;
1714 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001715 psa_ecc_family_t ec_psa_family = 0;
1716 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001717
1718 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001719 * struct {
1720 * ECParameters curve_params;
1721 * ECPoint public;
1722 * } ServerECDHParams;
1723 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001724 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001725 * 2..3 NamedCurve
1726 * 4 ECPoint.len
1727 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001728 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 if (end - *p < 4) {
1730 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1731 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001732
1733 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1735 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1736 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001737
1738 /* Next two bytes are the namedcurve value */
1739 tls_id = *(*p)++;
1740 tls_id <<= 8;
1741 tls_id |= *(*p)++;
1742
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001743 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1745 MBEDTLS_SSL_DEBUG_MSG(2,
1746 ("bad server key exchange message (ECDHE curve): %u",
1747 (unsigned) tls_id));
1748 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001749 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001750
Valerio Setti40d9ca92023-01-04 16:08:04 +01001751 /* Convert EC's TLS ID to PSA key type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ec_psa_family,
1753 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1754 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001755 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001757 handshake->ecdh_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001758
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001759 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001760 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 if ((size_t) (end - *p) < ecpoint_len) {
1762 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1763 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 if (ecpoint_len > sizeof(handshake->ecdh_psa_peerkey)) {
1766 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1767 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 memcpy(handshake->ecdh_psa_peerkey, *p, ecpoint_len);
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001770 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001771 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001774}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001775#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1776 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1777 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001778#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001779#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1780 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1781 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1782 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1783 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001784MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001785static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001786{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001787 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001788 mbedtls_ecp_group_id grp_id;
1789#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1790 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1791#else
1792 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1793#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1796 if (tls_id == 0) {
1797 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1798 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001799 }
1800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1802 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1805 return -1;
1806 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1809 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001812}
1813
Andrzej Kurek468c5062022-10-24 10:30:14 -04001814#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1815 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1816 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1817 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1818 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1819
1820#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1821 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1822 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001823MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001824static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1825 unsigned char **p,
1826 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001827{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001829
Paul Bakker29e1f122013-04-16 13:07:56 +02001830 /*
1831 * Ephemeral ECDH parameters:
1832 *
1833 * struct {
1834 * ECParameters curve_params;
1835 * ECPoint public;
1836 * } ServerECDHParams;
1837 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1839 (const unsigned char **) p, end)) != 0) {
1840 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001841#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001843 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001845#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001847 }
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (ssl_check_server_ecdh_params(ssl) != 0) {
1850 MBEDTLS_SSL_DEBUG_MSG(1,
1851 ("bad server key exchange message (ECDHE curve)"));
1852 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001853 }
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001856}
Gilles Peskine449bd832023-01-11 14:50:10 +01001857#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1858 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001859 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1860#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001861#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001862MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001863static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1864 unsigned char **p,
1865 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001866{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001868 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001869 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001870
1871 /*
1872 * PSK parameters:
1873 *
1874 * opaque psk_identity_hint<0..2^16-1>;
1875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (end - (*p) < 2) {
1877 MBEDTLS_SSL_DEBUG_MSG(1,
1878 ("bad server key exchange message (psk_identity_hint length)"));
1879 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001880 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001881 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001882 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 if (end - (*p) < len) {
1885 MBEDTLS_SSL_DEBUG_MSG(1,
1886 ("bad server key exchange message (psk_identity_hint length)"));
1887 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001888 }
1889
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001890 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001891 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001892 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001893 * someone needs that feature.
1894 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001895 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001896 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001899}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001900#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1903 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001904/*
1905 * Generate a pre-master secret and encrypt it with the server's RSA key
1906 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001907MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001908static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1909 size_t offset, size_t *olen,
1910 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001911{
Janos Follath865b3eb2019-12-16 11:46:15 +00001912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001913 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001914 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1918 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1919 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001920 }
1921
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001922 /*
1923 * Generate (part of) the pre-master as
1924 * struct {
1925 * ProtocolVersion client_version;
1926 * opaque random[46];
1927 * } PreMasterSecret;
1928 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 mbedtls_ssl_write_version(p, ssl->conf->transport,
1930 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1933 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1934 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001935 }
1936
1937 ssl->handshake->pmslen = 48;
1938
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001939#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1940 peer_pk = &ssl->handshake->peer_pubkey;
1941#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001943 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1945 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001946 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001947 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1948#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001949
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001950 /*
1951 * Now write it out, encrypted
1952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1954 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1955 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001956 }
1957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if ((ret = mbedtls_pk_encrypt(peer_pk,
1959 p, ssl->handshake->pmslen,
1960 ssl->out_msg + offset + len_bytes, olen,
1961 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1962 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1963 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1964 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001965 }
1966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 if (len_bytes == 2) {
1968 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001969 *olen += 2;
1970 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001971
Hanno Beckerae553dd2019-02-08 14:06:00 +00001972#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1973 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001975#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
1979 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1982 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001983MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001984static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001985{
Janos Follath865b3eb2019-12-16 11:46:15 +00001986 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001988
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001989#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1990 peer_pk = &ssl->handshake->peer_pubkey;
1991#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001993 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1995 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001996 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001997 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1998#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001999
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002000 /* This is a public key, so it can't be opaque, so can_do() is a good
2001 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2003 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2004 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002005 }
2006
Valerio Setti97207782023-05-18 18:59:06 +02002007#if defined(MBEDTLS_ECP_C)
2008 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
2009#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002010
Przemek Stekielea4000f2022-03-16 09:49:33 +01002011#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002012 uint16_t tls_id = 0;
2013 psa_ecc_family_t ecc_family;
Valerio Setti97207782023-05-18 18:59:06 +02002014 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002015
Valerio Setti97207782023-05-18 18:59:06 +02002016 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2018 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002019 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002020
Valerio Setti97207782023-05-18 18:59:06 +02002021 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 if (tls_id == 0) {
2023 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002024 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002026 }
2027
Valerio Setti1e868cc2023-01-09 17:30:01 +01002028 /* If the above conversion to TLS ID was fine, then also this one will be,
2029 so there is no need to check the return value here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ecc_family,
2031 &ssl->handshake->ecdh_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 ssl->handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ecc_family);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002034
Przemek Stekielea4000f2022-03-16 09:49:33 +01002035 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002036#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
2037 memcpy(ssl->handshake->ecdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2038 ssl->handshake->ecdh_psa_peerkey_len = peer_pk->pub_raw_len;
2039 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002040#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002041 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2043 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2044 ssl->handshake->ecdh_psa_peerkey,
2045 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 if (ret != 0) {
2048 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2049 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002050 }
Przemek Stekiel561a4232022-03-16 13:16:24 +01002051 ssl->handshake->ecdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002052#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2053#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2055 MBEDTLS_ECDH_THEIRS)) != 0) {
2056 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2057 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002058 }
2059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (ssl_check_server_ecdh_params(ssl) != 0) {
2061 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2062 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002063 }
Valerio Setti97207782023-05-18 18:59:06 +02002064#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002065#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2066 /* We don't need the peer's public key anymore. Free it,
2067 * so that more RAM is available for upcoming expensive
2068 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002070#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2075 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002076
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002077MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002078static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002079{
Janos Follath865b3eb2019-12-16 11:46:15 +00002080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002081 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002082 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002083 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2089 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002093 ((void) p);
2094 ((void) end);
2095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2098 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2100 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2101 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2102 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002103 mbedtls_ssl_send_alert_message(
2104 ssl,
2105 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2107 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002108 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002111 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002113 }
2114 ((void) p);
2115 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2117 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002118
Gilles Peskineeccd8882020-03-10 12:19:08 +01002119#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (ssl->handshake->ecrs_enabled &&
2121 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002122 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002123 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002124#endif
2125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2127 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2128 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 }
2130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2132 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002133 mbedtls_ssl_send_alert_message(
2134 ssl,
2135 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2137 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
2139
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002140 /*
2141 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2142 * doesn't use a psk_identity_hint
2143 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2145 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2146 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002147 /* Current message is probably either
2148 * CertificateRequest or ServerHelloDone */
2149 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002150 goto exit;
2151 }
2152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 MBEDTLS_SSL_DEBUG_MSG(1,
2154 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002155 mbedtls_ssl_send_alert_message(
2156 ssl,
2157 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 }
2162
Gilles Peskineeccd8882020-03-10 12:19:08 +01002163#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002165 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002167
2168start_processing:
2169#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002171 end = ssl->in_msg + ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, end - p);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002173
Gilles Peskineeccd8882020-03-10 12:19:08 +01002174#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2177 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2179 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2180 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002181 mbedtls_ssl_send_alert_message(
2182 ssl,
2183 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2185 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002186 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002187 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002188#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2191 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2193 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002194 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2197 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2198#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2199 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2201 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2202 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2203 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002204 mbedtls_ssl_send_alert_message(
2205 ssl,
2206 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2208 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002209 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2212 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002213#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2214 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2219 if (ssl_parse_server_ecdh_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 Bakker41c83d32013-03-20 14:39:14 +01002226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2229 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2230 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002231#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002233#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002234 /*
2235 * The first 3 bytes are:
2236 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2237 * [1, 2] elliptic curve's TLS ID
2238 *
2239 * However since we only support secp256r1 for now, we check only
2240 * that TLS ID here
2241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002243 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (exp_tls_id == 0) {
2247 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002248 }
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2251 (read_tls_id != exp_tls_id)) {
2252 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002253 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002254
2255 p += 3;
2256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if ((ret = mbedtls_psa_ecjpake_read_round(
2258 &ssl->handshake->psa_pake_ctx, p, end - p,
2259 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2260 psa_destroy_key(ssl->handshake->psa_pake_password);
2261 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002264 mbedtls_ssl_send_alert_message(
2265 ssl,
2266 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2268 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002269 }
2270#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2272 p, end - p);
2273 if (ret != 0) {
2274 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002275 mbedtls_ssl_send_alert_message(
2276 ssl,
2277 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2279 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002280 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002281#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002283#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002284 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2286 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002287 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002288
Gilles Peskineeccd8882020-03-10 12:19:08 +01002289#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002291 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002292 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2295 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002297 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002298 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002299 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002302
Jerry Yu693a47a2022-06-23 14:02:28 +08002303#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2304 peer_pk = &ssl->handshake->peer_pubkey;
2305#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002307 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2309 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002310 }
2311 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2312#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2313
Paul Bakker29e1f122013-04-16 13:07:56 +02002314 /*
2315 * Handle the digitally-signed structure
2316 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2318 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2319 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2320 sig_alg, &pk_alg, &md_alg) != 0 &&
2321 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2322 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2323 MBEDTLS_SSL_DEBUG_MSG(1,
2324 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002325 mbedtls_ssl_send_alert_message(
2326 ssl,
2327 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2329 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002330 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002331 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2334 MBEDTLS_SSL_DEBUG_MSG(1,
2335 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002336 mbedtls_ssl_send_alert_message(
2337 ssl,
2338 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2340 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002341 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002342
2343 /*
2344 * Read signature
2345 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (p > end - 2) {
2348 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002349 mbedtls_ssl_send_alert_message(
2350 ssl,
2351 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2353 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002354 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 sig_len = (p[0] << 8) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002356 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (p != end - sig_len) {
2359 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002360 mbedtls_ssl_send_alert_message(
2361 ssl,
2362 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2364 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002365 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002368
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002369 /*
2370 * Compute the hash that has been signed
2371 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 if (md_alg != MBEDTLS_MD_NONE) {
2373 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2374 params, params_len,
2375 md_alg);
2376 if (ret != 0) {
2377 return ret;
2378 }
2379 } else {
2380 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2381 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002382 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002385
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002386 /*
2387 * Verify signature
2388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2390 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002391 mbedtls_ssl_send_alert_message(
2392 ssl,
2393 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2395 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002396 }
2397
Gilles Peskineeccd8882020-03-10 12:19:08 +01002398#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002400 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002402#endif
2403
Jerry Yu693a47a2022-06-23 14:02:28 +08002404#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002406 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2407 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002408 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002409 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (rsassa_pss_options.expected_salt_len == 0) {
2411 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2412 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2415 peer_pk,
2416 md_alg, hash, hashlen,
2417 p, sig_len);
2418 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002419#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 ret = mbedtls_pk_verify_restartable(peer_pk,
2421 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002424 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002425#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002427#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002429 mbedtls_ssl_send_alert_message(
2430 ssl,
2431 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2433 }
2434 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002435#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002437 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002439#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002441 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002442
2443#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2444 /* We don't need the peer's public key anymore. Free it,
2445 * so that more RAM is available for upcoming expensive
2446 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002448#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002450#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002452exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 ssl->state++;
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002458}
2459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002461MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002462static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002463{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002464 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002465 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2470 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002471 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002473 }
2474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2476 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002477}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002478#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002479MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002480static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002481{
Janos Follath865b3eb2019-12-16 11:46:15 +00002482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002483 unsigned char *buf;
2484 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002485 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002486 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002487 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002488 size_t sig_alg_len;
2489#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 unsigned char *sig_alg;
2491 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002492#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2497 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002498 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002500 }
2501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2503 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2504 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 }
2506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2508 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002509 mbedtls_ssl_send_alert_message(
2510 ssl,
2511 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2513 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002514 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002516 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002517 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2521 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002524 /* Current message is probably the ServerHelloDone */
2525 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002526 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002527 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002528
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002529 /*
2530 * struct {
2531 * ClientCertificateType certificate_types<1..2^8-1>;
2532 * SignatureAndHashAlgorithm
2533 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2534 * DistinguishedName certificate_authorities<0..2^16-1>;
2535 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002536 *
2537 * Since we only support a single certificate on clients, let's just
2538 * ignore all the information that's supposed to help us pick a
2539 * certificate.
2540 *
2541 * We could check that our certificate matches the request, and bail out
2542 * if it doesn't, but it's simpler to just send the certificate anyway,
2543 * and give the server the opportunity to decide if it should terminate
2544 * the connection when it doesn't like our certificate.
2545 *
2546 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2547 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002548 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002549 *
2550 * However, we still minimally parse the message to check it is at least
2551 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002552 */
Paul Bakker926af752012-11-23 13:38:07 +01002553 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002554
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002555 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2557 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2558 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2559 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2560 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002561 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002563 n = cert_type_len;
2564
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002565 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002566 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002567 * * the length of the signature algorithms field (if minor version of
2568 * SSL is 3),
2569 * * distinguished name length otherwise.
2570 * Both reach at most the index:
2571 * ...hdr_len + 2 + n,
2572 * therefore the buffer length at this point must be greater than that
2573 * regardless of the actual code path.
2574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2576 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2577 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2578 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2579 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002580 }
2581
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002582 /* supported_signature_algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 sig_alg_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2584 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Ronald Cron90915f22022-03-07 11:11:36 +01002585
2586 /*
2587 * The furthest access in buf is in the loop few lines below:
2588 * sig_alg[i + 1],
2589 * where:
2590 * sig_alg = buf + ...hdr_len + 3 + n,
2591 * max(i) = sig_alg_len - 1.
2592 * Therefore the furthest access is:
2593 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2594 * which reduces to:
2595 * buf[...hdr_len + 3 + n + sig_alg_len],
2596 * which is one less than we need the buf to be.
2597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2599 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002600 mbedtls_ssl_send_alert_message(
2601 ssl,
2602 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2604 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002605 }
Paul Bakker926af752012-11-23 13:38:07 +01002606
Ronald Cron90915f22022-03-07 11:11:36 +01002607#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2609 for (size_t i = 0; i < sig_alg_len; i += 2) {
2610 MBEDTLS_SSL_DEBUG_MSG(3,
2611 ("Supported Signature Algorithm found: %02x %02x",
2612 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002613 }
2614#endif
2615
2616 n += 2 + sig_alg_len;
2617
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002618 /* certificate_authorities */
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 dn_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2620 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Paul Bakker926af752012-11-23 13:38:07 +01002621
2622 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2624 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2625 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2626 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2627 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002628 }
2629
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002630#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2632 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002633 unsigned char *p = dn + i + 2;
2634 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002635 size_t asn1_len;
2636 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 memset(&name, 0, sizeof(name));
2638 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2639 if (dni_len > dn_len - i - 2 ||
2640 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2641 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2642 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2643 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002644 mbedtls_ssl_send_alert_message(
2645 ssl,
2646 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2648 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002649 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 MBEDTLS_SSL_DEBUG_MSG(3,
2651 ("DN hint: %.*s",
2652 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2653 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002654 }
2655#endif
2656
Paul Bakker926af752012-11-23 13:38:07 +01002657exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002661}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002662#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002664MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002665static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002666{
Janos Follath865b3eb2019-12-16 11:46:15 +00002667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2672 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2673 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2677 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2678 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2682 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2683 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2684 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2685 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2686 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 }
2688
2689 ssl->state++;
2690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2693 mbedtls_ssl_recv_flight_completed(ssl);
2694 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002695#endif
2696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700}
2701
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002702MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002703static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002704{
Janos Follath865b3eb2019-12-16 11:46:15 +00002705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002706
2707 size_t header_len;
2708 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002709 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002710 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 /*
2717 * DHM key exchange -- send G^X mod P
2718 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002722 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2725 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2726 &ssl->out_msg[header_len], content_len,
2727 ssl->conf->f_rng, ssl->conf->p_rng);
2728 if (ret != 0) {
2729 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2730 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 }
2732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2734 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2737 ssl->handshake->premaster,
2738 MBEDTLS_PREMASTER_SIZE,
2739 &ssl->handshake->pmslen,
2740 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2741 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2742 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 }
2744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2746 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002748#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2749 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2750 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2751 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002753 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2754 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002756#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002757 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2758 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002759 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002760
2761 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2762
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002763 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002766
Hanno Becker4a63ed42019-01-08 11:39:35 +00002767 /*
2768 * Generate EC private key for ECDHE exchange.
2769 */
2770
Hanno Becker4a63ed42019-01-08 11:39:35 +00002771 /* The master secret is obtained from the shared ECDH secret by
2772 * applying the TLS 1.2 PRF with a specific salt and label. While
2773 * the PSA Crypto API encourages combining key agreement schemes
2774 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2775 * yet support the provisioning of salt + label to the KDF.
2776 * For the time being, we therefore need to split the computation
2777 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002778 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2780 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2781 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2782 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002783
2784 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 status = psa_generate_key(&key_attributes,
2786 &handshake->ecdh_psa_privkey);
2787 if (status != PSA_SUCCESS) {
2788 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2789 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002790
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002791 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002792 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002793 * but we just need to add a length byte before that. */
2794 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2795 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002797 size_t own_pubkey_len;
2798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2800 own_pubkey, own_pubkey_max_len,
2801 &own_pubkey_len);
2802 if (status != PSA_SUCCESS) {
2803 psa_destroy_key(handshake->ecdh_psa_privkey);
Andrzej Kureka0237f82022-02-24 13:24:52 -05002804 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002806 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002807
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002808 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2809 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002810
Hanno Becker4a63ed42019-01-08 11:39:35 +00002811 /* The ECDH secret is the premaster secret used for key derivation. */
2812
Janos Follathdf3b0892019-08-08 11:12:24 +01002813 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2815 handshake->ecdh_psa_privkey,
2816 handshake->ecdh_psa_peerkey,
2817 handshake->ecdh_psa_peerkey_len,
2818 ssl->handshake->premaster,
2819 sizeof(ssl->handshake->premaster),
2820 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Ronald Croncf56a0a2020-08-04 09:51:30 +02002823 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2826 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2827 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002828#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002829 /*
2830 * ECDH key exchange -- send client public value
2831 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002832 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002833
Gilles Peskineeccd8882020-03-10 12:19:08 +01002834#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 if (ssl->handshake->ecrs_enabled) {
2836 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002837 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002841 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002842#endif
2843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2845 &content_len,
2846 &ssl->out_msg[header_len], 1000,
2847 ssl->conf->f_rng, ssl->conf->p_rng);
2848 if (ret != 0) {
2849 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002850#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002852 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002854#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002856 }
2857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2859 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002860
Gilles Peskineeccd8882020-03-10 12:19:08 +01002861#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002863 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002864 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002865 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002866
2867ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002869 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002871#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2873 &ssl->handshake->pmslen,
2874 ssl->handshake->premaster,
2875 MBEDTLS_MPI_MAX_SIZE,
2876 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2877 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002878#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002880 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002882#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002884 }
2885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2887 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002888#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2891 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2892 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2893 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002894#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2895 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2898 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2899 psa_key_attributes_t key_attributes;
2900
2901 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2902
2903 /*
2904 * opaque psk_identity<0..2^16-1>;
2905 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002907 /* We don't offer PSK suites if we don't have a PSK,
2908 * and we check that the server's choice is among the
2909 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2911 }
Neil Armstrong868af822022-03-09 10:26:25 +01002912
Neil Armstrongfc834f22022-03-23 17:54:38 +01002913 /* uint16 to store content length */
2914 const size_t content_len_size = 2;
2915
Neil Armstrong868af822022-03-09 10:26:25 +01002916 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 if (header_len + content_len_size + ssl->conf->psk_identity_len
2919 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2920 MBEDTLS_SSL_DEBUG_MSG(1,
2921 ("psk identity too long or SSL buffer too short"));
2922 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002923 }
2924
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002925 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2928 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002929 header_len += content_len_size;
2930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 memcpy(p, ssl->conf->psk_identity,
2932 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002933 p += ssl->conf->psk_identity_len;
2934
Neil Armstrong868af822022-03-09 10:26:25 +01002935 header_len += ssl->conf->psk_identity_len;
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002938
2939 /*
2940 * Generate EC private key for ECDHE exchange.
2941 */
2942
2943 /* The master secret is obtained from the shared ECDH secret by
2944 * applying the TLS 1.2 PRF with a specific salt and label. While
2945 * the PSA Crypto API encourages combining key agreement schemes
2946 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2947 * yet support the provisioning of salt + label to the KDF.
2948 * For the time being, we therefore need to split the computation
2949 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2950 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2952 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2953 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2954 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002955
2956 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 status = psa_generate_key(&key_attributes,
2958 &handshake->ecdh_psa_privkey);
2959 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002960 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 }
Neil Armstrong868af822022-03-09 10:26:25 +01002962
2963 /* Export the public part of the ECDH private key from PSA.
2964 * The export format is an ECPoint structure as expected by TLS,
2965 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002966 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002967 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002969 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2972 own_pubkey, own_pubkey_max_len,
2973 &own_pubkey_len);
2974 if (status != PSA_SUCCESS) {
2975 psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01002976 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002977 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002978 }
2979
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002980 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002981 content_len = own_pubkey_len + 1;
2982
Neil Armstrong25400452022-03-23 17:44:07 +01002983 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2984 * - a uint16 containing the length (in octets) of the ECDH computation
2985 * - the octet string produced by the ECDH computation
2986 * - a uint16 containing the length (in octets) of the PSK
2987 * - the PSK itself
2988 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002989 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 const unsigned char * const pms_end = pms +
2991 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002992 /* uint16 to store length (in octets) of the ECDH computation */
2993 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002994 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002995
Neil Armstrong25400452022-03-23 17:44:07 +01002996 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2998 handshake->ecdh_psa_privkey,
2999 handshake->ecdh_psa_peerkey,
3000 handshake->ecdh_psa_peerkey_len,
3001 pms + zlen_size,
3002 pms_end - (pms + zlen_size),
3003 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01003006 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003009 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003011 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 }
Neil Armstrong868af822022-03-09 10:26:25 +01003013
Neil Armstrong25400452022-03-23 17:44:07 +01003014 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003016 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003018#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3019 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003020#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003022 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003023 * opaque psk_identity<0..2^16-1>;
3024 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003026 /* We don't offer PSK suites if we don't have a PSK,
3027 * and we check that the server's choice is among the
3028 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003030 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003031
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003032 header_len = 4;
3033 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3036 MBEDTLS_SSL_DEBUG_MSG(1,
3037 ("psk identity too long or SSL buffer too short"));
3038 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003039 }
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3042 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 memcpy(ssl->out_msg + header_len,
3045 ssl->conf->psk_identity,
3046 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003047 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003051 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3056 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3057 &content_len, 2)) != 0) {
3058 return ret;
3059 }
3060 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003064 /*
3065 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 if (header_len + 2 + content_len >
3070 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3071 MBEDTLS_SSL_DEBUG_MSG(1,
3072 ("psk identity or DHM size too long or SSL buffer too short"));
3073 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003074 }
3075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3077 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003078
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3080 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3081 &ssl->out_msg[header_len], content_len,
3082 ssl->conf->f_rng, ssl->conf->p_rng);
3083 if (ret != 0) {
3084 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3085 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003086 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003087
3088#if defined(MBEDTLS_USE_PSA_CRYPTO)
3089 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003091 size_t pms_len;
3092
3093 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3095 pms + 2, pms_end - (pms + 2), &pms_len,
3096 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3097 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3098 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003099 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003101 pms += 2 + pms_len;
3102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003104#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003107#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3109 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003110 /*
3111 * ClientECDiffieHellmanPublic public;
3112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3114 &content_len,
3115 &ssl->out_msg[header_len],
3116 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3117 ssl->conf->f_rng, ssl->conf->p_rng);
3118 if (ret != 0) {
3119 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3120 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003121 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3124 MBEDTLS_DEBUG_ECDH_Q);
3125 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003126#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003127 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3129 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003130 }
3131
Neil Armstrong80f6f322022-05-03 17:56:38 +02003132#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3134 ciphersuite_info->key_exchange)) != 0) {
3135 MBEDTLS_SSL_DEBUG_RET(1,
3136 "mbedtls_ssl_psk_derive_premaster", ret);
3137 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003138 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003139#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003141#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003144 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3146 &content_len, 0)) != 0) {
3147 return ret;
3148 }
3149 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003151#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003153 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003154
Neil Armstrongca7d5062022-05-31 14:43:23 +02003155#if defined(MBEDTLS_USE_PSA_CRYPTO)
3156 unsigned char *out_p = ssl->out_msg + header_len;
3157 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3158 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3160 out_p, end_p - out_p, &content_len,
3161 MBEDTLS_ECJPAKE_ROUND_TWO);
3162 if (ret != 0) {
3163 psa_destroy_key(ssl->handshake->psa_pake_password);
3164 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3165 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3166 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003167 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003168#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3170 ssl->out_msg + header_len,
3171 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3172 &content_len,
3173 ssl->conf->f_rng, ssl->conf->p_rng);
3174 if (ret != 0) {
3175 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3176 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003177 }
3178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3180 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3181 ssl->conf->f_rng, ssl->conf->p_rng);
3182 if (ret != 0) {
3183 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3184 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003185 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003188#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003189 {
3190 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3192 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003193 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003194
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003195 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3197 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
3199 ssl->state++;
3200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3202 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3203 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 }
3205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003209}
3210
Gilles Peskineeccd8882020-03-10 12:19:08 +01003211#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003212MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003213static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003214{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003215 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003216 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3222 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3223 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003224 }
3225
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3227 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003228 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003230 }
3231
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3233 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003234}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003235#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003236MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003237static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003240 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003241 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003242 size_t n = 0, offset = 0;
3243 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003244 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003246 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003247 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003248#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 size_t out_buf_len = ssl->out_buf_len - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003250#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003252#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003255
Gilles Peskineeccd8882020-03-10 12:19:08 +01003256#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 if (ssl->handshake->ecrs_enabled &&
3258 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003259 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003260 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003261#endif
3262
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3264 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3265 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003266 }
3267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3269 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003270 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003272 }
3273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (ssl->handshake->client_auth == 0 ||
3275 mbedtls_ssl_own_cert(ssl) == NULL) {
3276 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003277 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 }
3280
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 if (mbedtls_ssl_own_key(ssl) == NULL) {
3282 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3283 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 }
3285
3286 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003287 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003289#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003291 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003293
3294sign:
3295#endif
3296
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003297 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3298 if (0 != ret) {
3299 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3300 return ret;
3301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
Ronald Cron90915f22022-03-07 11:11:36 +01003303 /*
3304 * digitally-signed struct {
3305 * opaque handshake_messages[handshake_messages_length];
3306 * };
3307 *
3308 * Taking shortcut here. We assume that the server always allows the
3309 * PRF Hash function and has sent it in the allowed signature
3310 * algorithms list received in the Certificate Request message.
3311 *
3312 * Until we encounter a server that does not, we will take this
3313 * shortcut.
3314 *
3315 * Reason: Otherwise we should have running hashes for SHA512 and
3316 * SHA224 in order to satisfy 'weird' needs from the server
3317 * side.
3318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003320 md_alg = MBEDTLS_MD_SHA384;
3321 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003323 md_alg = MBEDTLS_MD_SHA256;
3324 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003325 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003327
3328 /* Info from md_alg will be used instead */
3329 hashlen = 0;
3330 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003331
Gilles Peskineeccd8882020-03-10 12:19:08 +01003332#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003334 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003336#endif
3337
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3339 md_alg, hash_start, hashlen,
3340 ssl->out_msg + 6 + offset,
3341 out_buf_len - 6 - offset,
3342 &n,
3343 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3344 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003345#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003347 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003349#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003351 }
Paul Bakker926af752012-11-23 13:38:07 +01003352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003354
Paul Bakker1ef83d62012-04-11 12:09:53 +00003355 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3357 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003358
3359 ssl->state++;
3360
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3362 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3363 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 }
3365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003369}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003370#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003373MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003374static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003375{
Janos Follath865b3eb2019-12-16 11:46:15 +00003376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003377 uint32_t lifetime;
3378 size_t ticket_len;
3379 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003380 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3385 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3386 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003387 }
3388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3390 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003391 mbedtls_ssl_send_alert_message(
3392 ssl,
3393 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3395 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003396 }
3397
3398 /*
3399 * struct {
3400 * uint32 ticket_lifetime_hint;
3401 * opaque ticket<0..2^16-1>;
3402 * } NewSessionTicket;
3403 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003404 * 0 . 3 ticket_lifetime_hint
3405 * 4 . 5 ticket_len (n)
3406 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003407 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3409 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3410 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3411 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3412 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3413 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003414 }
3415
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 lifetime = (((uint32_t) msg[0]) << 24) | (msg[1] << 16) |
3419 (msg[2] << 8) | (msg[3]);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003420
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 ticket_len = (msg[4] << 8) | (msg[5]);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3424 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3425 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3426 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3427 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003428 }
3429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003431
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003432 /* We're not waiting for a NewSessionTicket message any more */
3433 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003435
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003436 /*
3437 * Zero-length ticket means the server changed his mind and doesn't want
3438 * to send a ticket after all, so just forget it
3439 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 if (ticket_len == 0) {
3441 return 0;
3442 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003443
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 if (ssl->session != NULL && ssl->session->ticket != NULL) {
3445 mbedtls_platform_zeroize(ssl->session->ticket,
3446 ssl->session->ticket_len);
3447 mbedtls_free(ssl->session->ticket);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003448 ssl->session->ticket = NULL;
3449 ssl->session->ticket_len = 0;
3450 }
3451
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 mbedtls_platform_zeroize(ssl->session_negotiate->ticket,
3453 ssl->session_negotiate->ticket_len);
3454 mbedtls_free(ssl->session_negotiate->ticket);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003455 ssl->session_negotiate->ticket = NULL;
3456 ssl->session_negotiate->ticket_len = 0;
3457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3459 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3460 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3461 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3462 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003463 }
3464
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003466
3467 ssl->session_negotiate->ticket = ticket;
3468 ssl->session_negotiate->ticket_len = ticket_len;
3469 ssl->session_negotiate->ticket_lifetime = lifetime;
3470
3471 /*
3472 * RFC 5077 section 3.4:
3473 * "If the client receives a session ticket from the server, then it
3474 * discards any Session ID that was sent in the ServerHello."
3475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003477 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003478
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003480
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003484
Paul Bakker5121ce52009-01-03 21:22:43 +00003485/*
Paul Bakker1961b702013-01-25 14:49:24 +01003486 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003488int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003489{
3490 int ret = 0;
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003493 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003495 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3496 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003497 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003498 }
3499#endif
3500
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 case MBEDTLS_SSL_HELLO_REQUEST:
3503 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003504 break;
3505
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 /*
3507 * ==> ClientHello
3508 */
3509 case MBEDTLS_SSL_CLIENT_HELLO:
3510 ret = mbedtls_ssl_write_client_hello(ssl);
3511 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003512
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 /*
3514 * <== ServerHello
3515 * Certificate
3516 * ( ServerKeyExchange )
3517 * ( CertificateRequest )
3518 * ServerHelloDone
3519 */
3520 case MBEDTLS_SSL_SERVER_HELLO:
3521 ret = ssl_parse_server_hello(ssl);
3522 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3525 ret = mbedtls_ssl_parse_certificate(ssl);
3526 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3529 ret = ssl_parse_server_key_exchange(ssl);
3530 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003531
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3533 ret = ssl_parse_certificate_request(ssl);
3534 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3537 ret = ssl_parse_server_hello_done(ssl);
3538 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 /*
3541 * ==> ( Certificate/Alert )
3542 * ClientKeyExchange
3543 * ( CertificateVerify )
3544 * ChangeCipherSpec
3545 * Finished
3546 */
3547 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3548 ret = mbedtls_ssl_write_certificate(ssl);
3549 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003550
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3552 ret = ssl_write_client_key_exchange(ssl);
3553 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003554
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3556 ret = ssl_write_certificate_verify(ssl);
3557 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003558
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3560 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3561 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003562
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 case MBEDTLS_SSL_CLIENT_FINISHED:
3564 ret = mbedtls_ssl_write_finished(ssl);
3565 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003566
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 /*
3568 * <== ( NewSessionTicket )
3569 * ChangeCipherSpec
3570 * Finished
3571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3574 ret = ssl_parse_new_session_ticket(ssl);
3575 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003576#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003577
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3579 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3580 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003581
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 case MBEDTLS_SSL_SERVER_FINISHED:
3583 ret = mbedtls_ssl_parse_finished(ssl);
3584 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003585
Gilles Peskine449bd832023-01-11 14:50:10 +01003586 case MBEDTLS_SSL_FLUSH_BUFFERS:
3587 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3588 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3589 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003590
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3592 mbedtls_ssl_handshake_wrapup(ssl);
3593 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003594
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 default:
3596 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3597 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003599
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003601}
Jerry Yuc5aef882021-12-23 20:15:02 +08003602
Jerry Yufb4b6472022-01-27 15:03:26 +08003603#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */