blob: 75b79bfadfabae57a84e2bd57aabc1e39b699574 [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 Kurek00644842023-05-30 05:45:00 -040036/* Define a local translating function to save code size by not using too many
37 * arguments in each translating place. */
38static int local_err_translation(psa_status_t status)
39{
40 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
41 sizeof(psa_to_ssl_errors),
42 psa_generic_status_to_mbedtls);
43}
44#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Hanno Beckerbb89e272019-01-08 12:54:37 +000045#endif /* MBEDTLS_USE_PSA_CRYPTO */
46
SimonBd5800b72016-04-26 07:43:27 +010047#include <string.h>
48
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020049#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010052#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050056#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020060MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010061static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
62 unsigned char *buf,
63 const unsigned char *end,
64 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010065{
66 unsigned char *p = buf;
67
68 *olen = 0;
69
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010070 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010071 * initial ClientHello, in which case also adding the renegotiation
72 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
74 return 0;
75 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 MBEDTLS_SSL_DEBUG_MSG(3,
78 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010081
Paul Bakkerd3edc862013-03-20 16:07:17 +010082 /*
83 * Secure renegotiation
84 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010086 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010087
88 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010089 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
90 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010093
94 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010099
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200100#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100101 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100102
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200103MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100104static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
105 unsigned char *buf,
106 const unsigned char *end,
107 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100108{
109 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100110 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100111
112 *olen = 0;
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 MBEDTLS_SSL_DEBUG_MSG(3,
115 ("client hello, adding supported_point_formats extension"));
116 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100119 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100120
121 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100122 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200123
124 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100126
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200127 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100130}
Darryl Green11999bb2018-03-13 15:22:58 +0000131#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100132 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100133
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200134#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200135MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100136static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
137 unsigned char *buf,
138 const unsigned char *end,
139 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200140{
Janos Follath865b3eb2019-12-16 11:46:15 +0000141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200142 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100143 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200144
145 *olen = 0;
146
147 /* Skip costly extension if we can't use EC J-PAKE anyway */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200148#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
150 return 0;
151 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200152#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
154 return 0;
155 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200156#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 MBEDTLS_SSL_DEBUG_MSG(3,
159 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100164 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200165
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200166 /*
167 * We may need to send ClientHello multiple times for Hello verification.
168 * We don't want to compute fresh values every time (both for performance
169 * and consistency reasons), so cache the extension content.
170 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ssl->handshake->ecjpake_cache == NULL ||
172 ssl->handshake->ecjpake_cache_len == 0) {
173 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200174
Neil Armstrongca7d5062022-05-31 14:43:23 +0200175#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100176 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 p + 2, end - p - 2, &kkpp_len,
178 MBEDTLS_ECJPAKE_ROUND_ONE);
179 if (ret != 0) {
180 psa_destroy_key(ssl->handshake->psa_pake_password);
181 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
182 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
183 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200184 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200185#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
187 p + 2, end - p - 2, &kkpp_len,
188 ssl->conf->f_rng, ssl->conf->p_rng);
189 if (ret != 0) {
190 MBEDTLS_SSL_DEBUG_RET(1,
191 "mbedtls_ecjpake_write_round_one", ret);
192 return ret;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200193 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200194#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
197 if (ssl->handshake->ecjpake_cache == NULL) {
198 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
199 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200200 }
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200203 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 } else {
205 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200206
207 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200211 }
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100214 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200215
216 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200219}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200220#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000221
Hanno Beckera0e20d02019-05-15 14:03:01 +0100222#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200223MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100224static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
225 unsigned char *buf,
226 const unsigned char *end,
227 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100228{
229 unsigned char *p = buf;
230 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100231
232 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100233 * struct {
234 * opaque cid<0..2^8-1>;
235 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100237
238 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
240 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
241 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100242 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100244
245 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
246 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100248
249 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100251 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100252 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100254 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100255
256 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100258
259 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100262}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100263#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200266MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100267static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
268 unsigned char *buf,
269 const unsigned char *end,
270 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200271{
272 unsigned char *p = buf;
273
Simon Butcher0fc94e92015-09-28 20:52:04 +0100274 *olen = 0;
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
277 return 0;
278 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 MBEDTLS_SSL_DEBUG_MSG(3,
281 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100286 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200287
288 *p++ = 0x00;
289 *p++ = 1;
290
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200291 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200292
293 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200300MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100301static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
302 unsigned char *buf,
303 const unsigned char *end,
304 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100305{
306 unsigned char *p = buf;
307
Simon Butcher0fc94e92015-09-28 20:52:04 +0100308 *olen = 0;
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
311 return 0;
312 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 MBEDTLS_SSL_DEBUG_MSG(3,
315 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100320 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100321
322 *p++ = 0x00;
323 *p++ = 0x00;
324
325 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200332MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100333static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
334 unsigned char *buf,
335 const unsigned char *end,
336 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200337{
338 unsigned char *p = buf;
339
Simon Butcher0fc94e92015-09-28 20:52:04 +0100340 *olen = 0;
341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
343 return 0;
344 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 MBEDTLS_SSL_DEBUG_MSG(3,
347 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100352 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200353
354 *p++ = 0x00;
355 *p++ = 0x00;
356
357 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200364MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100365static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
366 unsigned char *buf,
367 const unsigned char *end,
368 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200369{
370 unsigned char *p = buf;
371 size_t tlen = ssl->session_negotiate->ticket_len;
372
Simon Butcher0fc94e92015-09-28 20:52:04 +0100373 *olen = 0;
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
376 return 0;
377 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 MBEDTLS_SSL_DEBUG_MSG(3,
380 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200381
Hanno Becker261602c2017-04-12 14:54:42 +0100382 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100386 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100389 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200390
391 *olen = 4;
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
394 return 0;
395 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 MBEDTLS_SSL_DEBUG_MSG(3,
398 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200401
402 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200407
Ron Eldora9788042018-12-05 11:04:31 +0200408#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200409MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100410static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
411 unsigned char *buf,
412 const unsigned char *end,
413 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100414{
415 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200416 size_t protection_profiles_index = 0, ext_len = 0;
417 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100418
419 *olen = 0;
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
422 (ssl->conf->dtls_srtp_profile_list == NULL) ||
423 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
424 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100425 }
426
Ron Eldora9788042018-12-05 11:04:31 +0200427 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100428 * uint8 SRTPProtectionProfile[2];
429 *
430 * struct {
431 * SRTPProtectionProfiles SRTPProtectionProfiles;
432 * opaque srtp_mki<0..255>;
433 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100434 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100435 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200437 mki_len = ssl->dtls_srtp_info.mki_len;
438 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300439 /* Extension length = 2 bytes for profiles length,
440 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
441 * 1 byte for srtp_mki vector length and the mki_len value
442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200446
447 /* Check there is room in the buffer for the extension + 4 bytes
448 * - the extension tag (2 bytes)
449 * - the extension length (2 bytes)
450 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100454 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100457 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100458
Ron Eldor3adb9922017-12-21 10:15:08 +0200459 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200460 /* micro-optimization:
461 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
462 * which is lower than 127, so the upper byte of the length is always 0
463 * For the documentation, the more generic code is left in comments
464 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
465 * >> 8 ) & 0xFF );
466 */
467 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300471 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200473 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
475 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
476 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
477 profile_value));
478 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100479 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200481 /*
482 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200483 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200484 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 MBEDTLS_SSL_DEBUG_MSG(3,
486 ("client hello, "
487 "illegal DTLS-SRTP protection profile %d",
488 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
489 ));
490 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100491 }
492 }
493
Ron Eldor591f1622018-01-22 12:30:04 +0200494 *p++ = mki_len & 0xFF;
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (mki_len != 0) {
497 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200498 /*
499 * Increment p to point to the current position.
500 */
501 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
503 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200504 }
505
Ron Eldoref72faf2018-07-12 11:54:20 +0300506 /*
507 * total extension length: extension type (2 bytes)
508 * + extension length (2 bytes)
509 * + protection profile length (2 bytes)
510 * + 2 * number of protection profiles
511 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200512 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300513 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200514 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100517}
518#endif /* MBEDTLS_SSL_DTLS_SRTP */
519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
521 unsigned char *buf,
522 const unsigned char *end,
523 int uses_ec,
524 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100525{
526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
527 unsigned char *p = buf;
528 size_t ext_len = 0;
529
530 (void) ssl;
531 (void) end;
532 (void) uses_ec;
533 (void) ret;
534 (void) ext_len;
535
536 *out_len = 0;
537
538 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
539 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
540#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
542 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
543 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100544 }
545 p += ext_len;
546#endif
547
548#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
549 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (uses_ec) {
551 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
552 &ext_len)) != 0) {
553 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
554 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100555 }
556 p += ext_len;
557 }
558#endif
559
560#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
562 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
563 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100564 }
565 p += ext_len;
566#endif
567
568#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
570 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
571 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100572 }
573 p += ext_len;
574#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
575
576#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
578 &ext_len)) != 0) {
579 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
580 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100581 }
582 p += ext_len;
583#endif
584
585#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
587 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
588 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100589 }
590 p += ext_len;
591#endif
592
593#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
595 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
596 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100597 }
598 p += ext_len;
599#endif
600
601#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
603 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
604 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100605 }
606 p += ext_len;
607#endif
608
609#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
611 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
612 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100613 }
614 p += ext_len;
615#endif
616
617 *out_len = p - buf;
618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100620}
621
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200622MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100623static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
624 const unsigned char *buf,
625 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100629 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000631 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 mbedtls_ct_memcmp(buf + 1,
633 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
634 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
635 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
636 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100637 mbedtls_ssl_send_alert_message(
638 ssl,
639 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
641 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000642 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100645 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (len != 1 || buf[0] != 0x00) {
647 MBEDTLS_SSL_DEBUG_MSG(1,
648 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100649 mbedtls_ssl_send_alert_message(
650 ssl,
651 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
653 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100654 }
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100657 }
Paul Bakker48916f92012-09-16 19:57:18 +0000658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000660}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200663MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100664static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
665 const unsigned char *buf,
666 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200667{
668 /*
669 * server should use the extension only if we did,
670 * and if so the server's value should match ours (and len is always 1)
671 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200673 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 buf[0] != ssl->conf->mfl_code) {
675 MBEDTLS_SSL_DEBUG_MSG(1,
676 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100677 mbedtls_ssl_send_alert_message(
678 ssl,
679 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
681 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200682 }
683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000687
Hanno Beckera0e20d02019-05-15 14:03:01 +0100688#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200689MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100690static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
691 const unsigned char *buf,
692 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100693{
694 size_t peer_cid_len;
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100697 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
698 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
700 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
701 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
702 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
703 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100704 }
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (len == 0) {
707 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
708 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
709 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
710 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100711 }
712
713 peer_cid_len = *buf++;
714 len--;
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
717 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
718 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
719 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
720 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100721 }
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (len != peer_cid_len) {
724 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
725 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
726 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
727 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100728 }
729
Hanno Becker5a299902019-05-03 12:47:49 +0100730 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100731 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
735 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100738}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100739#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200742MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100743static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
744 const unsigned char *buf,
745 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100746{
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
748 len != 0) {
749 MBEDTLS_SSL_DEBUG_MSG(1,
750 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100751 mbedtls_ssl_send_alert_message(
752 ssl,
753 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
755 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100756 }
757
758 ((void) buf);
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100763}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200767MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100768static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
769 const unsigned char *buf,
770 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200771{
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
773 len != 0) {
774 MBEDTLS_SSL_DEBUG_MSG(1,
775 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100776 mbedtls_ssl_send_alert_message(
777 ssl,
778 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
780 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200781 }
782
783 ((void) buf);
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200792MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100793static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
794 const unsigned char *buf,
795 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200796{
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
798 len != 0) {
799 MBEDTLS_SSL_DEBUG_MSG(1,
800 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100801 mbedtls_ssl_send_alert_message(
802 ssl,
803 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
805 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200806 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200807
808 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200809
810 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200813}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200815
Robert Cragie136884c2015-10-02 13:34:31 +0100816#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100817 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200818MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100819static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
820 const unsigned char *buf,
821 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200822{
823 size_t list_size;
824 const unsigned char *p;
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (len == 0 || (size_t) (buf[0] + 1) != len) {
827 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
828 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
829 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
830 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200831 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200832 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200833
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200834 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 while (list_size > 0) {
836 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
837 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Valerio Setti46423162023-03-27 14:33:27 +0200838#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200839 ssl->handshake->ecdh_ctx.point_format = p[0];
Valerio Setti77a904c2023-03-24 07:28:49 +0100840#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_ECDH_C */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200841#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
843 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
844 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200845#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
847 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200848 }
849
850 list_size--;
851 p++;
852 }
853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
855 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
856 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
857 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200858}
Darryl Green11999bb2018-03-13 15:22:58 +0000859#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100860 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200861
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200862#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200863MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100864static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
865 const unsigned char *buf,
866 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200867{
Janos Follath865b3eb2019-12-16 11:46:15 +0000868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (ssl->handshake->ciphersuite_info->key_exchange !=
871 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
872 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
873 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200874 }
875
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200876 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200878 ssl->handshake->ecjpake_cache = NULL;
879 ssl->handshake->ecjpake_cache_len = 0;
880
Neil Armstrongca7d5062022-05-31 14:43:23 +0200881#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if ((ret = mbedtls_psa_ecjpake_read_round(
883 &ssl->handshake->psa_pake_ctx, buf, len,
884 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
885 psa_destroy_key(ssl->handshake->psa_pake_password);
886 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100889 mbedtls_ssl_send_alert_message(
890 ssl,
891 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
893 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200894 }
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 return 0;
897#else
898 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
899 buf, len)) != 0) {
900 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
901 mbedtls_ssl_send_alert_message(
902 ssl,
903 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
904 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
905 return ret;
906 }
907
908 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200909#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200910}
911#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200914MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100915static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
916 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200917{
918 size_t list_len, name_len;
919 const char **p;
920
921 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (ssl->conf->alpn_list == NULL) {
923 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100924 mbedtls_ssl_send_alert_message(
925 ssl,
926 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
928 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200929 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200930
931 /*
932 * opaque ProtocolName<1..2^8-1>;
933 *
934 * struct {
935 * ProtocolName protocol_name_list<2..2^16-1>
936 * } ProtocolNameList;
937 *
938 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
939 */
940
941 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (len < 4) {
943 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
944 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
945 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200946 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 list_len = (buf[0] << 8) | buf[1];
949 if (list_len != len - 2) {
950 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
951 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
952 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200953 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200954
955 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (name_len != list_len - 1) {
957 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
958 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
959 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200960 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200961
962 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
964 if (name_len == strlen(*p) &&
965 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200966 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200968 }
969 }
970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
972 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
973 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
974 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200975}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200977
Johan Pascalb62bb512015-12-03 21:56:45 +0100978#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200979MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100980static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
981 const unsigned char *buf,
982 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100983{
Johan Pascal43f94902020-09-22 12:25:52 +0200984 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200985 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100986 uint16_t server_protection_profile_value = 0;
987
988 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
990 (ssl->conf->dtls_srtp_profile_list == NULL) ||
991 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
992 return 0;
993 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100994
Ron Eldora9788042018-12-05 11:04:31 +0200995 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100996 * uint8 SRTPProtectionProfile[2];
997 *
998 * struct {
999 * SRTPProtectionProfiles SRTPProtectionProfiles;
1000 * opaque srtp_mki<0..255>;
1001 * } UseSRTPData;
1002
1003 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1004 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001007 mki_len = ssl->dtls_srtp_info.mki_len;
1008 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001009
Ron Eldoref72faf2018-07-12 11:54:20 +03001010 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001011 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1012 * + protection profile (2 bytes)
1013 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001014 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if ((len < 5) || (len != (buf[4] + 5u))) {
1017 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1018 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001019
1020 /*
1021 * get the server protection profile
1022 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001023
1024 /*
1025 * protection profile length must be 0x0002 as we must have only
1026 * one protection profile in server Hello
1027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if ((buf[0] != 0) || (buf[1] != 2)) {
1029 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1030 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001033 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 server_protection_profile_value);
1035 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1036 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1037 mbedtls_ssl_get_srtp_profile_as_string(
1038 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001039 }
1040
Johan Pascal43f94902020-09-22 12:25:52 +02001041 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001042
Johan Pascalb62bb512015-12-03 21:56:45 +01001043 /*
1044 * Check we have the server profile in our list
1045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1047 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001048 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001050 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001052 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001053 }
1054 }
1055
Ron Eldor591f1622018-01-22 12:30:04 +02001056 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1058 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1059 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1060 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001061 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001062
1063 /* If server does not use mki in its reply, make sure the client won't keep
1064 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001066 ssl->dtls_srtp_info.mki_len = 0;
1067 }
1068
Ron Eldoref72faf2018-07-12 11:54:20 +03001069 /*
1070 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001071 * If the client detects a nonzero-length MKI in the server's response
1072 * that is different than the one the client offered, then the client
1073 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1074 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (len > 5 && (buf[4] != mki_len ||
1076 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1077 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1078 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1079 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001080 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001081#if defined(MBEDTLS_DEBUG_C)
1082 if (len > 5) {
1083 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1084 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001085 }
1086#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001088}
1089#endif /* MBEDTLS_SSL_DTLS_SRTP */
1090
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001091/*
1092 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001095MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001096static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001097{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001098 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001100 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001101
1102#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1103 uint8_t cookie_len;
1104#else
1105 uint16_t cookie_len;
1106#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001109
Gilles Peskineb64bf062019-09-27 14:02:44 +02001110 /* Check that there is enough room for:
1111 * - 2 bytes of version
1112 * - 1 byte of cookie_len
1113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1115 MBEDTLS_SSL_DEBUG_MSG(1,
1116 ("incoming HelloVerifyRequest message is too short"));
1117 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1118 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1119 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001120 }
1121
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001122 /*
1123 * struct {
1124 * ProtocolVersion server_version;
1125 * opaque cookie<0..2^8-1>;
1126 * } HelloVerifyRequest;
1127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1129 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001130 p += 2;
1131
TRodziewicz2d8800e2021-05-13 19:14:19 +02001132 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001133 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1134 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1135 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1138 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1141 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001144 }
1145
1146 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1148 MBEDTLS_SSL_DEBUG_MSG(1,
1149 ("cookie length does not match incoming message size"));
1150 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1151 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1152 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001153 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1159 if (ssl->handshake->cookie == NULL) {
1160 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1161 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001162 }
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001165 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001166
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001167 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001169 ret = mbedtls_ssl_reset_checksum(ssl);
1170 if (0 != ret) {
1171 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
1172 return ret;
1173 }
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001180}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001182
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001183MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001184static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001185{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001186 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001187 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001188 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001189 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001190 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001192 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001193#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001194 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001200 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1202 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 }
1204
Hanno Becker79594fd2019-05-08 09:38:41 +01001205 buf = ssl->in_msg;
1206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001210 ssl->renego_records_seen++;
1211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (ssl->conf->renego_max_records >= 0 &&
1213 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1214 MBEDTLS_SSL_DEBUG_MSG(1,
1215 ("renegotiation requested, but not honored by server"));
1216 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001217 }
1218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 MBEDTLS_SSL_DEBUG_MSG(1,
1220 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001221
1222 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001224 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001228 mbedtls_ssl_send_alert_message(
1229 ssl,
1230 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1232 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 }
1234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1237 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1238 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1239 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1240 return ssl_parse_hello_verify_request(ssl);
1241 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001242 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001244 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001245 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001246 }
1247 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1251 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1252 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1253 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1254 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1255 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 }
1257
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001258 /*
1259 * 0 . 1 server_version
1260 * 2 . 33 random (maybe including 4 bytes of Unix time)
1261 * 34 . 34 session_id length = n
1262 * 35 . 34+n session_id
1263 * 35+n . 36+n cipher_suite
1264 * 37+n . 37+n compression_method
1265 *
1266 * 38+n . 39+n extensions length (optional)
1267 * 40+n . .. extensions
1268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
1272 ssl->tls_version = mbedtls_ssl_read_version(buf, ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001273 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (ssl->tls_version < ssl->conf->min_tls_version ||
1276 ssl->tls_version > ssl->conf->max_tls_version) {
1277 MBEDTLS_SSL_DEBUG_MSG(1,
1278 (
1279 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1280 (unsigned) ssl->conf->min_tls_version,
1281 (unsigned) ssl->tls_version,
1282 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1285 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001288 }
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1291 ((unsigned long) buf[2] << 24) |
1292 ((unsigned long) buf[3] << 16) |
1293 ((unsigned long) buf[4] << 8) |
1294 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001298 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (n > 32) {
1303 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1304 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1305 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1306 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001307 }
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
1310 ext_len = ((buf[38 + n] << 8)
1311 | (buf[39 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if ((ext_len > 0 && ext_len < 4) ||
1314 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1315 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001316 mbedtls_ssl_send_alert_message(
1317 ssl,
1318 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1320 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001321 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001323 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 } else {
1325 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1326 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1327 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1328 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001331 /* ciphersuite (used later) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 i = (buf[35 + n] << 8) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001333
1334 /*
1335 * Read and check compression
1336 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001337 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1340 MBEDTLS_SSL_DEBUG_MSG(1,
1341 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001342 mbedtls_ssl_send_alert_message(
1343 ssl,
1344 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1346 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001347 }
1348
Paul Bakker380da532012-04-18 16:10:25 +00001349 /*
1350 * Initialize update checksum functions
1351 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1353 if (ssl->handshake->ciphersuite_info == NULL) {
1354 MBEDTLS_SSL_DEBUG_MSG(1,
1355 ("ciphersuite info for %04x not found", (unsigned int) i));
1356 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1357 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1358 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001359 }
Paul Bakker380da532012-04-18 16:10:25 +00001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1364 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
1366 /*
1367 * Check if the session can be resumed
1368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_SSL_RENEGOTIATION)
1371 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001372#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001373 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001374 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001377 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001380#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001381 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001382 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 memcpy(ssl->session_negotiate->id, buf + 35, n);
1384 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 }
1387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1389 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1392 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1393 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Andrzej Kurek03bac442018-04-25 05:06:07 -04001395 /*
1396 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001397 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 while (1) {
1400 if (ssl->conf->ciphersuite_list[i] == 0) {
1401 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001402 mbedtls_ssl_send_alert_message(
1403 ssl,
1404 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1406 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 }
1408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 if (ssl->conf->ciphersuite_list[i++] ==
1410 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001412 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 }
1414
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001415 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 ssl->session_negotiate->ciphersuite);
1417 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1418 ssl->tls_version) != 0) {
1419 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001420 mbedtls_ssl_send_alert_message(
1421 ssl,
1422 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1424 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001425 }
1426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 MBEDTLS_SSL_DEBUG_MSG(3,
1428 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001429
Gilles Peskineeccd8882020-03-10 12:19:08 +01001430#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1432 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001433 ssl->handshake->ecrs_enabled = 1;
1434 }
1435#endif
1436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1438 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001439 mbedtls_ssl_send_alert_message(
1440 ssl,
1441 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1443 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 }
1445
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001446 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 MBEDTLS_SSL_DEBUG_MSG(2,
1449 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1450 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 while (ext_len) {
1453 unsigned int ext_id = ((ext[0] << 8)
1454 | (ext[1]));
1455 unsigned int ext_size = ((ext[2] << 8)
1456 | (ext[3]));
Paul Bakker48916f92012-09-16 19:57:18 +00001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (ext_size + 4 > ext_len) {
1459 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001460 mbedtls_ssl_send_alert_message(
1461 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1463 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001464 }
1465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 switch (ext_id) {
1467 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1468 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001471#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1474 ext_size)) != 0) {
1475 return ret;
1476 }
Paul Bakker48916f92012-09-16 19:57:18 +00001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1482 MBEDTLS_SSL_DEBUG_MSG(3,
1483 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1486 ext + 4, ext_size)) != 0) {
1487 return ret;
1488 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001492
Hanno Beckera0e20d02019-05-15 14:03:01 +01001493#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 case MBEDTLS_TLS_EXT_CID:
1495 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if ((ret = ssl_parse_cid_ext(ssl,
1498 ext + 4,
1499 ext_size)) != 0) {
1500 return ret;
1501 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001504#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1508 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1511 ext + 4, ext_size)) != 0) {
1512 return ret;
1513 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1520 MBEDTLS_SSL_DEBUG_MSG(3,
1521 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if ((ret = ssl_parse_extended_ms_ext(ssl,
1524 ext + 4, ext_size)) != 0) {
1525 return ret;
1526 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1533 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if ((ret = ssl_parse_session_ticket_ext(ssl,
1536 ext + 4, ext_size)) != 0) {
1537 return ret;
1538 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001542
Robert Cragie136884c2015-10-02 13:34:31 +01001543#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1545 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1546 MBEDTLS_SSL_DEBUG_MSG(3,
1547 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1550 ext + 4, ext_size)) != 0) {
1551 return ret;
1552 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001555#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1556 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001557
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001558#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1560 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1563 ext + 4, ext_size)) != 0) {
1564 return ret;
1565 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001568#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 case MBEDTLS_TLS_EXT_ALPN:
1572 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1575 return ret;
1576 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001580
Johan Pascalb62bb512015-12-03 21:56:45 +01001581#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 case MBEDTLS_TLS_EXT_USE_SRTP:
1583 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1586 return ret;
1587 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001590#endif /* MBEDTLS_SSL_DTLS_SRTP */
1591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 default:
1593 MBEDTLS_SSL_DEBUG_MSG(3,
1594 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001595 }
1596
1597 ext_len -= 4 + ext_size;
1598 ext += 4 + ext_size;
1599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (ext_len > 0 && ext_len < 4) {
1601 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1602 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001603 }
1604 }
1605
1606 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001607 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1608 * extensions. It sets the transform data for the resumed session which in
1609 * case of DTLS includes the server CID extracted from the CID extension.
1610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (ssl->handshake->resume) {
1612 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1613 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001614 mbedtls_ssl_send_alert_message(
1615 ssl,
1616 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1618 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001619 }
1620 }
1621
Paul Bakker48916f92012-09-16 19:57:18 +00001622 /*
1623 * Renegotiation security checks
1624 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001626 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1628 MBEDTLS_SSL_DEBUG_MSG(1,
1629 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001630 handshake_failure = 1;
1631 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 renegotiation_info_seen == 0) {
1636 MBEDTLS_SSL_DEBUG_MSG(1,
1637 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +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 ssl->conf->allow_legacy_renegotiation ==
1642 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1643 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001644 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1646 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1647 renegotiation_info_seen == 1) {
1648 MBEDTLS_SSL_DEBUG_MSG(1,
1649 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001650 handshake_failure = 1;
1651 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001655 mbedtls_ssl_send_alert_message(
1656 ssl,
1657 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1659 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001665}
1666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1668 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001669MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001670static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1671 unsigned char **p,
1672 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001673{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001675 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001676
Paul Bakker29e1f122013-04-16 13:07:56 +02001677 /*
1678 * Ephemeral DH parameters:
1679 *
1680 * struct {
1681 * opaque dh_p<1..2^16-1>;
1682 * opaque dh_g<1..2^16-1>;
1683 * opaque dh_Ys<1..2^16-1>;
1684 * } ServerDHParams;
1685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1687 p, end)) != 0) {
1688 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1689 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001690 }
1691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1693 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1694 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1695 dhm_actual_bitlen,
1696 ssl->conf->dhm_min_bitlen));
1697 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001698 }
1699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1701 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1702 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1707 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001708
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001709#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001710#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1711 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1712 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001713MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001714static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1715 unsigned char **p,
1716 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001717{
1718 uint16_t tls_id;
1719 uint8_t ecpoint_len;
1720 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001721 psa_ecc_family_t ec_psa_family = 0;
1722 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001723
1724 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001725 * struct {
1726 * ECParameters curve_params;
1727 * ECPoint public;
1728 * } ServerECDHParams;
1729 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001730 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001731 * 2..3 NamedCurve
1732 * 4 ECPoint.len
1733 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001734 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 if (end - *p < 4) {
1736 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1737 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001738
1739 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1741 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1742 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001743
1744 /* Next two bytes are the namedcurve value */
1745 tls_id = *(*p)++;
1746 tls_id <<= 8;
1747 tls_id |= *(*p)++;
1748
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001749 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1751 MBEDTLS_SSL_DEBUG_MSG(2,
1752 ("bad server key exchange message (ECDHE curve): %u",
1753 (unsigned) tls_id));
1754 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001755 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001756
Valerio Setti40d9ca92023-01-04 16:08:04 +01001757 /* Convert EC's TLS ID to PSA key type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ec_psa_family,
1759 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1760 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001761 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001763 handshake->ecdh_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001764
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001765 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001766 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if ((size_t) (end - *p) < ecpoint_len) {
1768 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1769 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (ecpoint_len > sizeof(handshake->ecdh_psa_peerkey)) {
1772 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1773 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 memcpy(handshake->ecdh_psa_peerkey, *p, ecpoint_len);
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001776 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001777 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001780}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001781#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1782 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1783 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001784#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001785#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1786 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1787 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1788 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1789 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001790MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001791static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001792{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001793 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001794 mbedtls_ecp_group_id grp_id;
1795#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1796 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1797#else
1798 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1799#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1802 if (tls_id == 0) {
1803 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1804 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001805 }
1806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1808 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1811 return -1;
1812 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1815 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001818}
1819
Andrzej Kurek468c5062022-10-24 10:30:14 -04001820#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1821 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1822 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1823 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1824 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1825
1826#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1827 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1828 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001829MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001830static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1831 unsigned char **p,
1832 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001835
Paul Bakker29e1f122013-04-16 13:07:56 +02001836 /*
1837 * Ephemeral ECDH parameters:
1838 *
1839 * struct {
1840 * ECParameters curve_params;
1841 * ECPoint public;
1842 * } ServerECDHParams;
1843 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1845 (const unsigned char **) p, end)) != 0) {
1846 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001847#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001849 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001851#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001853 }
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 if (ssl_check_server_ecdh_params(ssl) != 0) {
1856 MBEDTLS_SSL_DEBUG_MSG(1,
1857 ("bad server key exchange message (ECDHE curve)"));
1858 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001859 }
1860
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001862}
Gilles Peskine449bd832023-01-11 14:50:10 +01001863#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1864 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001865 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1866#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001867#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001868MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001869static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1870 unsigned char **p,
1871 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001872{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001874 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001875 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001876
1877 /*
1878 * PSK parameters:
1879 *
1880 * opaque psk_identity_hint<0..2^16-1>;
1881 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 if (end - (*p) < 2) {
1883 MBEDTLS_SSL_DEBUG_MSG(1,
1884 ("bad server key exchange message (psk_identity_hint length)"));
1885 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001886 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001887 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001888 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (end - (*p) < len) {
1891 MBEDTLS_SSL_DEBUG_MSG(1,
1892 ("bad server key exchange message (psk_identity_hint length)"));
1893 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001894 }
1895
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001896 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001897 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001898 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001899 * someone needs that feature.
1900 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001901 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001902 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001905}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001906#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1909 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001910/*
1911 * Generate a pre-master secret and encrypt it with the server's RSA key
1912 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001913MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001914static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1915 size_t offset, size_t *olen,
1916 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001917{
Janos Follath865b3eb2019-12-16 11:46:15 +00001918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001919 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001920 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1924 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1925 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001926 }
1927
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001928 /*
1929 * Generate (part of) the pre-master as
1930 * struct {
1931 * ProtocolVersion client_version;
1932 * opaque random[46];
1933 * } PreMasterSecret;
1934 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 mbedtls_ssl_write_version(p, ssl->conf->transport,
1936 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1939 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1940 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001941 }
1942
1943 ssl->handshake->pmslen = 48;
1944
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001945#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1946 peer_pk = &ssl->handshake->peer_pubkey;
1947#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001949 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1951 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001952 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001953 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1954#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001955
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001956 /*
1957 * Now write it out, encrypted
1958 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1960 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1961 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001962 }
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 if ((ret = mbedtls_pk_encrypt(peer_pk,
1965 p, ssl->handshake->pmslen,
1966 ssl->out_msg + offset + len_bytes, olen,
1967 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1968 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1969 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1970 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001971 }
1972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (len_bytes == 2) {
1974 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001975 *olen += 2;
1976 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001977
Hanno Beckerae553dd2019-02-08 14:06:00 +00001978#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1979 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001981#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
1985 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1988 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001989MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001990static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001991{
Janos Follath865b3eb2019-12-16 11:46:15 +00001992 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001994
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001995#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1996 peer_pk = &ssl->handshake->peer_pubkey;
1997#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001999 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2001 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002002 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002003 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2004#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002005
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002006 /* This is a public key, so it can't be opaque, so can_do() is a good
2007 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2009 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2010 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002011 }
2012
Valerio Setti97207782023-05-18 18:59:06 +02002013#if defined(MBEDTLS_ECP_C)
2014 const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
2015#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002016
Przemek Stekielea4000f2022-03-16 09:49:33 +01002017#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002018 uint16_t tls_id = 0;
2019 psa_ecc_family_t ecc_family;
Valerio Setti97207782023-05-18 18:59:06 +02002020 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_group_id(peer_pk);
Przemek Stekiel561a4232022-03-16 13:16:24 +01002021
Valerio Setti97207782023-05-18 18:59:06 +02002022 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2024 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002025 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002026
Valerio Setti97207782023-05-18 18:59:06 +02002027 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (tls_id == 0) {
2029 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
Valerio Setti97207782023-05-18 18:59:06 +02002030 grp_id));
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002032 }
2033
Valerio Setti1e868cc2023-01-09 17:30:01 +01002034 /* If the above conversion to TLS ID was fine, then also this one will be,
2035 so there is no need to check the return value here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ecc_family,
2037 &ssl->handshake->ecdh_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 ssl->handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ecc_family);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002040
Przemek Stekielea4000f2022-03-16 09:49:33 +01002041 /* Store peer's public key in psa format. */
Valerio Settid7ca3952023-05-17 15:36:18 +02002042#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
2043 memcpy(ssl->handshake->ecdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
2044 ssl->handshake->ecdh_psa_peerkey_len = peer_pk->pub_raw_len;
2045 ret = 0;
Valerio Setti97207782023-05-18 18:59:06 +02002046#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settid7ca3952023-05-17 15:36:18 +02002047 size_t olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2049 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2050 ssl->handshake->ecdh_psa_peerkey,
2051 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 if (ret != 0) {
2054 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2055 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002056 }
Przemek Stekiel561a4232022-03-16 13:16:24 +01002057 ssl->handshake->ecdh_psa_peerkey_len = olen;
Valerio Setti97207782023-05-18 18:59:06 +02002058#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2059#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2061 MBEDTLS_ECDH_THEIRS)) != 0) {
2062 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2063 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002064 }
2065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if (ssl_check_server_ecdh_params(ssl) != 0) {
2067 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2068 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002069 }
Valerio Setti97207782023-05-18 18:59:06 +02002070#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerae553dd2019-02-08 14:06:00 +00002071#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2072 /* We don't need the peer's public key anymore. Free it,
2073 * so that more RAM is available for upcoming expensive
2074 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002076#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002079}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2081 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002082
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002083MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002084static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002085{
Janos Follath865b3eb2019-12-16 11:46:15 +00002086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002087 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002088 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002089 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2095 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002099 ((void) p);
2100 ((void) end);
2101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2104 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2106 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2107 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2108 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002109 mbedtls_ssl_send_alert_message(
2110 ssl,
2111 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2113 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002114 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002117 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002119 }
2120 ((void) p);
2121 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2123 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002124
Gilles Peskineeccd8882020-03-10 12:19:08 +01002125#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if (ssl->handshake->ecrs_enabled &&
2127 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002128 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002129 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002130#endif
2131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2133 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2134 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 }
2136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2138 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002139 mbedtls_ssl_send_alert_message(
2140 ssl,
2141 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2143 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 }
2145
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002146 /*
2147 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2148 * doesn't use a psk_identity_hint
2149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2151 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2152 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002153 /* Current message is probably either
2154 * CertificateRequest or ServerHelloDone */
2155 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002156 goto exit;
2157 }
2158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 MBEDTLS_SSL_DEBUG_MSG(1,
2160 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002161 mbedtls_ssl_send_alert_message(
2162 ssl,
2163 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 }
2168
Gilles Peskineeccd8882020-03-10 12:19:08 +01002169#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002171 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002173
2174start_processing:
2175#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002177 end = ssl->in_msg + ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, end - p);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002179
Gilles Peskineeccd8882020-03-10 12:19:08 +01002180#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2183 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2185 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2186 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002187 mbedtls_ssl_send_alert_message(
2188 ssl,
2189 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2191 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002192 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002193 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002194#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2197 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2199 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002200 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2203 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2204#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2205 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2207 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2208 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2209 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002210 mbedtls_ssl_send_alert_message(
2211 ssl,
2212 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2214 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002215 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2218 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002219#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2220 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2225 if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
2226 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002227 mbedtls_ssl_send_alert_message(
2228 ssl,
2229 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2231 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker41c83d32013-03-20 14:39:14 +01002232 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2235 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2236 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002237#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002239#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002240 /*
2241 * The first 3 bytes are:
2242 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2243 * [1, 2] elliptic curve's TLS ID
2244 *
2245 * However since we only support secp256r1 for now, we check only
2246 * that TLS ID here
2247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002249 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (exp_tls_id == 0) {
2253 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002254 }
2255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2257 (read_tls_id != exp_tls_id)) {
2258 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002259 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002260
2261 p += 3;
2262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if ((ret = mbedtls_psa_ecjpake_read_round(
2264 &ssl->handshake->psa_pake_ctx, p, end - p,
2265 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2266 psa_destroy_key(ssl->handshake->psa_pake_password);
2267 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002270 mbedtls_ssl_send_alert_message(
2271 ssl,
2272 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2274 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002275 }
2276#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2278 p, end - p);
2279 if (ret != 0) {
2280 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002281 mbedtls_ssl_send_alert_message(
2282 ssl,
2283 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2285 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002286 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002287#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002289#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002290 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2292 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002293 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002294
Gilles Peskineeccd8882020-03-10 12:19:08 +01002295#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002297 size_t sig_len, hashlen;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002298 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2301 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002303 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002304 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002305 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002308
Jerry Yu693a47a2022-06-23 14:02:28 +08002309#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2310 peer_pk = &ssl->handshake->peer_pubkey;
2311#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002313 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2315 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002316 }
2317 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2318#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2319
Paul Bakker29e1f122013-04-16 13:07:56 +02002320 /*
2321 * Handle the digitally-signed structure
2322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2324 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2325 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2326 sig_alg, &pk_alg, &md_alg) != 0 &&
2327 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2328 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2329 MBEDTLS_SSL_DEBUG_MSG(1,
2330 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002331 mbedtls_ssl_send_alert_message(
2332 ssl,
2333 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2335 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002336 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002337 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2340 MBEDTLS_SSL_DEBUG_MSG(1,
2341 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002342 mbedtls_ssl_send_alert_message(
2343 ssl,
2344 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2346 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002347 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002348
2349 /*
2350 * Read signature
2351 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (p > end - 2) {
2354 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002355 mbedtls_ssl_send_alert_message(
2356 ssl,
2357 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2359 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002360 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 sig_len = (p[0] << 8) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002362 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (p != end - sig_len) {
2365 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002366 mbedtls_ssl_send_alert_message(
2367 ssl,
2368 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2370 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002374
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002375 /*
2376 * Compute the hash that has been signed
2377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (md_alg != MBEDTLS_MD_NONE) {
2379 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2380 params, params_len,
2381 md_alg);
2382 if (ret != 0) {
2383 return ret;
2384 }
2385 } else {
2386 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2387 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002388 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002391
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002392 /*
2393 * Verify signature
2394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2396 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002397 mbedtls_ssl_send_alert_message(
2398 ssl,
2399 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2401 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002402 }
2403
Gilles Peskineeccd8882020-03-10 12:19:08 +01002404#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002406 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002408#endif
2409
Jerry Yu693a47a2022-06-23 14:02:28 +08002410#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002412 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2413 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002414 rsassa_pss_options.expected_salt_len =
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002415 mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (rsassa_pss_options.expected_salt_len == 0) {
2417 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2418 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2421 peer_pk,
2422 md_alg, hash, hashlen,
2423 p, sig_len);
2424 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002425#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 ret = mbedtls_pk_verify_restartable(peer_pk,
2427 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002430 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002431#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002433#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002435 mbedtls_ssl_send_alert_message(
2436 ssl,
2437 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2439 }
2440 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002441#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002443 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002445#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002447 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002448
2449#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2450 /* We don't need the peer's public key anymore. Free it,
2451 * so that more RAM is available for upcoming expensive
2452 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002454#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002456#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002458exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 ssl->state++;
2460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002464}
2465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002467MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002468static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002469{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002470 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002471 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2476 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002477 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002479 }
2480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2482 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002483}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002484#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002485MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002486static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002487{
Janos Follath865b3eb2019-12-16 11:46:15 +00002488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002489 unsigned char *buf;
2490 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002491 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002492 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002493 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002494 size_t sig_alg_len;
2495#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 unsigned char *sig_alg;
2497 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002498#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2503 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002504 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002506 }
2507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2509 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2510 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 }
2512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2514 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002515 mbedtls_ssl_send_alert_message(
2516 ssl,
2517 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2519 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002522 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002523 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2527 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002530 /* Current message is probably the ServerHelloDone */
2531 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002532 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002533 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002534
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002535 /*
2536 * struct {
2537 * ClientCertificateType certificate_types<1..2^8-1>;
2538 * SignatureAndHashAlgorithm
2539 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2540 * DistinguishedName certificate_authorities<0..2^16-1>;
2541 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002542 *
2543 * Since we only support a single certificate on clients, let's just
2544 * ignore all the information that's supposed to help us pick a
2545 * certificate.
2546 *
2547 * We could check that our certificate matches the request, and bail out
2548 * if it doesn't, but it's simpler to just send the certificate anyway,
2549 * and give the server the opportunity to decide if it should terminate
2550 * the connection when it doesn't like our certificate.
2551 *
2552 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2553 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002554 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002555 *
2556 * However, we still minimally parse the message to check it is at least
2557 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002558 */
Paul Bakker926af752012-11-23 13:38:07 +01002559 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002560
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002561 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2563 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2564 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2565 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2566 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002567 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002569 n = cert_type_len;
2570
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002571 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002572 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002573 * * the length of the signature algorithms field (if minor version of
2574 * SSL is 3),
2575 * * distinguished name length otherwise.
2576 * Both reach at most the index:
2577 * ...hdr_len + 2 + n,
2578 * therefore the buffer length at this point must be greater than that
2579 * regardless of the actual code path.
2580 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2582 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2583 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2584 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2585 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002586 }
2587
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002588 /* supported_signature_algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 sig_alg_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2590 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Ronald Cron90915f22022-03-07 11:11:36 +01002591
2592 /*
2593 * The furthest access in buf is in the loop few lines below:
2594 * sig_alg[i + 1],
2595 * where:
2596 * sig_alg = buf + ...hdr_len + 3 + n,
2597 * max(i) = sig_alg_len - 1.
2598 * Therefore the furthest access is:
2599 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2600 * which reduces to:
2601 * buf[...hdr_len + 3 + n + sig_alg_len],
2602 * which is one less than we need the buf to be.
2603 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2605 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002606 mbedtls_ssl_send_alert_message(
2607 ssl,
2608 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2610 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002611 }
Paul Bakker926af752012-11-23 13:38:07 +01002612
Ronald Cron90915f22022-03-07 11:11:36 +01002613#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2615 for (size_t i = 0; i < sig_alg_len; i += 2) {
2616 MBEDTLS_SSL_DEBUG_MSG(3,
2617 ("Supported Signature Algorithm found: %02x %02x",
2618 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002619 }
2620#endif
2621
2622 n += 2 + sig_alg_len;
2623
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002624 /* certificate_authorities */
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 dn_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2626 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Paul Bakker926af752012-11-23 13:38:07 +01002627
2628 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2630 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2631 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2632 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2633 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002634 }
2635
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002636#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2638 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002639 unsigned char *p = dn + i + 2;
2640 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002641 size_t asn1_len;
2642 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 memset(&name, 0, sizeof(name));
2644 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2645 if (dni_len > dn_len - i - 2 ||
2646 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2647 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2648 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2649 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002650 mbedtls_ssl_send_alert_message(
2651 ssl,
2652 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2654 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002655 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 MBEDTLS_SSL_DEBUG_MSG(3,
2657 ("DN hint: %.*s",
2658 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2659 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002660 }
2661#endif
2662
Paul Bakker926af752012-11-23 13:38:07 +01002663exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002667}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002668#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002670MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002671static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002672{
Janos Follath865b3eb2019-12-16 11:46:15 +00002673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2678 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2679 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2683 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2684 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2688 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2689 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2690 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2691 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2692 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 }
2694
2695 ssl->state++;
2696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2699 mbedtls_ssl_recv_flight_completed(ssl);
2700 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002701#endif
2702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002706}
2707
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002708MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002709static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002710{
Janos Follath865b3eb2019-12-16 11:46:15 +00002711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002712
2713 size_t header_len;
2714 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002715 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002716 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 /*
2723 * DHM key exchange -- send G^X mod P
2724 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002728 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2731 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2732 &ssl->out_msg[header_len], content_len,
2733 ssl->conf->f_rng, ssl->conf->p_rng);
2734 if (ret != 0) {
2735 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2736 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 }
2738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2740 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2743 ssl->handshake->premaster,
2744 MBEDTLS_PREMASTER_SIZE,
2745 &ssl->handshake->pmslen,
2746 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2747 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2748 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 }
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2752 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002754#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2755 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2756 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2757 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002759 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2760 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002762#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002763 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2764 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002765 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002766
2767 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2768
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002769 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002772
Hanno Becker4a63ed42019-01-08 11:39:35 +00002773 /*
2774 * Generate EC private key for ECDHE exchange.
2775 */
2776
Hanno Becker4a63ed42019-01-08 11:39:35 +00002777 /* The master secret is obtained from the shared ECDH secret by
2778 * applying the TLS 1.2 PRF with a specific salt and label. While
2779 * the PSA Crypto API encourages combining key agreement schemes
2780 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2781 * yet support the provisioning of salt + label to the KDF.
2782 * For the time being, we therefore need to split the computation
2783 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002784 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2786 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2787 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2788 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002789
2790 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 status = psa_generate_key(&key_attributes,
2792 &handshake->ecdh_psa_privkey);
2793 if (status != PSA_SUCCESS) {
2794 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2795 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002796
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002797 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002798 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002799 * but we just need to add a length byte before that. */
2800 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2801 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002803 size_t own_pubkey_len;
2804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2806 own_pubkey, own_pubkey_max_len,
2807 &own_pubkey_len);
2808 if (status != PSA_SUCCESS) {
2809 psa_destroy_key(handshake->ecdh_psa_privkey);
Andrzej Kureka0237f82022-02-24 13:24:52 -05002810 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002812 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002813
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002814 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2815 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002816
Hanno Becker4a63ed42019-01-08 11:39:35 +00002817 /* The ECDH secret is the premaster secret used for key derivation. */
2818
Janos Follathdf3b0892019-08-08 11:12:24 +01002819 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2821 handshake->ecdh_psa_privkey,
2822 handshake->ecdh_psa_peerkey,
2823 handshake->ecdh_psa_peerkey_len,
2824 ssl->handshake->premaster,
2825 sizeof(ssl->handshake->premaster),
2826 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002827
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Ronald Croncf56a0a2020-08-04 09:51:30 +02002829 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2832 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2833 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002834#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002835 /*
2836 * ECDH key exchange -- send client public value
2837 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002838 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002839
Gilles Peskineeccd8882020-03-10 12:19:08 +01002840#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 if (ssl->handshake->ecrs_enabled) {
2842 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002843 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002847 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002848#endif
2849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2851 &content_len,
2852 &ssl->out_msg[header_len], 1000,
2853 ssl->conf->f_rng, ssl->conf->p_rng);
2854 if (ret != 0) {
2855 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002856#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002858 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002860#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002862 }
2863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2865 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002866
Gilles Peskineeccd8882020-03-10 12:19:08 +01002867#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002869 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002870 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002871 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002872
2873ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002875 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002877#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2879 &ssl->handshake->pmslen,
2880 ssl->handshake->premaster,
2881 MBEDTLS_MPI_MAX_SIZE,
2882 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2883 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002884#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002886 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002888#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002890 }
2891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2893 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002894#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2897 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2898 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2899 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002900#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2901 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2904 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2905 psa_key_attributes_t key_attributes;
2906
2907 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2908
2909 /*
2910 * opaque psk_identity<0..2^16-1>;
2911 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002913 /* We don't offer PSK suites if we don't have a PSK,
2914 * and we check that the server's choice is among the
2915 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2917 }
Neil Armstrong868af822022-03-09 10:26:25 +01002918
Neil Armstrongfc834f22022-03-23 17:54:38 +01002919 /* uint16 to store content length */
2920 const size_t content_len_size = 2;
2921
Neil Armstrong868af822022-03-09 10:26:25 +01002922 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 if (header_len + content_len_size + ssl->conf->psk_identity_len
2925 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2926 MBEDTLS_SSL_DEBUG_MSG(1,
2927 ("psk identity too long or SSL buffer too short"));
2928 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002929 }
2930
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002931 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2934 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002935 header_len += content_len_size;
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 memcpy(p, ssl->conf->psk_identity,
2938 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002939 p += ssl->conf->psk_identity_len;
2940
Neil Armstrong868af822022-03-09 10:26:25 +01002941 header_len += ssl->conf->psk_identity_len;
2942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002944
2945 /*
2946 * Generate EC private key for ECDHE exchange.
2947 */
2948
2949 /* The master secret is obtained from the shared ECDH secret by
2950 * applying the TLS 1.2 PRF with a specific salt and label. While
2951 * the PSA Crypto API encourages combining key agreement schemes
2952 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2953 * yet support the provisioning of salt + label to the KDF.
2954 * For the time being, we therefore need to split the computation
2955 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2956 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2958 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2959 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2960 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002961
2962 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 status = psa_generate_key(&key_attributes,
2964 &handshake->ecdh_psa_privkey);
2965 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002966 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 }
Neil Armstrong868af822022-03-09 10:26:25 +01002968
2969 /* Export the public part of the ECDH private key from PSA.
2970 * The export format is an ECPoint structure as expected by TLS,
2971 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002972 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002973 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002975 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2978 own_pubkey, own_pubkey_max_len,
2979 &own_pubkey_len);
2980 if (status != PSA_SUCCESS) {
2981 psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01002982 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05002983 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002984 }
2985
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002986 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002987 content_len = own_pubkey_len + 1;
2988
Neil Armstrong25400452022-03-23 17:44:07 +01002989 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2990 * - a uint16 containing the length (in octets) of the ECDH computation
2991 * - the octet string produced by the ECDH computation
2992 * - a uint16 containing the length (in octets) of the PSK
2993 * - the PSK itself
2994 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002995 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 const unsigned char * const pms_end = pms +
2997 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002998 /* uint16 to store length (in octets) of the ECDH computation */
2999 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003000 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003001
Neil Armstrong25400452022-03-23 17:44:07 +01003002 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 status = psa_raw_key_agreement(PSA_ALG_ECDH,
3004 handshake->ecdh_psa_privkey,
3005 handshake->ecdh_psa_peerkey,
3006 handshake->ecdh_psa_peerkey_len,
3007 pms + zlen_size,
3008 pms_end - (pms + zlen_size),
3009 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01003012 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3013
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003015 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 } else if (destruction_status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05003017 return PSA_TO_MBEDTLS_ERR(destruction_status);
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 }
Neil Armstrong868af822022-03-09 10:26:25 +01003019
Neil Armstrong25400452022-03-23 17:44:07 +01003020 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003022 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003024#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3025 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003026#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003028 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003029 * opaque psk_identity<0..2^16-1>;
3030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003032 /* We don't offer PSK suites if we don't have a PSK,
3033 * and we check that the server's choice is among the
3034 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003036 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003037
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003038 header_len = 4;
3039 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3042 MBEDTLS_SSL_DEBUG_MSG(1,
3043 ("psk identity too long or SSL buffer too short"));
3044 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003045 }
3046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3048 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003049
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 memcpy(ssl->out_msg + header_len,
3051 ssl->conf->psk_identity,
3052 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003053 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003057 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3062 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3063 &content_len, 2)) != 0) {
3064 return ret;
3065 }
3066 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003070 /*
3071 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 if (header_len + 2 + content_len >
3076 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3077 MBEDTLS_SSL_DEBUG_MSG(1,
3078 ("psk identity or DHM size too long or SSL buffer too short"));
3079 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003080 }
3081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3083 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3086 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3087 &ssl->out_msg[header_len], content_len,
3088 ssl->conf->f_rng, ssl->conf->p_rng);
3089 if (ret != 0) {
3090 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3091 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003092 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003093
3094#if defined(MBEDTLS_USE_PSA_CRYPTO)
3095 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003097 size_t pms_len;
3098
3099 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3101 pms + 2, pms_end - (pms + 2), &pms_len,
3102 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3103 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3104 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003105 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003107 pms += 2 + pms_len;
3108
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003110#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003113#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3115 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003116 /*
3117 * ClientECDiffieHellmanPublic public;
3118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3120 &content_len,
3121 &ssl->out_msg[header_len],
3122 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3123 ssl->conf->f_rng, ssl->conf->p_rng);
3124 if (ret != 0) {
3125 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3126 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003127 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003128
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3130 MBEDTLS_DEBUG_ECDH_Q);
3131 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003132#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003133 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3135 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003136 }
3137
Neil Armstrong80f6f322022-05-03 17:56:38 +02003138#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3140 ciphersuite_info->key_exchange)) != 0) {
3141 MBEDTLS_SSL_DEBUG_RET(1,
3142 "mbedtls_ssl_psk_derive_premaster", ret);
3143 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003144 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003145#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003147#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003150 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3152 &content_len, 0)) != 0) {
3153 return ret;
3154 }
3155 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003157#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003159 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003160
Neil Armstrongca7d5062022-05-31 14:43:23 +02003161#if defined(MBEDTLS_USE_PSA_CRYPTO)
3162 unsigned char *out_p = ssl->out_msg + header_len;
3163 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3164 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3166 out_p, end_p - out_p, &content_len,
3167 MBEDTLS_ECJPAKE_ROUND_TWO);
3168 if (ret != 0) {
3169 psa_destroy_key(ssl->handshake->psa_pake_password);
3170 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3171 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3172 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003173 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003174#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3176 ssl->out_msg + header_len,
3177 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3178 &content_len,
3179 ssl->conf->f_rng, ssl->conf->p_rng);
3180 if (ret != 0) {
3181 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3182 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003183 }
3184
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3186 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3187 ssl->conf->f_rng, ssl->conf->p_rng);
3188 if (ret != 0) {
3189 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3190 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003191 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003192#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003194#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003195 {
3196 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3198 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003201 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3203 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
3205 ssl->state++;
3206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3208 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3209 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 }
3211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215}
3216
Gilles Peskineeccd8882020-03-10 12:19:08 +01003217#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003218MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003219static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003220{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003221 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003222 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003226
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3228 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3229 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003230 }
3231
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3233 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003234 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003236 }
3237
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3239 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003240}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003241#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003242MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003243static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003246 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003247 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003248 size_t n = 0, offset = 0;
3249 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003250 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003252 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003253 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003254#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 size_t out_buf_len = ssl->out_buf_len - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003256#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003258#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003261
Gilles Peskineeccd8882020-03-10 12:19:08 +01003262#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 if (ssl->handshake->ecrs_enabled &&
3264 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003265 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003266 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003267#endif
3268
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3270 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3271 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003272 }
3273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3275 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003276 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003278 }
3279
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 if (ssl->handshake->client_auth == 0 ||
3281 mbedtls_ssl_own_cert(ssl) == NULL) {
3282 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003283 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 }
3286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 if (mbedtls_ssl_own_key(ssl) == NULL) {
3288 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3289 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 }
3291
3292 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003293 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003295#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003297 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003299
3300sign:
3301#endif
3302
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003303 ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
3304 if (0 != ret) {
3305 MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
3306 return ret;
3307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003308
Ronald Cron90915f22022-03-07 11:11:36 +01003309 /*
3310 * digitally-signed struct {
3311 * opaque handshake_messages[handshake_messages_length];
3312 * };
3313 *
3314 * Taking shortcut here. We assume that the server always allows the
3315 * PRF Hash function and has sent it in the allowed signature
3316 * algorithms list received in the Certificate Request message.
3317 *
3318 * Until we encounter a server that does not, we will take this
3319 * shortcut.
3320 *
3321 * Reason: Otherwise we should have running hashes for SHA512 and
3322 * SHA224 in order to satisfy 'weird' needs from the server
3323 * side.
3324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003326 md_alg = MBEDTLS_MD_SHA384;
3327 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003329 md_alg = MBEDTLS_MD_SHA256;
3330 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003331 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003333
3334 /* Info from md_alg will be used instead */
3335 hashlen = 0;
3336 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003337
Gilles Peskineeccd8882020-03-10 12:19:08 +01003338#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003340 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003342#endif
3343
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3345 md_alg, hash_start, hashlen,
3346 ssl->out_msg + 6 + offset,
3347 out_buf_len - 6 - offset,
3348 &n,
3349 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3350 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003351#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003353 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003355#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003357 }
Paul Bakker926af752012-11-23 13:38:07 +01003358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003360
Paul Bakker1ef83d62012-04-11 12:09:53 +00003361 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3363 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
3365 ssl->state++;
3366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3368 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3369 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 }
3371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003375}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003376#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003379MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003380static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003381{
Janos Follath865b3eb2019-12-16 11:46:15 +00003382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003383 uint32_t lifetime;
3384 size_t ticket_len;
3385 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003386 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3391 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3392 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003393 }
3394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3396 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003397 mbedtls_ssl_send_alert_message(
3398 ssl,
3399 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3401 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003402 }
3403
3404 /*
3405 * struct {
3406 * uint32 ticket_lifetime_hint;
3407 * opaque ticket<0..2^16-1>;
3408 * } NewSessionTicket;
3409 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003410 * 0 . 3 ticket_lifetime_hint
3411 * 4 . 5 ticket_len (n)
3412 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3415 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3416 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3417 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3418 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3419 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003420 }
3421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 lifetime = (((uint32_t) msg[0]) << 24) | (msg[1] << 16) |
3425 (msg[2] << 8) | (msg[3]);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 ticket_len = (msg[4] << 8) | (msg[5]);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003428
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3430 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3431 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3432 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3433 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003434 }
3435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003437
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003438 /* We're not waiting for a NewSessionTicket message any more */
3439 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003441
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003442 /*
3443 * Zero-length ticket means the server changed his mind and doesn't want
3444 * to send a ticket after all, so just forget it
3445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 if (ticket_len == 0) {
3447 return 0;
3448 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 if (ssl->session != NULL && ssl->session->ticket != NULL) {
3451 mbedtls_platform_zeroize(ssl->session->ticket,
3452 ssl->session->ticket_len);
3453 mbedtls_free(ssl->session->ticket);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003454 ssl->session->ticket = NULL;
3455 ssl->session->ticket_len = 0;
3456 }
3457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 mbedtls_platform_zeroize(ssl->session_negotiate->ticket,
3459 ssl->session_negotiate->ticket_len);
3460 mbedtls_free(ssl->session_negotiate->ticket);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003461 ssl->session_negotiate->ticket = NULL;
3462 ssl->session_negotiate->ticket_len = 0;
3463
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3465 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3466 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3467 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3468 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003469 }
3470
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003472
3473 ssl->session_negotiate->ticket = ticket;
3474 ssl->session_negotiate->ticket_len = ticket_len;
3475 ssl->session_negotiate->ticket_lifetime = lifetime;
3476
3477 /*
3478 * RFC 5077 section 3.4:
3479 * "If the client receives a session ticket from the server, then it
3480 * discards any Session ID that was sent in the ServerHello."
3481 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003483 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003484
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003486
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003490
Paul Bakker5121ce52009-01-03 21:22:43 +00003491/*
Paul Bakker1961b702013-01-25 14:49:24 +01003492 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003494int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003495{
3496 int ret = 0;
3497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003499 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3502 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003503 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003504 }
3505#endif
3506
Gilles Peskine449bd832023-01-11 14:50:10 +01003507 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 case MBEDTLS_SSL_HELLO_REQUEST:
3509 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003510 break;
3511
Gilles Peskine449bd832023-01-11 14:50:10 +01003512 /*
3513 * ==> ClientHello
3514 */
3515 case MBEDTLS_SSL_CLIENT_HELLO:
3516 ret = mbedtls_ssl_write_client_hello(ssl);
3517 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003518
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 /*
3520 * <== ServerHello
3521 * Certificate
3522 * ( ServerKeyExchange )
3523 * ( CertificateRequest )
3524 * ServerHelloDone
3525 */
3526 case MBEDTLS_SSL_SERVER_HELLO:
3527 ret = ssl_parse_server_hello(ssl);
3528 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3531 ret = mbedtls_ssl_parse_certificate(ssl);
3532 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003533
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3535 ret = ssl_parse_server_key_exchange(ssl);
3536 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003537
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3539 ret = ssl_parse_certificate_request(ssl);
3540 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Gilles Peskine449bd832023-01-11 14:50:10 +01003542 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3543 ret = ssl_parse_server_hello_done(ssl);
3544 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003545
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 /*
3547 * ==> ( Certificate/Alert )
3548 * ClientKeyExchange
3549 * ( CertificateVerify )
3550 * ChangeCipherSpec
3551 * Finished
3552 */
3553 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3554 ret = mbedtls_ssl_write_certificate(ssl);
3555 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003556
Gilles Peskine449bd832023-01-11 14:50:10 +01003557 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3558 ret = ssl_write_client_key_exchange(ssl);
3559 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3562 ret = ssl_write_certificate_verify(ssl);
3563 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003564
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3566 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3567 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003568
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 case MBEDTLS_SSL_CLIENT_FINISHED:
3570 ret = mbedtls_ssl_write_finished(ssl);
3571 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003572
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 /*
3574 * <== ( NewSessionTicket )
3575 * ChangeCipherSpec
3576 * Finished
3577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3580 ret = ssl_parse_new_session_ticket(ssl);
3581 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003582#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003583
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3585 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3586 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 case MBEDTLS_SSL_SERVER_FINISHED:
3589 ret = mbedtls_ssl_parse_finished(ssl);
3590 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003591
Gilles Peskine449bd832023-01-11 14:50:10 +01003592 case MBEDTLS_SSL_FLUSH_BUFFERS:
3593 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3594 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3595 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003596
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3598 mbedtls_ssl_handshake_wrapup(ssl);
3599 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003600
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 default:
3602 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3603 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003605
Gilles Peskine449bd832023-01-11 14:50:10 +01003606 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607}
Jerry Yuc5aef882021-12-23 20:15:02 +08003608
Jerry Yufb4b6472022-01-27 15:03:26 +08003609#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */