blob: b427ae94441809be2c2e3cbfbef709a85eb0a3eb [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"
Hanno Beckerbb89e272019-01-08 12:54:37 +000036#endif /* MBEDTLS_USE_PSA_CRYPTO */
37
SimonBd5800b72016-04-26 07:43:27 +010038#include <string.h>
39
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020040#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010043#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020048#endif
49
Andrzej Kurek0ce59212022-08-17 07:54:34 -040050#include "hash_info.h"
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020053MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010054static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
55 unsigned char *buf,
56 const unsigned char *end,
57 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +010058{
59 unsigned char *p = buf;
60
61 *olen = 0;
62
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010063 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010064 * initial ClientHello, in which case also adding the renegotiation
65 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
67 return 0;
68 }
Paul Bakkerd3edc862013-03-20 16:07:17 +010069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 MBEDTLS_SSL_DEBUG_MSG(3,
71 ("client hello, adding renegotiation extension"));
Paul Bakkerd3edc862013-03-20 16:07:17 +010072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
Simon Butchered997662015-09-28 02:14:30 +010074
Paul Bakkerd3edc862013-03-20 16:07:17 +010075 /*
76 * Secure renegotiation
77 */
Gilles Peskine449bd832023-01-11 14:50:10 +010078 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010079 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010080
81 *p++ = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +010082 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
83 *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Paul Bakkerd3edc862013-03-20 16:07:17 +010086
87 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +010090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010092
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +020093#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010094 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010095
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020096MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
98 unsigned char *buf,
99 const unsigned char *end,
100 size_t *olen)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100101{
102 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100103 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100104
105 *olen = 0;
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 MBEDTLS_SSL_DEBUG_MSG(3,
108 ("client hello, adding supported_point_formats extension"));
109 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Simon Butchered997662015-09-28 02:14:30 +0100110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100112 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100113
114 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100115 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200116
117 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100119
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200120 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 return 0;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100123}
Darryl Green11999bb2018-03-13 15:22:58 +0000124#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100125 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100126
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200127#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200128MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100129static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
130 unsigned char *buf,
131 const unsigned char *end,
132 size_t *olen)
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200133{
Janos Follath865b3eb2019-12-16 11:46:15 +0000134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200135 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100136 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200137
138 *olen = 0;
139
140 /* Skip costly extension if we can't use EC J-PAKE anyway */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200141#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
143 return 0;
144 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200145#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
147 return 0;
148 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200149#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 MBEDTLS_SSL_DEBUG_MSG(3,
152 ("client hello, adding ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100157 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200158
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200159 /*
160 * We may need to send ClientHello multiple times for Hello verification.
161 * We don't want to compute fresh values every time (both for performance
162 * and consistency reasons), so cache the extension content.
163 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if (ssl->handshake->ecjpake_cache == NULL ||
165 ssl->handshake->ecjpake_cache_len == 0) {
166 MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200167
Neil Armstrongca7d5062022-05-31 14:43:23 +0200168#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100169 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 p + 2, end - p - 2, &kkpp_len,
171 MBEDTLS_ECJPAKE_ROUND_ONE);
172 if (ret != 0) {
173 psa_destroy_key(ssl->handshake->psa_pake_password);
174 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
175 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
176 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200177 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200178#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
180 p + 2, end - p - 2, &kkpp_len,
181 ssl->conf->f_rng, ssl->conf->p_rng);
182 if (ret != 0) {
183 MBEDTLS_SSL_DEBUG_RET(1,
184 "mbedtls_ecjpake_write_round_one", ret);
185 return ret;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200186 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200187#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
190 if (ssl->handshake->ecjpake_cache == NULL) {
191 MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
192 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200193 }
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200196 ssl->handshake->ecjpake_cache_len = kkpp_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 } else {
198 MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200199
200 kkpp_len = ssl->handshake->ecjpake_cache_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200204 }
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100207 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200208
209 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200212}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200213#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000214
Hanno Beckera0e20d02019-05-15 14:03:01 +0100215#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200216MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100217static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
218 unsigned char *buf,
219 const unsigned char *end,
220 size_t *olen)
Hanno Becker49770ff2019-04-25 16:55:15 +0100221{
222 unsigned char *p = buf;
223 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100224
225 /*
Hanno Becker49770ff2019-04-25 16:55:15 +0100226 * struct {
227 * opaque cid<0..2^8-1>;
228 * } ConnectionId;
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 */
Hanno Becker49770ff2019-04-25 16:55:15 +0100230
231 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
233 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
234 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100235 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
Hanno Becker49770ff2019-04-25 16:55:15 +0100237
238 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
239 * which is at most 255, so the increment cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
Hanno Becker49770ff2019-04-25 16:55:15 +0100241
242 /* Add extension ID + size */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100244 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100245 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100247 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100248
249 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker49770ff2019-04-25 16:55:15 +0100251
252 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return 0;
Hanno Becker49770ff2019-04-25 16:55:15 +0100255}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100256#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200259MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100260static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
261 unsigned char *buf,
262 const unsigned char *end,
263 size_t *olen)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200264{
265 unsigned char *p = buf;
266
Simon Butcher0fc94e92015-09-28 20:52:04 +0100267 *olen = 0;
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
270 return 0;
271 }
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 MBEDTLS_SSL_DEBUG_MSG(3,
274 ("client hello, adding max_fragment_length extension"));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
Simon Butchered997662015-09-28 02:14:30 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100279 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200280
281 *p++ = 0x00;
282 *p++ = 1;
283
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200284 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200285
286 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return 0;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200293MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100294static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
295 unsigned char *buf,
296 const unsigned char *end,
297 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100298{
299 unsigned char *p = buf;
300
Simon Butcher0fc94e92015-09-28 20:52:04 +0100301 *olen = 0;
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
304 return 0;
305 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 MBEDTLS_SSL_DEBUG_MSG(3,
308 ("client hello, adding encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100313 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100314
315 *p++ = 0x00;
316 *p++ = 0x00;
317
318 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100321}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200325MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100326static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
327 unsigned char *buf,
328 const unsigned char *end,
329 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200330{
331 unsigned char *p = buf;
332
Simon Butcher0fc94e92015-09-28 20:52:04 +0100333 *olen = 0;
334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
336 return 0;
337 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 MBEDTLS_SSL_DEBUG_MSG(3,
340 ("client hello, adding extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Simon Butchered997662015-09-28 02:14:30 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100345 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200346
347 *p++ = 0x00;
348 *p++ = 0x00;
349
350 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200357MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100358static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
359 unsigned char *buf,
360 const unsigned char *end,
361 size_t *olen)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200362{
363 unsigned char *p = buf;
364 size_t tlen = ssl->session_negotiate->ticket_len;
365
Simon Butcher0fc94e92015-09-28 20:52:04 +0100366 *olen = 0;
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
369 return 0;
370 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 MBEDTLS_SSL_DEBUG_MSG(3,
373 ("client hello, adding session ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200374
Hanno Becker261602c2017-04-12 14:54:42 +0100375 /* The addition is safe here since the ticket length is 16 bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
Simon Butchered997662015-09-28 02:14:30 +0100377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100379 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100382 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200383
384 *olen = 4;
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
387 return 0;
388 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 MBEDTLS_SSL_DEBUG_MSG(3,
391 ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 memcpy(p, ssl->session_negotiate->ticket, tlen);
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200394
395 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200400
Ron Eldora9788042018-12-05 11:04:31 +0200401#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100403static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
404 unsigned char *buf,
405 const unsigned char *end,
406 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +0100407{
408 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200409 size_t protection_profiles_index = 0, ext_len = 0;
410 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100411
412 *olen = 0;
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
415 (ssl->conf->dtls_srtp_profile_list == NULL) ||
416 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
417 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100418 }
419
Ron Eldora9788042018-12-05 11:04:31 +0200420 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100421 * uint8 SRTPProtectionProfile[2];
422 *
423 * struct {
424 * SRTPProtectionProfiles SRTPProtectionProfiles;
425 * opaque srtp_mki<0..255>;
426 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100427 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100428 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +0200430 mki_len = ssl->dtls_srtp_info.mki_len;
431 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300432 /* Extension length = 2 bytes for profiles length,
433 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
434 * 1 byte for srtp_mki vector length and the mki_len value
435 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
Johan Pascal77696ee2020-09-22 21:49:40 +0200439
440 /* Check there is room in the buffer for the extension + 4 bytes
441 * - the extension tag (2 bytes)
442 * - the extension length (2 bytes)
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
Johan Pascal77696ee2020-09-22 21:49:40 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100447 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100450 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100451
Ron Eldor3adb9922017-12-21 10:15:08 +0200452 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200453 /* micro-optimization:
454 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
455 * which is lower than 127, so the upper byte of the length is always 0
456 * For the documentation, the more generic code is left in comments
457 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
458 * >> 8 ) & 0xFF );
459 */
460 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 for (protection_profiles_index = 0;
Ron Eldoref72faf2018-07-12 11:54:20 +0300464 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 protection_profiles_index++) {
Johan Pascal43f94902020-09-22 12:25:52 +0200466 profile_value = mbedtls_ssl_check_srtp_profile_value
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
468 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
469 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
470 profile_value));
471 MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100472 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 } else {
Ron Eldor089c9fe2018-12-06 17:12:49 +0200474 /*
475 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200476 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200477 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 MBEDTLS_SSL_DEBUG_MSG(3,
479 ("client hello, "
480 "illegal DTLS-SRTP protection profile %d",
481 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
482 ));
483 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Johan Pascalb62bb512015-12-03 21:56:45 +0100484 }
485 }
486
Ron Eldor591f1622018-01-22 12:30:04 +0200487 *p++ = mki_len & 0xFF;
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (mki_len != 0) {
490 memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
Ron Eldor313d7b52018-12-10 14:56:21 +0200491 /*
492 * Increment p to point to the current position.
493 */
494 p += mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
496 ssl->dtls_srtp_info.mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +0200497 }
498
Ron Eldoref72faf2018-07-12 11:54:20 +0300499 /*
500 * total extension length: extension type (2 bytes)
501 * + extension length (2 bytes)
502 * + protection profile length (2 bytes)
503 * + 2 * number of protection profiles
504 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200505 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300506 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200507 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100510}
511#endif /* MBEDTLS_SSL_DTLS_SRTP */
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
514 unsigned char *buf,
515 const unsigned char *end,
516 int uses_ec,
517 size_t *out_len)
Ronald Cron12dcdf02022-02-16 15:28:22 +0100518{
519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
520 unsigned char *p = buf;
521 size_t ext_len = 0;
522
523 (void) ssl;
524 (void) end;
525 (void) uses_ec;
526 (void) ret;
527 (void) ext_len;
528
529 *out_len = 0;
530
531 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
532 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
533#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
535 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
536 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100537 }
538 p += ext_len;
539#endif
540
541#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
542 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (uses_ec) {
544 if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
545 &ext_len)) != 0) {
546 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
547 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100548 }
549 p += ext_len;
550 }
551#endif
552
553#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
555 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
556 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100557 }
558 p += ext_len;
559#endif
560
561#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
563 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
564 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100565 }
566 p += ext_len;
567#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
568
569#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
571 &ext_len)) != 0) {
572 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
573 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100574 }
575 p += ext_len;
576#endif
577
578#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
580 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
581 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100582 }
583 p += ext_len;
584#endif
585
586#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
588 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
589 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100590 }
591 p += ext_len;
592#endif
593
594#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
596 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
597 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100598 }
599 p += ext_len;
600#endif
601
602#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
604 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
605 return ret;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100606 }
607 p += ext_len;
608#endif
609
610 *out_len = p - buf;
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return 0;
Ronald Cron12dcdf02022-02-16 15:28:22 +0100613}
614
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200615MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100616static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
617 const unsigned char *buf,
618 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100622 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (len != 1 + ssl->verify_data_len * 2 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000624 buf[0] != ssl->verify_data_len * 2 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 mbedtls_ct_memcmp(buf + 1,
626 ssl->own_verify_data, ssl->verify_data_len) != 0 ||
627 mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
628 ssl->peer_verify_data, ssl->verify_data_len) != 0) {
629 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100630 mbedtls_ssl_send_alert_message(
631 ssl,
632 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
634 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +0000635 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100638 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (len != 1 || buf[0] != 0x00) {
640 MBEDTLS_SSL_DEBUG_MSG(1,
641 ("non-zero length renegotiation info"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100642 mbedtls_ssl_send_alert_message(
643 ssl,
644 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
646 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100647 }
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100650 }
Paul Bakker48916f92012-09-16 19:57:18 +0000651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000653}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200656MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100657static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
658 const unsigned char *buf,
659 size_t len)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200660{
661 /*
662 * server should use the extension only if we did,
663 * and if so the server's value should match ours (and len is always 1)
664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200666 len != 1 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 buf[0] != ssl->conf->mfl_code) {
668 MBEDTLS_SSL_DEBUG_MSG(1,
669 ("non-matching max fragment length extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100670 mbedtls_ssl_send_alert_message(
671 ssl,
672 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
674 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200675 }
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return 0;
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000680
Hanno Beckera0e20d02019-05-15 14:03:01 +0100681#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200682MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100683static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
684 const unsigned char *buf,
685 size_t len)
Hanno Beckera8373a12019-04-26 15:37:26 +0100686{
687 size_t peer_cid_len;
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if ( /* CID extension only makes sense in DTLS */
Hanno Beckera8373a12019-04-26 15:37:26 +0100690 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
691 /* The server must only send the CID extension if we have offered it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
693 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
694 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
695 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
696 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Hanno Becker22626482019-05-03 12:46:59 +0100697 }
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (len == 0) {
700 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
701 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
702 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
703 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100704 }
705
706 peer_cid_len = *buf++;
707 len--;
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
710 MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
711 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
712 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
713 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Hanno Beckera8373a12019-04-26 15:37:26 +0100714 }
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (len != peer_cid_len) {
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_DECODE_ERROR);
720 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Hanno Beckera8373a12019-04-26 15:37:26 +0100721 }
722
Hanno Becker5a299902019-05-03 12:47:49 +0100723 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100724 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
728 MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
Hanno Beckera8373a12019-04-26 15:37:26 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 return 0;
Hanno Beckera8373a12019-04-26 15:37:26 +0100731}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100732#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200735MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100736static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
737 const unsigned char *buf,
738 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100739{
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
741 len != 0) {
742 MBEDTLS_SSL_DEBUG_MSG(1,
743 ("non-matching encrypt-then-MAC extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100744 mbedtls_ssl_send_alert_message(
745 ssl,
746 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
748 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100749 }
750
751 ((void) buf);
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200760MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100761static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
762 const unsigned char *buf,
763 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200764{
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
766 len != 0) {
767 MBEDTLS_SSL_DEBUG_MSG(1,
768 ("non-matching extended master secret extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100769 mbedtls_ssl_send_alert_message(
770 ssl,
771 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
773 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200774 }
775
776 ((void) buf);
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200785MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100786static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
787 const unsigned char *buf,
788 size_t len)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200789{
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
791 len != 0) {
792 MBEDTLS_SSL_DEBUG_MSG(1,
793 ("non-matching session ticket extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100794 mbedtls_ssl_send_alert_message(
795 ssl,
796 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
798 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200799 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200800
801 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200802
803 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 return 0;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200808
Robert Cragie136884c2015-10-02 13:34:31 +0100809#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100810 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200811MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100812static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
813 const unsigned char *buf,
814 size_t len)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200815{
816 size_t list_size;
817 const unsigned char *p;
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (len == 0 || (size_t) (buf[0] + 1) != len) {
820 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
821 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
822 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
823 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200824 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200825 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200826
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200827 p = buf + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 while (list_size > 0) {
829 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
830 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200831#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 (defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200833 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200834#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
835 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200836#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
838 mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
839 p[0]);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200840#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
842 return 0;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200843 }
844
845 list_size--;
846 p++;
847 }
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
850 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
851 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
852 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200853}
Darryl Green11999bb2018-03-13 15:22:58 +0000854#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100855 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200856
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200857#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200858MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100859static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
860 const unsigned char *buf,
861 size_t len)
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200862{
Janos Follath865b3eb2019-12-16 11:46:15 +0000863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (ssl->handshake->ciphersuite_info->key_exchange !=
866 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
867 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
868 return 0;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200869 }
870
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200871 /* If we got here, we no longer need our cached extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 mbedtls_free(ssl->handshake->ecjpake_cache);
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200873 ssl->handshake->ecjpake_cache = NULL;
874 ssl->handshake->ecjpake_cache_len = 0;
875
Neil Armstrongca7d5062022-05-31 14:43:23 +0200876#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if ((ret = mbedtls_psa_ecjpake_read_round(
878 &ssl->handshake->psa_pake_ctx, buf, len,
879 MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
880 psa_destroy_key(ssl->handshake->psa_pake_password);
881 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100884 mbedtls_ssl_send_alert_message(
885 ssl,
886 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
888 return ret;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200889 }
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 return 0;
892#else
893 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
894 buf, len)) != 0) {
895 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
896 mbedtls_ssl_send_alert_message(
897 ssl,
898 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
899 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
900 return ret;
901 }
902
903 return 0;
Neil Armstrongca7d5062022-05-31 14:43:23 +0200904#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200905}
906#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200909MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100910static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
911 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200912{
913 size_t list_len, name_len;
914 const char **p;
915
916 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (ssl->conf->alpn_list == NULL) {
918 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100919 mbedtls_ssl_send_alert_message(
920 ssl,
921 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
923 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200924 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200925
926 /*
927 * opaque ProtocolName<1..2^8-1>;
928 *
929 * struct {
930 * ProtocolName protocol_name_list<2..2^16-1>
931 * } ProtocolNameList;
932 *
933 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
934 */
935
936 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (len < 4) {
938 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
939 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
940 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200941 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 list_len = (buf[0] << 8) | buf[1];
944 if (list_len != len - 2) {
945 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
946 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
947 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200948 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200949
950 name_len = buf[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (name_len != list_len - 1) {
952 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
953 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
954 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200955 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200956
957 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 for (p = ssl->conf->alpn_list; *p != NULL; p++) {
959 if (name_len == strlen(*p) &&
960 memcmp(buf + 3, *p, name_len) == 0) {
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200961 ssl->alpn_chosen = *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200963 }
964 }
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
967 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
968 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
969 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200970}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200972
Johan Pascalb62bb512015-12-03 21:56:45 +0100973#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200974MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100975static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
976 const unsigned char *buf,
977 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100978{
Johan Pascal43f94902020-09-22 12:25:52 +0200979 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200980 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100981 uint16_t server_protection_profile_value = 0;
982
983 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
985 (ssl->conf->dtls_srtp_profile_list == NULL) ||
986 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
987 return 0;
988 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100989
Ron Eldora9788042018-12-05 11:04:31 +0200990 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100991 * uint8 SRTPProtectionProfile[2];
992 *
993 * struct {
994 * SRTPProtectionProfiles SRTPProtectionProfiles;
995 * opaque srtp_mki<0..255>;
996 * } UseSRTPData;
997
998 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
999 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02001002 mki_len = ssl->dtls_srtp_info.mki_len;
1003 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001004
Ron Eldoref72faf2018-07-12 11:54:20 +03001005 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001006 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1007 * + protection profile (2 bytes)
1008 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001009 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if ((len < 5) || (len != (buf[4] + 5u))) {
1012 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1013 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001014
1015 /*
1016 * get the server protection profile
1017 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001018
1019 /*
1020 * protection profile length must be 0x0002 as we must have only
1021 * one protection profile in server Hello
1022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if ((buf[0] != 0) || (buf[1] != 2)) {
1024 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1025 }
Ron Eldor089c9fe2018-12-06 17:12:49 +02001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 server_protection_profile_value = (buf[2] << 8) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001028 server_protection = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 server_protection_profile_value);
1030 if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
1031 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
1032 mbedtls_ssl_get_srtp_profile_as_string(
1033 server_protection)));
Johan Pascalb62bb512015-12-03 21:56:45 +01001034 }
1035
Johan Pascal43f94902020-09-22 12:25:52 +02001036 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001037
Johan Pascalb62bb512015-12-03 21:56:45 +01001038 /*
1039 * Check we have the server profile in our list
1040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
1042 if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +02001043 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
Johan Pascal43f94902020-09-22 12:25:52 +02001045 mbedtls_ssl_get_srtp_profile_as_string(
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 server_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +02001047 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001048 }
1049 }
1050
Ron Eldor591f1622018-01-22 12:30:04 +02001051 /* If no match was found : server problem, it shall never answer with incompatible profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
1053 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1054 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1055 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Ron Eldor591f1622018-01-22 12:30:04 +02001056 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001057
1058 /* If server does not use mki in its reply, make sure the client won't keep
1059 * one as negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (len == 5) {
Johan Pascal20c7db32020-10-26 22:45:58 +01001061 ssl->dtls_srtp_info.mki_len = 0;
1062 }
1063
Ron Eldoref72faf2018-07-12 11:54:20 +03001064 /*
1065 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001066 * If the client detects a nonzero-length MKI in the server's response
1067 * that is different than the one the client offered, then the client
1068 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1069 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (len > 5 && (buf[4] != mki_len ||
1071 (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
1072 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1073 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1074 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ron Eldor591f1622018-01-22 12:30:04 +02001075 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001076#if defined(MBEDTLS_DEBUG_C)
1077 if (len > 5) {
1078 MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
1079 ssl->dtls_srtp_info.mki_len);
Ron Eldorb4655392018-07-05 18:25:39 +03001080 }
1081#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001083}
1084#endif /* MBEDTLS_SSL_DTLS_SRTP */
1085
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001086/*
1087 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001090MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001091static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001092{
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Glenn Strauss83158112022-04-13 14:59:34 -04001094 uint16_t dtls_legacy_version;
Jerry Yue01304f2022-04-07 10:51:55 +08001095
1096#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
1097 uint8_t cookie_len;
1098#else
1099 uint16_t cookie_len;
1100#endif
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001103
Gilles Peskineb64bf062019-09-27 14:02:44 +02001104 /* Check that there is enough room for:
1105 * - 2 bytes of version
1106 * - 1 byte of cookie_len
1107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
1109 MBEDTLS_SSL_DEBUG_MSG(1,
1110 ("incoming HelloVerifyRequest message is too short"));
1111 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1112 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1113 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskineb64bf062019-09-27 14:02:44 +02001114 }
1115
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001116 /*
1117 * struct {
1118 * ProtocolVersion server_version;
1119 * opaque cookie<0..2^8-1>;
1120 * } HelloVerifyRequest;
1121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1123 dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001124 p += 2;
1125
TRodziewicz2d8800e2021-05-13 19:14:19 +02001126 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001127 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1128 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1129 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001130 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
1132 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1135 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001138 }
1139
1140 cookie_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
1142 MBEDTLS_SSL_DEBUG_MSG(1,
1143 ("cookie length does not match incoming message size"));
1144 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1145 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1146 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Andres AG5a87c932016-09-26 14:53:05 +01001147 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
Andres AG5a87c932016-09-26 14:53:05 +01001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 mbedtls_free(ssl->handshake->cookie);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
1153 if (ssl->handshake->cookie == NULL) {
1154 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
1155 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001156 }
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 memcpy(ssl->handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001159 ssl->handshake->cookie_len = cookie_len;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001160
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001161 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 mbedtls_ssl_reset_checksum(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 mbedtls_ssl_recv_flight_completed(ssl);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 return 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001172
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001173MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001174static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001175{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001176 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001177 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001178 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001179 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001180 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001182 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001183#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001184 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001190 /* No alert on a read error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
1192 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 }
1194
Hanno Becker79594fd2019-05-08 09:38:41 +01001195 buf = ssl->in_msg;
1196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001200 ssl->renego_records_seen++;
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (ssl->conf->renego_max_records >= 0 &&
1203 ssl->renego_records_seen > ssl->conf->renego_max_records) {
1204 MBEDTLS_SSL_DEBUG_MSG(1,
1205 ("renegotiation requested, but not honored by server"));
1206 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001207 }
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 MBEDTLS_SSL_DEBUG_MSG(1,
1210 ("non-handshake message during renegotiation"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001211
1212 ssl->keep_current_message = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001214 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001218 mbedtls_ssl_send_alert_message(
1219 ssl,
1220 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
1222 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 }
1224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1227 if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
1228 MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
1229 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
1230 return ssl_parse_hello_verify_request(ssl);
1231 } else {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001232 /* We made it through the verification process */
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 mbedtls_free(ssl->handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001234 ssl->handshake->cookie = NULL;
Jerry Yuac5ca5a2022-03-04 12:50:46 +08001235 ssl->handshake->cookie_len = 0;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001236 }
1237 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
1241 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
1242 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1243 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1244 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1245 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 }
1247
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001248 /*
1249 * 0 . 1 server_version
1250 * 2 . 33 random (maybe including 4 bytes of Unix time)
1251 * 34 . 34 session_id length = n
1252 * 35 . 34+n session_id
1253 * 35+n . 36+n cipher_suite
1254 * 37+n . 37+n compression_method
1255 *
1256 * 38+n . 39+n extensions length (optional)
1257 * 40+n . .. extensions
1258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 buf += mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
1262 ssl->tls_version = mbedtls_ssl_read_version(buf, ssl->conf->transport);
Glenn Strauss60bfe602022-03-14 19:04:24 -04001263 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (ssl->tls_version < ssl->conf->min_tls_version ||
1266 ssl->tls_version > ssl->conf->max_tls_version) {
1267 MBEDTLS_SSL_DEBUG_MSG(1,
1268 (
1269 "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1270 (unsigned) ssl->conf->min_tls_version,
1271 (unsigned) ssl->tls_version,
1272 (unsigned) ssl->conf->max_tls_version));
Paul Bakker1d29fb52012-09-28 13:28:45 +00001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1275 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001278 }
1279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
1281 ((unsigned long) buf[2] << 24) |
1282 ((unsigned long) buf[3] << 16) |
1283 ((unsigned long) buf[4] << 8) |
1284 ((unsigned long) buf[5])));
Paul Bakker5121ce52009-01-03 21:22:43 +00001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001288 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (n > 32) {
1293 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1294 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1295 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1296 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001297 }
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
1300 ext_len = ((buf[38 + n] << 8)
1301 | (buf[39 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if ((ext_len > 0 && ext_len < 4) ||
1304 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
1305 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001306 mbedtls_ssl_send_alert_message(
1307 ssl,
1308 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1310 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001311 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001313 ext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 } else {
1315 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1316 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1317 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1318 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001321 /* ciphersuite (used later) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 i = (buf[35 + n] << 8) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001323
1324 /*
1325 * Read and check compression
1326 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001327 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1330 MBEDTLS_SSL_DEBUG_MSG(1,
1331 ("server hello, bad compression: %d", comp));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001332 mbedtls_ssl_send_alert_message(
1333 ssl,
1334 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1336 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001337 }
1338
Paul Bakker380da532012-04-18 16:10:25 +00001339 /*
1340 * Initialize update checksum functions
1341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
1343 if (ssl->handshake->ciphersuite_info == NULL) {
1344 MBEDTLS_SSL_DEBUG_MSG(1,
1345 ("ciphersuite info for %04x not found", (unsigned int) i));
1346 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1347 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1348 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001349 }
Paul Bakker380da532012-04-18 16:10:25 +00001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
1354 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00001355
1356 /*
1357 * Check if the session can be resumed
1358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#if defined(MBEDTLS_SSL_RENEGOTIATION)
1361 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001362#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001363 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001364 ssl->session_negotiate->id_len != n ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001367 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 ssl->session_negotiate->start = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001370#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001371 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001372 ssl->session_negotiate->id_len = n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 memcpy(ssl->session_negotiate->id, buf + 35, n);
1374 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 }
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
1379 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
1382 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
1383 buf[37 + n]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Andrzej Kurek03bac442018-04-25 05:06:07 -04001385 /*
1386 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001387 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 while (1) {
1390 if (ssl->conf->ciphersuite_list[i] == 0) {
1391 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001392 mbedtls_ssl_send_alert_message(
1393 ssl,
1394 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1396 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 }
1398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if (ssl->conf->ciphersuite_list[i++] ==
1400 ssl->session_negotiate->ciphersuite) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001402 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 }
1404
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001405 suite_info = mbedtls_ssl_ciphersuite_from_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 ssl->session_negotiate->ciphersuite);
1407 if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
1408 ssl->tls_version) != 0) {
1409 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001410 mbedtls_ssl_send_alert_message(
1411 ssl,
1412 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1414 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001415 }
1416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 MBEDTLS_SSL_DEBUG_MSG(3,
1418 ("server hello, chosen ciphersuite: %s", suite_info->name));
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001419
Gilles Peskineeccd8882020-03-10 12:19:08 +01001420#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1422 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001423 ssl->handshake->ecrs_enabled = 1;
1424 }
1425#endif
1426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
1428 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001429 mbedtls_ssl_send_alert_message(
1430 ssl,
1431 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
1433 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 }
1435
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001436 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_SSL_DEBUG_MSG(2,
1439 ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1440 ext_len));
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 while (ext_len) {
1443 unsigned int ext_id = ((ext[0] << 8)
1444 | (ext[1]));
1445 unsigned int ext_size = ((ext[2] << 8)
1446 | (ext[3]));
Paul Bakker48916f92012-09-16 19:57:18 +00001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if (ext_size + 4 > ext_len) {
1449 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001450 mbedtls_ssl_send_alert_message(
1451 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1453 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001454 }
1455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 switch (ext_id) {
1457 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1458 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001461#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
1464 ext_size)) != 0) {
1465 return ret;
1466 }
Paul Bakker48916f92012-09-16 19:57:18 +00001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1472 MBEDTLS_SSL_DEBUG_MSG(3,
1473 ("found max_fragment_length extension"));
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if ((ret = ssl_parse_max_fragment_length_ext(ssl,
1476 ext + 4, ext_size)) != 0) {
1477 return ret;
1478 }
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001482
Hanno Beckera0e20d02019-05-15 14:03:01 +01001483#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 case MBEDTLS_TLS_EXT_CID:
1485 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Beckera8373a12019-04-26 15:37:26 +01001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if ((ret = ssl_parse_cid_ext(ssl,
1488 ext + 4,
1489 ext_size)) != 0) {
1490 return ret;
1491 }
Hanno Beckera8373a12019-04-26 15:37:26 +01001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001494#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1498 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
1501 ext + 4, ext_size)) != 0) {
1502 return ret;
1503 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1510 MBEDTLS_SSL_DEBUG_MSG(3,
1511 ("found extended_master_secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if ((ret = ssl_parse_extended_ms_ext(ssl,
1514 ext + 4, ext_size)) != 0) {
1515 return ret;
1516 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1523 MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if ((ret = ssl_parse_session_ticket_ext(ssl,
1526 ext + 4, ext_size)) != 0) {
1527 return ret;
1528 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001532
Robert Cragie136884c2015-10-02 13:34:31 +01001533#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1535 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1536 MBEDTLS_SSL_DEBUG_MSG(3,
1537 ("found supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if ((ret = ssl_parse_supported_point_formats_ext(ssl,
1540 ext + 4, ext_size)) != 0) {
1541 return ret;
1542 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001545#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1546 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001547
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001548#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1550 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 if ((ret = ssl_parse_ecjpake_kkpp(ssl,
1553 ext + 4, ext_size)) != 0) {
1554 return ret;
1555 }
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 break;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001558#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 case MBEDTLS_TLS_EXT_ALPN:
1562 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
1565 return ret;
1566 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001570
Johan Pascalb62bb512015-12-03 21:56:45 +01001571#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 case MBEDTLS_TLS_EXT_USE_SRTP:
1573 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
1576 return ret;
1577 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001580#endif /* MBEDTLS_SSL_DTLS_SRTP */
1581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 default:
1583 MBEDTLS_SSL_DEBUG_MSG(3,
1584 ("unknown extension found: %u (ignoring)", ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00001585 }
1586
1587 ext_len -= 4 + ext_size;
1588 ext += 4 + ext_size;
1589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (ext_len > 0 && ext_len < 4) {
1591 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
1592 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker48916f92012-09-16 19:57:18 +00001593 }
1594 }
1595
1596 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001597 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1598 * extensions. It sets the transform data for the resumed session which in
1599 * case of DTLS includes the server CID extracted from the CID extension.
1600 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (ssl->handshake->resume) {
1602 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
1603 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001604 mbedtls_ssl_send_alert_message(
1605 ssl,
1606 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1608 return ret;
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001609 }
1610 }
1611
Paul Bakker48916f92012-09-16 19:57:18 +00001612 /*
1613 * Renegotiation security checks
1614 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001616 ssl->conf->allow_legacy_renegotiation ==
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1618 MBEDTLS_SSL_DEBUG_MSG(1,
1619 ("legacy renegotiation, breaking off handshake"));
Paul Bakker48916f92012-09-16 19:57:18 +00001620 handshake_failure = 1;
1621 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 renegotiation_info_seen == 0) {
1626 MBEDTLS_SSL_DEBUG_MSG(1,
1627 ("renegotiation_info extension missing (secure)"));
Paul Bakker48916f92012-09-16 19:57:18 +00001628 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1630 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1631 ssl->conf->allow_legacy_renegotiation ==
1632 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1633 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001634 handshake_failure = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1636 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1637 renegotiation_info_seen == 1) {
1638 MBEDTLS_SSL_DEBUG_MSG(1,
1639 ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001640 handshake_failure = 1;
1641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 if (handshake_failure == 1) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001645 mbedtls_ssl_send_alert_message(
1646 ssl,
1647 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1649 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker48916f92012-09-16 19:57:18 +00001650 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001655}
1656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1658 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001659MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001660static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
1661 unsigned char **p,
1662 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001663{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001665 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001666
Paul Bakker29e1f122013-04-16 13:07:56 +02001667 /*
1668 * Ephemeral DH parameters:
1669 *
1670 * struct {
1671 * opaque dh_p<1..2^16-1>;
1672 * opaque dh_g<1..2^16-1>;
1673 * opaque dh_Ys<1..2^16-1>;
1674 * } ServerDHParams;
1675 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
1677 p, end)) != 0) {
1678 MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
1679 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001680 }
1681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
1683 if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
1684 MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
1685 dhm_actual_bitlen,
1686 ssl->conf->dhm_min_bitlen));
1687 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001688 }
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
1691 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
1692 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker29e1f122013-04-16 13:07:56 +02001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001695}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1697 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001698
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001699#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001700#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1701 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1702 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001703MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001704static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1705 unsigned char **p,
1706 unsigned char *end)
Hanno Beckerbb89e272019-01-08 12:54:37 +00001707{
1708 uint16_t tls_id;
1709 uint8_t ecpoint_len;
1710 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001711 psa_ecc_family_t ec_psa_family = 0;
1712 size_t ec_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001713
1714 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001715 * struct {
1716 * ECParameters curve_params;
1717 * ECPoint public;
1718 * } ServerECDHParams;
1719 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001720 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001721 * 2..3 NamedCurve
1722 * 4 ECPoint.len
1723 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001724 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 if (end - *p < 4) {
1726 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1727 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001728
1729 /* First byte is curve_type; only named_curve is handled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
1731 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1732 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001733
1734 /* Next two bytes are the namedcurve value */
1735 tls_id = *(*p)++;
1736 tls_id <<= 8;
1737 tls_id |= *(*p)++;
1738
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001739 /* Check it's a curve we offered */
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
1741 MBEDTLS_SSL_DEBUG_MSG(2,
1742 ("bad server key exchange message (ECDHE curve): %u",
1743 (unsigned) tls_id));
1744 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001745 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001746
Valerio Setti40d9ca92023-01-04 16:08:04 +01001747 /* Convert EC's TLS ID to PSA key type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ec_psa_family,
1749 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1750 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001751 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001753 handshake->ecdh_bits = ec_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001754
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001755 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001756 ecpoint_len = *(*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if ((size_t) (end - *p) < ecpoint_len) {
1758 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1759 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 if (ecpoint_len > sizeof(handshake->ecdh_psa_peerkey)) {
1762 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1763 }
Hanno Beckerbb89e272019-01-08 12:54:37 +00001764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 memcpy(handshake->ecdh_psa_peerkey, *p, ecpoint_len);
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001766 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001767 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 return 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001770}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001771#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1772 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1773 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001774#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001775#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1776 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1777 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1778 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1779 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001780MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001781static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
Neil Armstrong1f198d82022-04-13 15:02:30 +02001782{
Valerio Setti18c9fed2022-12-30 17:44:24 +01001783 uint16_t tls_id;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001784 mbedtls_ecp_group_id grp_id;
1785#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1786 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1787#else
1788 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1789#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
1792 if (tls_id == 0) {
1793 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1794 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001795 }
1796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
1798 mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
Neil Armstrong1f198d82022-04-13 15:02:30 +02001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
1801 return -1;
1802 }
Neil Armstrong1f198d82022-04-13 15:02:30 +02001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
1805 MBEDTLS_DEBUG_ECDH_QP);
Neil Armstrong1f198d82022-04-13 15:02:30 +02001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 return 0;
Neil Armstrong1f198d82022-04-13 15:02:30 +02001808}
1809
Andrzej Kurek468c5062022-10-24 10:30:14 -04001810#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1811 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1812 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1813 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1814 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1815
1816#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1817 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1818 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001819MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001820static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
1821 unsigned char **p,
1822 unsigned char *end)
Paul Bakker29e1f122013-04-16 13:07:56 +02001823{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001825
Paul Bakker29e1f122013-04-16 13:07:56 +02001826 /*
1827 * Ephemeral ECDH parameters:
1828 *
1829 * struct {
1830 * ECParameters curve_params;
1831 * ECPoint public;
1832 * } ServerECDHParams;
1833 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
1835 (const unsigned char **) p, end)) != 0) {
1836 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01001837#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001839 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001841#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001843 }
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (ssl_check_server_ecdh_params(ssl) != 0) {
1846 MBEDTLS_SSL_DEBUG_MSG(1,
1847 ("bad server key exchange message (ECDHE curve)"));
1848 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001849 }
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 return ret;
Paul Bakker29e1f122013-04-16 13:07:56 +02001852}
Gilles Peskine449bd832023-01-11 14:50:10 +01001853#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1854 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
Andrzej Kurek468c5062022-10-24 10:30:14 -04001855 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1856#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001857#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001858MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001859static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
1860 unsigned char **p,
1861 unsigned char *end)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001862{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001864 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001865 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001866
1867 /*
1868 * PSK parameters:
1869 *
1870 * opaque psk_identity_hint<0..2^16-1>;
1871 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 if (end - (*p) < 2) {
1873 MBEDTLS_SSL_DEBUG_MSG(1,
1874 ("bad server key exchange message (psk_identity_hint length)"));
1875 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001876 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001877 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001878 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if (end - (*p) < len) {
1881 MBEDTLS_SSL_DEBUG_MSG(1,
1882 ("bad server key exchange message (psk_identity_hint length)"));
1883 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001884 }
1885
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001886 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001887 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001888 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001889 * someone needs that feature.
1890 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001891 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001892 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 return ret;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001895}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001896#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1899 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001900/*
1901 * Generate a pre-master secret and encrypt it with the server's RSA key
1902 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001903MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001904static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
1905 size_t offset, size_t *olen,
1906 size_t pms_offset)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001907{
Janos Follath865b3eb2019-12-16 11:46:15 +00001908 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001909 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001910 unsigned char *p = ssl->handshake->premaster + pms_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1914 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
1915 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001916 }
1917
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001918 /*
1919 * Generate (part of) the pre-master as
1920 * struct {
1921 * ProtocolVersion client_version;
1922 * opaque random[46];
1923 * } PreMasterSecret;
1924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 mbedtls_ssl_write_version(p, ssl->conf->transport,
1926 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
1929 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1930 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001931 }
1932
1933 ssl->handshake->pmslen = 48;
1934
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001935#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1936 peer_pk = &ssl->handshake->peer_pubkey;
1937#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001939 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1941 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001942 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001943 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1944#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001945
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001946 /*
1947 * Now write it out, encrypted
1948 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
1950 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
1951 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001952 }
1953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if ((ret = mbedtls_pk_encrypt(peer_pk,
1955 p, ssl->handshake->pmslen,
1956 ssl->out_msg + offset + len_bytes, olen,
1957 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
1958 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1959 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
1960 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001961 }
1962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 if (len_bytes == 2) {
1964 MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001965 *olen += 2;
1966 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001967
Hanno Beckerae553dd2019-02-08 14:06:00 +00001968#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1969 /* We don't need the peer's public key anymore. Free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00001971#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 return 0;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
1975 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1978 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001979MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001980static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001981{
Janos Follath865b3eb2019-12-16 11:46:15 +00001982 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 const mbedtls_ecp_keypair *peer_key;
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 mbedtls_pk_context *peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001985
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001986#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1987 peer_pk = &ssl->handshake->peer_pubkey;
1988#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Becker8273df82019-02-06 17:37:32 +00001990 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1992 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001993 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00001994 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1995#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001996
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02001997 /* This is a public key, so it can't be opaque, so can_do() is a good
1998 * enough check to ensure pk_ec() is safe to use below. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
2000 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2001 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002002 }
2003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 peer_key = mbedtls_pk_ec(*peer_pk);
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002005
Przemek Stekielea4000f2022-03-16 09:49:33 +01002006#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel561a4232022-03-16 13:16:24 +01002007 size_t olen = 0;
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002008 uint16_t tls_id = 0;
2009 psa_ecc_family_t ecc_family;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (mbedtls_ssl_check_curve(ssl, peer_key->grp.id) != 0) {
2012 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2013 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002014 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(peer_key->grp.id);
2017 if (tls_id == 0) {
2018 MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
2019 peer_key->grp.id));
2020 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002021 }
2022
Valerio Setti1e868cc2023-01-09 17:30:01 +01002023 /* If the above conversion to TLS ID was fine, then also this one will be,
2024 so there is no need to check the return value here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ecc_family,
2026 &ssl->handshake->ecdh_bits);
Valerio Setti2b5d3de2023-01-09 11:04:52 +01002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 ssl->handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ecc_family);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002029
Przemek Stekielea4000f2022-03-16 09:49:33 +01002030 /* Store peer's public key in psa format. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
2032 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2033 ssl->handshake->ecdh_psa_peerkey,
2034 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH);
Przemek Stekielea4000f2022-03-16 09:49:33 +01002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 if (ret != 0) {
2037 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
2038 return ret;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002039 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002040
Przemek Stekiel561a4232022-03-16 13:16:24 +01002041 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002042#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
2044 MBEDTLS_ECDH_THEIRS)) != 0) {
2045 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2046 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002047 }
2048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (ssl_check_server_ecdh_params(ssl) != 0) {
2050 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
2051 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002052 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002053#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002054#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2055 /* We don't need the peer's public key anymore. Free it,
2056 * so that more RAM is available for upcoming expensive
2057 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002059#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 return ret;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2064 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002065
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002066MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002067static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker41c83d32013-03-20 14:39:14 +01002068{
Janos Follath865b3eb2019-12-16 11:46:15 +00002069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002070 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002071 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002072 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
2078 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002082 ((void) p);
2083 ((void) end);
2084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2087 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2089 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
2090 if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
2091 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002092 mbedtls_ssl_send_alert_message(
2093 ssl,
2094 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2096 return ret;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002097 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002100 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 return 0;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002102 }
2103 ((void) p);
2104 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2106 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002107
Gilles Peskineeccd8882020-03-10 12:19:08 +01002108#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (ssl->handshake->ecrs_enabled &&
2110 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002111 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002112 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002113#endif
2114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2116 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2117 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 }
2119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2121 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002122 mbedtls_ssl_send_alert_message(
2123 ssl,
2124 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2126 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 }
2128
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002129 /*
2130 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2131 * doesn't use a psk_identity_hint
2132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
2134 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2135 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002136 /* Current message is probably either
2137 * CertificateRequest or ServerHelloDone */
2138 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002139 goto exit;
2140 }
2141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 MBEDTLS_SSL_DEBUG_MSG(1,
2143 ("server key exchange message must not be skipped"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002144 mbedtls_ssl_send_alert_message(
2145 ssl,
2146 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 }
2151
Gilles Peskineeccd8882020-03-10 12:19:08 +01002152#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002154 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002156
2157start_processing:
2158#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002160 end = ssl->in_msg + ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, end - p);
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002162
Gilles Peskineeccd8882020-03-10 12:19:08 +01002163#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2166 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2168 if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
2169 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002170 mbedtls_ssl_send_alert_message(
2171 ssl,
2172 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2174 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002175 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002176 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002177#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2180 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2182 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002183 ; /* nothing more to do */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2186 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2187#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2188 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2190 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
2191 if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
2192 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002193 mbedtls_ssl_send_alert_message(
2194 ssl,
2195 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2197 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002198 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2201 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002202#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2203 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
2208 if (ssl_parse_server_ecdh_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 Bakker41c83d32013-03-20 14:39:14 +01002215 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2218 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2219 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002220#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002223 /*
2224 * The first 3 bytes are:
2225 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2226 * [1, 2] elliptic curve's TLS ID
2227 *
2228 * However since we only support secp256r1 for now, we check only
2229 * that TLS ID here
2230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
Valerio Setti18c9fed2022-12-30 17:44:24 +01002232 uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 MBEDTLS_ECP_DP_SECP256R1);
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 if (exp_tls_id == 0) {
2236 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002237 }
2238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
2240 (read_tls_id != exp_tls_id)) {
2241 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Valerio Setti5151bdf2022-11-21 14:30:02 +01002242 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002243
2244 p += 3;
2245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if ((ret = mbedtls_psa_ecjpake_read_round(
2247 &ssl->handshake->psa_pake_ctx, p, end - p,
2248 MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
2249 psa_destroy_key(ssl->handshake->psa_pake_password);
2250 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
Neil Armstrongca7d5062022-05-31 14:43:23 +02002253 mbedtls_ssl_send_alert_message(
2254 ssl,
2255 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2257 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002258 }
2259#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
2261 p, end - p);
2262 if (ret != 0) {
2263 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002264 mbedtls_ssl_send_alert_message(
2265 ssl,
2266 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2268 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002269 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002270#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002272#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002273 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2275 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002276 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002277
Gilles Peskineeccd8882020-03-10 12:19:08 +01002278#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002280 size_t sig_len, hashlen;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002281 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
2282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2284 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002286 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002287 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002288 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 mbedtls_pk_context *peer_pk;
Hanno Beckera6899bb2019-02-06 18:26:03 +00002291
Jerry Yu693a47a2022-06-23 14:02:28 +08002292#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2293 peer_pk = &ssl->handshake->peer_pubkey;
2294#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if (ssl->session_negotiate->peer_cert == NULL) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002296 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2298 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu693a47a2022-06-23 14:02:28 +08002299 }
2300 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2301#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2302
Paul Bakker29e1f122013-04-16 13:07:56 +02002303 /*
2304 * Handle the digitally-signed structure
2305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2307 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
2308 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2309 sig_alg, &pk_alg, &md_alg) != 0 &&
2310 !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
2311 !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
2312 MBEDTLS_SSL_DEBUG_MSG(1,
2313 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002314 mbedtls_ssl_send_alert_message(
2315 ssl,
2316 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2318 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker29e1f122013-04-16 13:07:56 +02002319 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002320 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2323 MBEDTLS_SSL_DEBUG_MSG(1,
2324 ("bad server key exchange message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002325 mbedtls_ssl_send_alert_message(
2326 ssl,
2327 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
2329 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Paul Bakker9659dae2013-08-28 16:21:34 +02002330 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002331
2332 /*
2333 * Read signature
2334 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (p > end - 2) {
2337 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002338 mbedtls_ssl_send_alert_message(
2339 ssl,
2340 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2342 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002343 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 sig_len = (p[0] << 8) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002345 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (p != end - sig_len) {
2348 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002349 mbedtls_ssl_send_alert_message(
2350 ssl,
2351 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2353 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker41c83d32013-03-20 14:39:14 +01002354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002357
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002358 /*
2359 * Compute the hash that has been signed
2360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (md_alg != MBEDTLS_MD_NONE) {
2362 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
2363 params, params_len,
2364 md_alg);
2365 if (ret != 0) {
2366 return ret;
2367 }
2368 } else {
2369 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2370 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02002371 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker29e1f122013-04-16 13:07:56 +02002374
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002375 /*
2376 * Verify signature
2377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
2379 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002380 mbedtls_ssl_send_alert_message(
2381 ssl,
2382 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2384 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002385 }
2386
Gilles Peskineeccd8882020-03-10 12:19:08 +01002387#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002389 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002391#endif
2392
Jerry Yu693a47a2022-06-23 14:02:28 +08002393#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Jerry Yu693a47a2022-06-23 14:02:28 +08002395 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2396 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002397 rsassa_pss_options.expected_salt_len =
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 mbedtls_hash_info_get_size(md_alg);
2399 if (rsassa_pss_options.expected_salt_len == 0) {
2400 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2401 }
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
2404 peer_pk,
2405 md_alg, hash, hashlen,
2406 p, sig_len);
2407 } else
Jerry Yu693a47a2022-06-23 14:02:28 +08002408#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 ret = mbedtls_pk_verify_restartable(peer_pk,
2410 md_alg, hash, hashlen, p, sig_len, rs_ctx);
Jerry Yu693a47a2022-06-23 14:02:28 +08002411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 if (ret != 0) {
David Horstmannb21bbef2022-10-06 17:49:31 +01002413 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002414#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002416#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (send_alert_msg) {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002418 mbedtls_ssl_send_alert_message(
2419 ssl,
2420 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
2422 }
2423 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002424#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002426 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002428#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return ret;
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002430 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002431
2432#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2433 /* We don't need the peer's public key anymore. Free it,
2434 * so that more RAM is available for upcoming expensive
2435 * operations like ECDHE. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 mbedtls_pk_free(peer_pk);
Hanno Beckerae553dd2019-02-08 14:06:00 +00002437#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002439#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002441exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 ssl->state++;
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447}
2448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002450MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002451static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002452{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002453 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002454 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2459 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002460 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002462 }
2463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2465 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002466}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002467#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002468MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002469static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002470{
Janos Follath865b3eb2019-12-16 11:46:15 +00002471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002472 unsigned char *buf;
2473 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002474 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002475 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002476 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002477 size_t sig_alg_len;
2478#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 unsigned char *sig_alg;
2480 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002481#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2486 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002487 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 return 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002489 }
2490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2492 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2493 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 }
2495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2497 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002498 mbedtls_ssl_send_alert_message(
2499 ssl,
2500 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2502 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002503 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002504
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002505 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002506 ssl->handshake->client_auth =
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
2510 ssl->handshake->client_auth ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (ssl->handshake->client_auth == 0) {
Johan Pascala89ca862020-08-25 10:03:19 +02002513 /* Current message is probably the ServerHelloDone */
2514 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002515 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002516 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002517
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002518 /*
2519 * struct {
2520 * ClientCertificateType certificate_types<1..2^8-1>;
2521 * SignatureAndHashAlgorithm
2522 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2523 * DistinguishedName certificate_authorities<0..2^16-1>;
2524 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002525 *
2526 * Since we only support a single certificate on clients, let's just
2527 * ignore all the information that's supposed to help us pick a
2528 * certificate.
2529 *
2530 * We could check that our certificate matches the request, and bail out
2531 * if it doesn't, but it's simpler to just send the certificate anyway,
2532 * and give the server the opportunity to decide if it should terminate
2533 * the connection when it doesn't like our certificate.
2534 *
2535 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2536 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002537 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002538 *
2539 * However, we still minimally parse the message to check it is at least
2540 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002541 */
Paul Bakker926af752012-11-23 13:38:07 +01002542 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002543
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002544 /* certificate_types */
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
2546 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2547 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2548 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2549 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002550 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
Paul Bakker926af752012-11-23 13:38:07 +01002552 n = cert_type_len;
2553
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002554 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002555 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002556 * * the length of the signature algorithms field (if minor version of
2557 * SSL is 3),
2558 * * distinguished name length otherwise.
2559 * Both reach at most the index:
2560 * ...hdr_len + 2 + n,
2561 * therefore the buffer length at this point must be greater than that
2562 * regardless of the actual code path.
2563 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
2565 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2566 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2567 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2568 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002569 }
2570
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002571 /* supported_signature_algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 sig_alg_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2573 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Ronald Cron90915f22022-03-07 11:11:36 +01002574
2575 /*
2576 * The furthest access in buf is in the loop few lines below:
2577 * sig_alg[i + 1],
2578 * where:
2579 * sig_alg = buf + ...hdr_len + 3 + n,
2580 * max(i) = sig_alg_len - 1.
2581 * Therefore the furthest access is:
2582 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2583 * which reduces to:
2584 * buf[...hdr_len + 3 + n + sig_alg_len],
2585 * which is one less than we need the buf to be.
2586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
2588 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Ronald Cron90915f22022-03-07 11:11:36 +01002589 mbedtls_ssl_send_alert_message(
2590 ssl,
2591 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2593 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002594 }
Paul Bakker926af752012-11-23 13:38:07 +01002595
Ronald Cron90915f22022-03-07 11:11:36 +01002596#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
2598 for (size_t i = 0; i < sig_alg_len; i += 2) {
2599 MBEDTLS_SSL_DEBUG_MSG(3,
2600 ("Supported Signature Algorithm found: %02x %02x",
2601 sig_alg[i], sig_alg[i + 1]));
Ronald Cron90915f22022-03-07 11:11:36 +01002602 }
2603#endif
2604
2605 n += 2 + sig_alg_len;
2606
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002607 /* certificate_authorities */
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 dn_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
2609 | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
Paul Bakker926af752012-11-23 13:38:07 +01002610
2611 n += dn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
2613 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
2614 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2615 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2616 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker926af752012-11-23 13:38:07 +01002617 }
2618
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002619#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
2621 for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002622 unsigned char *p = dn + i + 2;
2623 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002624 size_t asn1_len;
2625 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 memset(&name, 0, sizeof(name));
2627 dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
2628 if (dni_len > dn_len - i - 2 ||
2629 mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
2630 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
2631 mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
2632 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002633 mbedtls_ssl_send_alert_message(
2634 ssl,
2635 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2637 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002638 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 MBEDTLS_SSL_DEBUG_MSG(3,
2640 ("DN hint: %.*s",
2641 mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
2642 mbedtls_asn1_free_named_data_list_shallow(name.next);
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002643 }
2644#endif
2645
Paul Bakker926af752012-11-23 13:38:07 +01002646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002651#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002653MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002654static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002655{
Janos Follath865b3eb2019-12-16 11:46:15 +00002656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
2661 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2662 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2666 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2667 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
2671 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
2672 MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
2673 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2674 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2675 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 }
2677
2678 ssl->state++;
2679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2682 mbedtls_ssl_recv_flight_completed(ssl);
2683 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002684#endif
2685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002689}
2690
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002691MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002692static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002693{
Janos Follath865b3eb2019-12-16 11:46:15 +00002694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002695
2696 size_t header_len;
2697 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002698 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002699 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 /*
2706 * DHM key exchange -- send G^X mod P
2707 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002711 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
2714 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2715 &ssl->out_msg[header_len], content_len,
2716 ssl->conf->f_rng, ssl->conf->p_rng);
2717 if (ret != 0) {
2718 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
2719 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2723 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2726 ssl->handshake->premaster,
2727 MBEDTLS_PREMASTER_SIZE,
2728 &ssl->handshake->pmslen,
2729 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2730 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2731 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 }
2733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2735 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002737#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2738 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2739 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2740 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002742 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2743 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
Neil Armstrong11d49452022-04-13 15:03:43 +02002745#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002746 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2747 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002748 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002749
2750 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2751
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002752 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Hanno Becker0a94a642019-01-11 14:35:30 +00002755
Hanno Becker4a63ed42019-01-08 11:39:35 +00002756 /*
2757 * Generate EC private key for ECDHE exchange.
2758 */
2759
Hanno Becker4a63ed42019-01-08 11:39:35 +00002760 /* The master secret is obtained from the shared ECDH secret by
2761 * applying the TLS 1.2 PRF with a specific salt and label. While
2762 * the PSA Crypto API encourages combining key agreement schemes
2763 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2764 * yet support the provisioning of salt + label to the KDF.
2765 * For the time being, we therefore need to split the computation
2766 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002767 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2769 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2770 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2771 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002772
2773 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 status = psa_generate_key(&key_attributes,
2775 &handshake->ecdh_psa_privkey);
2776 if (status != PSA_SUCCESS) {
2777 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2778 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002779
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002780 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002781 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002782 * but we just need to add a length byte before that. */
2783 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2784 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002786 size_t own_pubkey_len;
2787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2789 own_pubkey, own_pubkey_max_len,
2790 &own_pubkey_len);
2791 if (status != PSA_SUCCESS) {
2792 psa_destroy_key(handshake->ecdh_psa_privkey);
Andrzej Kureka0237f82022-02-24 13:24:52 -05002793 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002795 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002796
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002797 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2798 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002799
Hanno Becker4a63ed42019-01-08 11:39:35 +00002800 /* The ECDH secret is the premaster secret used for key derivation. */
2801
Janos Follathdf3b0892019-08-08 11:12:24 +01002802 /* Compute ECDH shared secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2804 handshake->ecdh_psa_privkey,
2805 handshake->ecdh_psa_peerkey,
2806 handshake->ecdh_psa_peerkey_len,
2807 ssl->handshake->premaster,
2808 sizeof(ssl->handshake->premaster),
2809 &ssl->handshake->pmslen);
Hanno Becker4a63ed42019-01-08 11:39:35 +00002810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Ronald Croncf56a0a2020-08-04 09:51:30 +02002812 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
2815 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2816 }
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002817#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002818 /*
2819 * ECDH key exchange -- send client public value
2820 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002821 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002822
Gilles Peskineeccd8882020-03-10 12:19:08 +01002823#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 if (ssl->handshake->ecrs_enabled) {
2825 if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002826 goto ecdh_calc_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 }
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002830 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002831#endif
2832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
2834 &content_len,
2835 &ssl->out_msg[header_len], 1000,
2836 ssl->conf->f_rng, ssl->conf->p_rng);
2837 if (ret != 0) {
2838 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002839#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002841 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002843#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002845 }
2846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2848 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01002849
Gilles Peskineeccd8882020-03-10 12:19:08 +01002850#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002852 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002853 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002854 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002855
2856ecdh_calc_secret:
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (ssl->handshake->ecrs_enabled) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002858 content_len = ssl->handshake->ecrs_n;
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002860#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
2862 &ssl->handshake->pmslen,
2863 ssl->handshake->premaster,
2864 MBEDTLS_MPI_MAX_SIZE,
2865 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2866 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01002867#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002869 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002871#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002873 }
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2876 MBEDTLS_DEBUG_ECDH_Z);
Neil Armstrong11d49452022-04-13 15:03:43 +02002877#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2880 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2881 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2882 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002883#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2884 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Neil Armstrong868af822022-03-09 10:26:25 +01002886 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2887 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2888 psa_key_attributes_t key_attributes;
2889
2890 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2891
2892 /*
2893 * opaque psk_identity<0..2^16-1>;
2894 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Neil Armstrong868af822022-03-09 10:26:25 +01002896 /* We don't offer PSK suites if we don't have a PSK,
2897 * and we check that the server's choice is among the
2898 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2900 }
Neil Armstrong868af822022-03-09 10:26:25 +01002901
Neil Armstrongfc834f22022-03-23 17:54:38 +01002902 /* uint16 to store content length */
2903 const size_t content_len_size = 2;
2904
Neil Armstrong868af822022-03-09 10:26:25 +01002905 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (header_len + content_len_size + ssl->conf->psk_identity_len
2908 > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2909 MBEDTLS_SSL_DEBUG_MSG(1,
2910 ("psk identity too long or SSL buffer too short"));
2911 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Neil Armstrong868af822022-03-09 10:26:25 +01002912 }
2913
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002914 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
2917 *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002918 header_len += content_len_size;
2919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 memcpy(p, ssl->conf->psk_identity,
2921 ssl->conf->psk_identity_len);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002922 p += ssl->conf->psk_identity_len;
2923
Neil Armstrong868af822022-03-09 10:26:25 +01002924 header_len += ssl->conf->psk_identity_len;
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Neil Armstrong868af822022-03-09 10:26:25 +01002927
2928 /*
2929 * Generate EC private key for ECDHE exchange.
2930 */
2931
2932 /* The master secret is obtained from the shared ECDH secret by
2933 * applying the TLS 1.2 PRF with a specific salt and label. While
2934 * the PSA Crypto API encourages combining key agreement schemes
2935 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2936 * yet support the provisioning of salt + label to the KDF.
2937 * For the time being, we therefore need to split the computation
2938 * of the ECDH secret and the application of the TLS 1.2 PRF. */
2939 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2941 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2942 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
2943 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Neil Armstrong868af822022-03-09 10:26:25 +01002944
2945 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 status = psa_generate_key(&key_attributes,
2947 &handshake->ecdh_psa_privkey);
2948 if (status != PSA_SUCCESS) {
2949 return psa_ssl_status_to_mbedtls(status);
2950 }
Neil Armstrong868af822022-03-09 10:26:25 +01002951
2952 /* Export the public part of the ECDH private key from PSA.
2953 * The export format is an ECPoint structure as expected by TLS,
2954 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002955 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01002956 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002958 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 status = psa_export_public_key(handshake->ecdh_psa_privkey,
2961 own_pubkey, own_pubkey_max_len,
2962 &own_pubkey_len);
2963 if (status != PSA_SUCCESS) {
2964 psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01002965 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 return psa_ssl_status_to_mbedtls(status);
Neil Armstrong868af822022-03-09 10:26:25 +01002967 }
2968
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002969 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002970 content_len = own_pubkey_len + 1;
2971
Neil Armstrong25400452022-03-23 17:44:07 +01002972 /* As RFC 5489 section 2, the premaster secret is formed as follows:
2973 * - a uint16 containing the length (in octets) of the ECDH computation
2974 * - the octet string produced by the ECDH computation
2975 * - a uint16 containing the length (in octets) of the PSK
2976 * - the PSK itself
2977 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002978 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 const unsigned char * const pms_end = pms +
2980 sizeof(ssl->handshake->premaster);
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01002981 /* uint16 to store length (in octets) of the ECDH computation */
2982 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01002983 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01002984
Neil Armstrong25400452022-03-23 17:44:07 +01002985 /* Perform ECDH computation after the uint16 reserved for the length */
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 status = psa_raw_key_agreement(PSA_ALG_ECDH,
2987 handshake->ecdh_psa_privkey,
2988 handshake->ecdh_psa_peerkey,
2989 handshake->ecdh_psa_peerkey_len,
2990 pms + zlen_size,
2991 pms_end - (pms + zlen_size),
2992 &zlen);
Neil Armstrong868af822022-03-09 10:26:25 +01002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
Neil Armstrong868af822022-03-09 10:26:25 +01002995 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
2996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 if (status != PSA_SUCCESS) {
2998 return psa_ssl_status_to_mbedtls(status);
2999 } else if (destruction_status != PSA_SUCCESS) {
3000 return psa_ssl_status_to_mbedtls(destruction_status);
3001 }
Neil Armstrong868af822022-03-09 10:26:25 +01003002
Neil Armstrong25400452022-03-23 17:44:07 +01003003 /* Write the ECDH computation length before the ECDH computation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003005 pms += zlen_size + zlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 } else
Neil Armstrong868af822022-03-09 10:26:25 +01003007#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3008 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003009#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003011 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003012 * opaque psk_identity<0..2^16-1>;
3013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003015 /* We don't offer PSK suites if we don't have a PSK,
3016 * and we check that the server's choice is among the
3017 * ciphersuites we offered, so this should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003019 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003020
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003021 header_len = 4;
3022 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
3025 MBEDTLS_SSL_DEBUG_MSG(1,
3026 ("psk identity too long or SSL buffer too short"));
3027 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003028 }
3029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3031 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 memcpy(ssl->out_msg + header_len,
3034 ssl->conf->psk_identity,
3035 ssl->conf->psk_identity_len);
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003036 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003040 content_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 } else
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3045 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3046 &content_len, 2)) != 0) {
3047 return ret;
3048 }
3049 } else
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003053 /*
3054 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3055 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 if (header_len + 2 + content_len >
3059 MBEDTLS_SSL_OUT_CONTENT_LEN) {
3060 MBEDTLS_SSL_DEBUG_MSG(1,
3061 ("psk identity or DHM size too long or SSL buffer too short"));
3062 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003063 }
3064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
3066 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
3069 (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
3070 &ssl->out_msg[header_len], content_len,
3071 ssl->conf->f_rng, ssl->conf->p_rng);
3072 if (ret != 0) {
3073 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
3074 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003075 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003076
3077#if defined(MBEDTLS_USE_PSA_CRYPTO)
3078 unsigned char *pms = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003080 size_t pms_len;
3081
3082 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3084 pms + 2, pms_end - (pms + 2), &pms_len,
3085 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3086 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3087 return ret;
Neil Armstrong80f6f322022-05-03 17:56:38 +02003088 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003090 pms += 2 + pms_len;
3091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
Neil Armstrong80f6f322022-05-03 17:56:38 +02003093#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003096#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3098 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003099 /*
3100 * ClientECDiffieHellmanPublic public;
3101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
3103 &content_len,
3104 &ssl->out_msg[header_len],
3105 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3106 ssl->conf->f_rng, ssl->conf->p_rng);
3107 if (ret != 0) {
3108 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
3109 return ret;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003110 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003111
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3113 MBEDTLS_DEBUG_ECDH_Q);
3114 } else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003115#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003116 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3118 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003119 }
3120
Neil Armstrong80f6f322022-05-03 17:56:38 +02003121#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3123 ciphersuite_info->key_exchange)) != 0) {
3124 MBEDTLS_SSL_DEBUG_RET(1,
3125 "mbedtls_ssl_psk_derive_premaster", ret);
3126 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003127 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003128#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003130#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003133 header_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 if ((ret = ssl_write_encrypted_pms(ssl, header_len,
3135 &content_len, 0)) != 0) {
3136 return ret;
3137 }
3138 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003140#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003142 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003143
Neil Armstrongca7d5062022-05-31 14:43:23 +02003144#if defined(MBEDTLS_USE_PSA_CRYPTO)
3145 unsigned char *out_p = ssl->out_msg + header_len;
3146 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3147 header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
3149 out_p, end_p - out_p, &content_len,
3150 MBEDTLS_ECJPAKE_ROUND_TWO);
3151 if (ret != 0) {
3152 psa_destroy_key(ssl->handshake->psa_pake_password);
3153 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3154 MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
3155 return ret;
Neil Armstrongca7d5062022-05-31 14:43:23 +02003156 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003157#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
3159 ssl->out_msg + header_len,
3160 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3161 &content_len,
3162 ssl->conf->f_rng, ssl->conf->p_rng);
3163 if (ret != 0) {
3164 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3165 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003166 }
3167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3169 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3170 ssl->conf->f_rng, ssl->conf->p_rng);
3171 if (ret != 0) {
3172 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3173 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003174 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003175#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003177#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003178 {
3179 ((void) ciphersuite_info);
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3181 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkered27a042013-04-18 22:46:23 +02003182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003183
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003184 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3186 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003187
3188 ssl->state++;
3189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3191 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3192 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 }
3194
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003198}
3199
Gilles Peskineeccd8882020-03-10 12:19:08 +01003200#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003201MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003202static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003203{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003204 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003205 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3211 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3212 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003213 }
3214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3216 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02003217 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02003219 }
3220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3222 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003223}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003224#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003225MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003226static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003229 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003230 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003231 size_t n = 0, offset = 0;
3232 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003233 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003235 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003236 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003237#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 size_t out_buf_len = ssl->out_buf_len - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003239#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (ssl->out_msg - ssl->out_buf);
Gilles Peskinef00f1522021-06-22 00:09:00 +02003241#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003244
Gilles Peskineeccd8882020-03-10 12:19:08 +01003245#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (ssl->handshake->ecrs_enabled &&
3247 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003248 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003249 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003250#endif
3251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3253 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3254 return ret;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003255 }
3256
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3258 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003259 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003261 }
3262
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 if (ssl->handshake->client_auth == 0 ||
3264 mbedtls_ssl_own_cert(ssl) == NULL) {
3265 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
Johan Pascala89ca862020-08-25 10:03:19 +02003266 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 }
3269
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 if (mbedtls_ssl_own_key(ssl) == NULL) {
3271 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
3272 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 }
3274
3275 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003276 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003278#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003280 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 }
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003282
3283sign:
3284#endif
3285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 ssl->handshake->calc_verify(ssl, hash, &hashlen);
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Ronald Cron90915f22022-03-07 11:11:36 +01003288 /*
3289 * digitally-signed struct {
3290 * opaque handshake_messages[handshake_messages_length];
3291 * };
3292 *
3293 * Taking shortcut here. We assume that the server always allows the
3294 * PRF Hash function and has sent it in the allowed signature
3295 * algorithms list received in the Certificate Request message.
3296 *
3297 * Until we encounter a server that does not, we will take this
3298 * shortcut.
3299 *
3300 * Reason: Otherwise we should have running hashes for SHA512 and
3301 * SHA224 in order to satisfy 'weird' needs from the server
3302 * side.
3303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Ronald Cron90915f22022-03-07 11:11:36 +01003305 md_alg = MBEDTLS_MD_SHA384;
3306 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 } else {
Ronald Cron90915f22022-03-07 11:11:36 +01003308 md_alg = MBEDTLS_MD_SHA256;
3309 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003310 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
Ronald Cron90915f22022-03-07 11:11:36 +01003312
3313 /* Info from md_alg will be used instead */
3314 hashlen = 0;
3315 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003316
Gilles Peskineeccd8882020-03-10 12:19:08 +01003317#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003319 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003321#endif
3322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
3324 md_alg, hash_start, hashlen,
3325 ssl->out_msg + 6 + offset,
3326 out_buf_len - 6 - offset,
3327 &n,
3328 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
3329 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
Gilles Peskineeccd8882020-03-10 12:19:08 +01003330#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003332 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 }
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003334#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 return ret;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003336 }
Paul Bakker926af752012-11-23 13:38:07 +01003337
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
Paul Bakker5121ce52009-01-03 21:22:43 +00003339
Paul Bakker1ef83d62012-04-11 12:09:53 +00003340 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3342 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003343
3344 ssl->state++;
3345
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3347 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3348 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 }
3350
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003354}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003355#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003358MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003359static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003360{
Janos Follath865b3eb2019-12-16 11:46:15 +00003361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003362 uint32_t lifetime;
3363 size_t ticket_len;
3364 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003365 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3370 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3371 return ret;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003372 }
3373
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3375 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003376 mbedtls_ssl_send_alert_message(
3377 ssl,
3378 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
3380 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003381 }
3382
3383 /*
3384 * struct {
3385 * uint32 ticket_lifetime_hint;
3386 * opaque ticket<0..2^16-1>;
3387 * } NewSessionTicket;
3388 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003389 * 0 . 3 ticket_lifetime_hint
3390 * 4 . 5 ticket_len (n)
3391 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3394 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
3395 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3396 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3397 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3398 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003399 }
3400
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 lifetime = (((uint32_t) msg[0]) << 24) | (msg[1] << 16) |
3404 (msg[2] << 8) | (msg[3]);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 ticket_len = (msg[4] << 8) | (msg[5]);
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003407
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
3409 MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
3410 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3411 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
3412 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003413 }
3414
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003416
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003417 /* We're not waiting for a NewSessionTicket message any more */
3418 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003420
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003421 /*
3422 * Zero-length ticket means the server changed his mind and doesn't want
3423 * to send a ticket after all, so just forget it
3424 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 if (ticket_len == 0) {
3426 return 0;
3427 }
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003428
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 if (ssl->session != NULL && ssl->session->ticket != NULL) {
3430 mbedtls_platform_zeroize(ssl->session->ticket,
3431 ssl->session->ticket_len);
3432 mbedtls_free(ssl->session->ticket);
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003433 ssl->session->ticket = NULL;
3434 ssl->session->ticket_len = 0;
3435 }
3436
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 mbedtls_platform_zeroize(ssl->session_negotiate->ticket,
3438 ssl->session_negotiate->ticket_len);
3439 mbedtls_free(ssl->session_negotiate->ticket);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003440 ssl->session_negotiate->ticket = NULL;
3441 ssl->session_negotiate->ticket_len = 0;
3442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
3444 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
3445 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3446 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
3447 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003448 }
3449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 memcpy(ticket, msg + 6, ticket_len);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003451
3452 ssl->session_negotiate->ticket = ticket;
3453 ssl->session_negotiate->ticket_len = ticket_len;
3454 ssl->session_negotiate->ticket_lifetime = lifetime;
3455
3456 /*
3457 * RFC 5077 section 3.4:
3458 * "If the client receives a session ticket from the server, then it
3459 * discards any Session ID that was sent in the ServerHello."
3460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003462 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003463
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003465
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 return 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003469
Paul Bakker5121ce52009-01-03 21:22:43 +00003470/*
Paul Bakker1961b702013-01-25 14:49:24 +01003471 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003473int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003474{
3475 int ret = 0;
3476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003478 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3481 ssl->handshake->new_session_ticket != 0) {
Jerry Yua357cf42022-07-12 05:36:45 +00003482 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003483 }
3484#endif
3485
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 case MBEDTLS_SSL_HELLO_REQUEST:
3488 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 break;
3490
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 /*
3492 * ==> ClientHello
3493 */
3494 case MBEDTLS_SSL_CLIENT_HELLO:
3495 ret = mbedtls_ssl_write_client_hello(ssl);
3496 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 /*
3499 * <== ServerHello
3500 * Certificate
3501 * ( ServerKeyExchange )
3502 * ( CertificateRequest )
3503 * ServerHelloDone
3504 */
3505 case MBEDTLS_SSL_SERVER_HELLO:
3506 ret = ssl_parse_server_hello(ssl);
3507 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3510 ret = mbedtls_ssl_parse_certificate(ssl);
3511 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003512
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3514 ret = ssl_parse_server_key_exchange(ssl);
3515 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3518 ret = ssl_parse_certificate_request(ssl);
3519 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3522 ret = ssl_parse_server_hello_done(ssl);
3523 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 /*
3526 * ==> ( Certificate/Alert )
3527 * ClientKeyExchange
3528 * ( CertificateVerify )
3529 * ChangeCipherSpec
3530 * Finished
3531 */
3532 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3533 ret = mbedtls_ssl_write_certificate(ssl);
3534 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3537 ret = ssl_write_client_key_exchange(ssl);
3538 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3541 ret = ssl_write_certificate_verify(ssl);
3542 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3545 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
3546 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 case MBEDTLS_SSL_CLIENT_FINISHED:
3549 ret = mbedtls_ssl_write_finished(ssl);
3550 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003551
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 /*
3553 * <== ( NewSessionTicket )
3554 * ChangeCipherSpec
3555 * Finished
3556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 case MBEDTLS_SSL_NEW_SESSION_TICKET:
3559 ret = ssl_parse_new_session_ticket(ssl);
3560 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003561#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003562
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3564 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
3565 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003566
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 case MBEDTLS_SSL_SERVER_FINISHED:
3568 ret = mbedtls_ssl_parse_finished(ssl);
3569 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003570
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 case MBEDTLS_SSL_FLUSH_BUFFERS:
3572 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
3573 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3574 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003575
Gilles Peskine449bd832023-01-11 14:50:10 +01003576 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3577 mbedtls_ssl_handshake_wrapup(ssl);
3578 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003579
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 default:
3581 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3582 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003584
Gilles Peskine449bd832023-01-11 14:50:10 +01003585 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003586}
Jerry Yuc5aef882021-12-23 20:15:02 +08003587
Jerry Yufb4b6472022-01-27 15:03:26 +08003588#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */