blob: 994661a44cfcb88587c47376d63ad37d8c1c9248 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
SimonBd5800b72016-04-26 07:43:27 +010024#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020027#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Andres Amaya Garcia84914062018-04-24 08:40:46 -050030#include "mbedtls/platform_util.h"
Gabor Mezeic0ae1cf2021-10-20 12:09:35 +020031#include "constant_time_internal.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Rich Evans00ab4702015-02-06 13:43:58 +000033
34#include <string.h>
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010041#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045int mbedtls_ssl_set_client_transport_id(mbedtls_ssl_context *ssl,
46 const unsigned char *info,
47 size_t ilen)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020048{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049 if (ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER) {
50 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
51 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 if ((ssl->cli_id = mbedtls_calloc(1, ilen)) == NULL) {
56 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
57 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 memcpy(ssl->cli_id, info, ilen);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020060 ssl->cli_id_len = ilen;
61
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 return 0;
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020063}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065void mbedtls_ssl_conf_dtls_cookies(mbedtls_ssl_config *conf,
66 mbedtls_ssl_cookie_write_t *f_cookie_write,
67 mbedtls_ssl_cookie_check_t *f_cookie_check,
68 void *p_cookie)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020069{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020070 conf->f_cookie_write = f_cookie_write;
71 conf->f_cookie_check = f_cookie_check;
72 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +020077MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078static int ssl_parse_servername_ext(mbedtls_ssl_context *ssl,
79 const unsigned char *buf,
80 size_t len)
Paul Bakker5701cdc2012-09-27 21:49:42 +000081{
Janos Follath865b3eb2019-12-16 11:46:15 +000082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5701cdc2012-09-27 21:49:42 +000083 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000084 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 if (len < 2) {
89 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
90 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
91 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
92 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Philippe Antoine747fd532018-05-30 09:13:21 +020093 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 servername_list_size = ((buf[0] << 8) | (buf[1]));
95 if (servername_list_size + 2 != len) {
96 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
97 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
98 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
99 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000100 }
101
102 p = buf + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 while (servername_list_size > 2) {
104 hostname_len = ((p[1] << 8) | p[2]);
105 if (hostname_len + 3 > servername_list_size) {
106 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
107 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
108 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
109 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000110 }
111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
113 ret = ssl->conf->f_sni(ssl->conf->p_sni,
114 ssl, p + 3, hostname_len);
115 if (ret != 0) {
116 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
117 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
118 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME);
119 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000120 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 return 0;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000122 }
123
124 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000125 p += hostname_len + 3;
126 }
127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 if (servername_list_size != 0) {
129 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
130 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
131 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
132 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000133 }
134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 return 0;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000138
Gilles Peskineeccd8882020-03-10 12:19:08 +0100139#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200140MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141static int ssl_conf_has_psk_or_cb(mbedtls_ssl_config const *conf)
Hanno Becker845b9462018-10-26 12:07:29 +0100142{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 if (conf->f_psk != NULL) {
144 return 1;
145 }
Hanno Becker845b9462018-10-26 12:07:29 +0100146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 if (conf->psk_identity_len == 0 || conf->psk_identity == NULL) {
148 return 0;
149 }
Hanno Becker845b9462018-10-26 12:07:29 +0100150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 if (conf->psk != NULL && conf->psk_len != 0) {
152 return 1;
153 }
Hanno Becker845b9462018-10-26 12:07:29 +0100154
155#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
157 return 1;
158 }
Hanno Becker845b9462018-10-26 12:07:29 +0100159#endif /* MBEDTLS_USE_PSA_CRYPTO */
160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 return 0;
Hanno Becker845b9462018-10-26 12:07:29 +0100162}
163
164#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200165MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl)
Hanno Becker845b9462018-10-26 12:07:29 +0100167{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 if (ssl->conf->f_psk != NULL) {
Hanno Becker845b9462018-10-26 12:07:29 +0100169 /* If we've used a callback to select the PSK,
170 * the static configuration is irrelevant. */
171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
173 return 1;
174 }
Hanno Becker845b9462018-10-26 12:07:29 +0100175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 return 0;
Hanno Becker845b9462018-10-26 12:07:29 +0100177 }
178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
180 return 1;
181 }
Hanno Becker845b9462018-10-26 12:07:29 +0100182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 return 0;
Hanno Becker845b9462018-10-26 12:07:29 +0100184}
185#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100186#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100187
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200188MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
190 const unsigned char *buf,
191 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100195 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 if (len != 1 + ssl->verify_data_len ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100197 buf[0] != ssl->verify_data_len ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 mbedtls_ct_memcmp(buf + 1, ssl->peer_verify_data,
199 ssl->verify_data_len) != 0) {
200 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
201 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
202 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
203 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100204 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000207 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 if (len != 1 || buf[0] != 0x0) {
209 MBEDTLS_SSL_DEBUG_MSG(1, ("non-zero length renegotiation info"));
210 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
211 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
212 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker48916f92012-09-16 19:57:18 +0000213 }
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000216 }
Paul Bakker48916f92012-09-16 19:57:18 +0000217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000219}
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100222 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100223
224/*
225 * Status of the implementation of signature-algorithms extension:
226 *
227 * Currently, we are only considering the signature-algorithm extension
228 * to pick a ciphersuite which allows us to send the ServerKeyExchange
229 * message with a signature-hash combination that the user allows.
230 *
231 * We do *not* check whether all certificates in our certificate
232 * chain are signed with an allowed signature-hash pair.
233 * This needs to be done at a later stage.
234 *
235 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200236MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237static int ssl_parse_signature_algorithms_ext(mbedtls_ssl_context *ssl,
238 const unsigned char *buf,
239 size_t len)
Paul Bakker23f36802012-09-28 14:15:14 +0000240{
241 size_t sig_alg_list_size;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100242
Paul Bakker23f36802012-09-28 14:15:14 +0000243 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200244 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200245
Hanno Becker7e5437a2017-04-28 17:15:26 +0100246 mbedtls_md_type_t md_cur;
247 mbedtls_pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 if (len < 2) {
250 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
251 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
252 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
253 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Philippe Antoine747fd532018-05-30 09:13:21 +0200254 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 sig_alg_list_size = ((buf[0] << 8) | (buf[1]));
256 if (sig_alg_list_size + 2 != len ||
257 sig_alg_list_size % 2 != 0) {
258 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
259 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
260 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
261 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker23f36802012-09-28 14:15:14 +0000262 }
263
Hanno Becker7e5437a2017-04-28 17:15:26 +0100264 /* Currently we only guarantee signing the ServerKeyExchange message according
265 * to the constraints specified in this extension (see above), so it suffices
266 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200267 *
Hanno Becker7e5437a2017-04-28 17:15:26 +0100268 * This will change when we also consider certificate signatures,
269 * in which case we will need to remember the whole signature-hash
270 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200271 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 for (p = buf + 2; p < end; p += 2) {
Hanno Becker7e5437a2017-04-28 17:15:26 +0100274 /* Silently ignore unknown signature or hash algorithms. */
275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 if ((sig_cur = mbedtls_ssl_pk_alg_from_sig(p[1])) == MBEDTLS_PK_NONE) {
277 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext"
278 " unknown sig alg encoding %d", p[1]));
Hanno Becker7e5437a2017-04-28 17:15:26 +0100279 continue;
280 }
281
282 /* Check if we support the hash the user proposes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 md_cur = mbedtls_ssl_md_alg_from_hash(p[0]);
284 if (md_cur == MBEDTLS_MD_NONE) {
285 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext:"
286 " unknown hash alg encoding %d", p[0]));
Hanno Becker7e5437a2017-04-28 17:15:26 +0100287 continue;
288 }
289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 if (mbedtls_ssl_check_sig_hash(ssl, md_cur) == 0) {
291 mbedtls_ssl_sig_hash_set_add(&ssl->handshake->hash_algs, sig_cur, md_cur);
292 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext:"
293 " match sig %u and hash %u",
294 (unsigned) sig_cur, (unsigned) md_cur));
295 } else {
296 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: "
297 "hash alg %u not supported", (unsigned) md_cur));
Paul Bakker23f36802012-09-28 14:15:14 +0000298 }
Paul Bakker23f36802012-09-28 14:15:14 +0000299 }
300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 return 0;
Paul Bakker23f36802012-09-28 14:15:14 +0000302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100304 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000305
Robert Cragie136884c2015-10-02 13:34:31 +0100306#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100307 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200308MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309static int ssl_parse_supported_elliptic_curves(mbedtls_ssl_context *ssl,
310 const unsigned char *buf,
311 size_t len)
Paul Bakker41c83d32013-03-20 14:39:14 +0100312{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200313 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100314 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 if (len < 2) {
318 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
319 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
320 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
321 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Philippe Antoine747fd532018-05-30 09:13:21 +0200322 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 list_size = ((buf[0] << 8) | (buf[1]));
324 if (list_size + 2 != len ||
325 list_size % 2 != 0) {
326 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
327 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
328 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
329 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker41c83d32013-03-20 14:39:14 +0100330 }
331
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200332 /* Should never happen unless client duplicates the extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 if (ssl->handshake->curves != NULL) {
334 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
335 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
336 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
337 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200338 }
339
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100340 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200341 * and leave room for a final 0 */
342 our_size = list_size / 2 + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 if (our_size > MBEDTLS_ECP_DP_MAX) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 our_size = MBEDTLS_ECP_DP_MAX;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 if ((curves = mbedtls_calloc(our_size, sizeof(*curves))) == NULL) {
348 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
349 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
350 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200351 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200352
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200353 ssl->handshake->curves = curves;
354
Paul Bakker41c83d32013-03-20 14:39:14 +0100355 p = buf + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 while (list_size > 0 && our_size > 1) {
357 curve_info = mbedtls_ecp_curve_info_from_tls_id((p[0] << 8) | p[1]);
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 if (curve_info != NULL) {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200360 *curves++ = curve_info;
361 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100362 }
363
364 list_size -= 2;
365 p += 2;
366 }
367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 return 0;
Paul Bakker41c83d32013-03-20 14:39:14 +0100369}
370
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200371MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372static int ssl_parse_supported_point_formats(mbedtls_ssl_context *ssl,
373 const unsigned char *buf,
374 size_t len)
Paul Bakker41c83d32013-03-20 14:39:14 +0100375{
376 size_t list_size;
377 const unsigned char *p;
378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 if (len == 0 || (size_t) (buf[0] + 1) != len) {
380 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
381 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
382 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
383 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker41c83d32013-03-20 14:39:14 +0100384 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200385 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100386
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200387 p = buf + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 while (list_size > 0) {
389 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
390 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Robert Cragie136884c2015-10-02 13:34:31 +0100391#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200392 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100393#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100394#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Robert Cragie136884c2015-10-02 13:34:31 +0100395 ssl->handshake->ecjpake_ctx.point_format = p[0];
396#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
398 return 0;
Paul Bakker41c83d32013-03-20 14:39:14 +0100399 }
400
401 list_size--;
402 p++;
403 }
404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 return 0;
Paul Bakker41c83d32013-03-20 14:39:14 +0100406}
Robert Cragieae8535d2015-10-06 17:11:18 +0100407#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
408 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100409
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200410#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200411MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
413 const unsigned char *buf,
414 size_t len)
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200415{
Janos Follath865b3eb2019-12-16 11:46:15 +0000416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
419 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
420 return 0;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200421 }
422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
424 buf, len)) != 0) {
425 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
426 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
427 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
428 return ret;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200429 }
430
431 /* Only mark the extension as OK when we're sure it is */
432 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 return 0;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200435}
436#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200439MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
441 const unsigned char *buf,
442 size_t len)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200443{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 if (len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID) {
445 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
446 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
447 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
448 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200449 }
450
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200451 ssl->session_negotiate->mfl_code = buf[0];
452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 return 0;
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200456
Hanno Beckera0e20d02019-05-15 14:03:01 +0100457#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200458MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
460 const unsigned char *buf,
461 size_t len)
Hanno Becker89dcc882019-04-26 13:56:39 +0100462{
463 size_t peer_cid_len;
464
465 /* CID extension only makes sense in DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
467 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
468 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
469 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
470 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Hanno Becker89dcc882019-04-26 13:56:39 +0100471 }
472
473 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100474 * Quoting draft-ietf-tls-dtls-connection-id-05
475 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100476 *
477 * struct {
478 * opaque cid<0..2^8-1>;
479 * } ConnectionId;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 */
Hanno Becker89dcc882019-04-26 13:56:39 +0100481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 if (len < 1) {
483 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
484 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
485 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
486 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Hanno Becker89dcc882019-04-26 13:56:39 +0100487 }
488
489 peer_cid_len = *buf++;
490 len--;
491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 if (len != peer_cid_len) {
493 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
494 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
495 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
496 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Hanno Becker89dcc882019-04-26 13:56:39 +0100497 }
498
499 /* Ignore CID if the user has disabled its use. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 if (ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
Hanno Becker89dcc882019-04-26 13:56:39 +0100501 /* Leave ssl->handshake->cid_in_use in its default
502 * value of MBEDTLS_SSL_CID_DISABLED. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 MBEDTLS_SSL_DEBUG_MSG(3, ("Client sent CID extension, but CID disabled"));
504 return 0;
Hanno Becker89dcc882019-04-26 13:56:39 +0100505 }
506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
508 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
509 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
510 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
511 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Hanno Becker89dcc882019-04-26 13:56:39 +0100512 }
513
Hanno Becker08556bf2019-05-03 12:43:44 +0100514 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100515 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Becker89dcc882019-04-26 13:56:39 +0100517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100518 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
519 MBEDTLS_SSL_DEBUG_BUF(3, "Client CID", buf, peer_cid_len);
Hanno Becker89dcc882019-04-26 13:56:39 +0100520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 return 0;
Hanno Becker89dcc882019-04-26 13:56:39 +0100522}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100523#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200526MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527static int ssl_parse_truncated_hmac_ext(mbedtls_ssl_context *ssl,
528 const unsigned char *buf,
529 size_t len)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200530{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100531 if (len != 0) {
532 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
533 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
534 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
535 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200536 }
537
538 ((void) buf);
539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 if (ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 }
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544 return 0;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200549MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
551 const unsigned char *buf,
552 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100553{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 if (len != 0) {
555 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
556 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
557 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
558 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100559 }
560
561 ((void) buf);
562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
564 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100566 }
567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200573MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
575 const unsigned char *buf,
576 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200577{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 if (len != 0) {
579 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
580 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
581 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
582 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200583 }
584
585 ((void) buf);
586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
588 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200590 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100592 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200597MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
599 unsigned char *buf,
600 size_t len)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200601{
Janos Follath865b3eb2019-12-16 11:46:15 +0000602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200603 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 mbedtls_ssl_session_init(&session);
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 if (ssl->conf->f_ticket_parse == NULL ||
608 ssl->conf->f_ticket_write == NULL) {
609 return 0;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200610 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200611
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200612 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200613 ssl->handshake->new_session_ticket = 1;
614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, len));
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 if (len == 0) {
618 return 0;
619 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
623 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket rejected: renegotiating"));
624 return 0;
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200625 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200627
628 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200629 * Failures are ok: just ignore the ticket and proceed.
630 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100631 if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket, &session,
632 buf, len)) != 0) {
633 mbedtls_ssl_session_free(&session);
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100635 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
636 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
637 } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
638 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
639 } else {
640 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_parse", ret);
641 }
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 return 0;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200644 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200645
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200646 /*
647 * Keep the session ID sent by the client, since we MUST send it back to
648 * inform them we're accepting the ticket (RFC 5077 section 3.4)
649 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200650 session.id_len = ssl->session_negotiate->id_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 memcpy(&session.id, ssl->session_negotiate->id, session.id_len);
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 mbedtls_ssl_session_free(ssl->session_negotiate);
654 memcpy(ssl->session_negotiate, &session, sizeof(mbedtls_ssl_session));
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200655
656 /* Zeroize instead of free as we copied the content */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 mbedtls_platform_zeroize(&session, sizeof(mbedtls_ssl_session));
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from ticket"));
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200660
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200661 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200662
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200663 /* Don't send a new ticket after all, this one is OK */
664 ssl->handshake->new_session_ticket = 0;
665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666 return 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200671MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100672static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
673 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200674{
Paul Bakker14b16c62014-05-28 11:33:54 +0200675 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200676 const unsigned char *theirs, *start, *end;
677 const char **ours;
678
679 /* If ALPN not configured, just ignore the extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100680 if (ssl->conf->alpn_list == NULL) {
681 return 0;
682 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200683
684 /*
685 * opaque ProtocolName<1..2^8-1>;
686 *
687 * struct {
688 * ProtocolName protocol_name_list<2..2^16-1>
689 * } ProtocolNameList;
690 */
691
692 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 if (len < 4) {
694 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
695 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
696 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200697 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 list_len = (buf[0] << 8) | buf[1];
700 if (list_len != len - 2) {
701 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
702 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
703 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200704 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200705
706 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100707 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200708 */
709 start = buf + 2;
710 end = buf + len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 for (theirs = start; theirs != end; theirs += cur_len) {
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100712 cur_len = *theirs++;
713
714 /* Current identifier must fit in list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 if (cur_len > (size_t) (end - theirs)) {
716 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
717 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
718 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100719 }
720
721 /* Empty strings MUST NOT be included */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 if (cur_len == 0) {
723 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
724 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
725 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100726 }
727 }
728
729 /*
730 * Use our order of preference
731 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 for (ours = ssl->conf->alpn_list; *ours != NULL; ours++) {
733 ours_len = strlen(*ours);
734 for (theirs = start; theirs != end; theirs += cur_len) {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200735 cur_len = *theirs++;
736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737 if (cur_len == ours_len &&
738 memcmp(theirs, *ours, cur_len) == 0) {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200739 ssl->alpn_chosen = *ours;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100740 return 0;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200741 }
742 }
743 }
744
745 /* If we get there, no match was found */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
747 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL);
748 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200749}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200751
Johan Pascalb62bb512015-12-03 21:56:45 +0100752#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200753MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
755 const unsigned char *buf,
756 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100757{
Johan Pascal43f94902020-09-22 12:25:52 +0200758 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100759 size_t i, j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200760 size_t profile_length;
761 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200762 /*! 2 bytes for profile length and 1 byte for mki len */
763 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100764
765 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
767 (ssl->conf->dtls_srtp_profile_list == NULL) ||
768 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
769 return 0;
Johan Pascal85269572020-08-25 10:01:54 +0200770 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100771
772 /* RFC5764 section 4.1.1
773 * uint8 SRTPProtectionProfile[2];
774 *
775 * struct {
776 * SRTPProtectionProfiles SRTPProtectionProfiles;
777 * opaque srtp_mki<0..255>;
778 * } UseSRTPData;
779
780 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100781 */
782
Ron Eldoref72faf2018-07-12 11:54:20 +0300783 /*
784 * Min length is 5: at least one protection profile(2 bytes)
785 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200786 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200787 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300788 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100789 if (len < size_of_lengths) {
790 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
791 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
792 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Ron Eldor313d7b52018-12-10 14:56:21 +0200793 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200796
Ron Eldoref72faf2018-07-12 11:54:20 +0300797 /* first 2 bytes are protection profile length(in bytes) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 profile_length = (buf[0] << 8) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200799 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200800
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200801 /* The profile length cannot be bigger than input buffer size - lengths fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 if (profile_length > len - size_of_lengths ||
803 profile_length % 2 != 0) { /* profiles are 2 bytes long, so the length must be even */
804 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
805 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
806 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Ron Eldor313d7b52018-12-10 14:56:21 +0200807 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300808 /*
809 * parse the extension list values are defined in
810 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
811 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812 for (j = 0; j < profile_length; j += 2) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200813 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814 client_protection = mbedtls_ssl_check_srtp_profile_value(protection_profile_value);
Johan Pascalb62bb512015-12-03 21:56:45 +0100815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100816 if (client_protection != MBEDTLS_TLS_SRTP_UNSET) {
817 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
818 mbedtls_ssl_get_srtp_profile_as_string(
819 client_protection)));
820 } else {
Johan Pascal85269572020-08-25 10:01:54 +0200821 continue;
822 }
Ron Eldor591f1622018-01-22 12:30:04 +0200823 /* check if suggested profile is in our list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100824 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
825 if (client_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +0200826 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100827 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
828 mbedtls_ssl_get_srtp_profile_as_string(
829 client_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +0200830 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100831 }
832 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100833 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET) {
Ron Eldor591f1622018-01-22 12:30:04 +0200834 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100835 }
Ron Eldor591f1622018-01-22 12:30:04 +0200836 }
Johan Pascal042d4562020-08-25 12:14:02 +0200837 buf += profile_length; /* buf points to the mki length */
838 mki_length = *buf;
839 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100841 if (mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
842 mki_length + profile_length + size_of_lengths != len) {
843 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
844 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
845 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Johan Pascal042d4562020-08-25 12:14:02 +0200846 }
847
848 /* Parse the mki only if present and mki is supported locally */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
850 mki_length > 0) {
Johan Pascal042d4562020-08-25 12:14:02 +0200851 ssl->dtls_srtp_info.mki_len = mki_length;
852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100853 memcpy(ssl->dtls_srtp_info.mki_value, buf, mki_length);
Ron Eldorb4655392018-07-05 18:25:39 +0300854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100855 MBEDTLS_SSL_DEBUG_BUF(3, "using mki", ssl->dtls_srtp_info.mki_value,
856 ssl->dtls_srtp_info.mki_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100857 }
858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100859 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100860}
861#endif /* MBEDTLS_SSL_DTLS_SRTP */
862
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100863/*
864 * Auxiliary functions for ServerHello parsing and related actions
865 */
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100868/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100869 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200872MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873static int ssl_check_key_curve(mbedtls_pk_context *pk,
874 const mbedtls_ecp_curve_info **curves)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100875{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 const mbedtls_ecp_curve_info **crv = curves;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100877 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 while (*crv != NULL) {
880 if ((*crv)->grp_id == grp_id) {
881 return 0;
882 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100883 crv++;
884 }
885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100886 return -1;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100887}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100889
890/*
891 * Try picking a certificate for this ciphersuite,
892 * return 0 on success and -1 on failure.
893 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200894MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100895static int ssl_pick_cert(mbedtls_ssl_context *ssl,
896 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100899 mbedtls_pk_type_t pk_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900 mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200901 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100904 if (ssl->handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100905 list = ssl->handshake->sni_key_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 } else
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100907#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910 if (pk_alg == MBEDTLS_PK_NONE) {
911 return 0;
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200912 }
913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100914 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite requires certificate"));
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000915
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916 if (list == NULL) {
917 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
918 return -1;
919 }
920
921 for (cur = list; cur != NULL; cur = cur->next) {
922 flags = 0;
923 MBEDTLS_SSL_DEBUG_CRT(3, "candidate certificate chain, certificate",
924 cur->cert);
925
926 if (!mbedtls_pk_can_do(&cur->cert->pk, pk_alg)) {
927 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: key type"));
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100928 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000929 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100930
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200931 /*
932 * This avoids sending the client a cert it'll reject based on
933 * keyUsage or other extensions.
934 *
935 * It also allows the user to provision different certificates for
936 * different uses based on keyUsage, eg if they want to avoid signing
937 * and decrypting with the same RSA key.
938 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 if (mbedtls_ssl_check_cert_usage(cur->cert, ciphersuite_info,
940 MBEDTLS_SSL_IS_SERVER, &flags) != 0) {
941 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
942 "(extended) key usage extension"));
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200943 continue;
944 }
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 if (pk_alg == MBEDTLS_PK_ECDSA &&
948 ssl_check_key_curve(&cur->cert->pk, ssl->handshake->curves) != 0) {
949 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: elliptic curve"));
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100950 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000951 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100952#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100953
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100954 /*
955 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
956 * present them a SHA-higher cert rather than failing if it's the only
957 * one we got that satisfies the other conditions.
958 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100959 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
960 cur->cert->sig_md != MBEDTLS_MD_SHA1) {
961 if (fallback == NULL) {
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100962 fallback = cur;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 }
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000964 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100965 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate not preferred: "
966 "sha-2 with pre-TLS 1.2 client"));
967 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000968 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100969 }
970
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100971 /* If we get there, we got a winner */
972 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100973 }
974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100975 if (cur == NULL) {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000976 cur = fallback;
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100977 }
978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 /* Do not update ssl->handshake->key_cert unless there is a match */
980 if (cur != NULL) {
981 ssl->handshake->key_cert = cur;
982 MBEDTLS_SSL_DEBUG_CRT(3, "selected certificate chain, certificate",
983 ssl->handshake->key_cert->cert);
984 return 0;
985 }
986
987 return -1;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100988}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100990
991/*
992 * Check if a given ciphersuite is suitable for use with our config/keys/etc
993 * Sets ciphersuite_info only if the suite matches.
994 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200995MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996static int ssl_ciphersuite_match(mbedtls_ssl_context *ssl, int suite_id,
997 const mbedtls_ssl_ciphersuite_t **ciphersuite_info)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001000
Hanno Becker7e5437a2017-04-28 17:15:26 +01001001#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001002 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001003 mbedtls_pk_type_t sig_type;
1004#endif
1005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001006 suite_info = mbedtls_ssl_ciphersuite_from_id(suite_id);
1007 if (suite_info == NULL) {
1008 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1009 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001010 }
1011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001012 MBEDTLS_SSL_DEBUG_MSG(3, ("trying ciphersuite: %#04x (%s)",
1013 (unsigned int) suite_id, suite_info->name));
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001015 if (suite_info->min_minor_ver > ssl->minor_ver ||
1016 suite_info->max_minor_ver < ssl->minor_ver) {
1017 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: version"));
1018 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001019 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1023 (suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS)) {
1024 return 0;
1025 }
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001026#endif
1027
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001028#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 if (ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
1030 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128) {
1031 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: rc4"));
1032 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001033 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001034#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001035
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001036#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
1038 (ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK) == 0) {
1039 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: ecjpake "
1040 "not configured or ext missing"));
1041 return 0;
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001042 }
1043#endif
1044
1045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 if (mbedtls_ssl_ciphersuite_uses_ec(suite_info) &&
1048 (ssl->handshake->curves == NULL ||
1049 ssl->handshake->curves[0] == NULL)) {
1050 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
1051 "no common elliptic curve"));
1052 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001053 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001054#endif
1055
Gilles Peskineeccd8882020-03-10 12:19:08 +01001056#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001057 /* If the ciphersuite requires a pre-shared key and we don't
1058 * have one, skip it now rather than failing later */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
1060 ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
1061 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no pre-shared key"));
1062 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001063 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001064#endif
1065
Hanno Becker7e5437a2017-04-28 17:15:26 +01001066#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001067 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001068 /* If the ciphersuite requires signing, check whether
1069 * a suitable hash algorithm is present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001070 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1071 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg(suite_info);
1072 if (sig_type != MBEDTLS_PK_NONE &&
1073 mbedtls_ssl_sig_hash_set_find(&ssl->handshake->hash_algs,
1074 sig_type) == MBEDTLS_MD_NONE) {
1075 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no suitable hash algorithm "
1076 "for signature algorithm %u", (unsigned) sig_type));
1077 return 0;
Hanno Becker7e5437a2017-04-28 17:15:26 +01001078 }
1079 }
1080
1081#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001082 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001085 /*
1086 * Final check: if ciphersuite requires us to have a
1087 * certificate/key of a particular type:
1088 * - select the appropriate certificate if we have one, or
1089 * - try the next ciphersuite if we don't
1090 * This must be done last since we modify the key_cert list.
1091 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 if (ssl_pick_cert(ssl, suite_info) != 0) {
1093 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
1094 "no suitable certificate"));
1095 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001096 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001097#endif
1098
1099 *ciphersuite_info = suite_info;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001100 return 0;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001101}
1102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001104MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001105static int ssl_parse_client_hello_v2(mbedtls_ssl_context *ssl)
Paul Bakker78a8c712013-03-06 17:01:52 +01001106{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001107 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001108 unsigned int i, j;
1109 size_t n;
1110 unsigned int ciph_len, sess_len, chal_len;
1111 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001112 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001115 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello v2"));
Paul Bakker78a8c712013-03-06 17:01:52 +01001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
1119 MBEDTLS_SSL_DEBUG_MSG(1, ("client hello v2 illegal for renegotiation"));
1120 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1121 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1122 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001123 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001125
1126 buf = ssl->in_hdr;
1127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001128 MBEDTLS_SSL_DEBUG_BUF(4, "record header", buf, 5);
Paul Bakker78a8c712013-03-06 17:01:52 +01001129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001130 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, message type: %d",
1131 buf[2]));
1132 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, message len.: %d",
1133 ((buf[0] & 0x7F) << 8) | buf[1]));
1134 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, max. version: [%d:%d]",
1135 buf[3], buf[4]));
Paul Bakker78a8c712013-03-06 17:01:52 +01001136
1137 /*
1138 * SSLv2 Client Hello
1139 *
1140 * Record layer:
1141 * 0 . 1 message length
1142 *
1143 * SSL layer:
1144 * 2 . 2 message type
1145 * 3 . 4 protocol version
1146 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001147 if (buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1148 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3) {
1149 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1150 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001151 }
1152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001153 n = ((buf[0] << 8) | buf[1]) & 0x7FFF;
Paul Bakker78a8c712013-03-06 17:01:52 +01001154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155 if (n < 17 || n > 512) {
1156 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1157 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001158 }
1159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001161 ssl->minor_ver = (buf[4] <= ssl->conf->max_minor_ver)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001162 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 if (ssl->minor_ver < ssl->conf->min_minor_ver) {
1165 MBEDTLS_SSL_DEBUG_MSG(1, ("client only supports ssl smaller than minimum"
1166 " [%d:%d] < [%d:%d]",
1167 ssl->major_ver, ssl->minor_ver,
1168 ssl->conf->min_major_ver, ssl->conf->min_minor_ver));
Paul Bakker78a8c712013-03-06 17:01:52 +01001169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001170 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1171 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
1172 return MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001173 }
1174
Paul Bakker2fbefde2013-06-29 16:01:15 +02001175 ssl->handshake->max_major_ver = buf[3];
1176 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 if ((ret = mbedtls_ssl_fetch_input(ssl, 2 + n)) != 0) {
1179 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
1180 return ret;
Paul Bakker78a8c712013-03-06 17:01:52 +01001181 }
1182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001183 ssl->handshake->update_checksum(ssl, buf + 2, n);
Paul Bakker78a8c712013-03-06 17:01:52 +01001184
1185 buf = ssl->in_msg;
1186 n = ssl->in_left - 5;
1187
1188 /*
1189 * 0 . 1 ciphersuitelist length
1190 * 2 . 3 session id length
1191 * 4 . 5 challenge length
1192 * 6 . .. ciphersuitelist
1193 * .. . .. session id
1194 * .. . .. challenge
1195 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf, n);
Paul Bakker78a8c712013-03-06 17:01:52 +01001197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001198 ciph_len = (buf[0] << 8) | buf[1];
1199 sess_len = (buf[2] << 8) | buf[3];
1200 chal_len = (buf[4] << 8) | buf[5];
Paul Bakker78a8c712013-03-06 17:01:52 +01001201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202 MBEDTLS_SSL_DEBUG_MSG(3, ("ciph_len: %u, sess_len: %u, chal_len: %u",
1203 ciph_len, sess_len, chal_len));
Paul Bakker78a8c712013-03-06 17:01:52 +01001204
1205 /*
1206 * Make sure each parameter length is valid
1207 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 if (ciph_len < 3 || (ciph_len % 3) != 0) {
1209 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1210 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001211 }
1212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001213 if (sess_len > 32) {
1214 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1215 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001216 }
1217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001218 if (chal_len < 8 || chal_len > 32) {
1219 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1220 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001221 }
1222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001223 if (n != 6 + ciph_len + sess_len + chal_len) {
1224 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1225 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001226 }
1227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001228 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1229 buf + 6, ciph_len);
1230 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1231 buf + 6 + ciph_len, sess_len);
1232 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, challenge",
1233 buf + 6 + ciph_len + sess_len, chal_len);
Paul Bakker78a8c712013-03-06 17:01:52 +01001234
1235 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001236 ssl->session_negotiate->id_len = sess_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001237 memset(ssl->session_negotiate->id, 0,
1238 sizeof(ssl->session_negotiate->id));
1239 memcpy(ssl->session_negotiate->id, p, ssl->session_negotiate->id_len);
Paul Bakker78a8c712013-03-06 17:01:52 +01001240
1241 p += sess_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242 memset(ssl->handshake->randbytes, 0, 64);
1243 memcpy(ssl->handshake->randbytes + 32 - chal_len, p, chal_len);
Paul Bakker78a8c712013-03-06 17:01:52 +01001244
1245 /*
1246 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1247 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001248 for (i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3) {
1249 if (p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO) {
1250 MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001252 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
1253 MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
1254 "during renegotiation"));
Paul Bakker78a8c712013-03-06 17:01:52 +01001255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001256 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1257 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1258 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001259 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#endif /* MBEDTLS_SSL_RENEGOTIATION */
1261 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001262 break;
1263 }
1264 }
1265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 for (i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3) {
1268 if (p[0] == 0 &&
1269 MBEDTLS_GET_UINT16_BE(p, 1) != MBEDTLS_SSL_FALLBACK_SCSV_VALUE) {
1270 MBEDTLS_SSL_DEBUG_MSG(3, ("received FALLBACK_SCSV"));
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001272 if (ssl->minor_ver < ssl->conf->max_minor_ver) {
1273 MBEDTLS_SSL_DEBUG_MSG(1, ("inapropriate fallback"));
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001275 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1276 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK);
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001278 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001279 }
1280
1281 break;
1282 }
1283 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001285
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001286 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001287 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001288 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001290 for (j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3) {
1291 for (i = 0; ciphersuites[i] != 0; i++) {
1292 if (p[0] != 0 ||
1293 MBEDTLS_GET_UINT16_BE(p, 1) != ciphersuites[i]) {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001294 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001295 }
Paul Bakker59c28a22013-06-29 15:33:42 +02001296
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001297 got_common_suite = 1;
1298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001299 if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
1300 &ciphersuite_info)) != 0) {
1301 return ret;
1302 }
Paul Bakker59c28a22013-06-29 15:33:42 +02001303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001304 if (ciphersuite_info != NULL) {
Paul Bakker78a8c712013-03-06 17:01:52 +01001305 goto have_ciphersuite_v2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001306 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001307 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001309#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001310 for (i = 0; ciphersuites[i] != 0; i++) {
1311 for (j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3) {
1312 if (p[0] != 0 ||
1313 MBEDTLS_GET_UINT16_BE(p, 1) != ciphersuites[i]) {
Gilles Peskine9f540922022-12-08 21:50:34 +01001314 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001316
1317 got_common_suite = 1;
1318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001319 if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
1320 &ciphersuite_info)) != 0) {
1321 return ret;
1322 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324 if (ciphersuite_info != NULL) {
Gilles Peskine9f540922022-12-08 21:50:34 +01001325 goto have_ciphersuite_v2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001326 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001327 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001328 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001329#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331 if (got_common_suite) {
1332 MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
1333 "but none of them usable"));
1334 return MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE;
1335 } else {
1336 MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
1337 return MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN;
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001338 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001339
1340have_ciphersuite_v2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001341 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info->name));
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001342
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001343 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001344 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001345
1346 /*
1347 * SSLv2 Client Hello relevant renegotiation security checks
1348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1350 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1351 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
1352 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1353 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1354 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001355 }
1356
1357 ssl->in_left = 0;
1358 ssl->state++;
1359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001360 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello v2"));
Paul Bakker78a8c712013-03-06 17:01:52 +01001361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001362 return 0;
Paul Bakker78a8c712013-03-06 17:01:52 +01001363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001365
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001366/* This function doesn't alert on errors that happen early during
1367 ClientHello parsing because they might indicate that the client is
1368 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001369MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001370static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001371{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001372 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001373 size_t i, j;
1374 size_t ciph_offset, comp_offset, ext_offset;
1375 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001377 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001378#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001379 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001381 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001382#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001383 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001384 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001386 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Hanno Becker7e5437a2017-04-28 17:15:26 +01001388 /* If there is no signature-algorithm extension present,
1389 * we need to fall back to the default values for allowed
1390 * signature-hash pairs. */
1391#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001392 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001393 int sig_hash_alg_ext_present = 0;
1394#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001395 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001397 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
David Horstmann74ace592022-10-25 17:26:58 +01001399 int renegotiating = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001401read_record_header:
1402#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001403 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001405 * otherwise read it ourselves manually in order to support SSLv2
1406 * ClientHello, which doesn't use the same record layer format.
1407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
David Horstmann74ace592022-10-25 17:26:58 +01001410 renegotiating = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001412#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001413 if (!renegotiating) {
1414 if ((ret = mbedtls_ssl_fetch_input(ssl, 5)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001415 /* No alert on a read error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
1417 return ret;
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 }
1420
1421 buf = ssl->in_hdr;
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
David Horstmann74ace592022-10-25 17:26:58 +01001424 int is_dtls = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001426 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
David Horstmann74ace592022-10-25 17:26:58 +01001427 is_dtls = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001428 }
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001429#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001430 if (!is_dtls) {
1431 if ((buf[0] & 0x80) != 0) {
1432 return ssl_parse_client_hello_v2(ssl);
1433 }
1434 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001435#endif
1436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 MBEDTLS_SSL_DEBUG_BUF(4, "record header", buf, mbedtls_ssl_in_hdr_len(ssl));
Paul Bakkerec636f32012-09-09 19:17:02 +00001438
Paul Bakkerec636f32012-09-09 19:17:02 +00001439 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001440 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001441 *
1442 * Record layer:
1443 * 0 . 0 message type
1444 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001445 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001446 * 3 . 4 message length
1447 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, message type: %d",
1449 buf[0]));
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001451 if (buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE) {
1452 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1453 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001454 }
1455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, message len.: %d",
1457 (ssl->in_len[0] << 8) | ssl->in_len[1]));
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, protocol version: [%d:%d]",
1460 buf[1], buf[2]));
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001462 mbedtls_ssl_read_version(&major, &minor, ssl->conf->transport, buf + 1);
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001463
1464 /* According to RFC 5246 Appendix E.1, the version here is typically
1465 * "{03,00}, the lowest version number supported by the client, [or] the
1466 * value of ClientHello.client_version", so the only meaningful check here
1467 * is the major version shouldn't be less than 3 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468 if (major < MBEDTLS_SSL_MAJOR_VERSION_3) {
1469 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1470 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakkerec636f32012-09-09 19:17:02 +00001471 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001473 /* For DTLS if this is the initial handshake, remember the client sequence
1474 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001476 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_SSL_RENEGOTIATION)
1478 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001479#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480 ) {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001481 /* Epoch should be 0 for initial handshakes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001482 if (ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0) {
1483 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1484 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001485 }
1486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001487 memcpy(ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6);
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001490 if (mbedtls_ssl_dtls_replay_check(ssl) != 0) {
1491 MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record, discarding"));
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001492 ssl->next_record_offset = 0;
1493 ssl->in_left = 0;
1494 goto read_record_header;
1495 }
1496
1497 /* No MAC to check yet, so we can update right now */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001498 mbedtls_ssl_dtls_replay_update(ssl);
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001499#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001503 msg_len = (ssl->in_len[0] << 8) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001506 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001508 msg_len = ssl->in_hslen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001509 } else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001510#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001511 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 if (msg_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
1513 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1514 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001515 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001517 if ((ret = mbedtls_ssl_fetch_input(ssl,
1518 mbedtls_ssl_in_hdr_len(ssl) + msg_len)) != 0) {
1519 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
1520 return ret;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001521 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001523 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001525 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1526 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len(ssl);
1527 } else
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001528#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001529 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001530 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001531
1532 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001534 MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf, msg_len);
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 ssl->handshake->update_checksum(ssl, buf, msg_len);
Paul Bakkerec636f32012-09-09 19:17:02 +00001537
1538 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001539 * Handshake layer:
1540 * 0 . 0 handshake type
1541 * 1 . 3 handshake length
Shaun Case0e7791f2021-12-20 21:14:10 -08001542 * 4 . 5 DTLS only: message sequence number
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001543 * 6 . 8 DTLS only: fragment offset
1544 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001545 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001546 if (msg_len < mbedtls_ssl_hs_hdr_len(ssl)) {
1547 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1548 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001549 }
1550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake type: %d", buf[0]));
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001553 if (buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
1554 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1555 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001556 }
1557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001558 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake len.: %d",
1559 (buf[1] << 16) | (buf[2] << 8) | buf[3]));
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561 if (buf[1] != 0) {
1562 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != 0",
1563 (unsigned) buf[1]));
1564 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Andrzej Kurek4353d3d2022-06-08 11:53:59 -04001565 }
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001566 /* We don't support fragmentation of ClientHello (yet?) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001567 if (msg_len != mbedtls_ssl_hs_hdr_len(ssl) + ((buf[2] << 8) | buf[3])) {
1568 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != %u + %u",
1569 (unsigned) msg_len,
1570 (unsigned) mbedtls_ssl_hs_hdr_len(ssl),
1571 (unsigned) (buf[2] << 8) | buf[3]));
1572 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001573 }
1574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001576 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001577 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001578 * Copy the client's handshake message_seq on initial handshakes,
1579 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001582 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001583 /* This couldn't be done in ssl_prepare_handshake_record() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 unsigned int cli_msg_seq = (ssl->in_msg[4] << 8) |
1585 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 if (cli_msg_seq != ssl->handshake->in_msg_seq) {
1588 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message_seq: "
1589 "%u (expected %u)", cli_msg_seq,
1590 ssl->handshake->in_msg_seq));
1591 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001592 }
1593
1594 ssl->handshake->in_msg_seq++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001595 } else
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001596#endif
1597 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001598 unsigned int cli_msg_seq = (ssl->in_msg[4] << 8) |
1599 ssl->in_msg[5];
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001600 ssl->handshake->out_msg_seq = cli_msg_seq;
1601 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1602 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001603
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001604 /*
1605 * For now we don't support fragmentation, so make sure
1606 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001607 */
Gilles Peskinef333dfa2022-02-15 23:53:36 +01001608 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 4, ("fragment_offset=%u fragment_length=%u length=%u",
1610 (unsigned) (ssl->in_msg[6] << 16 | ssl->in_msg[7] << 8 | ssl->in_msg[8]),
1611 (unsigned) (ssl->in_msg[9] << 16 | ssl->in_msg[10] << 8 | ssl->in_msg[11]),
1612 (unsigned) (ssl->in_msg[1] << 16 | ssl->in_msg[2] << 8 | ssl->in_msg[3])));
1613 if (ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1614 memcmp(ssl->in_msg + 1, ssl->in_msg + 9, 3) != 0) {
1615 MBEDTLS_SSL_DEBUG_MSG(1, ("ClientHello fragmentation not supported"));
1616 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001617 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001618 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 buf += mbedtls_ssl_hs_hdr_len(ssl);
1622 msg_len -= mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001623
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001624 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001625 * ClientHello layer:
1626 * 0 . 1 protocol version
1627 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1628 * 34 . 35 session id length (1 byte)
1629 * 35 . 34+x session id
1630 * 35+x . 35+x DTLS only: cookie length (1 byte)
1631 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001632 * .. . .. ciphersuite list length (2 bytes)
1633 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001634 * .. . .. compression alg. list length (1 byte)
1635 * .. . .. compression alg. list
1636 * .. . .. extensions length (2 bytes, optional)
1637 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001638 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001639
1640 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001641 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001642 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1643 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001644 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001645 if (msg_len < 38) {
1646 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1647 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001648 }
1649
1650 /*
1651 * Check and save the protocol version
1652 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001653 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, version", buf, 2);
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001655 mbedtls_ssl_read_version(&ssl->major_ver, &ssl->minor_ver,
1656 ssl->conf->transport, buf);
Paul Bakkerec636f32012-09-09 19:17:02 +00001657
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001658 ssl->handshake->max_major_ver = ssl->major_ver;
1659 ssl->handshake->max_minor_ver = ssl->minor_ver;
1660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001661 if (ssl->major_ver < ssl->conf->min_major_ver ||
1662 ssl->minor_ver < ssl->conf->min_minor_ver) {
1663 MBEDTLS_SSL_DEBUG_MSG(1, ("client only supports ssl smaller than minimum"
1664 " [%d:%d] < [%d:%d]",
1665 ssl->major_ver, ssl->minor_ver,
1666 ssl->conf->min_major_ver, ssl->conf->min_minor_ver));
1667 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1668 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
1669 return MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001670 }
1671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001672 if (ssl->major_ver > ssl->conf->max_major_ver) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001673 ssl->major_ver = ssl->conf->max_major_ver;
1674 ssl->minor_ver = ssl->conf->max_minor_ver;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001675 } else if (ssl->minor_ver > ssl->conf->max_minor_ver) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001676 ssl->minor_ver = ssl->conf->max_minor_ver;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001677 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001678
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001679 /*
1680 * Save client random (inc. Unix time)
1681 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001682 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes", buf + 2, 32);
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001684 memcpy(ssl->handshake->randbytes, buf + 2, 32);
Paul Bakkerec636f32012-09-09 19:17:02 +00001685
1686 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001687 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001688 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001689 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001691 if (sess_len > sizeof(ssl->session_negotiate->id) ||
1692 sess_len + 34 + 2 > msg_len) { /* 2 for cipherlist length field */
1693 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1694 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1695 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1696 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakkerec636f32012-09-09 19:17:02 +00001697 }
1698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id", buf + 35, sess_len);
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001700
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001701 ssl->session_negotiate->id_len = sess_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001702 memset(ssl->session_negotiate->id, 0,
1703 sizeof(ssl->session_negotiate->id));
1704 memcpy(ssl->session_negotiate->id, buf + 35,
1705 ssl->session_negotiate->id_len);
Paul Bakkerec636f32012-09-09 19:17:02 +00001706
1707 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001708 * Check the cookie length and content
1709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001712 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001713 cookie_len = buf[cookie_offset];
1714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001715 if (cookie_offset + 1 + cookie_len + 2 > msg_len) {
1716 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1717 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1718 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
1719 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001720 }
1721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001722 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
1723 buf + cookie_offset + 1, cookie_len);
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001726 if (ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#if defined(MBEDTLS_SSL_RENEGOTIATION)
1728 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001729#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001730 ) {
1731 if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
1732 buf + cookie_offset + 1, cookie_len,
1733 ssl->cli_id, ssl->cli_id_len) != 0) {
1734 MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification failed"));
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001735 ssl->handshake->verify_cookie_len = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001736 } else {
1737 MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification passed"));
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001738 ssl->handshake->verify_cookie_len = 0;
1739 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001740 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001742 {
1743 /* We know we didn't send a cookie, so it should be empty */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001744 if (cookie_len != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001745 /* This may be an attacker's probe, so don't send an alert */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001746 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1747 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001748 }
1749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001750 MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification skipped"));
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001751 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001753 /*
1754 * Check the ciphersuitelist length (will be parsed later)
1755 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001756 ciph_offset = cookie_offset + 1 + cookie_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001757 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001759 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761 ciph_len = (buf[ciph_offset + 0] << 8)
1762 | (buf[ciph_offset + 1]);
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001764 if (ciph_len < 2 ||
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001765 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001766 (ciph_len % 2) != 0) {
1767 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1768 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1769 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1770 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakkerec636f32012-09-09 19:17:02 +00001771 }
1772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001773 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1774 buf + ciph_offset + 2, ciph_len);
Paul Bakkerec636f32012-09-09 19:17:02 +00001775
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001776 /*
1777 * Check the compression algorithms length and pick one
1778 */
1779 comp_offset = ciph_offset + 2 + ciph_len;
1780
1781 comp_len = buf[comp_offset];
1782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001783 if (comp_len < 1 ||
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001784 comp_len > 16 ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001785 comp_len + comp_offset + 1 > msg_len) {
1786 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1787 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1788 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1789 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakkerec636f32012-09-09 19:17:02 +00001790 }
1791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001792 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, compression",
1793 buf + comp_offset + 1, comp_len);
Paul Bakker48916f92012-09-16 19:57:18 +00001794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1796#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001797 for (i = 0; i < comp_len; ++i) {
1798 if (buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001800 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 }
1802 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001803#endif
1804
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001805 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001807 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001809 }
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001810#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001811
Janos Follathc6dab2b2016-05-23 14:27:02 +01001812 /* Do not parse the extensions if the protocol is SSLv3 */
1813#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 if ((ssl->major_ver != 3) || (ssl->minor_ver != 0)) {
Janos Follathc6dab2b2016-05-23 14:27:02 +01001815#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001816 /*
1817 * Check the extension length
1818 */
1819 ext_offset = comp_offset + 1 + comp_len;
1820 if (msg_len > ext_offset) {
1821 if (msg_len < ext_offset + 2) {
1822 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1823 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1824 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1825 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001826 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001828 ext_len = (buf[ext_offset + 0] << 8)
1829 | (buf[ext_offset + 1]);
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001831 if (msg_len != ext_offset + 2 + ext_len) {
1832 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1833 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1834 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1835 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
1836 }
1837 } else {
1838 ext_len = 0;
1839 }
Paul Bakker48916f92012-09-16 19:57:18 +00001840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001841 ext = buf + ext_offset + 2;
1842 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", ext, ext_len);
1843
1844 while (ext_len != 0) {
1845 unsigned int ext_id;
1846 unsigned int ext_size;
1847 if (ext_len < 4) {
1848 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1849 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1850 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1851 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
1852 }
1853 ext_id = ((ext[0] << 8) | (ext[1]));
1854 ext_size = ((ext[2] << 8) | (ext[3]));
1855
1856 if (ext_size + 4 > ext_len) {
1857 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1858 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1859 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1860 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
1861 }
1862 switch (ext_id) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001864 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001865 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1866 if (ssl->conf->f_sni == NULL) {
Simon Butcher584a5472016-05-23 16:24:52 +01001867 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001868 }
Paul Bakker5701cdc2012-09-27 21:49:42 +00001869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001870 ret = ssl_parse_servername_ext(ssl, ext + 4, ext_size);
1871 if (ret != 0) {
1872 return ret;
1873 }
Simon Butcher584a5472016-05-23 16:24:52 +01001874 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001876
Simon Butcher584a5472016-05-23 16:24:52 +01001877 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001878 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001880 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001881#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001883 ret = ssl_parse_renegotiation_info(ssl, ext + 4, ext_size);
1884 if (ret != 0) {
1885 return ret;
1886 }
Simon Butcher584a5472016-05-23 16:24:52 +01001887 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001890 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001891 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001892 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
Ron Eldor73a38172017-10-03 15:58:26 +03001893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001894 ret = ssl_parse_signature_algorithms_ext(ssl, ext + 4, ext_size);
1895 if (ret != 0) {
1896 return ret;
1897 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01001898
1899 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001900 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001902 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001903
Robert Cragie136884c2015-10-02 13:34:31 +01001904#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001905 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001906 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001907 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported elliptic curves extension"));
Paul Bakker41c83d32013-03-20 14:39:14 +01001908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001909 ret = ssl_parse_supported_elliptic_curves(ssl, ext + 4, ext_size);
1910 if (ret != 0) {
1911 return ret;
1912 }
Simon Butcher584a5472016-05-23 16:24:52 +01001913 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001914
Simon Butcher584a5472016-05-23 16:24:52 +01001915 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported point formats extension"));
Simon Butcher584a5472016-05-23 16:24:52 +01001917 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 ret = ssl_parse_supported_point_formats(ssl, ext + 4, ext_size);
1920 if (ret != 0) {
1921 return ret;
1922 }
Simon Butcher584a5472016-05-23 16:24:52 +01001923 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001924#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1925 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001926
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001927#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001928 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001929 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake kkpp extension"));
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001931 ret = ssl_parse_ecjpake_kkpp(ssl, ext + 4, ext_size);
1932 if (ret != 0) {
1933 return ret;
1934 }
Simon Butcher584a5472016-05-23 16:24:52 +01001935 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001936#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001939 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 MBEDTLS_SSL_DEBUG_MSG(3, ("found max fragment length extension"));
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001942 ret = ssl_parse_max_fragment_length_ext(ssl, ext + 4, ext_size);
1943 if (ret != 0) {
1944 return ret;
1945 }
Simon Butcher584a5472016-05-23 16:24:52 +01001946 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001950 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 MBEDTLS_SSL_DEBUG_MSG(3, ("found truncated hmac extension"));
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001953 ret = ssl_parse_truncated_hmac_ext(ssl, ext + 4, ext_size);
1954 if (ret != 0) {
1955 return ret;
1956 }
Simon Butcher584a5472016-05-23 16:24:52 +01001957 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001959
Hanno Beckera0e20d02019-05-15 14:03:01 +01001960#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01001961 case MBEDTLS_TLS_EXT_CID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Becker89dcc882019-04-26 13:56:39 +01001963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001964 ret = ssl_parse_cid_ext(ssl, ext + 4, ext_size);
1965 if (ret != 0) {
1966 return ret;
1967 }
Hanno Becker89dcc882019-04-26 13:56:39 +01001968 break;
1969#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001972 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001973 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt then mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 ret = ssl_parse_encrypt_then_mac_ext(ssl, ext + 4, ext_size);
1976 if (ret != 0) {
1977 return ret;
1978 }
Simon Butcher584a5472016-05-23 16:24:52 +01001979 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001983 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001984 MBEDTLS_SSL_DEBUG_MSG(3, ("found extended master secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001985
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001986 ret = ssl_parse_extended_ms_ext(ssl, ext + 4, ext_size);
1987 if (ret != 0) {
1988 return ret;
1989 }
Simon Butcher584a5472016-05-23 16:24:52 +01001990 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001994 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 MBEDTLS_SSL_DEBUG_MSG(3, ("found session ticket extension"));
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 ret = ssl_parse_session_ticket_ext(ssl, ext + 4, ext_size);
1998 if (ret != 0) {
1999 return ret;
2000 }
Simon Butcher584a5472016-05-23 16:24:52 +01002001 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01002005 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002006 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002008 ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size);
2009 if (ret != 0) {
2010 return ret;
2011 }
Simon Butcher584a5472016-05-23 16:24:52 +01002012 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002014
Johan Pascalb62bb512015-12-03 21:56:45 +01002015#if defined(MBEDTLS_SSL_DTLS_SRTP)
2016 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002017 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascald576fdb2020-09-22 10:39:53 +02002018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size);
2020 if (ret != 0) {
2021 return ret;
2022 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002023 break;
2024#endif /* MBEDTLS_SSL_DTLS_SRTP */
2025
Simon Butcher584a5472016-05-23 16:24:52 +01002026 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002027 MBEDTLS_SSL_DEBUG_MSG(3, ("unknown extension found: %u (ignoring)",
2028 ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00002029 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002030
2031 ext_len -= 4 + ext_size;
2032 ext += 4 + ext_size;
Janos Follathc6dab2b2016-05-23 14:27:02 +01002033 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034#if defined(MBEDTLS_SSL_PROTO_SSL3)
2035}
Janos Follathc6dab2b2016-05-23 14:27:02 +01002036#endif
2037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002039 for (i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2) {
2040 if (MBEDTLS_GET_UINT16_BE(p, 0) == MBEDTLS_SSL_FALLBACK_SCSV_VALUE) {
2041 MBEDTLS_SSL_DEBUG_MSG(2, ("received FALLBACK_SCSV"));
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002043 if (ssl->minor_ver < ssl->conf->max_minor_ver) {
2044 MBEDTLS_SSL_DEBUG_MSG(1, ("inapropriate fallback"));
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2047 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK);
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002049 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002050 }
2051
2052 break;
2053 }
2054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002056
Hanno Becker7e5437a2017-04-28 17:15:26 +01002057#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002058 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002059
2060 /*
2061 * Try to fall back to default hash SHA1 if the client
2062 * hasn't provided any preferred signature-hash combinations.
2063 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002064 if (sig_hash_alg_ext_present == 0) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01002065 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 if (mbedtls_ssl_check_sig_hash(ssl, md_default) != 0) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01002068 md_default = MBEDTLS_MD_NONE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002069 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01002070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071 mbedtls_ssl_sig_hash_set_const_hash(&ssl->handshake->hash_algs, md_default);
Hanno Becker7e5437a2017-04-28 17:15:26 +01002072 }
2073
2074#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002075 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002076
Paul Bakker48916f92012-09-16 19:57:18 +00002077 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002078 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2079 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002080 for (i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2) {
2081 if (p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO) {
2082 MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002084 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
2085 MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
2086 "during renegotiation"));
2087 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2088 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2089 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002090 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002091#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002093 break;
2094 }
2095 }
2096
2097 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002098 * Renegotiation security checks
2099 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002100 if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
2101 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
2102 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002103 handshake_failure = 1;
2104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002106 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 renegotiation_info_seen == 0) {
2109 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension missing (secure)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002110 handshake_failure = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002111 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2112 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2113 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
2114 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002115 handshake_failure = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002116 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2117 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2118 renegotiation_info_seen == 1) {
2119 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002120 handshake_failure = 1;
2121 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 if (handshake_failure == 1) {
2125 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2126 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2127 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker48916f92012-09-16 19:57:18 +00002128 }
Paul Bakker380da532012-04-18 16:10:25 +00002129
Paul Bakker41c83d32013-03-20 14:39:14 +01002130 /*
2131 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002132 * (At the end because we need information from the EC-based extensions
2133 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002134 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002135 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002136 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002137 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139 for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
2140 for (i = 0; ciphersuites[i] != 0; i++) {
2141 if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002142 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002144
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002145 got_common_suite = 1;
2146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002147 if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
2148 &ciphersuite_info)) != 0) {
2149 return ret;
2150 }
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 if (ciphersuite_info != NULL) {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002153 goto have_ciphersuite;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002154 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002155 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002156 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002157#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 for (i = 0; ciphersuites[i] != 0; i++) {
2159 for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
2160 if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
Gilles Peskine9f540922022-12-08 21:50:34 +01002161 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002163
2164 got_common_suite = 1;
2165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
2167 &ciphersuite_info)) != 0) {
2168 return ret;
2169 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002171 if (ciphersuite_info != NULL) {
Gilles Peskine9f540922022-12-08 21:50:34 +01002172 goto have_ciphersuite;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002174 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002175 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002176#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002178 if (got_common_suite) {
2179 MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
2180 "but none of them usable"));
2181 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2182 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2183 return MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE;
2184 } else {
2185 MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
2186 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2187 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2188 return MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN;
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002189 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002190
2191have_ciphersuite:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info->name));
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002193
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002194 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002195 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002196
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 ssl->state++;
2198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2201 mbedtls_ssl_recv_flight_completed(ssl);
2202 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002203#endif
2204
Hanno Becker7e5437a2017-04-28 17:15:26 +01002205 /* Debugging-only output for testsuite */
2206#if defined(MBEDTLS_DEBUG_C) && \
2207 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002208 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
2210 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg(ciphersuite_info);
2211 if (sig_alg != MBEDTLS_PK_NONE) {
2212 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find(&ssl->handshake->hash_algs,
2213 sig_alg);
2214 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: %d",
2215 mbedtls_ssl_hash_from_md_alg(md_alg)));
2216 } else {
2217 MBEDTLS_SSL_DEBUG_MSG(3, ("no hash algorithm for signature algorithm "
2218 "%u - should not happen", (unsigned) sig_alg));
Hanno Becker7e5437a2017-04-28 17:15:26 +01002219 }
2220 }
2221#endif
2222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002223 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002225 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002226}
2227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002229static void ssl_write_truncated_hmac_ext(mbedtls_ssl_context *ssl,
2230 unsigned char *buf,
2231 size_t *olen)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002232{
2233 unsigned char *p = buf;
2234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235 if (ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED) {
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002236 *olen = 0;
2237 return;
2238 }
2239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002240 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding truncated hmac extension"));
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002242 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002243 p += 2;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002244
2245 *p++ = 0x00;
2246 *p++ = 0x00;
2247
2248 *olen = 4;
2249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002251
Hanno Beckera0e20d02019-05-15 14:03:01 +01002252#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002253static void ssl_write_cid_ext(mbedtls_ssl_context *ssl,
2254 unsigned char *buf,
2255 size_t *olen)
Hanno Becker51de2d32019-04-26 15:46:55 +01002256{
2257 unsigned char *p = buf;
2258 size_t ext_len;
2259 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2260
2261 *olen = 0;
2262
2263 /* Skip writing the extension if we don't want to use it or if
2264 * the client hasn't offered it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED) {
Hanno Becker51de2d32019-04-26 15:46:55 +01002266 return;
2267 }
2268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002269 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2270 * which is at most 255, so the increment cannot overflow. */
2271 if (end < p || (size_t) (end - p) < (unsigned) (ssl->own_cid_len + 5)) {
2272 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2273 return;
2274 }
2275
2276 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding CID extension"));
Hanno Becker51de2d32019-04-26 15:46:55 +01002277
2278 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002279 * Quoting draft-ietf-tls-dtls-connection-id-05
2280 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002281 *
2282 * struct {
2283 * opaque cid<0..2^8-1>;
2284 * } ConnectionId;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002285 */
2286 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002287 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01002288 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002289 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002290 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01002291
2292 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002293 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker51de2d32019-04-26 15:46:55 +01002294
2295 *olen = ssl->own_cid_len + 5;
2296}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002297#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002300static void ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
2301 unsigned char *buf,
2302 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002303{
2304 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2306 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Ronald Cronf1ed5952022-03-24 14:15:28 +01002309 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002310 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002311
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002312 /*
2313 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2314 * from a client and then selects a stream or Authenticated Encryption
2315 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2316 * encrypt-then-MAC response extension back to the client."
2317 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318 if ((suite = mbedtls_ssl_ciphersuite_from_id(
2319 ssl->session_negotiate->ciphersuite)) == NULL ||
2320 (cipher = mbedtls_cipher_info_from_type(suite->cipher)) == NULL ||
2321 cipher->mode != MBEDTLS_MODE_CBC) {
Ronald Cronf1ed5952022-03-24 14:15:28 +01002322 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
2323 }
2324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002325 if (ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002326 *olen = 0;
2327 return;
2328 }
2329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding encrypt then mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002332 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002333 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002334
2335 *p++ = 0x00;
2336 *p++ = 0x00;
2337
2338 *olen = 4;
2339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343static void ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
2344 unsigned char *buf,
2345 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002346{
2347 unsigned char *p = buf;
2348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 if (ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2350 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002351 *olen = 0;
2352 return;
2353 }
2354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002355 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding extended master secret "
2356 "extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002358 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002359 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002360
2361 *p++ = 0x00;
2362 *p++ = 0x00;
2363
2364 *olen = 4;
2365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002369static void ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
2370 unsigned char *buf,
2371 size_t *olen)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002372{
2373 unsigned char *p = buf;
2374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002375 if (ssl->handshake->new_session_ticket == 0) {
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002376 *olen = 0;
2377 return;
2378 }
2379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002380 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding session ticket extension"));
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002382 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002383 p += 2;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002384
2385 *p++ = 0x00;
2386 *p++ = 0x00;
2387
2388 *olen = 4;
2389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392static void ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
2393 unsigned char *buf,
2394 size_t *olen)
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002395{
2396 unsigned char *p = buf;
2397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION) {
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002399 *olen = 0;
2400 return;
2401 }
2402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, secure renegotiation extension"));
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002406 p += 2;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002410 *p++ = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002411 *p++ = (ssl->verify_data_len * 2 + 1) & 0xFF;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002412 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002414 memcpy(p, ssl->peer_verify_data, ssl->verify_data_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002415 p += ssl->verify_data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002416 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002417 p += ssl->verify_data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002418 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002420 {
2421 *p++ = 0x00;
2422 *p++ = 0x01;
2423 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002424 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002425
2426 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002427}
2428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002430static void ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
2431 unsigned char *buf,
2432 size_t *olen)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002433{
2434 unsigned char *p = buf;
2435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002436 if (ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002437 *olen = 0;
2438 return;
2439 }
2440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002441 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, max_fragment_length extension"));
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002443 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002444 p += 2;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002445
2446 *p++ = 0x00;
2447 *p++ = 1;
2448
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002449 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002450
2451 *olen = 5;
2452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002454
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002455#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002456 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002457static void ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
2458 unsigned char *buf,
2459 size_t *olen)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002460{
2461 unsigned char *p = buf;
2462 ((void) ssl);
2463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464 if ((ssl->handshake->cli_exts &
2465 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT) == 0) {
Paul Bakker677377f2013-10-28 12:54:26 +01002466 *olen = 0;
2467 return;
2468 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002470 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002472 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002473 p += 2;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002474
2475 *p++ = 0x00;
2476 *p++ = 2;
2477
2478 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002480
2481 *olen = 6;
2482}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002483#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002484
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002485#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002486static void ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
2487 unsigned char *buf,
2488 size_t *olen)
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002489{
Janos Follath865b3eb2019-12-16 11:46:15 +00002490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002491 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002492 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002493 size_t kkpp_len;
2494
2495 *olen = 0;
2496
2497 /* Skip costly computation if not needed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002498 if (ssl->handshake->ciphersuite_info->key_exchange !=
2499 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002500 return;
2501 }
2502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002503 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, ecjpake kkpp extension"));
2504
2505 if (end - p < 4) {
2506 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2507 return;
2508 }
2509
2510 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002511 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002513 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
2514 p + 2, end - p - 2, &kkpp_len,
2515 ssl->conf->f_rng, ssl->conf->p_rng);
2516 if (ret != 0) {
2517 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_one", ret);
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002518 return;
2519 }
2520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002521 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002522 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002523
2524 *olen = kkpp_len + 4;
2525}
2526#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002528#if defined(MBEDTLS_SSL_ALPN)
2529static void ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
2530 unsigned char *buf, size_t *olen)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002531{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002532 if (ssl->alpn_chosen == NULL) {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002533 *olen = 0;
2534 return;
2535 }
2536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002537 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding alpn extension"));
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002538
2539 /*
2540 * 0 . 1 ext identifier
2541 * 2 . 3 ext length
2542 * 4 . 5 protocol list length
2543 * 6 . 6 protocol name length
2544 * 7 . 7+n protocol name
2545 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002546 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, buf, 0);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548 *olen = 7 + strlen(ssl->alpn_chosen);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002550 MBEDTLS_PUT_UINT16_BE(*olen - 4, buf, 2);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 MBEDTLS_PUT_UINT16_BE(*olen - 6, buf, 4);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 buf[6] = MBEDTLS_BYTE_0(*olen - 7);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002556 memcpy(buf + 7, ssl->alpn_chosen, *olen - 7);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002560#if defined(MBEDTLS_SSL_DTLS_SRTP) && defined(MBEDTLS_SSL_PROTO_DTLS)
2561static void ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
2562 unsigned char *buf,
2563 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +01002564{
Ron Eldor75870ec2018-12-06 17:31:55 +02002565 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002566 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002567 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2568
2569 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002571 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
2572 (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET)) {
Johan Pascalb62bb512015-12-03 21:56:45 +01002573 return;
2574 }
2575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002576 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01002577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002578 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02002579 mki_len = ssl->dtls_srtp_info.mki_len;
2580 }
2581
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002582 /* The extension total size is 9 bytes :
2583 * - 2 bytes for the extension tag
2584 * - 2 bytes for the total size
2585 * - 2 bytes for the protection profile length
2586 * - 2 bytes for the protection profile
2587 * - 1 byte for the mki length
2588 * + the actual mki length
2589 * Check we have enough room in the output buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002590 if ((size_t) (end - buf) < mki_len + 9) {
2591 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
Johan Pascal8f70fba2020-09-02 10:32:06 +02002592 return;
2593 }
2594
Johan Pascalb62bb512015-12-03 21:56:45 +01002595 /* extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002596 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0);
Ron Eldoref72faf2018-07-12 11:54:20 +03002597 /*
2598 * total length 5 and mki value: only one profile(2 bytes)
2599 * and length(2 bytes) and srtp_mki )
2600 */
Ron Eldor591f1622018-01-22 12:30:04 +02002601 ext_len = 5 + mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002602 MBEDTLS_PUT_UINT16_BE(ext_len, buf, 2);
Johan Pascalb62bb512015-12-03 21:56:45 +01002603
2604 /* protection profile length: 2 */
2605 buf[4] = 0x00;
2606 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002607 profile_value = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002608 ssl->dtls_srtp_info.chosen_dtls_srtp_profile);
2609 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
2610 MBEDTLS_PUT_UINT16_BE(profile_value, buf, 6);
2611 } else {
2612 MBEDTLS_SSL_DEBUG_MSG(1, ("use_srtp extension invalid profile"));
Ron Eldor089c9fe2018-12-06 17:12:49 +02002613 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002614 }
2615
Ron Eldor591f1622018-01-22 12:30:04 +02002616 buf[8] = mki_len & 0xFF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617 memcpy(&buf[9], ssl->dtls_srtp_info.mki_value, mki_len);
Johan Pascalb62bb512015-12-03 21:56:45 +01002618
Ron Eldor591f1622018-01-22 12:30:04 +02002619 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002620}
2621#endif /* MBEDTLS_SSL_DTLS_SRTP */
2622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002624MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002625static int ssl_write_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002626{
Janos Follath865b3eb2019-12-16 11:46:15 +00002627 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002628 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002629 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002631 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello verify request"));
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002632
2633 /*
2634 * struct {
2635 * ProtocolVersion server_version;
2636 * opaque cookie<0..2^8-1>;
2637 * } HelloVerifyRequest;
2638 */
2639
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002640 /* The RFC is not clear on this point, but sending the actual negotiated
2641 * version looks like the most interoperable thing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2643 ssl->conf->transport, p);
2644 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002645 p += 2;
2646
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002647 /* If we get here, f_cookie_check is not null */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648 if (ssl->conf->f_cookie_write == NULL) {
2649 MBEDTLS_SSL_DEBUG_MSG(1, ("inconsistent cookie callbacks"));
2650 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002651 }
2652
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002653 /* Skip length byte until we know the length */
2654 cookie_len_byte = p++;
2655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002656 if ((ret = ssl->conf->f_cookie_write(ssl->conf->p_cookie,
2657 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
2658 ssl->cli_id, ssl->cli_id_len)) != 0) {
2659 MBEDTLS_SSL_DEBUG_RET(1, "f_cookie_write", ret);
2660 return ret;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002661 }
2662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002663 *cookie_len_byte = (unsigned char) (p - (cookie_len_byte + 1));
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002665 MBEDTLS_SSL_DEBUG_BUF(3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte);
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002666
2667 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2669 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002673 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2674 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2675 return ret;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002676 }
2677
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002678#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002679 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2680 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
2681 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
2682 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002683 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002684#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002686 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello verify request"));
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688 return 0;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002692static void ssl_handle_id_based_session_resumption(mbedtls_ssl_context *ssl)
Hanno Becker83e36712021-04-15 08:19:40 +01002693{
2694 int ret;
2695 mbedtls_ssl_session session_tmp;
2696 mbedtls_ssl_session * const session = ssl->session_negotiate;
2697
2698 /* Resume is 0 by default, see ssl_handshake_init().
2699 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002700 if (ssl->handshake->resume == 1) {
Hanno Becker83e36712021-04-15 08:19:40 +01002701 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002702 }
2703 if (session->id_len == 0) {
Hanno Becker83e36712021-04-15 08:19:40 +01002704 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 }
2706 if (ssl->conf->f_get_cache == NULL) {
Hanno Becker83e36712021-04-15 08:19:40 +01002707 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002708 }
Hanno Becker83e36712021-04-15 08:19:40 +01002709#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Hanno Becker83e36712021-04-15 08:19:40 +01002711 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002712 }
Hanno Becker83e36712021-04-15 08:19:40 +01002713#endif
2714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002715 mbedtls_ssl_session_init(&session_tmp);
Hanno Becker83e36712021-04-15 08:19:40 +01002716
2717 session_tmp.id_len = session->id_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002718 memcpy(session_tmp.id, session->id, session->id_len);
Hanno Becker83e36712021-04-15 08:19:40 +01002719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002720 ret = ssl->conf->f_get_cache(ssl->conf->p_cache,
2721 &session_tmp);
2722 if (ret != 0) {
Hanno Becker83e36712021-04-15 08:19:40 +01002723 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 }
Hanno Becker83e36712021-04-15 08:19:40 +01002725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002726 if (session->ciphersuite != session_tmp.ciphersuite ||
2727 session->compression != session_tmp.compression) {
Hanno Becker83e36712021-04-15 08:19:40 +01002728 /* Mismatch between cached and negotiated session */
2729 goto exit;
2730 }
2731
2732 /* Move semantics */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002733 mbedtls_ssl_session_free(session);
Hanno Becker83e36712021-04-15 08:19:40 +01002734 *session = session_tmp;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002735 memset(&session_tmp, 0, sizeof(session_tmp));
Hanno Becker83e36712021-04-15 08:19:40 +01002736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002737 MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from cache"));
Hanno Becker83e36712021-04-15 08:19:40 +01002738 ssl->handshake->resume = 1;
2739
2740exit:
2741
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002742 mbedtls_ssl_session_free(&session_tmp);
Hanno Becker83e36712021-04-15 08:19:40 +01002743}
2744
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002745MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746static int ssl_write_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002747{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002749 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002750#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002752 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 unsigned char *buf, *p;
2754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002755 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002758 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2759 ssl->handshake->verify_cookie_len != 0) {
2760 MBEDTLS_SSL_DEBUG_MSG(2, ("client hello was not authenticated"));
2761 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002763 return ssl_write_hello_verify_request(ssl);
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002764 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002767 if (ssl->conf->f_rng == NULL) {
2768 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
2769 return MBEDTLS_ERR_SSL_NO_RNG;
Paul Bakkera9a028e2013-11-21 17:31:06 +01002770 }
2771
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 /*
2773 * 0 . 0 handshake type
2774 * 1 . 3 handshake length
2775 * 4 . 5 protocol version
2776 * 6 . 9 UNIX time()
2777 * 10 . 37 random bytes
2778 */
2779 buf = ssl->out_msg;
2780 p = buf + 4;
2781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2783 ssl->conf->transport, p);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002784 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002786 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen version: [%d:%d]",
2787 buf[4], buf[5]));
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002790 t = mbedtls_time(NULL);
2791 MBEDTLS_PUT_UINT32_BE(t, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002792 p += 4;
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2795 (long long) t));
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002796#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 4)) != 0) {
2798 return ret;
2799 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002800
2801 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002804 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 28)) != 0) {
2805 return ret;
2806 }
Paul Bakkera3d195c2011-11-27 21:07:34 +00002807
2808 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002810 memcpy(ssl->handshake->randbytes + 32, buf + 6, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 6, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002814 ssl_handle_id_based_session_resumption(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002816 if (ssl->handshake->resume == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002818 * New session, create a new session id,
2819 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 ssl->state++;
2822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824 ssl->session_negotiate->start = mbedtls_time(NULL);
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002825#endif
2826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002828 if (ssl->handshake->new_session_ticket != 0) {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002829 ssl->session_negotiate->id_len = n = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002830 memset(ssl->session_negotiate->id, 0, 32);
2831 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002833 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002834 ssl->session_negotiate->id_len = n = 32;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, ssl->session_negotiate->id,
2836 n)) != 0) {
2837 return ret;
2838 }
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002839 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002840 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002842 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002844 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002847 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2848 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2849 return ret;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002851 }
2852
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002853 /*
2854 * 38 . 38 session id length
2855 * 39 . 38+n session id
2856 * 39+n . 40+n chosen ciphersuite
2857 * 41+n . 41+n chosen compression alg.
2858 * 42+n . 43+n extensions length
2859 * 44+n . 43+n+m extensions
2860 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002861 *p++ = (unsigned char) ssl->session_negotiate->id_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002862 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002863 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002865 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
2866 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 39, n);
2867 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
2868 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002870 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002871 p += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002872 *p++ = MBEDTLS_BYTE_0(ssl->session_negotiate->compression);
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002874 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %s",
2875 mbedtls_ssl_get_ciphersuite_name(ssl->session_negotiate->ciphersuite)));
2876 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: 0x%02X",
2877 (unsigned int) ssl->session_negotiate->compression));
Paul Bakker48916f92012-09-16 19:57:18 +00002878
Janos Follathc6dab2b2016-05-23 14:27:02 +01002879 /* Do not write the extensions if the protocol is SSLv3 */
2880#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 if ((ssl->major_ver != 3) || (ssl->minor_ver != 0)) {
Janos Follathc6dab2b2016-05-23 14:27:02 +01002882#endif
2883
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002884 /*
2885 * First write extensions, then the total length
2886 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002887 ssl_write_renegotiation_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002888 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002891 ssl_write_max_fragment_length_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002892 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002893#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002896 ssl_write_truncated_hmac_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002897 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002898#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002899
Hanno Beckera0e20d02019-05-15 14:03:01 +01002900#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 ssl_write_cid_ext(ssl, p + 2 + ext_len, &olen);
Hanno Becker51de2d32019-04-26 15:46:55 +01002902 ext_len += olen;
2903#endif
2904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002906 ssl_write_encrypt_then_mac_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002907 ext_len += olen;
2908#endif
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 ssl_write_extended_ms_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002912 ext_len += olen;
2913#endif
2914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002916 ssl_write_session_ticket_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002917 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002918#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002919
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002920#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002921 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 if (mbedtls_ssl_ciphersuite_uses_ec(
2923 mbedtls_ssl_ciphersuite_from_id(ssl->session_negotiate->ciphersuite))) {
2924 ssl_write_supported_point_formats_ext(ssl, p + 2 + ext_len, &olen);
Ron Eldor755bb6a2018-02-14 19:30:48 +02002925 ext_len += olen;
2926 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002927#endif
2928
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002929#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930 ssl_write_ecjpake_kkpp_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002931 ext_len += olen;
2932#endif
2933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 ssl_write_alpn_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002936 ext_len += olen;
2937#endif
2938
Johan Pascalb62bb512015-12-03 21:56:45 +01002939#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002940 ssl_write_use_srtp_ext(ssl, p + 2 + ext_len, &olen);
Johan Pascalc3ccd982020-10-28 17:18:18 +01002941 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01002942#endif
2943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002944 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
2945 ext_len));
Paul Bakker48916f92012-09-16 19:57:18 +00002946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 if (ext_len > 0) {
2948 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani24647c52021-08-20 15:56:22 +01002949 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02002950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Janos Follathc6dab2b2016-05-23 14:27:02 +01002952#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002953}
Janos Follathc6dab2b2016-05-23 14:27:02 +01002954#endif
2955
Paul Bakker5121ce52009-01-03 21:22:43 +00002956 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2958 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002960 ret = mbedtls_ssl_write_handshake_msg(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002962 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002965}
2966
Gilles Peskineeccd8882020-03-10 12:19:08 +01002967#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002968MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002970{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002971 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002972 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002973
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002974 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002976 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2977 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002978 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002979 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002980 }
2981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002982 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2983 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002984}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002985#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002986MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002988{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002990 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002991 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03002992 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002993 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002995 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002997 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002999 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003000
3001 ssl->state++;
3002
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003003#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003004 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003005 authmode = ssl->handshake->sni_authmode;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003006 } else
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003007#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003008 authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003010 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info) ||
3011 authmode == MBEDTLS_SSL_VERIFY_NONE) {
3012 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
3013 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 }
3015
3016 /*
3017 * 0 . 0 handshake type
3018 * 1 . 3 handshake length
3019 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003020 * 5 .. m-1 cert types
3021 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003022 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003023 * n .. n+1 length of all DNs
3024 * n+2 .. n+3 length of DN 1
3025 * n+4 .. ... Distinguished Name #1
3026 * ... .. ... length of DN 2, etc.
3027 */
3028 buf = ssl->out_msg;
3029 p = buf + 4;
3030
3031 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003032 * Supported certificate types
3033 *
3034 * ClientCertificateType certificate_types<1..2^8-1>;
3035 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003037 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039#if defined(MBEDTLS_RSA_C)
3040 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#if defined(MBEDTLS_ECDSA_C)
3043 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003044#endif
3045
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003046 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003047 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003048
Paul Bakker577e0062013-08-28 11:57:20 +02003049 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003051 /*
3052 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003053 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003054 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3055 *
3056 * struct {
3057 * HashAlgorithm hash;
3058 * SignatureAlgorithm signature;
3059 * } SignatureAndHashAlgorithm;
3060 *
3061 * enum { (255) } HashAlgorithm;
3062 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003063 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003064 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01003065 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003066
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003067 /*
3068 * Supported signature algorithms
3069 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003070 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
3071 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*cur);
Simon Butcher99000142016-10-13 17:21:01 +01003072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003073 if (MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md(ssl, hash)) {
Simon Butcher99000142016-10-13 17:21:01 +01003074 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003075 }
Simon Butcher99000142016-10-13 17:21:01 +01003076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003078 p[2 + sa_len++] = hash;
3079 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003080#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003082 p[2 + sa_len++] = hash;
3083 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003084#endif
Simon Butcher99000142016-10-13 17:21:01 +01003085 }
Paul Bakker926af752012-11-23 13:38:07 +01003086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003087 MBEDTLS_PUT_UINT16_BE(sa_len, p, 0);
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003088 sa_len += 2;
3089 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003090 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003093 /*
3094 * DistinguishedName certificate_authorities<0..2^16-1>;
3095 * opaque DistinguishedName<1..2^16-1>;
3096 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003098
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003099 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003101 if (ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED) {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003102 /* NOTE: If trusted certificates are provisioned
3103 * via a CA callback (configured through
3104 * `mbedtls_ssl_conf_ca_cb()`, then the
3105 * CertificateRequest is currently left empty. */
3106
Janos Follath088ce432017-04-10 12:42:31 +01003107#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108 if (ssl->handshake->sni_ca_chain != NULL) {
Janos Follath088ce432017-04-10 12:42:31 +01003109 crt = ssl->handshake->sni_ca_chain;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 } else
Janos Follath088ce432017-04-10 12:42:31 +01003111#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003114 while (crt != NULL && crt->version != 0) {
irwirc9bc3002020-04-01 13:46:36 +03003115 /* It follows from RFC 5280 A.1 that this length
3116 * can be represented in at most 11 bits. */
3117 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119 if (end < p || (size_t) (end - p) < 2 + (size_t) dn_size) {
3120 MBEDTLS_SSL_DEBUG_MSG(1, ("skipping CAs: buffer too short"));
Janos Follath088ce432017-04-10 12:42:31 +01003121 break;
3122 }
3123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003124 MBEDTLS_PUT_UINT16_BE(dn_size, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01003125 p += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003126 memcpy(p, crt->subject_raw.p, dn_size);
Janos Follath088ce432017-04-10 12:42:31 +01003127 p += dn_size;
3128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 MBEDTLS_SSL_DEBUG_BUF(3, "requested DN", p - dn_size, dn_size);
Janos Follath088ce432017-04-10 12:42:31 +01003130
3131 total_dn_size += 2 + dn_size;
3132 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003133 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 }
3135
Paul Bakker926af752012-11-23 13:38:07 +01003136 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3138 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003139 MBEDTLS_PUT_UINT16_BE(total_dn_size, ssl->out_msg, 4 + ct_len + sa_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003141 ret = mbedtls_ssl_write_handshake_msg(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003143 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003146}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003147#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3150 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003151MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003152static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003153{
Janos Follath865b3eb2019-12-16 11:46:15 +00003154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 mbedtls_pk_context *own_key = mbedtls_ssl_own_key(ssl);
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003156
Manuel Pégourié-Gonnardb9c7ea42022-06-14 09:25:17 +02003157 /* Check if the key is a transparent ECDH key.
3158 * This also ensures that it is safe to call mbedtls_pk_ec(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003159 if (mbedtls_pk_get_type(own_key) != MBEDTLS_PK_ECKEY &&
3160 mbedtls_pk_get_type(own_key) != MBEDTLS_PK_ECKEY_DH) {
3161 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
3162 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003163 }
3164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003165 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx,
3166 mbedtls_pk_ec(*own_key),
3167 MBEDTLS_ECDH_OURS)) != 0) {
3168 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
3169 return ret;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003170 }
3171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172 return 0;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3175 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003176
Gilles Peskineeccd8882020-03-10 12:19:08 +01003177#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003178 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003179MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003180static int ssl_resume_server_key_exchange(mbedtls_ssl_context *ssl,
3181 size_t *signature_len)
Paul Bakker41c83d32013-03-20 14:39:14 +01003182{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003183 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3184 * signature length which will be added in ssl_write_server_key_exchange
3185 * after the call to ssl_prepare_server_key_exchange.
3186 * ssl_write_server_key_exchange also takes care of incrementing
3187 * ssl->out_msglen. */
3188 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003189 size_t sig_max_len = (ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
3190 - sig_start);
3191 int ret = ssl->conf->f_async_resume(ssl,
3192 sig_start, signature_len, sig_max_len);
3193 if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003194 ssl->handshake->async_in_progress = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003195 mbedtls_ssl_set_async_operation_data(ssl, NULL);
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003196 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003197 MBEDTLS_SSL_DEBUG_RET(2, "ssl_resume_server_key_exchange", ret);
3198 return ret;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003199}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003200#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003201 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003202
Gilles Peskined3eb0612018-01-08 17:07:44 +01003203/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003204 * calculating the signature if any, but excluding formatting the
3205 * signature and sending the message. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003206MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003207static int ssl_prepare_server_key_exchange(mbedtls_ssl_context *ssl,
3208 size_t *signature_len)
Paul Bakker5690efc2011-05-26 13:16:06 +00003209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003211 ssl->handshake->ciphersuite_info;
3212
Gilles Peskineeccd8882020-03-10 12:19:08 +01003213#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3214#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003215 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003216#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3217#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003218
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003219 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003220#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003221 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003222#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003223
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003224 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003226 /*
3227 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003228 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003229 *
3230 */
3231
3232 /*
3233 * - ECJPAKE key exchanges
3234 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003235#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Janos Follath865b3eb2019-12-16 11:46:15 +00003237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003238 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003239
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003240 ret = mbedtls_ecjpake_write_round_two(
3241 &ssl->handshake->ecjpake_ctx,
3242 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003243 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 ssl->conf->f_rng, ssl->conf->p_rng);
3245 if (ret != 0) {
3246 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3247 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003248 }
3249
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003250 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003251 }
3252#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3253
Hanno Becker1aa267c2017-04-28 17:08:27 +01003254 /*
3255 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3256 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3257 * we use empty support identity hints here.
3258 **/
3259#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3262 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003263 ssl->out_msg[ssl->out_msglen++] = 0x00;
3264 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003265 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3267 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003268
Hanno Becker7e5437a2017-04-28 17:15:26 +01003269 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003270 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003271 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003272#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003273 if (mbedtls_ssl_ciphersuite_uses_dhe(ciphersuite_info)) {
Janos Follath865b3eb2019-12-16 11:46:15 +00003274 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003275 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003277 if (ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL) {
3278 MBEDTLS_SSL_DEBUG_MSG(1, ("no DH parameters set"));
3279 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003280 }
3281
Paul Bakker41c83d32013-03-20 14:39:14 +01003282 /*
3283 * Ephemeral DH parameters:
3284 *
3285 * struct {
3286 * opaque dh_p<1..2^16-1>;
3287 * opaque dh_g<1..2^16-1>;
3288 * opaque dh_Ys<1..2^16-1>;
3289 * } ServerDHParams;
3290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003291 if ((ret = mbedtls_dhm_set_group(&ssl->handshake->dhm_ctx,
3292 &ssl->conf->dhm_P,
3293 &ssl->conf->dhm_G)) != 0) {
3294 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_set_group", ret);
3295 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003296 }
Paul Bakker48916f92012-09-16 19:57:18 +00003297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003298 if ((ret = mbedtls_dhm_make_params(
3299 &ssl->handshake->dhm_ctx,
3300 (int) mbedtls_mpi_size(&ssl->handshake->dhm_ctx.P),
3301 ssl->out_msg + ssl->out_msglen, &len,
3302 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3303 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_params", ret);
3304 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003305 }
3306
Gilles Peskineeccd8882020-03-10 12:19:08 +01003307#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003308 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003309#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003310
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003311 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003313 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
3314 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
3315 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
3316 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker41c83d32013-03-20 14:39:14 +01003317 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003318#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003319
Hanno Becker1aa267c2017-04-28 17:08:27 +01003320 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003321 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003322 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003323#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003324 if (mbedtls_ssl_ciphersuite_uses_ecdhe(ciphersuite_info)) {
Paul Bakker41c83d32013-03-20 14:39:14 +01003325 /*
3326 * Ephemeral ECDH parameters:
3327 *
3328 * struct {
3329 * ECParameters curve_params;
3330 * ECPoint public;
3331 * } ServerECDHParams;
3332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003336 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003337
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003338 /* Match our preference list against the offered curves */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
3340 for (curve = ssl->handshake->curves; *curve != NULL; curve++) {
3341 if ((*curve)->grp_id == *gid) {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003342 goto curve_matching_done;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 }
3344 }
3345 }
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003346
3347curve_matching_done:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003348 if (curve == NULL || *curve == NULL) {
3349 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching curve for ECDHE"));
3350 return MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN;
Gergely Budai987bfb52014-01-19 21:48:42 +01003351 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003353 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDHE curve: %s", (*curve)->name));
Gergely Budai987bfb52014-01-19 21:48:42 +01003354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003355 if ((ret = mbedtls_ecdh_setup(&ssl->handshake->ecdh_ctx,
3356 (*curve)->grp_id)) != 0) {
3357 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecp_group_load", ret);
3358 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003359 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 if ((ret = mbedtls_ecdh_make_params(
3362 &ssl->handshake->ecdh_ctx, &len,
3363 ssl->out_msg + ssl->out_msglen,
3364 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
3365 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3366 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_params", ret);
3367 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003368 }
3369
Gilles Peskineeccd8882020-03-10 12:19:08 +01003370#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003371 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003372#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003373
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003374 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3377 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01003378 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003379#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003380
Hanno Becker1aa267c2017-04-28 17:08:27 +01003381 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003382 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003383 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003384 * exchange parameters, compute and add the signature here.
3385 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003386 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003387#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003388 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Gilles Peskine1004c192018-01-08 16:59:14 +01003389 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003390 size_t hashlen = 0;
Ronald Cron3a95d2b2021-10-18 09:47:58 +02003391#if defined(MBEDTLS_USE_PSA_CRYPTO)
3392 unsigned char hash[PSA_HASH_MAX_SIZE];
3393#else
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003394 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Ronald Cron3a95d2b2021-10-18 09:47:58 +02003395#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00003396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003397
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003398 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003399 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003400 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003401 * to choose appropriate hash.
3402 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3403 * (RFC 4492, Sec. 5.4)
3404 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003405 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003406
3407 mbedtls_md_type_t md_alg;
3408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003410 mbedtls_pk_type_t sig_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003411 mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
3412 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003413 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3414 * (RFC 5246, Sec. 7.4.1.4.1). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003415 if (sig_alg == MBEDTLS_PK_NONE ||
3416 (md_alg = mbedtls_ssl_sig_hash_set_find(&ssl->handshake->hash_algs,
3417 sig_alg)) == MBEDTLS_MD_NONE) {
3418 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003419 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003420 * only if there is a matching hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003421 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003422 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003423 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3425#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003426 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3427 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003428 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 md_alg = MBEDTLS_MD_SHA1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003430 } else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003431#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3432 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003433 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003434 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003436 }
3437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 MBEDTLS_SSL_DEBUG_MSG(3, ("pick hash algorithm %u for signing", (unsigned) md_alg));
Hanno Becker7e5437a2017-04-28 17:15:26 +01003439
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003440 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003441 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003444 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3445 if (md_alg == MBEDTLS_MD_NONE) {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003446 hashlen = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003447 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls(ssl, hash,
3448 dig_signed,
3449 dig_signed_len);
3450 if (ret != 0) {
3451 return ret;
3452 }
3453 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3455 MBEDTLS_SSL_PROTO_TLS1_1 */
3456#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003457 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3458 if (md_alg != MBEDTLS_MD_NONE) {
3459 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
3460 dig_signed,
3461 dig_signed_len,
3462 md_alg);
3463 if (ret != 0) {
3464 return ret;
3465 }
3466 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3468 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003469 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003470 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3471 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02003472 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003474 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003475
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003476 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003477 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003481 /*
3482 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003483 * explicitly through a prefix to the signature.
3484 *
3485 * struct {
3486 * HashAlgorithm hash;
3487 * SignatureAlgorithm signature;
3488 * } SignatureAndHashAlgorithm;
3489 *
3490 * struct {
3491 * SignatureAndHashAlgorithm algorithm;
3492 * opaque signature<0..2^16-1>;
3493 * } DigitallySigned;
3494 *
3495 */
3496
Gilles Peskine1004c192018-01-08 16:59:14 +01003497 ssl->out_msg[ssl->out_msglen++] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003498 mbedtls_ssl_hash_from_md_alg(md_alg);
Gilles Peskine1004c192018-01-08 16:59:14 +01003499 ssl->out_msg[ssl->out_msglen++] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 mbedtls_ssl_sig_from_pk_alg(sig_alg);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003503
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003504#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003505 if (ssl->conf->f_async_sign_start != NULL) {
3506 ret = ssl->conf->f_async_sign_start(ssl,
3507 mbedtls_ssl_own_cert(ssl),
3508 md_alg, hash, hashlen);
3509 switch (ret) {
3510 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3511 /* act as if f_async_sign was null */
3512 break;
3513 case 0:
3514 ssl->handshake->async_in_progress = 1;
3515 return ssl_resume_server_key_exchange(ssl, signature_len);
3516 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3517 ssl->handshake->async_in_progress = 1;
3518 return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
3519 default:
3520 MBEDTLS_SSL_DEBUG_RET(1, "f_async_sign_start", ret);
3521 return ret;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003522 }
3523 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003524#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 if (mbedtls_ssl_own_key(ssl) == NULL) {
3527 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key"));
3528 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003529 }
3530
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003531 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3532 * signature length which will be added in ssl_write_server_key_exchange
3533 * after the call to ssl_prepare_server_key_exchange.
3534 * ssl_write_server_key_exchange also takes care of incrementing
3535 * ssl->out_msglen. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003536 if ((ret = mbedtls_pk_sign(mbedtls_ssl_own_key(ssl),
3537 md_alg, hash, hashlen,
3538 ssl->out_msg + ssl->out_msglen + 2,
3539 signature_len,
3540 ssl->conf->f_rng,
3541 ssl->conf->p_rng)) != 0) {
3542 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
3543 return ret;
Paul Bakker23f36802012-09-28 14:15:14 +00003544 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003545 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003546#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003548 return 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003549}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003550
Gilles Peskined3eb0612018-01-08 17:07:44 +01003551/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003552 * that do not include a ServerKeyExchange message, do nothing. Either
3553 * way, if successful, move on to the next step in the SSL state
3554 * machine. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003555MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003556static int ssl_write_server_key_exchange(mbedtls_ssl_context *ssl)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003557{
Janos Follath865b3eb2019-12-16 11:46:15 +00003558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003559 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003560#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003561 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003562 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003563#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server key exchange"));
Gilles Peskined3eb0612018-01-08 17:07:44 +01003566
Gilles Peskineeccd8882020-03-10 12:19:08 +01003567#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003568 /* Extract static ECDH parameters and abort if ServerKeyExchange
3569 * is not needed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003570 if (mbedtls_ssl_ciphersuite_no_pfs(ciphersuite_info)) {
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003571 /* For suites involving ECDH, extract DH parameters
3572 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003573#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003574 if (mbedtls_ssl_ciphersuite_uses_ecdh(ciphersuite_info)) {
3575 ret = ssl_get_ecdh_params_from_cert(ssl);
3576 if (ret != 0) {
3577 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
3578 return ret;
Manuel Pégourié-Gonnard5b3f24f2022-06-10 09:34:20 +02003579 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003580 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003581#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003582
3583 /* Key exchanges not involving ephemeral keys don't use
3584 * ServerKeyExchange, so end here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003585 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write server key exchange"));
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003586 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 return 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003588 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003589#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003590
Gilles Peskineeccd8882020-03-10 12:19:08 +01003591#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003592 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003593 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003594 * signature operation, resume signing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003595 if (ssl->handshake->async_in_progress != 0) {
3596 MBEDTLS_SSL_DEBUG_MSG(2, ("resuming signature operation"));
3597 ret = ssl_resume_server_key_exchange(ssl, &signature_len);
3598 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003599#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003600 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003601 {
3602 /* ServerKeyExchange is needed. Prepare the message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 ret = ssl_prepare_server_key_exchange(ssl, &signature_len);
Gilles Peskined3eb0612018-01-08 17:07:44 +01003604 }
3605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003606 if (ret != 0) {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003607 /* If we're starting to write a new message, set ssl->out_msglen
3608 * to 0. But if we're resuming after an asynchronous message,
3609 * out_msglen is the amount of data written so far and mst be
3610 * preserved. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003611 if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
3612 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange (pending)"));
3613 } else {
Gilles Peskined3eb0612018-01-08 17:07:44 +01003614 ssl->out_msglen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003615 }
3616 return ret;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003617 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003618
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003619 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003620 * ssl_prepare_server_key_exchange already wrote the signature
3621 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003622#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003623 if (signature_len != 0) {
3624 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1(signature_len);
3625 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0(signature_len);
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003627 MBEDTLS_SSL_DEBUG_BUF(3, "my signature",
3628 ssl->out_msg + ssl->out_msglen,
3629 signature_len);
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003630
3631 /* Skip over the already-written signature */
3632 ssl->out_msglen += signature_len;
3633 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003634#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003635
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003636 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3638 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003639
3640 ssl->state++;
3641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003642 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3643 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3644 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 }
3646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange"));
3648 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003649}
3650
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003651MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003652static int ssl_write_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003653{
Janos Follath865b3eb2019-12-16 11:46:15 +00003654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003656 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003657
3658 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3660 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
3662 ssl->state++;
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003665 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3666 mbedtls_ssl_send_flight_completed(ssl);
3667 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003668#endif
3669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3671 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3672 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 }
3674
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003675#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003676 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3677 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3678 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3679 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003680 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003681#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003683 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003685 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003686}
3687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3689 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003690MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003691static int ssl_parse_client_dh_public(mbedtls_ssl_context *ssl, unsigned char **p,
3692 const unsigned char *end)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003695 size_t n;
3696
3697 /*
3698 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3699 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003700 if (*p + 2 > end) {
3701 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3702 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003703 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003705 n = ((*p)[0] << 8) | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003706 *p += 2;
3707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003708 if (*p + n > end) {
3709 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3710 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003711 }
3712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003713 if ((ret = mbedtls_dhm_read_public(&ssl->handshake->dhm_ctx, *p, n)) != 0) {
3714 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_read_public", ret);
3715 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003716 }
3717
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003718 *p += n;
3719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker70df2fb2013-04-17 17:19:09 +02003721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 return ret;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003723}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3725 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3728 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003729
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003730#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003731MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003732static int ssl_resume_decrypt_pms(mbedtls_ssl_context *ssl,
3733 unsigned char *peer_pms,
3734 size_t *peer_pmslen,
3735 size_t peer_pmssize)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003736{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003737 int ret = ssl->conf->f_async_resume(ssl,
3738 peer_pms, peer_pmslen, peer_pmssize);
3739 if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003740 ssl->handshake->async_in_progress = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003741 mbedtls_ssl_set_async_operation_data(ssl, NULL);
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003742 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 MBEDTLS_SSL_DEBUG_RET(2, "ssl_decrypt_encrypted_pms", ret);
3744 return ret;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003745}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003746#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003747
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003748MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003749static int ssl_decrypt_encrypted_pms(mbedtls_ssl_context *ssl,
3750 const unsigned char *p,
3751 const unsigned char *end,
3752 unsigned char *peer_pms,
3753 size_t *peer_pmslen,
3754 size_t peer_pmssize)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003755{
Janos Follath865b3eb2019-12-16 11:46:15 +00003756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Leonid Rozenboim81e74232022-08-08 15:43:44 -07003757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 mbedtls_x509_crt *own_cert = mbedtls_ssl_own_cert(ssl);
3759 if (own_cert == NULL) {
3760 MBEDTLS_SSL_DEBUG_MSG(1, ("got no local certificate"));
3761 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Leonid Rozenboim81e74232022-08-08 15:43:44 -07003762 }
3763 mbedtls_pk_context *public_key = &own_cert->pk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003764 mbedtls_pk_context *private_key = mbedtls_ssl_own_key(ssl);
3765 size_t len = mbedtls_pk_get_len(public_key);
Paul Bakker70df2fb2013-04-17 17:19:09 +02003766
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003767#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003768 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003769 * decryption operation, resume signing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003770 if (ssl->handshake->async_in_progress != 0) {
3771 MBEDTLS_SSL_DEBUG_MSG(2, ("resuming decryption operation"));
3772 return ssl_resume_decrypt_pms(ssl,
3773 peer_pms, peer_pmslen, peer_pmssize);
Paul Bakker70df2fb2013-04-17 17:19:09 +02003774 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003775#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003776
3777 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003778 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3781 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003782 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0) {
3783 if (p + 2 > end) {
3784 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3785 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Philippe Antoine747fd532018-05-30 09:13:21 +02003786 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003787 if (*p++ != MBEDTLS_BYTE_1(len) ||
3788 *p++ != MBEDTLS_BYTE_0(len)) {
3789 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3790 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003791 }
3792 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003793#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003795 if (p + len != end) {
3796 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3797 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003798 }
3799
Gilles Peskine422ccab2018-01-11 18:29:01 +01003800 /*
3801 * Decrypt the premaster secret
3802 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003803#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003804 if (ssl->conf->f_async_decrypt_start != NULL) {
3805 ret = ssl->conf->f_async_decrypt_start(ssl,
3806 mbedtls_ssl_own_cert(ssl),
3807 p, len);
3808 switch (ret) {
3809 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3810 /* act as if f_async_decrypt_start was null */
3811 break;
3812 case 0:
3813 ssl->handshake->async_in_progress = 1;
3814 return ssl_resume_decrypt_pms(ssl,
3815 peer_pms,
3816 peer_pmslen,
3817 peer_pmssize);
3818 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3819 ssl->handshake->async_in_progress = 1;
3820 return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
3821 default:
3822 MBEDTLS_SSL_DEBUG_RET(1, "f_async_decrypt_start", ret);
3823 return ret;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003824 }
3825 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003826#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 if (!mbedtls_pk_can_do(private_key, MBEDTLS_PK_RSA)) {
3829 MBEDTLS_SSL_DEBUG_MSG(1, ("got no RSA private key"));
3830 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003831 }
3832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003833 ret = mbedtls_pk_decrypt(private_key, p, len,
3834 peer_pms, peer_pmslen, peer_pmssize,
3835 ssl->conf->f_rng, ssl->conf->p_rng);
3836 return ret;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003837}
3838
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003839MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840static int ssl_parse_encrypted_pms(mbedtls_ssl_context *ssl,
3841 const unsigned char *p,
3842 const unsigned char *end,
3843 size_t pms_offset)
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003844{
Janos Follath865b3eb2019-12-16 11:46:15 +00003845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003846 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3847 unsigned char ver[2];
3848 unsigned char fake_pms[48], peer_pms[48];
3849 unsigned char mask;
3850 size_t i, peer_pmslen;
3851 unsigned int diff;
3852
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003853 /* In case of a failure in decryption, the decryption may write less than
3854 * 2 bytes of output, but we always read the first two bytes. It doesn't
3855 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003856 * ret being nonzero, and we only care whether diff is 0.
3857 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3858 * also makes memory analyzers happy (don't access uninitialized memory,
3859 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003860 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003861 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003863 ret = ssl_decrypt_encrypted_pms(ssl, p, end,
3864 peer_pms,
3865 &peer_pmslen,
3866 sizeof(peer_pms));
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003867
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003868#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003869 if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
3870 return ret;
3871 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003872#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003874 mbedtls_ssl_write_version(ssl->handshake->max_major_ver,
3875 ssl->handshake->max_minor_ver,
3876 ssl->conf->transport, ver);
Gilles Peskine2e333372018-04-24 13:22:10 +02003877
3878 /* Avoid data-dependent branches while checking for invalid
3879 * padding, to protect against timing-based Bleichenbacher-type
3880 * attacks. */
3881 diff = (unsigned int) ret;
3882 diff |= peer_pmslen ^ 48;
3883 diff |= peer_pms[0] ^ ver[0];
3884 diff |= peer_pms[1] ^ ver[1];
3885
3886 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003887 mask = mbedtls_ct_uint_mask(diff);
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003888
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003889 /*
3890 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3891 * must not cause the connection to end immediately; instead, send a
3892 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003893 * To protect against timing-based variants of the attack, we must
3894 * not have any branch that depends on whether the decryption was
3895 * successful. In particular, always generate the fake premaster secret,
3896 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003897 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003898 ret = ssl->conf->f_rng(ssl->conf->p_rng, fake_pms, sizeof(fake_pms));
3899 if (ret != 0) {
Gilles Peskinee1416382018-04-26 10:23:21 +02003900 /* It's ok to abort on an RNG failure, since this does not reveal
3901 * anything about the RSA decryption. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902 return ret;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003903 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003904
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003905#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003906 if (diff != 0) {
3907 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3908 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003909#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003911 if (sizeof(ssl->handshake->premaster) < pms_offset ||
3912 sizeof(ssl->handshake->premaster) - pms_offset < 48) {
3913 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3914 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003915 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003916 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003917
Gilles Peskine422ccab2018-01-11 18:29:01 +01003918 /* Set pms to either the true or the fake PMS, without
3919 * data-dependent branches. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003920 for (i = 0; i < ssl->handshake->pmslen; i++) {
3921 pms[i] = (mask & fake_pms[i]) | ((~mask) & peer_pms[i]);
3922 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003924 return 0;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003925}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3927 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003928
Gilles Peskineeccd8882020-03-10 12:19:08 +01003929#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003930MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003931static int ssl_parse_client_psk_identity(mbedtls_ssl_context *ssl, unsigned char **p,
3932 const unsigned char *end)
Paul Bakkerfbb17802013-04-17 19:10:21 +02003933{
Paul Bakker6db455e2013-09-18 17:29:31 +02003934 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03003935 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003937 if (ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
3938 MBEDTLS_SSL_DEBUG_MSG(1, ("got no pre-shared key"));
3939 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003940 }
3941
3942 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003943 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003944 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003945 if (end - *p < 2) {
3946 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3947 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003948 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950 n = ((*p)[0] << 8) | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003951 *p += 2;
3952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953 if (n == 0 || n > end - *p) {
3954 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3955 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003956 }
3957
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003958 if (ssl->conf->f_psk != NULL) {
3959 if (ssl->conf->f_psk(ssl->conf->p_psk, ssl, *p, n) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003961 }
3962 } else {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003963 /* Identity is not a big secret since clients send it in the clear,
3964 * but treat it carefully anyway, just in case */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003965 if (n != ssl->conf->psk_identity_len ||
3966 mbedtls_ct_memcmp(ssl->conf->psk_identity, *p, n) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003968 }
3969 }
3970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003971 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
3972 MBEDTLS_SSL_DEBUG_BUF(3, "Unknown PSK identity", *p, n);
3973 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3974 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY);
3975 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003976 }
3977
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003978 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003979
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003980 return 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003981}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003982#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003983
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003984MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003985static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003986{
Janos Follath865b3eb2019-12-16 11:46:15 +00003987 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003989 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003990
Hanno Beckere694c3e2017-12-27 21:34:08 +00003991 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003993 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003994
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003995#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003996 (defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3997 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED))
3998 if ((ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3999 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) &&
4000 (ssl->handshake->async_in_progress != 0)) {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004001 /* We've already read a record and there is an asynchronous
4002 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02004003 * record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004004 MBEDTLS_SSL_DEBUG_MSG(3, ("will resume decryption of previously-read record"));
4005 } else
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004006#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
4008 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
4009 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 }
4011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004012 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004013 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004015 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
4016 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
4017 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 }
4019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004020 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE) {
4021 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
4022 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004023 }
4024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004026 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
4027 if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
4028 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
4029 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004030 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004032 if (p != end) {
4033 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
4034 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004035 }
4036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
4038 ssl->handshake->premaster,
4039 MBEDTLS_PREMASTER_SIZE,
4040 &ssl->handshake->pmslen,
4041 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
4042 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
4043 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004044 }
4045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004046 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
4047 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4049#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4050 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4051 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4052 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004053 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4055 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004056 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
4057 if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
4058 p, end - p)) != 0) {
4059 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
4060 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP;
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004061 }
4062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004063 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
4064 MBEDTLS_DEBUG_ECDH_QP);
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004066 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
4067 &ssl->handshake->pmslen,
4068 ssl->handshake->premaster,
4069 MBEDTLS_MPI_MAX_SIZE,
4070 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
4071 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
4072 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004073 }
4074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004075 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
4076 MBEDTLS_DEBUG_ECDH_Z);
4077 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4079 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4080 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4081 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4082#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
4084 if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
4085 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
4086 return ret;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004087 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089 if (p != end) {
4090 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
4091 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004092 }
4093
Hanno Becker845b9462018-10-26 12:07:29 +01004094#if defined(MBEDTLS_USE_PSA_CRYPTO)
Shaun Case0e7791f2021-12-20 21:14:10 -08004095 /* For opaque PSKs, we perform the PSK-to-MS derivation automatically
Hanno Becker845b9462018-10-26 12:07:29 +01004096 * and skip the intermediate PMS. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004097 if (ssl_use_opaque_psk(ssl) == 1) {
4098 MBEDTLS_SSL_DEBUG_MSG(1, ("skip PMS generation for opaque PSK"));
4099 } else
Hanno Becker845b9462018-10-26 12:07:29 +01004100#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004101 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
4102 ciphersuite_info->key_exchange)) != 0) {
4103 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
4104 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004105 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004106 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4108#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004109 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004110#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004111 if (ssl->handshake->async_in_progress != 0) {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004112 /* There is an asynchronous operation in progress to
4113 * decrypt the encrypted premaster secret, so skip
4114 * directly to resuming this operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 MBEDTLS_SSL_DEBUG_MSG(3, ("PSK identity already parsed"));
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004116 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4117 * won't actually use it, but maintain p anyway for robustness. */
4118 p += ssl->conf->psk_identity_len + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004119 } else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004120#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004121 if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
4122 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
4123 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004124 }
4125
Hanno Becker845b9462018-10-26 12:07:29 +01004126#if defined(MBEDTLS_USE_PSA_CRYPTO)
4127 /* Opaque PSKs are currently only supported for PSK-only. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004128 if (ssl_use_opaque_psk(ssl) == 1) {
4129 MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with RSA-PSK"));
4130 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardd80d8a42022-06-14 10:53:15 +02004131 }
Hanno Becker845b9462018-10-26 12:07:29 +01004132#endif
4133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004134 if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 2)) != 0) {
4135 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_encrypted_pms"), ret);
4136 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004137 }
4138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004139 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
4140 ciphersuite_info->key_exchange)) != 0) {
4141 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
4142 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004143 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004144 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4146#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
4148 if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
4149 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
4150 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004151 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004152 if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
4153 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
4154 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004155 }
4156
Hanno Becker845b9462018-10-26 12:07:29 +01004157#if defined(MBEDTLS_USE_PSA_CRYPTO)
4158 /* Opaque PSKs are currently only supported for PSK-only. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004159 if (ssl_use_opaque_psk(ssl) == 1) {
4160 MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with DHE-PSK"));
4161 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardd80d8a42022-06-14 10:53:15 +02004162 }
Hanno Becker845b9462018-10-26 12:07:29 +01004163#endif
4164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004165 if (p != end) {
4166 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
4167 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004168 }
4169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004170 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
4171 ciphersuite_info->key_exchange)) != 0) {
4172 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
4173 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004174 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004175 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4177#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004178 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
4179 if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
4180 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
4181 return ret;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004182 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004184 if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
4185 p, end - p)) != 0) {
4186 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
4187 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004188 }
4189
Hanno Becker845b9462018-10-26 12:07:29 +01004190#if defined(MBEDTLS_USE_PSA_CRYPTO)
4191 /* Opaque PSKs are currently only supported for PSK-only. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 if (ssl_use_opaque_psk(ssl) == 1) {
4193 MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with ECDHE-PSK"));
4194 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardd80d8a42022-06-14 10:53:15 +02004195 }
Hanno Becker845b9462018-10-26 12:07:29 +01004196#endif
4197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004198 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
4199 MBEDTLS_DEBUG_ECDH_QP);
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004201 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
4202 ciphersuite_info->key_exchange)) != 0) {
4203 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
4204 return ret;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004205 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004206 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4208#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004209 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
4210 if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 0)) != 0) {
4211 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_parse_encrypted_pms_secret"), ret);
4212 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004213 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004214 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004216#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004217 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
4218 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
4219 p, end - p);
4220 if (ret != 0) {
4221 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
4222 return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004223 }
4224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004225 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
4226 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4227 ssl->conf->f_rng, ssl->conf->p_rng);
4228 if (ret != 0) {
4229 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
4230 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004231 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004232 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004233#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004234 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004235 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4236 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004237 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004239 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
4240 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
4241 return ret;
Paul Bakkerff60ee62010-03-16 21:09:09 +00004242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004243
Paul Bakker5121ce52009-01-03 21:22:43 +00004244 ssl->state++;
4245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004246 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004248 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004249}
4250
Gilles Peskineeccd8882020-03-10 12:19:08 +01004251#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004252MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004253static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004254{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004255 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004256 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004258 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004260 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
4261 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02004262 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004263 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02004264 }
4265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004266 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4267 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004268}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004269#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004270MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004273 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004274 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004275 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004276 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004277 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4279 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004280#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004282 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004283 ssl->handshake->ciphersuite_info;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004284 mbedtls_pk_context *peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
4289 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004290 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004291 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004292 }
4293
Hanno Becker2a831a42019-02-07 13:17:25 +00004294#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295 if (ssl->session_negotiate->peer_cert == NULL) {
4296 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
Hanno Becker2a831a42019-02-07 13:17:25 +00004297 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004298 return 0;
Hanno Becker2a831a42019-02-07 13:17:25 +00004299 }
4300#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301 if (ssl->session_negotiate->peer_cert_digest == NULL) {
4302 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
Hanno Becker2a831a42019-02-07 13:17:25 +00004303 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004304 return 0;
Hanno Becker2a831a42019-02-07 13:17:25 +00004305 }
4306#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4307
Simon Butcher99000142016-10-13 17:21:01 +01004308 /* Read the message without adding it to the checksum */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004309 ret = mbedtls_ssl_read_record(ssl, 0 /* no checksum update */);
4310 if (0 != ret) {
4311 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_read_record"), ret);
4312 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004313 }
4314
4315 ssl->state++;
4316
Simon Butcher99000142016-10-13 17:21:01 +01004317 /* Process the message contents */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4319 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY) {
4320 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4321 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 }
4323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324 i = mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004325
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004326#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4327 peer_pk = &ssl->handshake->peer_pubkey;
4328#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004329 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004330 /* Should never happen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004331 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004332 }
4333 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4334#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4335
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004336 /*
4337 * struct {
4338 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4339 * opaque signature<0..2^16-1>;
4340 * } DigitallySigned;
4341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4343 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004344 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004346 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004347
4348 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004349 if (mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECDSA)) {
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004350 hash_start += 16;
4351 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004353 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4356 MBEDTLS_SSL_PROTO_TLS1_1 */
4357#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
4359 if (i + 2 > ssl->in_hslen) {
4360 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4361 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004362 }
4363
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004365 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004366 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 md_alg = mbedtls_ssl_md_alg_from_hash(ssl->in_msg[i]);
Simon Butcher99000142016-10-13 17:21:01 +01004368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004369 if (md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md(ssl, ssl->in_msg[i])) {
4370 MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
4371 " for verify message"));
4372 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 }
4374
Simon Butcher99000142016-10-13 17:21:01 +01004375#if !defined(MBEDTLS_MD_SHA1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004376 if (MBEDTLS_MD_SHA1 == md_alg) {
Simon Butcher99000142016-10-13 17:21:01 +01004377 hash_start += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004378 }
Simon Butcher99000142016-10-13 17:21:01 +01004379#endif
Paul Bakker926af752012-11-23 13:38:07 +01004380
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004381 /* Info from md_alg will be used instead */
4382 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004383
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004384 i++;
4385
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004386 /*
4387 * Signature
4388 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004389 if ((pk_alg = mbedtls_ssl_pk_alg_from_sig(ssl->in_msg[i]))
4390 == MBEDTLS_PK_NONE) {
4391 MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
4392 " for verify message"));
4393 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004394 }
4395
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004396 /*
4397 * Check the certificate's key type matches the signature alg
4398 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004399 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
4400 MBEDTLS_SSL_DEBUG_MSG(1, ("sig_alg doesn't match cert key"));
4401 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004402 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004403
4404 i++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004405 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004407 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004408 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4409 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004410 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004412 if (i + 2 > ssl->in_hslen) {
4413 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4414 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004415 }
4416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004417 sig_len = (ssl->in_msg[i] << 8) | ssl->in_msg[i+1];
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004418 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004420 if (i + sig_len != ssl->in_hslen) {
4421 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4422 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 }
4424
Simon Butcher99000142016-10-13 17:21:01 +01004425 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004426 {
4427 size_t dummy_hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004428 ssl->handshake->calc_verify(ssl, hash, &dummy_hlen);
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004429 }
Simon Butcher99000142016-10-13 17:21:01 +01004430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004431 if ((ret = mbedtls_pk_verify(peer_pk,
4432 md_alg, hash_start, hashlen,
4433 ssl->in_msg + i, sig_len)) != 0) {
4434 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
4435 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004436 }
4437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 mbedtls_ssl_update_handshake_status(ssl);
Simon Butcher99000142016-10-13 17:21:01 +01004439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004440 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004442 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004443}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004444#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004447MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004448static int ssl_write_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004449{
Janos Follath865b3eb2019-12-16 11:46:15 +00004450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004451 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004452 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004454 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write new session ticket"));
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4457 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004458
4459 /*
4460 * struct {
4461 * uint32 ticket_lifetime_hint;
4462 * opaque ticket<0..2^16-1>;
4463 * } NewSessionTicket;
4464 *
4465 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4466 * 8 . 9 ticket_len (n)
4467 * 10 . 9+n ticket content
4468 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004470 if ((ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
4471 ssl->session_negotiate,
4472 ssl->out_msg + 10,
4473 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
4474 &tlen, &lifetime)) != 0) {
4475 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_write", ret);
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004476 tlen = 0;
4477 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004479 MBEDTLS_PUT_UINT32_BE(lifetime, ssl->out_msg, 4);
4480 MBEDTLS_PUT_UINT16_BE(tlen, ssl->out_msg, 8);
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004481 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004482
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004483 /*
4484 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4485 * ChangeCipherSpec share the same state.
4486 */
4487 ssl->handshake->new_session_ticket = 0;
4488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4490 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4491 return ret;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004492 }
4493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004494 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496 return 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004499
Paul Bakker5121ce52009-01-03 21:22:43 +00004500/*
Paul Bakker1961b702013-01-25 14:49:24 +01004501 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004502 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004503int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004504{
4505 int ret = 0;
4506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004507 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
4508 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004511 MBEDTLS_SSL_DEBUG_MSG(2, ("server state: %d", ssl->state));
Paul Bakker1961b702013-01-25 14:49:24 +01004512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004513 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4514 return ret;
4515 }
Paul Bakker1961b702013-01-25 14:49:24 +01004516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004518 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4519 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4520 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4521 return ret;
4522 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004523 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004524#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004526 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527 case MBEDTLS_SSL_HELLO_REQUEST:
4528 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004529 break;
4530
Paul Bakker1961b702013-01-25 14:49:24 +01004531 /*
4532 * <== ClientHello
4533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004535 ret = ssl_parse_client_hello(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004536 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538#if defined(MBEDTLS_SSL_PROTO_DTLS)
4539 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004541#endif
4542
Paul Bakker1961b702013-01-25 14:49:24 +01004543 /*
4544 * ==> ServerHello
4545 * Certificate
4546 * ( ServerKeyExchange )
4547 * ( CertificateRequest )
4548 * ServerHelloDone
4549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004551 ret = ssl_write_server_hello(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004552 break;
4553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004555 ret = mbedtls_ssl_write_certificate(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004556 break;
4557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004559 ret = ssl_write_server_key_exchange(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004560 break;
4561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 ret = ssl_write_certificate_request(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004564 break;
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 ret = ssl_write_server_hello_done(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004568 break;
4569
4570 /*
4571 * <== ( Certificate/Alert )
4572 * ClientKeyExchange
4573 * ( CertificateVerify )
4574 * ChangeCipherSpec
4575 * Finished
4576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578 ret = mbedtls_ssl_parse_certificate(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004579 break;
4580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004582 ret = ssl_parse_client_key_exchange(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004583 break;
4584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004586 ret = ssl_parse_certificate_verify(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004587 break;
4588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004590 ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004591 break;
4592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594 ret = mbedtls_ssl_parse_finished(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004595 break;
4596
4597 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004598 * ==> ( NewSessionTicket )
4599 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004600 * Finished
4601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4603#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004604 if (ssl->handshake->new_session_ticket != 0) {
4605 ret = ssl_write_new_session_ticket(ssl);
4606 } else
Paul Bakkera503a632013-08-14 13:48:06 +02004607#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004608 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004609 break;
4610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612 ret = mbedtls_ssl_write_finished(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004613 break;
4614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 case MBEDTLS_SSL_FLUSH_BUFFERS:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004616 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004618 break;
4619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004621 mbedtls_ssl_handshake_wrapup(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004622 break;
4623
4624 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004625 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
4626 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 }
4628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631#endif /* MBEDTLS_SSL_SRV_C */